Merge "Added deprecation comment to constant that when used throws deprecation exception"
[lhc/web/wiklou.git] / includes / api / ApiQueryBase.php
index 2c48aca..59e6652 100644 (file)
@@ -351,7 +351,7 @@ abstract class ApiQueryBase extends ApiBase {
                }
                $result = $this->getResult();
                $fit = $result->addValue( array( 'query', 'pages', $pageId,
-                                        $this->getModuleName() ), null, $item );
+                       $this->getModuleName() ), null, $item );
                if ( !$fit ) {
                        return false;
                }
@@ -374,14 +374,26 @@ abstract class ApiQueryBase extends ApiBase {
                $result->enableSizeCheck();
        }
 
+       /**
+        * Die with the $prefix.'badcontinue' error. This call is common enough to make it into the base method.
+        * @param $condition boolean will only die if this value is true
+        * @since 1.21
+        */
+       protected function dieContinueUsageIf( $condition ) {
+               if ( $condition ) {
+                       $this->dieUsage(
+                               'Invalid continue param. You should pass the original value returned by the previous query',
+                               'badcontinue' );
+               }
+       }
+
        /**
         * Get the Query database connection (read-only)
         * @return DatabaseBase
         */
        protected function getDB() {
                if ( is_null( $this->mDb ) ) {
-                       $apiQuery = $this->getQuery();
-                       $this->mDb = $apiQuery->getDB();
+                       $this->mDb = $this->getQuery()->getDB();
                }
                return $this->mDb;
        }
@@ -534,7 +546,7 @@ abstract class ApiQueryBase extends ApiBase {
         * @return bool
         */
        public function validateSha1Hash( $hash ) {
-               return preg_match( '/[a-fA-F0-9]{40}/', $hash );
+               return preg_match( '/^[a-f0-9]{40}$/', $hash );
        }
 
        /**
@@ -542,25 +554,28 @@ abstract class ApiQueryBase extends ApiBase {
         * @return bool
         */
        public function validateSha1Base36Hash( $hash ) {
-               return preg_match( '/[a-zA-Z0-9]{31}/', $hash );
+               return preg_match( '/^[a-z0-9]{31}$/', $hash );
        }
 
        /**
         * @return array
         */
        public function getPossibleErrors() {
-               return array_merge( parent::getPossibleErrors(), array(
+               $errors = parent::getPossibleErrors();
+               $errors = array_merge( $errors, array(
                        array( 'invalidtitle', 'title' ),
                        array( 'invalidtitle', 'key' ),
                ) );
-       }
-
-       /**
-        * Get version string for use in the API help output
-        * @return string
-        */
-       public static function getBaseVersion() {
-               return __CLASS__ . ': $Id$';
+               $params = $this->getFinalParams();
+               if ( array_key_exists( 'continue', $params ) ) {
+                       $errors = array_merge( $errors, array(
+                               array(
+                                       'code' => 'badcontinue',
+                                       'info' => 'Invalid continue param. You should pass the original value returned by the previous query'
+                               ),
+                       ) );
+               }
+               return $errors;
        }
 }
 
@@ -569,24 +584,32 @@ abstract class ApiQueryBase extends ApiBase {
  */
 abstract class ApiQueryGeneratorBase extends ApiQueryBase {
 
-       private $mIsGenerator;
+       private $mGeneratorPageSet = null;
 
        /**
-        * @param $query ApiBase
-        * @param $moduleName string
-        * @param $paramPrefix string
+        * Switch this module to generator mode. By default, generator mode is
+        * switched off and the module acts like a normal query module.
+        * @since 1.21 requires pageset parameter
+        * @param $generatorPageSet ApiPageSet object that the module will get
+        *        by calling getPageSet() when in generator mode.
         */
-       public function __construct( $query, $moduleName, $paramPrefix = '' ) {
-               parent::__construct( $query, $moduleName, $paramPrefix );
-               $this->mIsGenerator = false;
+       public function setGeneratorMode( ApiPageSet $generatorPageSet ) {
+               if ( $generatorPageSet === null ) {
+                       ApiBase::dieDebug( __METHOD__, 'Required parameter missing - $generatorPageSet' );
+               }
+               $this->mGeneratorPageSet = $generatorPageSet;
        }
 
        /**
-        * Switch this module to generator mode. By default, generator mode is
-        * switched off and the module acts like a normal query module.
+        * Get the PageSet object to work on.
+        * If this module is generator, the pageSet object is different from other module's
+        * @return ApiPageSet
         */
-       public function setGeneratorMode() {
-               $this->mIsGenerator = true;
+       protected function getPageSet() {
+               if ( $this->mGeneratorPageSet !== null ) {
+                       return $this->mGeneratorPageSet;
+               }
+               return parent::getPageSet();
        }
 
        /**
@@ -595,7 +618,7 @@ abstract class ApiQueryGeneratorBase extends ApiQueryBase {
         * @return string Prefixed parameter name
         */
        public function encodeParamName( $paramName ) {
-               if ( $this->mIsGenerator ) {
+               if ( $this->mGeneratorPageSet !== null ) {
                        return 'g' . parent::encodeParamName( $paramName );
                } else {
                        return parent::encodeParamName( $paramName );
@@ -607,5 +630,5 @@ abstract class ApiQueryGeneratorBase extends ApiQueryBase {
         * @param $resultPageSet ApiPageSet: All output should be appended to
         *  this object
         */
-       public abstract function executeGenerator( $resultPageSet );
+       abstract public function executeGenerator( $resultPageSet );
 }