includes/lang.php
changeset 514 ecbfb747743e
parent 507 586fd7d3202d
child 516 9a52679143db
equal deleted inserted replaced
513:c15fbf197a54 514:ecbfb747743e
   305   }
   305   }
   306   
   306   
   307   /**
   307   /**
   308    * Imports a JSON-format language file into the database and merges with current strings.
   308    * Imports a JSON-format language file into the database and merges with current strings.
   309    * @param string Path to the JSON file to load
   309    * @param string Path to the JSON file to load
   310    */
   310    * @param bool Enable debugging output, makes the process over CLI more interesting
   311   
   311    */
   312   function import($file)
   312   
       
   313   function import($file, $debug = false)
   313   {
   314   {
   314     global $db, $session, $paths, $template, $plugins; // Common objects
   315     global $db, $session, $paths, $template, $plugins; // Common objects
   315     
   316     
   316     if ( !file_exists($file) )
   317     if ( !file_exists($file) )
   317       $db->_die('lang.php - can\'t import language file: string file doesn\'t exist');
   318       $db->_die('lang.php - can\'t import language file: string file doesn\'t exist');
   318     
   319     
   319     if ( $this->lang_id == 0 )
   320     if ( $this->lang_id == 0 )
   320       $db->_die('lang.php - BUG: trying to perform import when $lang->lang_id == 0');
   321       $db->_die('lang.php - BUG: trying to perform import when $lang->lang_id == 0');
   321     
   322     
       
   323     if ( $debug )
       
   324       echo "Importing file: $file\n  Checking file...\n";
       
   325     
   322     $contents = trim(@file_get_contents($file));
   326     $contents = trim(@file_get_contents($file));
   323     
   327     
   324     if ( empty($contents) )
   328     if ( empty($contents) )
   325       $db->_die('lang.php - can\'t load the contents of the language file');
   329       $db->_die('lang.php - can\'t load the contents of the language file');
       
   330     
       
   331     if ( $debug )
       
   332       echo "  Cleaning up JSON\n";
   326     
   333     
   327     // Trim off all text before and after the starting and ending braces
   334     // Trim off all text before and after the starting and ending braces
   328     $contents = preg_replace('/^([^{]+)\{/', '{', $contents);
   335     $contents = preg_replace('/^([^{]+)\{/', '{', $contents);
   329     $contents = preg_replace('/\}([^}]+)$/', '}', $contents);
   336     $contents = preg_replace('/\}([^}]+)$/', '}', $contents);
   330     
   337     
   331     // Correct syntax to be nice to the json parser
   338     // Correct syntax to be nice to the json parser
   332     $contents = enano_clean_json($contents);
   339     $contents = enano_clean_json($contents);
   333     
   340     
       
   341     if ( $debug )
       
   342       echo "  Decoding JSON stream\n";
       
   343     
   334     try
   344     try
   335     {
   345     {
   336       $langdata = enano_json_decode($contents);
   346       $langdata = enano_json_decode($contents);
   337     }
   347     }
   338     catch(Zend_Json_Exception $e)
   348     catch(Zend_Json_Exception $e)
   344     if ( !is_array($langdata) )
   354     if ( !is_array($langdata) )
   345     {
   355     {
   346       $db->_die('lang.php - invalid or non-well-formed language file');
   356       $db->_die('lang.php - invalid or non-well-formed language file');
   347     }
   357     }
   348     
   358     
   349     return $this->import_array($langdata);
   359     if ( $debug )
       
   360       echo "  Starting string import\n";
       
   361     
       
   362     return $this->import_array($langdata, $debug);
   350   }
   363   }
   351   
   364   
   352   /**
   365   /**
   353    * Imports a JSON-format language file into the database and merges with current strings.
   366    * Imports a JSON-format language file into the database and merges with current strings.
   354    * @param string Path to plugin file
   367    * @param string Path to plugin file
   415   }
   428   }
   416   
   429   
   417   /**
   430   /**
   418    * Performs the actual import of string data.
   431    * Performs the actual import of string data.
   419    * @param array Parsed JSON object, should be in the form of an array
   432    * @param array Parsed JSON object, should be in the form of an array
       
   433    * @param bool Enable debugging output
   420    * @access private
   434    * @access private
   421    */
   435    */
   422   
   436   
   423   protected function import_array($langdata) 
   437   protected function import_array($langdata, $debug = false) 
   424   {
   438   {
   425     global $db, $session, $paths, $template, $plugins; // Common objects
   439     global $db, $session, $paths, $template, $plugins; // Common objects
   426     
   440     
   427     if ( !isset($langdata['categories']) || !isset($langdata['strings']) )
   441     if ( !isset($langdata['categories']) || !isset($langdata['strings']) )
   428       $db->_die('lang.php - language file does not contain the proper items');
   442       $db->_die('lang.php - language file does not contain the proper items');
   432     
   446     
   433     foreach ( $langdata['categories'] as $category )
   447     foreach ( $langdata['categories'] as $category )
   434     {
   448     {
   435       if ( isset($langdata['strings'][$category]) )
   449       if ( isset($langdata['strings'][$category]) )
   436       {
   450       {
       
   451         if ( $debug )
       
   452         {
       
   453           $desc = ( isset($langdata['strings']['meta'][$category]) ) ? $langdata['strings']['meta'][$category] : $this->get("meta_$category");
       
   454           echo "  Indexing category: $category ({$desc})\n";
       
   455         }
   437         foreach ( $langdata['strings'][$category] as $string_name => $string_value )
   456         foreach ( $langdata['strings'][$category] as $string_name => $string_value )
   438         {
   457         {
   439           $string_name = $db->escape($string_name);
   458           $string_name = $db->escape($string_name);
   440           $string_value = $db->escape($string_value);
   459           $string_value = $db->escape($string_value);
   441           $category_name = $db->escape($category);
   460           $category_name = $db->escape($category);
   443           $delete_list[] = "( lang_id = {$this->lang_id} AND string_category = '$category_name' AND string_name = '$string_name' )";
   462           $delete_list[] = "( lang_id = {$this->lang_id} AND string_category = '$category_name' AND string_name = '$string_name' )";
   444         }
   463         }
   445       }
   464       }
   446     }
   465     }
   447     
   466     
       
   467     if ( $debug )
       
   468     {
       
   469       echo "  Running deletion of old strings...";
       
   470       $start = microtime_float();
       
   471     }
   448     $delete_list = implode(" OR\n  ", $delete_list);
   472     $delete_list = implode(" OR\n  ", $delete_list);
   449     
   473     
   450     if ( !empty($delete_list) )
   474     if ( !empty($delete_list) )
   451     {
   475     {
   452       $sql = "DELETE FROM " . table_prefix . "language_strings WHERE $delete_list;";
   476       $sql = "DELETE FROM " . table_prefix . "language_strings WHERE $delete_list;";
   458       $q = $db->sql_query($sql);
   482       $q = $db->sql_query($sql);
   459       if ( !$q )
   483       if ( !$q )
   460         $db->_die('lang.php - couldn\'t kill off them old strings');
   484         $db->_die('lang.php - couldn\'t kill off them old strings');
   461     }
   485     }
   462     
   486     
       
   487     if ( $debug )
       
   488     {
       
   489       $time = round(microtime_float() - $start, 5);
       
   490       echo "({$time}s)\n";
       
   491     }
       
   492     
   463     if ( !empty($insert_list) )
   493     if ( !empty($insert_list) )
   464     {
   494     {
       
   495       if ( $debug )
       
   496       {
       
   497         echo "  Inserting strings...";
       
   498         $start = microtime_float();
       
   499       }
   465       $insert_list = implode(",\n  ", $insert_list);
   500       $insert_list = implode(",\n  ", $insert_list);
   466       $sql = "INSERT INTO " . table_prefix . "language_strings(lang_id, string_category, string_name, string_content) VALUES\n  $insert_list;";
   501       $sql = "INSERT INTO " . table_prefix . "language_strings(lang_id, string_category, string_name, string_content) VALUES\n  $insert_list;";
   467       
   502       
   468       // Free some memory
   503       // Free some memory
   469       unset($insert_list);
   504       unset($insert_list);
   470       
   505       
   471       // Run the query
   506       // Run the query
   472       $q = $db->sql_query($sql);
   507       $q = $db->sql_query($sql);
   473       if ( !$q )
   508       if ( !$q )
   474         $db->_die('lang.php - couldn\'t insert strings in import()');
   509         $db->_die('lang.php - couldn\'t insert strings in import()');
       
   510       
       
   511       if ( $debug )
       
   512       {
       
   513         $time = round(microtime_float() - $start, 5);
       
   514         echo "({$time}s)\n";
       
   515       }
   475     }
   516     }
   476     
   517     
   477     // YAY! done!
   518     // YAY! done!
   478     // This will regenerate the cache file if possible.
   519     // This will regenerate the cache file if possible.
       
   520     if ( $debug )
       
   521       echo "  Regenerating cache file\n";
   479     $this->regen_caches();
   522     $this->regen_caches();
   480   }
   523   }
   481   
   524   
   482   /**
   525   /**
   483    * Refetches the strings and writes out the cache file.
   526    * Refetches the strings and writes out the cache file.