Merge "Add support for PHP7 random_bytes in favor of mcrypt_create_iv"
[lhc/web/wiklou.git] / includes / api / ApiBase.php
index c1ad947..c03faf0 100644 (file)
@@ -180,6 +180,12 @@ abstract class ApiBase extends ContextSource {
         */
        const PARAM_ALL = 17;
 
+       /**
+        * (int[]) When PARAM_TYPE is 'namespace', include these as additional possible values.
+        * @since 1.29
+        */
+       const PARAM_EXTRA_NAMESPACES = 18;
+
        /**@}*/
 
        const ALL_DEFAULT_STRING = '*';
@@ -694,7 +700,7 @@ abstract class ApiBase extends ContextSource {
         * @return array
         */
        public function extractRequestParams( $parseLimit = true ) {
-               // Cache parameters, for performance and to avoid bug 24564.
+               // Cache parameters, for performance and to avoid T26564.
                if ( !isset( $this->mParamCache[$parseLimit] ) ) {
                        $params = $this->getFinalParams();
                        $results = [];
@@ -899,6 +905,34 @@ abstract class ApiBase extends ContextSource {
                return $pageObj;
        }
 
+       /**
+        * Get a Title object from a title or pageid param, if possible.
+        * Can die, if no param is set or if the title or page id is not valid.
+        *
+        * @since 1.29
+        * @param array $params
+        * @return Title
+        */
+       public function getTitleFromTitleOrPageId( $params ) {
+               $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
+
+               $titleObj = null;
+               if ( isset( $params['title'] ) ) {
+                       $titleObj = Title::newFromText( $params['title'] );
+                       if ( !$titleObj || $titleObj->isExternal() ) {
+                               $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
+                       }
+                       return $titleObj;
+               } elseif ( isset( $params['pageid'] ) ) {
+                       $titleObj = Title::newFromID( $params['pageid'] );
+                       if ( !$titleObj ) {
+                               $this->dieWithError( [ 'apierror-nosuchpageid', $params['pageid'] ] );
+                       }
+               }
+
+               return $titleObj;
+       }
+
        /**
         * Return true if we're to watch the page, false if not, null if no change.
         * @param string $watchlist Valid values: 'watch', 'unwatch', 'preferences', 'nochange'
@@ -953,44 +987,41 @@ abstract class ApiBase extends ContextSource {
                // Some classes may decide to change parameter names
                $encParamName = $this->encodeParamName( $paramName );
 
+               // Shorthand
                if ( !is_array( $paramSettings ) ) {
-                       $default = $paramSettings;
-                       $multi = false;
-                       $type = gettype( $paramSettings );
-                       $dupes = false;
-                       $deprecated = false;
-                       $required = false;
-                       $allowAll = false;
-               } else {
-                       $default = isset( $paramSettings[self::PARAM_DFLT] )
-                               ? $paramSettings[self::PARAM_DFLT]
-                               : null;
-                       $multi = isset( $paramSettings[self::PARAM_ISMULTI] )
-                               ? $paramSettings[self::PARAM_ISMULTI]
-                               : false;
-                       $type = isset( $paramSettings[self::PARAM_TYPE] )
-                               ? $paramSettings[self::PARAM_TYPE]
-                               : null;
-                       $dupes = isset( $paramSettings[self::PARAM_ALLOW_DUPLICATES] )
-                               ? $paramSettings[self::PARAM_ALLOW_DUPLICATES]
-                               : false;
-                       $deprecated = isset( $paramSettings[self::PARAM_DEPRECATED] )
-                               ? $paramSettings[self::PARAM_DEPRECATED]
-                               : false;
-                       $required = isset( $paramSettings[self::PARAM_REQUIRED] )
-                               ? $paramSettings[self::PARAM_REQUIRED]
-                               : false;
-                       $allowAll = isset( $paramSettings[self::PARAM_ALL] )
-                               ? $paramSettings[self::PARAM_ALL]
-                               : false;
-
-                       // When type is not given, and no choices, the type is the same as $default
-                       if ( !isset( $type ) ) {
-                               if ( isset( $default ) ) {
-                                       $type = gettype( $default );
-                               } else {
-                                       $type = 'NULL'; // allow everything
-                               }
+                       $paramSettings = [
+                               self::PARAM_DFLT => $paramSettings,
+                       ];
+               }
+
+               $default = isset( $paramSettings[self::PARAM_DFLT] )
+                       ? $paramSettings[self::PARAM_DFLT]
+                       : null;
+               $multi = isset( $paramSettings[self::PARAM_ISMULTI] )
+                       ? $paramSettings[self::PARAM_ISMULTI]
+                       : false;
+               $type = isset( $paramSettings[self::PARAM_TYPE] )
+                       ? $paramSettings[self::PARAM_TYPE]
+                       : null;
+               $dupes = isset( $paramSettings[self::PARAM_ALLOW_DUPLICATES] )
+                       ? $paramSettings[self::PARAM_ALLOW_DUPLICATES]
+                       : false;
+               $deprecated = isset( $paramSettings[self::PARAM_DEPRECATED] )
+                       ? $paramSettings[self::PARAM_DEPRECATED]
+                       : false;
+               $required = isset( $paramSettings[self::PARAM_REQUIRED] )
+                       ? $paramSettings[self::PARAM_REQUIRED]
+                       : false;
+               $allowAll = isset( $paramSettings[self::PARAM_ALL] )
+                       ? $paramSettings[self::PARAM_ALL]
+                       : false;
+
+               // When type is not given, and no choices, the type is the same as $default
+               if ( !isset( $type ) ) {
+                       if ( isset( $default ) ) {
+                               $type = gettype( $default );
+                       } else {
+                               $type = 'NULL'; // allow everything
                        }
                }
 
@@ -1034,6 +1065,11 @@ abstract class ApiBase extends ContextSource {
 
                        if ( isset( $value ) && $type == 'namespace' ) {
                                $type = MWNamespace::getValidNamespaces();
+                               if ( isset( $paramSettings[self::PARAM_EXTRA_NAMESPACES] ) &&
+                                       is_array( $paramSettings[self::PARAM_EXTRA_NAMESPACES] )
+                               ) {
+                                       $type = array_merge( $type, $paramSettings[self::PARAM_EXTRA_NAMESPACES] );
+                               }
                                // By default, namespace parameters allow ALL_DEFAULT_STRING to be used to specify
                                // all namespaces.
                                $allowAll = true;
@@ -1290,7 +1326,7 @@ abstract class ApiBase extends ContextSource {
                }
 
                if ( !$allowMultiple && count( $valuesList ) != 1 ) {
-                       // Bug 33482 - Allow entries with | in them for non-multiple values
+                       // T35482 - Allow entries with | in them for non-multiple values
                        if ( in_array( $value, $allowedValues, true ) ) {
                                return $value;
                        }
@@ -1682,6 +1718,18 @@ abstract class ApiBase extends ContextSource {
                        $this->logFeatureUsage( $feature );
                }
                $this->addWarning( $msg, 'deprecation', $data );
+
+               // No real need to deduplicate here, ApiErrorFormatter does that for
+               // us (assuming the hook is deterministic).
+               $msgs = [ $this->msg( 'api-usage-mailinglist-ref' ) ];
+               Hooks::run( 'ApiDeprecationHelp', [ &$msgs ] );
+               if ( count( $msgs ) > 1 ) {
+                       $key = '$' . join( ' $', range( 1, count( $msgs ) ) );
+                       $msg = ( new RawMessage( $key ) )->params( $msgs );
+               } else {
+                       $msg = reset( $msgs );
+               }
+               $this->getMain()->addWarning( $msg, 'deprecation-help' );
        }
 
        /**
@@ -1936,7 +1984,10 @@ abstract class ApiBase extends ContextSource {
         */
        public function getFinalDescription() {
                $desc = $this->getDescription();
-               Hooks::run( 'APIGetDescription', [ &$this, &$desc ] );
+
+               // Avoid PHP 7.1 warning of passing $this by reference
+               $apiModule = $this;
+               Hooks::run( 'APIGetDescription', [ &$apiModule, &$desc ] );
                $desc = self::escapeWikiText( $desc );
                if ( is_array( $desc ) ) {
                        $desc = implode( "\n", $desc );
@@ -1984,7 +2035,9 @@ abstract class ApiBase extends ContextSource {
                        ] + ( isset( $params['token'] ) ? $params['token'] : [] );
                }
 
-               Hooks::run( 'APIGetAllowedParams', [ &$this, &$params, $flags ] );
+               // Avoid PHP 7.1 warning of passing $this by reference
+               $apiModule = $this;
+               Hooks::run( 'APIGetAllowedParams', [ &$apiModule, &$params, $flags ] );
 
                return $params;
        }
@@ -2002,7 +2055,10 @@ abstract class ApiBase extends ContextSource {
                $path = $this->getModulePath();
 
                $desc = $this->getParamDescription();
-               Hooks::run( 'APIGetParamDescription', [ &$this, &$desc ] );
+
+               // Avoid PHP 7.1 warning of passing $this by reference
+               $apiModule = $this;
+               Hooks::run( 'APIGetParamDescription', [ &$apiModule, &$desc ] );
 
                if ( !$desc ) {
                        $desc = [];