ApiQueryBase::titleToKey and ApiQueryBase::keyToTitle;
[lhc/web/wiklou.git] / includes / api / ApiQueryBase.php
index d3b7d8d..fc1e7dc 100644 (file)
@@ -32,11 +32,11 @@ if (!defined('MEDIAWIKI')) {
  * This is a base class for all Query modules.
  * It provides some common functionality such as constructing various SQL queries.
  *
- * @addtogroup API
+ * @ingroup API
  */
 abstract class ApiQueryBase extends ApiBase {
 
-       private $mQueryModule, $mDb, $tables, $where, $fields, $options;
+       private $mQueryModule, $mDb, $tables, $where, $fields, $options, $join_conds;
 
        public function __construct($query, $moduleName, $paramPrefix = '') {
                parent :: __construct($query->getMain(), $moduleName, $paramPrefix);
@@ -53,6 +53,7 @@ abstract class ApiQueryBase extends ApiBase {
                $this->where = array ();
                $this->fields = array ();
                $this->options = array ();
+               $this->join_conds = array ();
        }
 
        /**
@@ -67,10 +68,33 @@ abstract class ApiQueryBase extends ApiBase {
                        $this->tables = array_merge($this->tables, $tables);
                } else {
                        if (!is_null($alias))
-                               $tables = $this->getDB()->tableName($tables) . ' ' . $alias;
+                               $tables = $this->getAliasedName($tables, $alias);
                        $this->tables[] = $tables;
                }
        }
+       
+       /**
+        * Get the SQL for a table name with alias
+        * @param string $table Table name
+        * @param string $alias Alias
+        * @return string SQL
+        */
+       protected function getAliasedName($table, $alias) {
+               return $this->getDB()->tableName($table) . ' ' . $alias;
+       }
+       
+       /**
+        * Add a set of JOIN conditions to the internal array
+        *
+        * JOIN conditions are formatted as array( tablename => array(jointype, conditions)
+        * e.g. array('page' => array('LEFT JOIN', 'page_id=rev_page'))
+        * @param array $join_conds JOIN conditions
+        */
+       protected function addJoinConds($join_conds) {
+               if(!is_array($join_conds))
+                       ApiBase::dieDebug(__METHOD__, 'Join conditions have to be arrays');
+               $this->join_conds = array_merge($this->join_conds, $join_conds);
+       }
 
        /**
         * Add a set of fields to select to the internal array
@@ -187,7 +211,7 @@ abstract class ApiQueryBase extends ApiBase {
                $db = $this->getDB();
 
                $this->profileDBIn();
-               $res = $db->select($this->tables, $this->fields, $this->where, $method, $this->options);
+               $res = $db->select($this->tables, $this->fields, $this->where, $method, $this->options, $this->join_conds);
                $this->profileDBOut();
 
                return $res;
@@ -295,14 +319,16 @@ abstract class ApiQueryBase extends ApiBase {
        }
 
        /**
-        * This is a very simplistic utility function
-        * to convert a non-namespaced title string to a db key.
-        * It will replace all ' ' with '_'
+        * Convert a title to a DB key
         * @param string $title Page title with spaces
         * @return string Page title with underscores
         */
-       public static function titleToKey($title) {
-               return str_replace(' ', '_', $title);
+       public function titleToKey($title) {
+               # Don't throw an error if we got an empty string
+               if(trim($title) == '') return '';
+               $t = Title::newFromText($title);
+               if(!$t) $this->dieUsageMsg(array('invalidtitle', $title));
+               return $t->getDbKey();
        }
 
        /**
@@ -310,30 +336,13 @@ abstract class ApiQueryBase extends ApiBase {
         * @param string $key Page title with underscores
         * @return string Page title with spaces
         */
-       public static function keyToTitle($key) {
-               return str_replace('_', ' ', $key);
-       }
-
-       /**
-        * Check whether the current user requested a certain token and 
-        * is actually allowed to request it.
-        * @param array $tokenArr Array of tokens the user requested
-        * @param string $action Action to check for
-        * @return bool true if the user requested the token and is allowed to, false otherwise
-        */
-       public function getTokenFlag($tokenArr, $action) {
-               if ($this->getMain()->getRequest()->getVal('callback') !== null) {
-                       // Don't do any session-specific data.
-                       return false;
-               }
-               if (in_array($action, $tokenArr)) {
-                       global $wgUser;
-                       if ($wgUser->isAllowed($action))
-                               return true;
-                       else
-                               $this->dieUsage("Action '$action' is not allowed for the current user", 'permissiondenied');
-               }
-               return false;
+       public function keyToTitle($key) {
+               # Don't throw an error if we got an empty string
+               if(trim($key) == '') return '';
+               $t = Title::newFromDbKey($key);
+               # This really shouldn't happen but we gotta check anyway
+               if(!$t) $this->dieUsageMsg(array('invalidtitle', $key));
+               return $t->getPrefixedText();
        }
 
        /**
@@ -346,7 +355,7 @@ abstract class ApiQueryBase extends ApiBase {
 }
 
 /**
- * @addtogroup API
+ * @ingroup API
  */
 abstract class ApiQueryGeneratorBase extends ApiQueryBase {