X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fapi%2FApiBase.php;h=32156d8b92c5bd2045c844b7666d7dc4e4b3f48e;hb=85fa38d0b71cae9a3065105b39d8c36dc6517285;hp=1ca54c1c14e54c078321d3cb36b186115e2d176a;hpb=d1d8b54b7535e91820dd9930de6217ed19bdc58b;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index 1ca54c1c14..32156d8b92 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -21,6 +21,7 @@ */ use Wikimedia\Rdbms\IDatabase; +use MediaWiki\MediaWikiServices; /** * This abstract class implements many basic API functions, and is the base of @@ -271,7 +272,7 @@ abstract class ApiBase extends ContextSource { private $mMainModule; /** @var string */ private $mModuleName, $mModulePrefix; - private $mSlaveDB = null; + private $mReplicaDB = null; private $mParamCache = []; /** @var array|null|bool */ private $mModuleSource = false; @@ -348,45 +349,7 @@ abstract class ApiBase extends ContextSource { * @return array */ protected function getExamplesMessages() { - // Fall back to old non-localised method - $ret = []; - - $examples = $this->getExamples(); - if ( $examples ) { - if ( !is_array( $examples ) ) { - $examples = [ $examples ]; - } elseif ( $examples && ( count( $examples ) & 1 ) == 0 && - array_keys( $examples ) === range( 0, count( $examples ) - 1 ) && - !preg_match( '/^\s*api\.php\?/', $examples[0] ) - ) { - // Fix up the ugly "even numbered elements are description, odd - // numbered elemts are the link" format (see doc for self::getExamples) - $tmp = []; - $examplesCount = count( $examples ); - for ( $i = 0; $i < $examplesCount; $i += 2 ) { - $tmp[$examples[$i + 1]] = $examples[$i]; - } - $examples = $tmp; - } - - foreach ( $examples as $k => $v ) { - if ( is_numeric( $k ) ) { - $qs = $v; - $msg = ''; - } else { - $qs = $k; - $msg = self::escapeWikiText( $v ); - if ( is_array( $msg ) ) { - $msg = implode( ' ', $msg ); - } - } - - $qs = preg_replace( '/^\s*api\.php\?/', '', $qs ); - $ret[$qs] = $this->msg( 'api-help-fallback-example', [ $msg ] ); - } - } - - return $ret; + return []; } /** @@ -685,11 +648,11 @@ abstract class ApiBase extends ContextSource { * @return IDatabase */ protected function getDB() { - if ( !isset( $this->mSlaveDB ) ) { - $this->mSlaveDB = wfGetDB( DB_REPLICA, 'api' ); + if ( !isset( $this->mReplicaDB ) ) { + $this->mReplicaDB = wfGetDB( DB_REPLICA, 'api' ); } - return $this->mSlaveDB; + return $this->mReplicaDB; } /** @@ -1784,25 +1747,6 @@ abstract class ApiBase extends ContextSource { return $user; } - /** - * A subset of wfEscapeWikiText for BC texts - * - * @since 1.25 - * @param string|array $v - * @return string|array - */ - private static function escapeWikiText( $v ) { - if ( is_array( $v ) ) { - return array_map( 'self::escapeWikiText', $v ); - } else { - return strtr( $v, [ - '__' => '__', '{' => '{', '}' => '}', - '[[Category:' => '[[:Category:', - '[[File:' => '[[:File:', '[[Image:' => '[[:Image:', - ] ); - } - } - /** * Create a Message from a string or array * @@ -2126,11 +2070,41 @@ abstract class ApiBase extends ContextSource { foreach ( (array)$actions as $action ) { $errors = array_merge( $errors, $title->getUserPermissionsErrors( $action, $user ) ); } + if ( $errors ) { + // track block notices + if ( $this->getConfig()->get( 'EnableBlockNoticeStats' ) ) { + $this->trackBlockNotices( $errors ); + } + $this->dieStatus( $this->errorArrayToStatus( $errors, $user ) ); } } + /** + * Keep track of errors messages resulting from a block + * + * @param array $errors + */ + private function trackBlockNotices( array $errors ) { + $errorMessageKeys = [ + 'blockedtext', + 'blockedtext-partial', + 'autoblockedtext', + 'systemblockedtext', + ]; + + $statsd = MediaWikiServices::getInstance()->getStatsdDataFactory(); + + foreach ( $errors as $error ) { + if ( in_array( $error[0], $errorMessageKeys ) ) { + $wiki = $this->getConfig()->get( 'DBname' ); + $statsd->increment( 'BlockNotices.' . $wiki . '.MediaWikiApi.returned' ); + break; + } + } + } + /** * Will only set a warning instead of failing if the global $wgDebugAPI * is set to true. Otherwise behaves exactly as self::dieWithError(). @@ -2230,10 +2204,6 @@ abstract class ApiBase extends ContextSource { /** * Get final module summary * - * Ideally this will just be the getSummaryMessage(). However, for - * backwards compatibility, if that message does not exist then the first - * line of wikitext from the description message will be used instead. - * * @since 1.30 * @return Message */ @@ -2243,17 +2213,6 @@ abstract class ApiBase extends ContextSource { $this->getModuleName(), $this->getModulePath(), ] ); - if ( !$msg->exists() ) { - wfDeprecated( 'API help "description" messages', '1.30' ); - $msg = self::makeMessage( $this->getDescriptionMessage(), $this->getContext(), [ - $this->getModulePrefix(), - $this->getModuleName(), - $this->getModulePath(), - ] ); - $msg = self::makeMessage( 'rawmessage', $this->getContext(), [ - preg_replace( '/\n.*/s', '', $msg->text() ) - ] ); - } return $msg; } @@ -2265,18 +2224,6 @@ abstract class ApiBase extends ContextSource { * @return Message[] */ public function getFinalDescription() { - $desc = $this->getDescription(); - - // Avoid PHP 7.1 warning of passing $this by reference - $apiModule = $this; - Hooks::run( 'APIGetDescription', [ &$apiModule, &$desc ], '1.25' ); - $desc = self::escapeWikiText( $desc ); - if ( is_array( $desc ) ) { - $desc = implode( "\n", $desc ); - } else { - $desc = (string)$desc; - } - $summary = self::makeMessage( $this->getSummaryMessage(), $this->getContext(), [ $this->getModulePrefix(), $this->getModuleName(), @@ -2290,20 +2237,7 @@ abstract class ApiBase extends ContextSource { ] ); - if ( $summary->exists() ) { - $msgs = [ $summary, $extendedDescription ]; - } else { - wfDeprecated( 'API help "description" messages', '1.30' ); - $description = self::makeMessage( $this->getDescriptionMessage(), $this->getContext(), [ - $this->getModulePrefix(), - $this->getModuleName(), - $this->getModulePath(), - ] ); - if ( !$description->exists() ) { - $description = $this->msg( 'api-help-fallback-description', $desc ); - } - $msgs = [ $description ]; - } + $msgs = [ $summary, $extendedDescription ]; Hooks::run( 'APIGetDescriptionMessages', [ $this, &$msgs ] ); @@ -2355,17 +2289,6 @@ abstract class ApiBase extends ContextSource { $name = $this->getModuleName(); $path = $this->getModulePath(); - $desc = $this->getParamDescription(); - - // Avoid PHP 7.1 warning of passing $this by reference - $apiModule = $this; - Hooks::run( 'APIGetParamDescription', [ &$apiModule, &$desc ], '1.25' ); - - if ( !$desc ) { - $desc = []; - } - $desc = self::escapeWikiText( $desc ); - $params = $this->getFinalParams( self::GET_VALUES_FOR_HELP ); $msgs = []; foreach ( $params as $param => $settings ) { @@ -2373,25 +2296,10 @@ abstract class ApiBase extends ContextSource { $settings = []; } - $d = $desc[$param] ?? ''; - if ( is_array( $d ) ) { - // Special handling for prop parameters - $d = array_map( function ( $line ) { - if ( preg_match( '/^\s+(\S+)\s+-\s+(.+)$/', $line, $m ) ) { - $line = "\n;{$m[1]}:{$m[2]}"; - } - return $line; - }, $d ); - $d = implode( ' ', $d ); - } - if ( isset( $settings[self::PARAM_HELP_MSG] ) ) { $msg = $settings[self::PARAM_HELP_MSG]; } else { $msg = $this->msg( "apihelp-{$path}-param-{$param}" ); - if ( !$msg->exists() ) { - $msg = $this->msg( 'api-help-fallback-parameter', $d ); - } } $msg = self::makeMessage( $msg, $this->getContext(), [ $prefix, $param, $name, $path ] ); @@ -2654,6 +2562,7 @@ abstract class ApiBase extends ContextSource { * @return Message|string|array|false */ protected function getDescription() { + wfDeprecated( __METHOD__, '1.25' ); return false; } @@ -2670,6 +2579,7 @@ abstract class ApiBase extends ContextSource { * @return array|bool False on no parameter descriptions */ protected function getParamDescription() { + wfDeprecated( __METHOD__, '1.25' ); return []; } @@ -2690,6 +2600,7 @@ abstract class ApiBase extends ContextSource { * @return bool|string|array */ protected function getExamples() { + wfDeprecated( __METHOD__, '1.25' ); return false; } @@ -2702,6 +2613,7 @@ abstract class ApiBase extends ContextSource { * @return string|array|Message */ protected function getDescriptionMessage() { + wfDeprecated( __METHOD__, '1.30' ); return [ [ "apihelp-{$this->getModulePath()}-description", "apihelp-{$this->getModulePath()}-summary",