Merge "Deprecate no longer used Skin::getCommonStylePath."
[lhc/web/wiklou.git] / includes / api / ApiQueryBase.php
index 8d43479..92fabdd 100644 (file)
  * @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 ) {
@@ -111,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 ) ) {
@@ -123,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
         */
@@ -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,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 ) ) {
@@ -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
@@ -513,6 +529,22 @@ 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
         */