Merge "Replace dev.w3.org with more permanent or stable urls"
[lhc/web/wiklou.git] / includes / api / ApiBase.php
index 2935321..3a31b2a 100644 (file)
@@ -66,13 +66,11 @@ abstract class ApiBase extends ContextSource {
        const PARAM_RANGE_ENFORCE = 9;
        /// @since 1.25
        // Specify an alternative i18n message for this help parameter.
-       // Value can be a string key, an array giving key and parameters, or a
-       // Message object.
+       // Value is $msg for ApiBase::makeMessage()
        const PARAM_HELP_MSG = 10;
        /// @since 1.25
        // Specify additional i18n messages to append to the normal message. Value
-       // is an array of any of strings giving the message key, arrays giving key and
-       // parameters, or Message objects.
+       // is an array of $msg for ApiBase::makeMessage()
        const PARAM_HELP_MSG_APPEND = 11;
        /// @since 1.25
        // Specify additional information tags for the parameter. Value is an array
@@ -82,9 +80,14 @@ abstract class ApiBase extends ContextSource {
        // comma-joined list of values, $3 = module prefix.
        const PARAM_HELP_MSG_INFO = 12;
        /// @since 1.25
-       // When PARAM_DFLT is an array, this may be an array mapping those values
+       // When PARAM_TYPE is an array, this may be an array mapping those values
        // to page titles which will be linked in the help.
        const PARAM_VALUE_LINKS = 13;
+       /// @since 1.25
+       // When PARAM_TYPE is an array, this is an array mapping those values to
+       // $msg for ApiBase::makeMessage(). Any value not having a mapping will use
+       // apihelp-{$path}-paramvalue-{$param}-{$value} is used.
+       const PARAM_HELP_MSG_PER_VALUE = 14;
 
        const LIMIT_BIG1 = 500; // Fast query, std user limit
        const LIMIT_BIG2 = 5000; // Fast query, bot/sysop limit
@@ -284,7 +287,8 @@ abstract class ApiBase extends ContextSource {
        }
 
        /**
-        * Indicates whether this module is "internal" or unstable
+        * Indicates whether this module is "internal"
+        * Internal API modules are not (yet) intended for 3rd party use and may be unstable.
         * @since 1.25
         * @return bool
         */
@@ -1041,6 +1045,7 @@ abstract class ApiBase extends ContextSource {
         * @param string $token Supplied token
         * @param array $params All supplied parameters for the module
         * @return bool
+        * @throws MWException
         */
        final public function validateToken( $token, array $params ) {
                $tokenType = $this->needsToken();
@@ -1290,6 +1295,7 @@ abstract class ApiBase extends ContextSource {
         * @since 1.23
         * @param Status $status
         * @return array Array of code and error string
+        * @throws MWException
         */
        public function getErrorFromStatus( $status ) {
                if ( $status->isGood() ) {
@@ -1638,6 +1644,10 @@ abstract class ApiBase extends ContextSource {
                        'code' => 'missingtitle',
                        'info' => "The article you tried to edit doesn't exist"
                ),
+               'cantchangecontentmodel' => array(
+                       'code' => 'cantchangecontentmodel',
+                       'info' => "You don't have permission to change the content model of a page"
+               ),
                'nosuchrcid' => array(
                        'code' => 'nosuchrcid',
                        'info' => "There is no change with rcid \"\$1\""
@@ -1952,7 +1962,7 @@ abstract class ApiBase extends ContextSource {
         */
        public function getFinalDescription() {
                $desc = $this->getDescription();
-               wfRunHooks( 'APIGetDescription', array( &$this, &$desc ) );
+               Hooks::run( 'APIGetDescription', array( &$this, &$desc ) );
                $desc = self::escapeWikiText( $desc );
                if ( is_array( $desc ) ) {
                        $desc = join( "\n", $desc );
@@ -1970,7 +1980,7 @@ abstract class ApiBase extends ContextSource {
                }
                $msgs = array( $msg );
 
-               wfRunHooks( 'APIGetDescriptionMessages', array( $this, &$msgs ) );
+               Hooks::run( 'APIGetDescriptionMessages', array( $this, &$msgs ) );
 
                return $msgs;
        }
@@ -2000,7 +2010,7 @@ abstract class ApiBase extends ContextSource {
                        ) + ( isset( $params['token'] ) ? $params['token'] : array() );
                }
 
-               wfRunHooks( 'APIGetAllowedParams', array( &$this, &$params, $flags ) );
+               Hooks::run( 'APIGetAllowedParams', array( &$this, &$params, $flags ) );
 
                return $params;
        }
@@ -2013,8 +2023,12 @@ abstract class ApiBase extends ContextSource {
         * @return array Keys are parameter names, values are arrays of Message objects
         */
        public function getFinalParamDescription() {
+               $prefix = $this->getModulePrefix();
+               $name = $this->getModuleName();
+               $path = $this->getModulePath();
+
                $desc = $this->getParamDescription();
-               wfRunHooks( 'APIGetParamDescription', array( &$this, &$desc ) );
+               Hooks::run( 'APIGetParamDescription', array( &$this, &$desc ) );
 
                if ( !$desc ) {
                        $desc = array();
@@ -2043,35 +2057,61 @@ abstract class ApiBase extends ContextSource {
                        if ( isset( $settings[ApiBase::PARAM_HELP_MSG] ) ) {
                                $msg = $settings[ApiBase::PARAM_HELP_MSG];
                        } else {
-                               $msg = $this->msg( "apihelp-{$this->getModulePath()}-param-{$param}" );
+                               $msg = $this->msg( "apihelp-{$path}-param-{$param}" );
                                if ( !$msg->exists() ) {
                                        $msg = $this->msg( 'api-help-fallback-parameter', $d );
                                }
                        }
-                       $msg = ApiBase::makeMessage( $msg, $this->getContext(), array(
-                               $this->getModulePrefix(),
-                               $param,
-                               $this->getModuleName(),
-                               $this->getModulePath(),
-                       ) );
+                       $msg = ApiBase::makeMessage( $msg, $this->getContext(),
+                               array( $prefix, $param, $name, $path ) );
                        if ( !$msg ) {
                                $this->dieDebug( __METHOD__,
                                        'Value in ApiBase::PARAM_HELP_MSG is not valid' );
                        }
                        $msgs[$param] = array( $msg );
 
+                       if ( isset( $settings[ApiBase::PARAM_HELP_MSG_PER_VALUE] ) ) {
+                               if ( !is_array( $settings[ApiBase::PARAM_HELP_MSG_PER_VALUE] ) ) {
+                                       $this->dieDebug( __METHOD__,
+                                               'ApiBase::PARAM_HELP_MSG_PER_VALUE is not valid' );
+                               }
+                               if ( !is_array( $settings[ApiBase::PARAM_TYPE] ) ) {
+                                       $this->dieDebug( __METHOD__,
+                                               'ApiBase::PARAM_HELP_MSG_PER_VALUE may only be used when ' .
+                                               'ApiBase::PARAM_TYPE is an array' );
+                               }
+
+                               $valueMsgs = $settings[ApiBase::PARAM_HELP_MSG_PER_VALUE];
+                               foreach ( $settings[ApiBase::PARAM_TYPE] as $value ) {
+                                       if ( isset( $valueMsgs[$value] ) ) {
+                                               $msg = $valueMsgs[$value];
+                                       } else {
+                                               $msg = "apihelp-{$path}-paramvalue-{$param}-{$value}";
+                                       }
+                                       $m = ApiBase::makeMessage( $msg, $this->getContext(),
+                                               array( $prefix, $param, $name, $path, $value ) );
+                                       if ( $m ) {
+                                               $m = new ApiHelpParamValueMessage(
+                                                       $value,
+                                                       array( $m->getKey(), 'api-help-param-no-description' ),
+                                                       $m->getParams()
+                                               );
+                                               $msgs[$param][] = $m->setContext( $this->getContext() );
+                                       } else {
+                                               $this->dieDebug( __METHOD__,
+                                                       "Value in ApiBase::PARAM_HELP_MSG_PER_VALUE for $value is not valid" );
+                                       }
+                               }
+                       }
+
                        if ( isset( $settings[ApiBase::PARAM_HELP_MSG_APPEND] ) ) {
                                if ( !is_array( $settings[ApiBase::PARAM_HELP_MSG_APPEND] ) ) {
                                        $this->dieDebug( __METHOD__,
                                                'Value for ApiBase::PARAM_HELP_MSG_APPEND is not an array' );
                                }
                                foreach ( $settings[ApiBase::PARAM_HELP_MSG_APPEND] as $m ) {
-                                       $m = ApiBase::makeMessage( $m, $this->getContext(), array(
-                                               $this->getModulePrefix(),
-                                               $param,
-                                               $this->getModuleName(),
-                                               $this->getModulePath(),
-                                       ) );
+                                       $m = ApiBase::makeMessage( $m, $this->getContext(),
+                                               array( $prefix, $param, $name, $path ) );
                                        if ( $m ) {
                                                $msgs[$param][] = $m;
                                        } else {
@@ -2082,7 +2122,7 @@ abstract class ApiBase extends ContextSource {
                        }
                }
 
-               wfRunHooks( 'APIGetParamDescriptionMessages', array( $this, &$msgs ) );
+               Hooks::run( 'APIGetParamDescriptionMessages', array( $this, &$msgs ) );
 
                return $msgs;
        }
@@ -2141,6 +2181,10 @@ abstract class ApiBase extends ContextSource {
         * Profiling: total module execution time
         */
        private $mTimeIn = 0, $mModuleTime = 0;
+       /** @var ScopedCallback */
+       private $profile;
+       /** @var ScopedCallback */
+       private $dbProfile;
 
        /**
         * Get the name of the module as shown in the profiler log
@@ -2165,7 +2209,7 @@ abstract class ApiBase extends ContextSource {
                        ApiBase::dieDebug( __METHOD__, 'Called twice without calling profileOut()' );
                }
                $this->mTimeIn = microtime( true );
-               wfProfileIn( $this->getModuleProfileName() );
+               $this->profile = Profiler::instance()->scopedProfileIn( $this->getModuleProfileName() );
        }
 
        /**
@@ -2184,7 +2228,7 @@ abstract class ApiBase extends ContextSource {
 
                $this->mModuleTime += microtime( true ) - $this->mTimeIn;
                $this->mTimeIn = 0;
-               wfProfileOut( $this->getModuleProfileName() );
+               Profiler::instance()->scopedProfileOut( $this->profile );
        }
 
        /**
@@ -2231,7 +2275,8 @@ abstract class ApiBase extends ContextSource {
                        ApiBase::dieDebug( __METHOD__, 'Called twice without calling profileDBOut()' );
                }
                $this->mDBTimeIn = microtime( true );
-               wfProfileIn( $this->getModuleProfileName( true ) );
+
+               $this->dbProfile = Profiler::instance()->scopedProfileIn( $this->getModuleProfileName( true ) );
        }
 
        /**
@@ -2251,7 +2296,7 @@ abstract class ApiBase extends ContextSource {
 
                $this->mDBTime += $time;
                $this->getMain()->mDBTime += $time;
-               wfProfileOut( $this->getModuleProfileName( true ) );
+               Profiler::instance()->scopedProfileOut( $this->dbProfile );
        }
 
        /**
@@ -2277,7 +2322,7 @@ abstract class ApiBase extends ContextSource {
                        ' "' . wfUrlencode( str_replace( ' ', '_', $this->getUser()->getName() ) ) . '"' .
                        ' "' . $request->getIP() . '"' .
                        ' "' . addslashes( $request->getHeader( 'Referer' ) ) . '"' .
-                       ' "' . addslashes( $request->getHeader( 'User-agent' ) ) . '"';
+                       ' "' . addslashes( $this->getMain()->getUserAgent() ) . '"';
                wfDebugLog( 'api-feature-usage', $s, 'private' );
        }