includes/lang.php
changeset 626 be0e904eec17
parent 623 a7a14bc57855
child 654 18dbf386d356
equal deleted inserted replaced
625:0122f538c242 626:be0e904eec17
    74   
    74   
    75   function __construct($lang)
    75   function __construct($lang)
    76   {
    76   {
    77     global $db, $session, $paths, $template, $plugins; // Common objects
    77     global $db, $session, $paths, $template, $plugins; // Common objects
    78     
    78     
    79     if ( defined('IN_ENANO_INSTALL') && ( !defined('ENANO_CONFIG_FETCHED') || defined('IN_ENANO_UPGRADE') ) )
    79     if ( defined('IN_ENANO_INSTALL') && ( !defined('ENANO_CONFIG_FETCHED') || ( defined('IN_ENANO_UPGRADE') && !defined('IN_ENANO_UPGRADE_POST') ) ) )
    80     {
    80     {
    81       // special case for the Enano installer: it will load its own strings from a JSON file and just use this API for fetching
    81       // special case for the Enano installer: it will load its own strings from a JSON file and just use this API for fetching
    82       // and templatizing them.
    82       // and templatizing them.
    83       // 1.1.4 fix: this was still being called after main API startup from installer payload
    83       // 1.1.4 fix: this was still being called after main API startup from installer payload
    84       $this->lang_id   = 1;
    84       $this->lang_id   = 1;
   310   }
   310   }
   311   
   311   
   312   /**
   312   /**
   313    * Imports a JSON-format language file into the database and merges with current strings.
   313    * Imports a JSON-format language file into the database and merges with current strings.
   314    * @param string Path to the JSON file to load
   314    * @param string Path to the JSON file to load
       
   315    * @param bool If true, only imports new strings and skips those that already exist. Defaults to false (import all strings)
   315    * @param bool Enable debugging output, makes the process over CLI more interesting
   316    * @param bool Enable debugging output, makes the process over CLI more interesting
   316    */
   317    */
   317   
   318   
   318   function import($file, $debug = false)
   319   function import($file, $skip_existing = false, $debug = false)
   319   {
   320   {
   320     global $db, $session, $paths, $template, $plugins; // Common objects
   321     global $db, $session, $paths, $template, $plugins; // Common objects
   321     
   322     
   322     if ( !file_exists($file) )
   323     if ( !file_exists($file) )
   323       $db->_die('lang.php - can\'t import language file: string file doesn\'t exist');
   324       $db->_die('lang.php - can\'t import language file: string file doesn\'t exist');
   365     }
   366     }
   366     
   367     
   367     if ( $debug )
   368     if ( $debug )
   368       echo "  Starting string import$br\n";
   369       echo "  Starting string import$br\n";
   369     
   370     
   370     return $this->import_array($langdata, $debug);
   371     return $this->import_array($langdata, $skip_existing, $debug);
   371   }
   372   }
   372   
   373   
   373   /**
   374   /**
   374    * Imports a JSON-format language file into the database and merges with current strings.
   375    * Imports a JSON-format language file into the database and merges with current strings.
   375    * @param string Path to plugin file
   376    * @param string Path to plugin file
   435   }
   436   }
   436   
   437   
   437   /**
   438   /**
   438    * Performs the actual import of string data.
   439    * Performs the actual import of string data.
   439    * @param array Parsed JSON object, should be in the form of an array
   440    * @param array Parsed JSON object, should be in the form of an array
       
   441    * @param bool If true, only imports new strings and skips those that already exist. Defaults to false.
   440    * @param bool Enable debugging output
   442    * @param bool Enable debugging output
   441    * @access private
   443    * @access private
   442    */
   444    */
   443   
   445   
   444   protected function import_array($langdata, $debug = false) 
   446   protected function import_array($langdata, $skip_existing = false, $debug = false) 
   445   {
   447   {
   446     global $db, $session, $paths, $template, $plugins; // Common objects
   448     global $db, $session, $paths, $template, $plugins; // Common objects
   447     
   449     
   448     if ( !isset($langdata['categories']) || !isset($langdata['strings']) )
   450     if ( !isset($langdata['categories']) || !isset($langdata['strings']) )
   449       $db->_die('lang.php - language file does not contain the proper items');
   451       $db->_die('lang.php - language file does not contain the proper items');
   463           $desc = ( isset($langdata['strings']['meta'][$category]) ) ? $langdata['strings']['meta'][$category] : $this->get("meta_$category");
   465           $desc = ( isset($langdata['strings']['meta'][$category]) ) ? $langdata['strings']['meta'][$category] : $this->get("meta_$category");
   464           echo "  Indexing category: $category ({$desc})$br\n";
   466           echo "  Indexing category: $category ({$desc})$br\n";
   465         }
   467         }
   466         foreach ( $langdata['strings'][$category] as $string_name => $string_value )
   468         foreach ( $langdata['strings'][$category] as $string_name => $string_value )
   467         {
   469         {
       
   470           // should we skip this string?
       
   471           if ( isset($this->strings[$category]) && $skip_existing )
       
   472           {
       
   473             if ( isset($this->strings[$category][$string_name]) )
       
   474             {
       
   475               // already exists, skip
       
   476               if ( $debug )
       
   477                 echo "    Skipping string (already exists): {$category}_{$string_name}$br\n";
       
   478               continue;
       
   479             }
       
   480           }
   468           $string_name = $db->escape($string_name);
   481           $string_name = $db->escape($string_name);
   469           $string_value = $db->escape($string_value);
   482           $string_value = $db->escape($string_value);
   470           $category_name = $db->escape($category);
   483           $category_name = $db->escape($category);
   471           $insert_list[] = "({$this->lang_id}, '$category_name', '$string_name', '$string_value')";
   484           $insert_list[] = "({$this->lang_id}, '$category_name', '$string_name', '$string_value')";
   472           $delete_list[] = "( lang_id = {$this->lang_id} AND string_category = '$category_name' AND string_name = '$string_name' )";
   485           $delete_list[] = "( lang_id = {$this->lang_id} AND string_category = '$category_name' AND string_name = '$string_name' )";