Source for file extract.action.php

Documentation is available at extract.action.php

  1. <?php
  2. /**
  3. @package Mambo
  4. @subpackage Languages
  5. @copyright  Refer to copyright.php
  6. @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  7. @author Mambo Foundation Inc see README.php
  8. */ 
  9. defined'_VALID_MOS' or die'Direct Access to this location is not allowed.' );
  10. class extractAction extends Action
  11. {
  12.  
  13.     function execute(&$controller&$request)
  14.     {
  15.         //if (!isset($_POST['domain']))
  16.  
  17.         $path mamboCore::get('rootPath');
  18.         $frontpaths  array("$path/templates""$path/modules""$path/includes""$path/mambots""$path/");
  19.         $adminpaths  array("$path/administrator/modules""$path/administrator/popups""$path/administrator/includes""$path/administrator/templates");
  20.         $cmtpaths array_merge(glob("$path/administrator/components/com*")glob("$path/components/com*"));
  21.         //$mambopaths = array_merge($frontpaths,$adminpaths,$cmtpaths);
  22.         foreach ($cmtpaths as $p{
  23.             preg_match('/com_(.*)$/'$p$matches);
  24.             $components[$matches[1]][]  =  $p;
  25.         }
  26.  
  27.         $dir $path.'/language/untranslated';
  28.         // only continue if we have no .pot files
  29.         if (!is_dir($dir)) {
  30.             @mkdir($dir);
  31.         else {
  32.             return $controller->redirect('index''language');
  33.         }/**/
  34.  
  35.         set_time_limit(300);
  36.         if (!file_exists("$dir/installation.pot"&& file_exists("$path/installation")) {
  37.             $this->extract('installation'array($path.'/installation'));
  38.         }
  39.         /*if (!file_exists("$dir/mambo.pot")) {
  40.             $this->extract('mambo', $mambopaths);
  41.         }*/
  42.         if (!file_exists("$dir/frontend.pot")) {
  43.             $this->extract('frontend'$frontpaths);
  44.         }
  45.         if (!file_exists("$dir/administrator.pot")) {
  46.             $this->extract('administrator'$adminpaths);
  47.         }
  48.  
  49.         foreach ($components as $name => $dirs{
  50.             if (!file_exists("$dir/$name.pot")) {
  51.                 $this->extract($name$dirs);
  52.             }
  53.         }
  54.  
  55.         $controller->redirect('index''language');
  56.         #$controller->view('language');
  57.  
  58.     }
  59.  
  60.     function extract($domain$scandirs$language='untranslated')
  61.     {
  62.  
  63.         $path mamboCore::get('rootPath');
  64.         $textdomain $path.'/language';
  65.  
  66.         if (!file_exists("$textdomain/$language/$domain.pot")) {
  67.             $catalog new PHPGettext_Catalog($domain$textdomain);
  68.             $catalog->setproperty('mode'_MODE_POT_);
  69.             $catalog->setproperty('lang'$language);
  70.             $headers $this->header();
  71.             $catalog->setproperty('comments'$headers[0]);
  72.             $catalog->setproperty('headers'$headers[1]);
  73.             $catalog->save();
  74.         }
  75.  
  76.         $this->scan_xml($domain$textdomain$scandirs$language);
  77.         
  78.         $gettext_admin new PHPGettextAdmin();
  79.         $cwd getcwd();
  80.         chdir($path);
  81.  
  82.         $php_sources array();
  83.         if (is_array($scandirs))  {
  84.             foreach ($scandirs as $subdir)  {
  85.                 $php_sources array_merge($php_sources$this->read_dir($subdir'php'true));
  86.             }
  87.         else {
  88.             $php_sources $this->read_dir($scandirs'php'true);
  89.         }
  90.         $gettext_admin->xgettext($domain$textdomain$php_sources$language);
  91.  
  92.         chdir($cwd);
  93.  
  94.         return true;
  95.     }
  96.  
  97.     function scan_xml($domain$path$scandirs$language='untranslated')
  98.     {
  99.         $catalog new PHPGettext_Catalog($domain$path);
  100.         $catalog->setproperty('mode'_MODE_POT_);
  101.         $catalog->setproperty('lang'$language);
  102.         $catalog->load();
  103.         $xml_sources array();
  104.         if (is_array($scandirs))  {
  105.             foreach ($scandirs as $subdir)  {
  106.                 $xml_sources array_merge($xml_sources$this->read_dir($subdir'xml'true));
  107.             }
  108.         else {
  109.             $xml_sources $this->read_dir($scandirs'xml'true);
  110.         }
  111.  
  112.         if (count($xml_sources0{
  113.             $strings array();
  114.             foreach ($xml_sources as $file{
  115.                 $p xml_parser_create();
  116.                 xml_parser_set_option($pXML_OPTION_CASE_FOLDING0);
  117.                 xml_parser_set_option($pXML_OPTION_SKIP_WHITE1);
  118.                 xml_parse_into_struct($pfile_get_contents(mamboCore::get('rootPath').'/'.$file)$values);
  119.                 xml_parser_free($p);
  120.                 foreach($values as $key => $value)
  121.                 {
  122.                     switch ($value['tag'])
  123.                     {
  124.                         case 'name':
  125.                         case 'description':
  126.                         case 'option':
  127.                         if (isset($value['value']&& strlen($value['value']>=1$strings[$file][$value['value'];
  128.                         break;
  129.                         case 'param':
  130.                         if (isset($value['attributes']&& $value['attributes']['type'!= 'spacer'{
  131.                             if (isset($value['attributes']['label'])) $strings[$file][$value['attributes']['label'];
  132.                             if (isset($value['attributes']['description'])) $strings[$file][$value['attributes']['description'];
  133.                         }
  134.                         break;
  135.                     }
  136.                 }
  137.                 if (is_array($strings[$file]))
  138.                 $strings[$filearray_values(array_unique($strings[$file]));
  139.             }
  140.             foreach ($strings as $file => $str{
  141.                 foreach ($str as $msg)
  142.                 $messages[trim($msg)]['#: '.$file;
  143.             }
  144.             foreach ($messages as $msgid => $comments{
  145.                 if (!empty($msgid))
  146.                 $catalog->addentry($msgidnullnull$comments);#($msgid, $msgid_plural=null, $msgstr=null, $comments=array())
  147.             }
  148.             $catalog->save();
  149.         }
  150.     }
  151.  
  152.     function read_dir($dir$filetype='php'$checkSlash false)
  153.     {
  154.         static $root_path;
  155.         $deep true;
  156.         if (substr($dir,-1)=='/' && $checkSlash $deep false;
  157.         if (is_null($root_path))
  158.         $root_path str_replace'\\''/'mamboCore::get('rootPath') );
  159.         if (!file_exists($dir)) return false;
  160.         $array array();
  161.         $d dir($dir);
  162.         while (false !== ($entry $d->read())) {
  163.             if($entry!='.' && $entry!='..'{
  164.                 $entry "$dir/$entry";
  165.                 $entry str_replace'\\''/'$entry );
  166.                 if(is_dir($entry&& $deep{
  167.                     $array array_merge($array$this->read_dir($entry$filetype));
  168.                 elseif (preg_match('/.'.$filetype.'$/'$entry)) {
  169.                     $new_entry str_replace($root_path.'/'''$entry);
  170.                     if ($new_entry[0== '/'$new_entry substr($new_entry1);
  171.                     $array[$new_entry;
  172.                 }
  173.             }
  174.         }
  175.         $d->close();
  176.         return $array;
  177.     }
  178.  
  179.     function header($charset='utf-8'$plurals='nplurals=2; plural=n == 1 ? 0 : 1;'){
  180.         $year date('Y');
  181.         $comments = <<<EOT
  182. # Mambo Open Source.
  183. # Copyright (C) 2005 - $year Mambo Foundation Inc.
  184. # This file is distributed under the same license as the Mambo package.
  185. # Translation Team <translation@mambo-foundation.org>, $year#
  186. #
  187. #, fuzzy
  188. EOT;
  189.         $comments explode("\n"$comments);
  190.         $headers array(
  191.         'Project-Id-Version'    => 'Mambo 4.6',
  192.         'Report-Msgid-Bugs-To'  => 'translation@mambo-foundation.org',
  193.         'POT-Creation-Date'     => date('Y-m-d h:iO'),
  194.         'PO-Revision-Date'      => date('Y-m-d h:iO'),
  195.         'Last-Translator'       => 'Translation <translation@mambo-foundation.org>',
  196.         'Language-Team'         => 'Translation <translation@mambo-foundation.org>',
  197.         'MIME-Version'          => '1.0',
  198.         'Content-Type'          => 'text/plain; charset='.$charset,
  199.         'Content-Transfer-Encoding' => '8bit',
  200.         'Plural-Forms'              => $plurals
  201.         );
  202.         return array($comments$headers);
  203.     }
  204. }
  205. ?>

Documentation generated on Mon, 05 May 2008 16:19:27 +0400 by phpDocumentor 1.4.0