Merge "API: Avoid duplicate IDs in API documentation"
[lhc/web/wiklou.git] / includes / api / ApiMain.php
index 9e56819..9c54eac 100644 (file)
@@ -49,8 +49,14 @@ class ApiMain extends ApiBase {
         */
        private static $Modules = [
                'login' => 'ApiLogin',
+               'clientlogin' => 'ApiClientLogin',
                'logout' => 'ApiLogout',
-               'createaccount' => 'ApiCreateAccount',
+               'createaccount' => 'ApiAMCreateAccount',
+               'linkaccount' => 'ApiLinkAccount',
+               'unlinkaccount' => 'ApiRemoveAuthenticationData',
+               'changeauthenticationdata' => 'ApiChangeAuthenticationData',
+               'removeauthenticationdata' => 'ApiRemoveAuthenticationData',
+               'resetpassword' => 'ApiResetPassword',
                'query' => 'ApiQuery',
                'expandtemplates' => 'ApiExpandTemplates',
                'parse' => 'ApiParse',
@@ -134,7 +140,9 @@ class ApiMain extends ApiBase {
        private $mModuleMgr, $mResult, $mErrorFormatter, $mContinuationManager;
        private $mAction;
        private $mEnableWrite;
-       private $mInternalMode, $mSquidMaxage, $mModule;
+       private $mInternalMode, $mSquidMaxage;
+       /** @var ApiBase */
+       private $mModule;
 
        private $mCacheMode = 'private';
        private $mCacheControl = [];
@@ -427,8 +435,12 @@ class ApiMain extends ApiBase {
                $isError = false;
                try {
                        $this->executeAction();
-                       $this->logRequest( microtime( true ) - $t );
-
+                       $runTime = microtime( true ) - $t;
+                       $this->logRequest( $runTime );
+                       if ( $this->mModule->isWriteMode() && $this->getRequest()->wasPosted() ) {
+                               $this->getStats()->timing(
+                                       'api.' . $this->getModuleName() . '.executeTiming', 1000 * $runTime );
+                       }
                } catch ( Exception $e ) {
                        $this->handleException( $e );
                        $this->logRequest( microtime( true ) - $t, $e );
@@ -711,7 +723,7 @@ class ApiMain extends ApiBase {
 
        /**
         * Send caching headers
-        * @param boolean $isError Whether an error response is being output
+        * @param bool $isError Whether an error response is being output
         * @since 1.26 added $isError parameter
         */
        protected function sendCacheHeaders( $isError ) {
@@ -871,7 +883,7 @@ class ApiMain extends ApiBase {
 
                        $errMessage = [
                                'code' => 'internal_api_error_' . get_class( $e ),
-                               'info' => '[' . MWExceptionHandler::getLogId( $e ) . '] ' . $info,
+                               'info' => '[' . WebRequest::getRequestId() . '] ' . $info,
                        ];
                }
                return $errMessage;
@@ -978,7 +990,7 @@ class ApiMain extends ApiBase {
                if ( $module->needsToken() === true ) {
                        throw new MWException(
                                "Module '{$module->getModuleName()}' must be updated for the new token handling. " .
-                               "See documentation for ApiBase::needsToken for details."
+                               'See documentation for ApiBase::needsToken for details.'
                        );
                }
                if ( $module->needsToken() ) {
@@ -1129,7 +1141,7 @@ class ApiMain extends ApiBase {
                                                                        TS_MW, time() - $this->getConfig()->get( 'SquidMaxage' )
                                                                );
                                                        }
-                                                       Hooks::run( 'OutputPageCheckLastModified', [ &$modifiedTimes ] );
+                                                       Hooks::run( 'OutputPageCheckLastModified', [ &$modifiedTimes, $this->getOutput() ] );
                                                        $lastMod = max( $modifiedTimes );
                                                        $return304 = wfTimestamp( TS_MW, $lastMod ) <= $ts->getTimestamp( TS_MW );
                                                }
@@ -1174,7 +1186,7 @@ class ApiMain extends ApiBase {
                                $this->dieUsageMsg( 'writerequired' );
                        } elseif ( $this->getRequest()->getHeader( 'Promise-Non-Write-API-Action' ) ) {
                                $this->dieUsage(
-                                       "Promise-Non-Write-API-Action HTTP header cannot be sent to write API modules",
+                                       'Promise-Non-Write-API-Action HTTP header cannot be sent to write API modules',
                                        'promised-nonwrite-api'
                                );
                        }
@@ -1225,7 +1237,7 @@ class ApiMain extends ApiBase {
                // If a majority of slaves are too lagged then disallow writes
                $slaveCount = wfGetLB()->getServerCount() - 1;
                if ( $numLagged >= ceil( $slaveCount / 2 ) ) {
-                       $laggedServers = join( ', ', $laggedServers );
+                       $laggedServers = implode( ', ', $laggedServers );
                        wfDebugLog(
                                'api-readonly',
                                "Api request failed as read only because the following DBs are lagged: $laggedServers"
@@ -1349,6 +1361,7 @@ class ApiMain extends ApiBase {
                                $trxProfiler->setExpectations( $limits['POST'], __METHOD__ );
                        } else {
                                $trxProfiler->setExpectations( $limits['POST-nonwrite'], __METHOD__ );
+                               $this->getRequest()->markAsSafeRequest();
                        }
                } else {
                        $trxProfiler->setExpectations( $limits['GET'], __METHOD__ );
@@ -1367,7 +1380,7 @@ class ApiMain extends ApiBase {
                        'ip' => $request->getIP(),
                        'userAgent' => $this->getUserAgent(),
                        'wiki' => wfWikiID(),
-                       'timeSpentBackend' => round( $time * 1000 ),
+                       'timeSpentBackend' => (int) round( $time * 1000 ),
                        'hadError' => $e !== null,
                        'errorCodes' => [],
                        'params' => [],
@@ -1401,8 +1414,8 @@ class ApiMain extends ApiBase {
                }
 
                wfDebugLog( 'api', $msg, 'private' );
-               // ApiRequest channel is for structured data consumers
-               wfDebugLog( 'ApiRequest', '', 'private', $logCtx );
+               // ApiAction channel is for structured data consumers
+               wfDebugLog( 'ApiAction', '', 'private', $logCtx );
        }
 
        /**
@@ -1431,6 +1444,14 @@ class ApiMain extends ApiBase {
                return array_keys( $this->mParamsUsed );
        }
 
+       /**
+        * Mark parameters as used
+        * @param string|string[] $params
+        */
+       public function markParamsUsed( $params ) {
+               $this->mParamsUsed += array_fill_keys( (array)$params, true );
+       }
+
        /**
         * Get a request value, and register the fact that it was used, for logging.
         * @param string $name
@@ -1443,7 +1464,7 @@ class ApiMain extends ApiBase {
                $ret = $this->getRequest()->getVal( $name );
                if ( $ret === null ) {
                        if ( $this->getRequest()->getArray( $name ) !== null ) {
-                               // See bug 10262 for why we don't just join( '|', ... ) the
+                               // See bug 10262 for why we don't just implode( '|', ... ) the
                                // array.
                                $this->setWarning(
                                        "Parameter '$name' uses unsupported PHP array syntax"
@@ -1624,9 +1645,14 @@ class ApiMain extends ApiBase {
                        $tocnumber = &$options['tocnumber'];
 
                        $header = $this->msg( 'api-help-datatypes-header' )->parse();
+
+                       // Add an additional span with sanitized ID
+                       if ( !$this->getConfig()->get( 'ExperimentalHtmlIds' ) ) {
+                               $header = Html::element( 'span', [ 'id' => Sanitizer::escapeId( 'main/datatypes' ) ] ) .
+                                       $header;
+                       }
                        $help['datatypes'] .= Html::rawElement( 'h' . min( 6, $level ),
                                [ 'id' => 'main/datatypes', 'class' => 'apihelp-header' ],
-                               Html::element( 'span', [ 'id' => Sanitizer::escapeId( 'main/datatypes' ) ] ) .
                                $header
                        );
                        $help['datatypes'] .= $this->msg( 'api-help-datatypes' )->parseAsBlock();
@@ -1637,15 +1663,19 @@ class ApiMain extends ApiBase {
                                        'level' => $level,
                                        'anchor' => 'main/datatypes',
                                        'line' => $header,
-                                       'number' => join( '.', $tocnumber ),
+                                       'number' => implode( '.', $tocnumber ),
                                        'index' => false,
                                ];
                        }
 
+                       // Add an additional span with sanitized ID
+                       if ( !$this->getConfig()->get( 'ExperimentalHtmlIds' ) ) {
+                               $header = Html::element( 'span', [ 'id' => Sanitizer::escapeId( 'main/credits' ) ] ) .
+                                       $header;
+                       }
                        $header = $this->msg( 'api-credits-header' )->parse();
                        $help['credits'] .= Html::rawElement( 'h' . min( 6, $level ),
                                [ 'id' => 'main/credits', 'class' => 'apihelp-header' ],
-                               Html::element( 'span', [ 'id' => Sanitizer::escapeId( 'main/credits' ) ] ) .
                                $header
                        );
                        $help['credits'] .= $this->msg( 'api-credits' )->useDatabase( false )->parseAsBlock();
@@ -1656,7 +1686,7 @@ class ApiMain extends ApiBase {
                                        'level' => $level,
                                        'anchor' => 'main/credits',
                                        'line' => $header,
-                                       'number' => join( '.', $tocnumber ),
+                                       'number' => implode( '.', $tocnumber ),
                                        'index' => false,
                                ];
                        }
@@ -1750,8 +1780,8 @@ class ApiMain extends ApiBase {
                // Use parent to make default message for the main module
                $msg = parent::makeHelpMsg();
 
-               $astriks = str_repeat( '*** ', 14 );
-               $msg .= "\n\n$astriks Modules  $astriks\n\n";
+               $asterisks = str_repeat( '*** ', 14 );
+               $msg .= "\n\n$asterisks Modules  $asterisks\n\n";
 
                foreach ( $this->mModuleMgr->getNames( 'action' ) as $name ) {
                        $module = $this->mModuleMgr->getModule( $name );
@@ -1764,18 +1794,18 @@ class ApiMain extends ApiBase {
                        $msg .= "\n";
                }
 
-               $msg .= "\n$astriks Permissions $astriks\n\n";
+               $msg .= "\n$asterisks Permissions $asterisks\n\n";
                foreach ( self::$mRights as $right => $rightMsg ) {
                        $rightsMsg = $this->msg( $rightMsg['msg'], $rightMsg['params'] )
                                ->useDatabase( false )
                                ->inLanguage( 'en' )
                                ->text();
                        $groups = User::getGroupsWithPermission( $right );
-                       $msg .= "* " . $right . " *\n  $rightsMsg" .
+                       $msg .= '* ' . $right . " *\n  $rightsMsg" .
                                "\nGranted to:\n  " . str_replace( '*', 'all', implode( ', ', $groups ) ) . "\n\n";
                }
 
-               $msg .= "\n$astriks Formats  $astriks\n\n";
+               $msg .= "\n$asterisks Formats  $asterisks\n\n";
                foreach ( $this->mModuleMgr->getNames( 'format' ) as $name ) {
                        $module = $this->mModuleMgr->getModule( $name );
                        $msg .= self::makeHelpMsgHeader( $module, 'format' );
@@ -1810,53 +1840,6 @@ class ApiMain extends ApiBase {
                return "* $paramName={$module->getModuleName()} $modulePrefix*";
        }
 
-       /**
-        * Check whether the user wants us to show version information in the API help
-        * @return bool
-        * @deprecated since 1.21, always returns false
-        */
-       public function getShowVersions() {
-               wfDeprecated( __METHOD__, '1.21' );
-
-               return false;
-       }
-
-       /**
-        * Add or overwrite a module in this ApiMain instance. Intended for use by extending
-        * classes who wish to add their own modules to their lexicon or override the
-        * behavior of inherent ones.
-        *
-        * @deprecated since 1.21, Use getModuleManager()->addModule() instead.
-        * @param string $name The identifier for this module.
-        * @param ApiBase $class The class where this module is implemented.
-        */
-       protected function addModule( $name, $class ) {
-               $this->getModuleManager()->addModule( $name, 'action', $class );
-       }
-
-       /**
-        * Add or overwrite an output format for this ApiMain. Intended for use by extending
-        * classes who wish to add to or modify current formatters.
-        *
-        * @deprecated since 1.21, Use getModuleManager()->addModule() instead.
-        * @param string $name The identifier for this format.
-        * @param ApiFormatBase $class The class implementing this format.
-        */
-       protected function addFormat( $name, $class ) {
-               $this->getModuleManager()->addModule( $name, 'format', $class );
-       }
-
-       /**
-        * Returns the list of supported formats in form ( 'format' => 'ClassName' )
-        *
-        * @since 1.18
-        * @deprecated since 1.21, Use getModuleManager()'s methods instead.
-        * @return array
-        */
-       public function getFormats() {
-               return $this->getModuleManager()->getNamesWithClasses( 'format' );
-       }
-
        /**@}*/
 
 }