Merge "Making listToText() not break if passed a 1-item list."
[lhc/web/wiklou.git] / includes / api / ApiQueryBase.php
index 628611d..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;
@@ -55,6 +55,7 @@ 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 ) {
@@ -213,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
@@ -352,9 +368,10 @@ 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();
        }
 
        /**
@@ -375,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 );
@@ -491,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' );
@@ -503,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
@@ -555,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;