eZ Publish  [4.0]
ezchecktranslation.php
Go to the documentation of this file.
00001 #!/usr/bin/env php
00002 <?php
00003 //
00004 // Created on: <31-Mar-2004 11:12:27 amos>
00005 //
00006 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00007 // SOFTWARE NAME: eZ Publish
00008 // SOFTWARE RELEASE: 4.0.x
00009 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00010 // SOFTWARE LICENSE: GNU General Public License v2.0
00011 // NOTICE: >
00012 //   This program is free software; you can redistribute it and/or
00013 //   modify it under the terms of version 2.0  of the GNU General
00014 //   Public License as published by the Free Software Foundation.
00015 //
00016 //   This program is distributed in the hope that it will be useful,
00017 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //   GNU General Public License for more details.
00020 //
00021 //   You should have received a copy of version 2.0 of the GNU General
00022 //   Public License along with this program; if not, write to the Free
00023 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00024 //   MA 02110-1301, USA.
00025 //
00026 //
00027 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00028 //
00029 
00030 //include_once( 'lib/ezutils/classes/ezcli.php' );
00031 //include_once( 'kernel/classes/ezscript.php' );
00032 
00033 require 'autoload.php';
00034 
00035 $cli = eZCLI::instance();
00036 $script = eZScript::instance( array( 'description' => ( "eZ Publish Translation Checker\n\n" .
00037                                                         "Will display some statistics on a given translation" .
00038                                                         "\n" .
00039                                                         "ezchecktranslation.php ita-IT" ),
00040                                      'use-session' => false,
00041                                      'use-modules' => true,
00042                                      'use-extensions' => true ) );
00043 
00044 $script->startup();
00045 
00046 $options = $script->getOptions( "[ignore-tr-setup]",
00047                                 "[translation]",
00048                                 array( 'ignore-tr-setup' => 'Tells the analyzer to skip all translations regarding the setup' ) );
00049 $script->initialize();
00050 
00051 if ( count( $options['arguments'] ) < 1 )
00052     $script->shutdown( 1, "No translation specified" );
00053 
00054 $translationName = false;
00055 $translationFile = $options['arguments'][0];
00056 if ( !file_exists( $translationFile ) )
00057 {
00058     $translationName = $translationFile;
00059     $translationFile = 'share/translations/' . $translationName . '/translation.ts';
00060 }
00061 
00062 if ( !file_exists( $translationFile ) )
00063     $script->shutdown( 1, "Translation file $translationFile does not exist" );
00064 
00065 $cli->output( $cli->stylize( 'file', $translationFile ) . ":", false );
00066 $cli->output( " loading", false );
00067 $fd = fopen( $translationFile, "rb" );
00068 $transXML = fread( $fd, filesize( $translationFile ) );
00069 fclose( $fd );
00070 
00071 
00072 $cli->output( " parsing", false );
00073 
00074 $tree = new DOMDOcument();
00075 $success = $tree->loadXML( $transXML );
00076 
00077 $cli->output( " validating", false );
00078 //include_once( 'lib/ezi18n/classes/eztstranslator.php' );
00079 if ( !eZTSTranslator::validateDOMTree( $tree ) )
00080     $script->shutdown( 1, "XML text for file $translationFile did not validate" );
00081 
00082 
00083 function handleContextNode( $context, $cli, $data )
00084 {
00085     $contextName = null;
00086     $messages = array();
00087     $context_children = $context->childNodes;
00088     foreach ( $context_children as $context_child )
00089     {
00090         if ( $context_child->nodeType == XML_ELEMENT_NODE )
00091         {
00092             if ( $context_child->localName == "name" )
00093             {
00094                 $data['context_count']++;
00095                 $contextName = $context_child->textContent;
00096             }
00097             else if ( $context_child->localName == "message" )
00098             {
00099                 $messages[] = $context_child;
00100             }
00101             else
00102             {
00103                 $cli->warning( "Unknown element name: " . $context_child->localName );
00104             }
00105         }
00106     }
00107     if ( $contextName === null )
00108     {
00109         $cli->warning( "No context name found, skipping context" );
00110     }
00111     else if ( !in_array( $contextName, $data['ignored_context_list'] ) )
00112     {
00113         foreach( $messages as $message )
00114         {
00115             $data['element_count']++;
00116             $data = handleMessageNode( $contextName, $message, $cli, $data, true );
00117         }
00118     }
00119 
00120     return $data;
00121 }
00122 
00123 function handleMessageNode( $contextName, $message, $cli, $data, $requireTranslation )
00124 {
00125     $source = null;
00126     $translation = null;
00127     $comment = null;
00128     $message_children = $message->childNodes;
00129     foreach( $message_children as $message_child )
00130     {
00131         if ( $message_child->nodeType == XML_ELEMENT_NODE )
00132         {
00133             if ( $message_child->localName == "source" )
00134             {
00135                 $source = $message_child->textContent;
00136             }
00137             else if ( $message_child->localName == "translation" )
00138             {
00139                 $translation_el = $message_child->childNodes;
00140                 $type = $message_child->getAttribute( 'type' );
00141                 if ( $type == 'unfinished' )
00142                 {
00143                     $data['untranslated_element_count']++;
00144                 }
00145                 else if ( $type == 'obsolete' )
00146                 {
00147                     $data['obsolete_element_count']++;
00148                 }
00149                 else
00150                 {
00151                     $data['translated_element_count']++;
00152                 }
00153                 if ( $translation_el->length > 0 )
00154                 {
00155                     $translation_el = $translation_el->item( 0 );
00156                     $translation = $translation_el->textContent;
00157                 }
00158             }
00159             else if ( $message_child->localName == "comment" )
00160             {
00161                 $comment = $message_child->textContent;
00162             }
00163             else
00164             {
00165                 $cli->warning( "Unknown element name: " . $message_child->localName );
00166             }
00167         }
00168     }
00169     if ( $source === null )
00170     {
00171         $cli->warning( "No source name found, skipping message" );
00172     }
00173 
00174     return $data;
00175 }
00176 
00177 $data = array( 'element_count' => 0,
00178                'context_count' => 0,
00179                'translated_element_count' => 0,
00180                'untranslated_element_count' => 0,
00181                'obsolete_element_count' => 0 );
00182 $data['ignored_context_list'] = array();
00183 
00184 if ( $options['ignore-tr-setup'] )
00185 {
00186     $data['ignored_context_list'] = array_merge( $data['ignored_context_list'],
00187                                                  array( 'design/standard/setup',
00188                                                         'design/standard/setup/datatypecode',
00189                                                         'design/standard/setup',
00190                                                         'design/standard/setup/db',
00191                                                         'design/standard/setup/init',
00192                                                         'design/standard/setup/operatorcode',
00193                                                         'design/standard/setup/tests' ) );
00194 }
00195 
00196 $treeRoot = $tree->documentElement;
00197 $children = $treeRoot->childNodes;
00198 foreach( $children as $child )
00199 {
00200     if ( $child->nodeType == XML_ELEMENT_NODE )
00201     {
00202         if ( $child->localName == "context" )
00203         {
00204             $data = handleContextNode( $child, $cli, $data );
00205         }
00206         else
00207         {
00208             $cli->warning( "Unknown element name: " . $child->localName );
00209         }
00210     }
00211 }
00212 
00213 $cli->output();
00214 
00215 $cli->output( $cli->stylize( 'header', "Name" ) . "          " . $cli->stylize( "header", "Count" ) );
00216 $cli->output( "context:      " . $data['context_count'] );
00217 $cli->output( "element:      " . $data['element_count'] );
00218 $cli->output( "translated:   " . $data['translated_element_count'] );
00219 $cli->output( "untranslated: " . $data['untranslated_element_count'] );
00220 $cli->output( "obsolete:     " . $data['obsolete_element_count'] );
00221 $cli->output();
00222 $totalCount = $data['translated_element_count'] + $data['untranslated_element_count'];
00223 if ( $totalCount == 0 )
00224 {
00225     $percentText = "no elements";
00226 }
00227 else
00228 {
00229     $percent = ( $data['translated_element_count'] * 100 ) / $totalCount;
00230     $percentText = number_format( $percent, 2 ) . "%";
00231 }
00232 
00233 $cli->output( "Percent finished: " . $percentText );
00234 
00235 $script->shutdown();
00236 
00237 ?>