Login   Register  
PHP Classes
elePHPant
Icontem

File: tools/check_i18n_defs.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Alan H. Lake  >  I18N class  >  tools/check_i18n_defs.php  >  Download  
File: tools/check_i18n_defs.php
Role: Application script
Content type: text/plain
Description: Program to validate I18N code in programs and in Locale files/table
Class: I18N class
Get translation texts from different containers
Author: By
Last change: Change reference to ".inc" files to ".inc.php" for greater security.
Date: 9 years ago
Size: 10,278 bytes
 

Contents

Class file image Download
<?php

    /*
       check_i18n_defs.php - Validate XL() call definitions
       Copyright (C) 2005  Alan H. Lake

         This program is free software; you can redistribute it and/or modify
         it under the terms of the GNU General Public License as published by
         the Free Software Foundation; either version 2 of the License, or
         (at your option) any later version.

         This program is distributed in the hope that it will be useful,
         but WITHOUT ANY WARRANTY; without even the implied warranty of
         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
         GNU General Public License for more details.

         You should have received a copy of the GNU General Public License
         along with this program; if not, write to the Free Software
         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

       Current version: 1.0.1 (September 10, 2005)
    */

    require ("useful.php");
    require ("i18n_tools.inc.php");

    require ("../cnx_i18n.inc.php");

    // Name of this script (may be the path to this script)
    $ScriptName = "check_i18n_defs.php";

    // Array of known locale information storage types
    $localetypes = array (
        "onefile"      => "Monolithic locale file for each locale",
        "severalfiles" => "Locale files for each locale, " .
                          "split by first letter",
        "dbm"          => "DBM database for each locale",
        "SQL"          => "SQL database"
    );


/* -------------------------------------------------------------------------- */

    function DisplayLocaleTypeForm() {
        // Display the form requesting storage type

        // Global variables
        global $localetypes;
        global $ScriptName;

        // Display the form
        echo "<p><h2>Check I18N Definitions</h2></p>\n";
        echo "<p><br><form action=\"$ScriptName\" method=POST>\n";
        echo "Please select the appropriate storage mechanism.\n";
        DisplaySelect ($localetypes, "localetype");
        echo "<br><br><input type=\"SUBMIT\" value=\"Continue\">";
        echo "</form>\n";
    }

    function DisplayLocalePathForm() {
        // Display the form requesting locale path

        // Global variables
        global $localetypes;
        global $ScriptName;
        global $localetype;

        // Display the form
        echo "<p><h2>Check I18N Definitions</h2></p>\n";
        echo "<p>Locale Type: $localetype</p>";
        echo "<p><br><form action=\"$ScriptName\" method=POST>\n";
        echo "<input type=\"HIDDEN\" name=localetype value=\"$localetype\">\n";
        echo "Please enter the path to the locale files.\n";
        echo "<input maxlen=255 size=50 name=localepath><br><br>\n";
        echo "<br><br><input type=\"SUBMIT\" value=\"Continue\">";
        echo "</form>\n";
    }


    function DisplayLocaleSelectionForm ($localepath, $localetype) {
        // Display the form requesting selection of a locale to test

        global $localetypes;
        global $ScriptName;
        global $oLang;

        $locales = BuildLocaleList ($localepath, $localetype);
        if (empty ($locales) and $localetype != "SQL")
            die ("<p>The locale path you specified does not contain " .
                 "any translation files based on the storage mechanism " .
                 "you selected ($localetypes[$localetype] in $localepath). " .
                 "<a href=\"$ScriptName\">Please try again.</a>\n");

        echo "<p><h2>Check I18N Definitions</h2></p>\n";
        echo "<p>Locale Type: $localetype</p>";
        if(isset($localepath) and strlen($localepath) > 0)
          echo "<p>Locale Path: $localepath</p>";

        echo "<p><br><form action=\"$ScriptName\" method=POST>\n";
        echo "<input type=\"HIDDEN\" name=localepath value=\"$localepath\">\n";
        echo "<input type=\"HIDDEN\" name=localetype value=\"$localetype\">\n";
        echo "Locale to check: ";
        DisplaySelect ($locales, "selectedlocale");
        echo "<br><br><input type=\"SUBMIT\" value=\"Continue\">";
        echo "</form>\n";
    }

    function DisplayFileSelectionForm ($localepath, $localetype,$selectedlocale) {
        // Displays a form where people can enter filenames to check
        global $ScriptName;

        echo "<p><h2>Check I18N Definitions</h2></p>\n";
        echo "<p>Locale Type: $localetype</p>";
        if(isset($localepath) and strlen($localepath) > 0)
          echo "<p>Locale Path: $localepath</p>";
        echo "<p>Locale: $selectedlocale</p>";
        echo "<p>Please enter the list of files (including path) ",
             "you wish to check.<br>\n";
        echo "<p><br><form action=\"$ScriptName\" method=POST>\n";
        echo "<input type=\"HIDDEN\" name=localepath value=\"$localepath\">\n";
        echo "<input type=\"HIDDEN\" name=localetype value=\"$localetype\">\n";
        echo "<input type=\"HIDDEN\" name=selectedlocale value=",
             "\"$selectedlocale\">\n";
        echo "<textarea name=selectedfiles cols=60 rows=16>",
             "</textarea>\n";
        echo "<br><br><input type=\"SUBMIT\" value=\"Continue\">";
        echo "</form>\n";
    }


    function PerformAnalysis ($localepath, $localetype, $selectedlocale, $selectedfiles) {
        // Perform analysis of the source files and translation data,
        // displaying this analysis.
        // Global variables
        global $localetypes;
        global $oLang;

        echo "<p><h2>Check I18N Definitions</h2></p>\n";
        echo "<p><b>Performing analysis of selected locale and sources</b>\n";

        echo "<p>Locale type: $localetypes[$localetype]<br />\n";
        if(isset($localepath) and strlen($localepath) > 0)
          echo "Locale Path: $localepath<br />\n";
        echo "Selected locale: $selectedlocale<br />\n";

        // Split the source filenames up into an array
        $count = preg_match_all("/\S+/", $selectedfiles, $filelist);
        echo "Source files:<br />";

        // Generate an array of the strings requested from calls to XL
        $required_strings = array();
        while (list ($index, $value) = each ($filelist[0])) {
            if (!empty ($value)) {
                echo "  $value<br />";
                $required_strings = BuildXLCallList ($value,$required_strings);
            }
        }

        echo "<p>We have counted ", count ($required_strings),
             " unique requests in the specified source files.</p>";

        $available_strings = ReadAllLocaleData ($localepath, $localetype,$selectedlocale);
        echo "<br>Read ", count ($available_strings)," translated strings from files.\n";

        echo "<p>The following variables do not appear in the translation data:\n";
        CheckForMissing ($required_strings, $available_strings);
        echo "</p><p>The following Identifiers in the translation data are not used in a program\n";
        CheckForExtra ($required_strings, $available_strings);
        echo "</p>Done.\n";
    }


    // Look for XL() calls which look for nonexistant values
    function CheckForMissing($required_strings, $available_strings) {
        while (list ($key, $value) = each ($required_strings)) {
            if (!isset ($available_strings[$key])) {
                echo "<br />\"$key\"\n";
            }
        }
    }


    // Look for values which are never requested by XL() calls
    function CheckForExtra ($required_strings, $available_strings) {
        while (list ($key, $value) = each ($available_strings)) {
            if (!isset ($required_strings[$key])) {
                echo "<br />\"$key\"\n";
            }
        }
    }


/* -------------------------------------------------------------------------- */
    // Our main code blocks

    session_name("check_i18n_defs");
    session_start();

    if(isset($_POST['localetype']))
      $localetype = $_POST['localetype'];
    if(isset($_POST['localepath']))
      $localepath = $_POST['localepath'];
    if(isset($_POST['selectedlocale']))
      $selectedlocale = $_POST['selectedlocale'];
    if(isset($_POST['selectedfiles']))
      $selectedfiles = $_POST['selectedfiles'];

    if (isset ($localepath)) {
        if (substr ($localepath, strlen ($localepath) - 1) != "/")
            $localepath .= "/";
        if (!is_dir ($localepath))
            die ("<p>The locale path you specified does not exist or " .
                 "is otherwise invalid. <a href=\"$ScriptName\">" .
                 "Please try again.</a>\n");
    }

    if(!isset($localetype)){
        DisplayLocaleTypeForm();
    } elseif (!isset ($localepath) and $localetype != "SQL") {
        DisplayLocalePathForm();
    } elseif (!isset ($selectedlocale)) {
        if($localetype == "SQL") {
          include_once "../rfc1766.class.php";
          if(!isset($oRfc1766)){
            $oRfc1766 = new rfc1766_class;
            $_SESSION['oRfc1766'] = $oRfc1766;
          }
          include_once "../i18n.class.php";
          $oI18N = new I18N_class;
          include_once "../langTable.class.php";
          if(!isset($oLang)){
            $oLang = new LangTable_class($host,$user,$password,$db,$table);
            $_SESSION['oLang'] = $oLang;
          }
          $oLang->OpenDb();

          if(!$oLang->TableExists())
            $oLang->CreateTable();
          }
        if(!isset($localepath))
          $localepath = '/';
        DisplayLocaleSelectionForm ($localepath, $localetype);
    } elseif (!isset ($selectedfiles)) {
        DisplayFileSelectionForm ($localepath, $localetype, $selectedlocale);
    } else {
      $selectedfiles = trim($selectedfiles);
      if(substr($selectedfiles,-2) == '/*'){
        $path = substr($selectedfiles,0,strlen($selectedfiles)-2);
        $selectedfiles = GetFiles($path);
      }
      PerformAnalysis ($localepath, $localetype, $selectedlocale,$selectedfiles);
    }

    function GetFiles($path){
      if($h = opendir($path)){
        while(false !== ($file = readdir($h))){
          if(substr($file,-4) == '.php')
            $result .= ' '.$path.'/'.$file;
          elseif(is_dir($path.'/'.$file) and $file != '.' and $file != '..') {
            $result .= GetFiles($path.'/'.$file);
          }
        }
      }
      return($result);
    }
?>