From dbd17a4fcb11d819c1e5285cb7cfa9419ad7ff3c Mon Sep 17 00:00:00 2001 From: Cameron Date: Mon, 6 Jul 2015 14:04:40 -0700 Subject: [PATCH] Fix for failing multi-language methods. --- e107_handlers/mysql_class.php | 104 ++++++++++++++++++++++++---------- 1 file changed, 73 insertions(+), 31 deletions(-) diff --git a/e107_handlers/mysql_class.php b/e107_handlers/mysql_class.php index df50d8a6f..e3d2cd58f 100644 --- a/e107_handlers/mysql_class.php +++ b/e107_handlers/mysql_class.php @@ -1451,14 +1451,12 @@ class e_db_mysql /** * Check for the existence of a matching language table when multi-language tables are active. - * @param string $table Name of table, without the prefix. + * @param string $table Name of table, without the prefix. or an array of table names. * @access private - * @return name of the language table (eg. lan_french_news) + * @return mixed the name of the language table (eg. lan_french_news) or an array of all matching language tables. (with mprefix) */ - function db_IsLang($table,$multiple=FALSE) + function db_IsLang($table, $multiple=false) { - global $pref; - //When running a multi-language site with english included. English must be the main site language. // WARNING!!! FALSE is critical important - if missed, expect dead loop (prefs are calling db handler as well when loading) // Temporary solution, better one is needed @@ -1487,23 +1485,41 @@ class e_db_mysql $table = array($table); } - foreach($this->mySQLtablelist as $tab) + if(!$this->mySQLtableList) { - if(stristr($tab, $this->mySQLPrefix."lan_") !== FALSE) + $this->mySQLtableList = $this->db_mySQLtableList(); + } + + $lanlist = array(); + + foreach($this->mySQLtableList as $tab) + { + + if(substr($tab,0,4) == "lan_") { - $tmp = explode("_",str_replace($this->mySQLPrefix."lan_","",$tab)); - $lng = $tmp[0]; + list($tmp,$lng,$tableName) = explode("_",$tab,3); + foreach($table as $t) { - if(preg_match('/'.$t.'$/i', $tab)) // some str*() check instead? + if($tableName == $t) { - $lanlist[$lng][$this->mySQLPrefix.$t] = $tab; + $lanlist[$lng][$this->mySQLPrefix.$t] = $this->mySQLPrefix.$tab; // prefix needed. } + } } } - return (varset($lanlist)) ? $lanlist : FALSE; + if(empty($lanlist)) + { + return false; + } + else + { + return $lanlist; + } + + } // ------------------------- @@ -1597,53 +1613,67 @@ class e_db_mysql } - /* - Multi-language Query Function. - */ - function db_Query_all($query,$debug="") + /** + * Multi-language Query Function. Run a query on the same table across all languages. + * @param $query + * @param bool $debug + * @return bool + */ + function db_Query_all($query, $debug=false) { $error = ""; - $query = str_replace("#",$this->mySQLPrefix,$query); + $query = str_replace("#", $this->mySQLPrefix, $query); if(!$this->db_Query($query)) { // run query on the default language first. $error .= $query. " failed"; } - $tmp = explode(" ",$query); + $table = array(); + $search = array(); + + $tmp = explode(" ",$query); // split the query + foreach($tmp as $val) { - if(strpos($val,$this->mySQLPrefix) !== FALSE) + if(strpos($val,$this->mySQLPrefix) !== false) // search for table names references using the mprefix { - $table[] = str_replace($this->mySQLPrefix,"",$val); - $search[] = $val; + $table[] = str_replace(array($this->mySQLPrefix,"`"),"", $val); + $search[] = str_replace("`","",$val); } } + if(empty($table) || empty($search)) + { + return false; + } + // Loop thru relevant language tables and replace each tablename within the query. - if($tablist = $this->db_IsLang($table,TRUE)) + + if($tablist = $this->db_IsLang($table, true)) { foreach($tablist as $key=>$tab) { $querylan = $query; + foreach($search as $find) { - $lang = $key; $replace = ($tab[$find] !="") ? $tab[$find] : $find; $querylan = str_replace($find,$replace,$querylan); } - if(!$this->db_Query($querylan)) - { // run query on other language tables. + if(!$this->db_Query($querylan)) // run query on other language tables. + { $error .= $querylan." failed for language"; } + if($debug){ echo "
** lang= ".$querylan; } } } - return ($error)? FALSE : TRUE; + return ($error) ? false : true; } @@ -1892,17 +1922,28 @@ class e_db_mysql } /** - * Return a filtered list of DB tables. - * @param object $mode [optional] all|lan|nolan + * Legacy Alias of tables + * @deprecated + * @param string $mode * @return array */ public function db_TableList($mode='all') + { + return $this->tables($mode); + } + + + /** + * Return a filtered list of DB tables. + * @param object $mode [optional] all|lan|nolan|nologs + * @return array + */ + public function tables($mode='all') { if(!$this->mySQLtableList) { $this->mySQLtableList = $this->db_mySQLtableList(); - } if($mode == 'nologs') @@ -1947,7 +1988,8 @@ class e_db_mysql } } - + + /** * Duplicate a Table Row in a table. */ @@ -2037,7 +2079,7 @@ class e_db_mysql if($table=='*') { $nolog = vartrue($options['nologs']) ? 'nologs' : 'all'; - $tableList = $this->db_TableList($nolog); + $tableList = $this->tables($nolog); } else {