Merge "Making listToText() not break if passed a 1-item list."
[lhc/web/wiklou.git] / includes / api / ApiQueryBase.php
index c9c2273..2c48aca 100644 (file)
@@ -4,7 +4,7 @@
  *
  * Created on Sep 7, 2006
  *
- * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
+ * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * @file
  */
 
-if ( !defined( 'MEDIAWIKI' ) ) {
-       // Eclipse helper - will be ignored in production
-       require_once( 'ApiBase.php' );
-}
-
 /**
  * This is a base class for all Query modules.
  * It provides some common functionality such as constructing various SQL
@@ -40,6 +35,11 @@ abstract class ApiQueryBase extends ApiBase {
 
        private $mQueryModule, $mDb, $tables, $where, $fields, $options, $join_conds;
 
+       /**
+        * @param $query ApiBase
+        * @param $moduleName string
+        * @param $paramPrefix string
+        */
        public function __construct( ApiBase $query, $moduleName, $paramPrefix = '' ) {
                parent::__construct( $query->getMain(), $moduleName, $paramPrefix );
                $this->mQueryModule = $query;
@@ -54,6 +54,9 @@ abstract class ApiQueryBase extends ApiBase {
         *
         * Public caching will only be allowed if *all* the modules that supply
         * data for a given request return a cache mode of public.
+        *
+        * @param $params
+        * @return string
         */
        public function getCacheMode( $params ) {
                return 'private';
@@ -84,22 +87,13 @@ abstract class ApiQueryBase extends ApiBase {
                        $this->tables = array_merge( $this->tables, $tables );
                } else {
                        if ( !is_null( $alias ) ) {
-                               $tables = $this->getAliasedName( $tables, $alias );
+                               $this->tables[$alias] = $tables;
+                       } else {
+                               $this->tables[] = $tables;
                        }
-                       $this->tables[] = $tables;
                }
        }
 
-       /**
-        * Get the SQL for a table name with alias
-        * @param $table string Table name
-        * @param $alias string 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
         *
@@ -118,7 +112,7 @@ abstract class ApiQueryBase extends ApiBase {
 
        /**
         * Add a set of fields to select to the internal array
-        * @param $value mixed Field name or array of field names
+        * @param $value array|string Field name or array of field names
         */
        protected function addFields( $value ) {
                if ( is_array( $value ) ) {
@@ -130,7 +124,7 @@ abstract class ApiQueryBase extends ApiBase {
 
        /**
         * Same as addFields(), but add the fields only if a condition is met
-        * @param $value mixed See addFields()
+        * @param $value array|string See addFields()
         * @param $condition bool If false, do nothing
         * @return bool $condition
         */
@@ -220,14 +214,29 @@ abstract class ApiQueryBase extends ApiBase {
 
                if ( $sort ) {
                        $order = $field . ( $isDirNewer ? '' : ' DESC' );
-                       if ( !isset( $this->options['ORDER BY'] ) ) {
-                               $this->addOption( 'ORDER BY', $order );
-                       } else {
-                               $this->addOption( 'ORDER BY', $this->options['ORDER BY'] . ', ' . $order );
-                       }
+                       // Append ORDER BY
+                       $optionOrderBy = isset( $this->options['ORDER BY'] ) ? (array)$this->options['ORDER BY'] : array();
+                       $optionOrderBy[] = $order;
+                       $this->addOption( 'ORDER BY', $optionOrderBy );
                }
        }
 
+       /**
+        * Add a WHERE clause corresponding to a range, similar to addWhereRange,
+        * but converts $start and $end to database timestamps.
+        * @see addWhereRange
+        * @param $field
+        * @param $dir
+        * @param $start
+        * @param $end
+        * @param $sort bool
+        */
+       protected function addTimestampWhereRange( $field, $dir, $start, $end, $sort = true ) {
+               $db = $this->getDb();
+               $this->addWhereRange( $field, $dir,
+                       $db->timestampOrNull( $start ), $db->timestampOrNull( $end ), $sort );
+       }
+
        /**
         * Add an option such as LIMIT or USE INDEX. If an option was set
         * before, the old value will be overwritten
@@ -359,14 +368,15 @@ abstract class ApiQueryBase extends ApiBase {
        protected function setContinueEnumParameter( $paramName, $paramValue ) {
                $paramName = $this->encodeParamName( $paramName );
                $msg = array( $paramName => $paramValue );
-               $this->getResult()->disableSizeCheck();
-               $this->getResult()->addValue( 'query-continue', $this->getModuleName(), $msg );
-               $this->getResult()->enableSizeCheck();
+               $result = $this->getResult();
+               $result->disableSizeCheck();
+               $result->addValue( 'query-continue', $this->getModuleName(), $msg );
+               $result->enableSizeCheck();
        }
 
        /**
         * Get the Query database connection (read-only)
-        * @return Database
+        * @return DatabaseBase
         */
        protected function getDB() {
                if ( is_null( $this->mDb ) ) {
@@ -382,7 +392,7 @@ abstract class ApiQueryBase extends ApiBase {
         * @param $name string Name to assign to the database connection
         * @param $db int One of the DB_* constants
         * @param $groups array Query groups
-        * @return Database
+        * @return DatabaseBase
         */
        public function selectNamedDB( $name, $db, $groups ) {
                $this->mDb = $this->getQuery()->getNamedDB( $name, $db, $groups );
@@ -498,8 +508,7 @@ abstract class ApiQueryBase extends ApiBase {
         * @return void
         */
        public function showHiddenUsersAddBlockInfo( $showBlockInfo ) {
-               global $wgUser;
-               $userCanViewHiddenUsers = $wgUser->isAllowed( 'hideuser' );
+               $userCanViewHiddenUsers = $this->getUser()->isAllowed( 'hideuser' );
 
                if ( $showBlockInfo || !$userCanViewHiddenUsers ) {
                        $this->addTables( 'ipblocks' );
@@ -510,7 +519,7 @@ abstract class ApiQueryBase extends ApiBase {
                        $this->addFields( 'ipb_deleted' );
 
                        if ( $showBlockInfo ) {
-                               $this->addFields( array( 'ipb_reason', 'ipb_by_text', 'ipb_expiry' ) );
+                               $this->addFields( array( 'ipb_id', 'ipb_by', 'ipb_by_text', 'ipb_reason', 'ipb_expiry' ) );
                        }
 
                        // Don't show hidden names
@@ -520,6 +529,25 @@ abstract class ApiQueryBase extends ApiBase {
                }
        }
 
+       /**
+        * @param $hash string
+        * @return bool
+        */
+       public function validateSha1Hash( $hash ) {
+               return preg_match( '/[a-fA-F0-9]{40}/', $hash );
+       }
+
+       /**
+        * @param $hash string
+        * @return bool
+        */
+       public function validateSha1Base36Hash( $hash ) {
+               return preg_match( '/[a-zA-Z0-9]{31}/', $hash );
+       }
+
+       /**
+        * @return array
+        */
        public function getPossibleErrors() {
                return array_merge( parent::getPossibleErrors(), array(
                        array( 'invalidtitle', 'title' ),
@@ -543,6 +571,11 @@ abstract class ApiQueryGeneratorBase extends ApiQueryBase {
 
        private $mIsGenerator;
 
+       /**
+        * @param $query ApiBase
+        * @param $moduleName string
+        * @param $paramPrefix string
+        */
        public function __construct( $query, $moduleName, $paramPrefix = '' ) {
                parent::__construct( $query, $moduleName, $paramPrefix );
                $this->mIsGenerator = false;