Merge "Improve test coverage for ApiBase.php"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Tue, 10 Apr 2018 18:46:34 +0000 (18:46 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 10 Apr 2018 18:46:34 +0000 (18:46 +0000)
1  2 
includes/api/ApiBase.php
includes/api/ApiMain.php
includes/api/i18n/en.json
includes/api/i18n/qqq.json

diff --combined includes/api/ApiBase.php
@@@ -692,7 -692,7 +692,7 @@@ abstract class ApiBase extends ContextS
         * Set the continuation manager
         * @param ApiContinuationManager|null $manager
         */
 -      public function setContinuationManager( $manager ) {
 +      public function setContinuationManager( ApiContinuationManager $manager = null ) {
                // Main module has setContinuationManager() method overridden
                // Safety - avoid infinite loop:
                if ( $this->isMain() ) {
                                ) {
                                        $type = array_merge( $type, $paramSettings[self::PARAM_EXTRA_NAMESPACES] );
                                }
-                               // By default, namespace parameters allow ALL_DEFAULT_STRING to be used to specify
-                               // all namespaces.
+                               // Namespace parameters allow ALL_DEFAULT_STRING to be used to
+                               // specify all namespaces irrespective of PARAM_ALL.
                                $allowAll = true;
                        }
                        if ( isset( $value ) && $type == 'submodule' ) {
                                return $value;
                        }
  
-                       if ( is_array( $allowedValues ) ) {
-                               $values = array_map( function ( $v ) {
-                                       return '<kbd>' . wfEscapeWikiText( $v ) . '</kbd>';
-                               }, $allowedValues );
-                               $this->dieWithError( [
-                                       'apierror-multival-only-one-of',
-                                       $valueName,
-                                       Message::listParam( $values ),
-                                       count( $values ),
-                               ], "multival_$valueName" );
-                       } else {
-                               $this->dieWithError( [
-                                       'apierror-multival-only-one',
-                                       $valueName,
-                               ], "multival_$valueName" );
-                       }
+                       $values = array_map( function ( $v ) {
+                               return '<kbd>' . wfEscapeWikiText( $v ) . '</kbd>';
+                       }, $allowedValues );
+                       $this->dieWithError( [
+                               'apierror-multival-only-one-of',
+                               $valueName,
+                               Message::listParam( $values ),
+                               count( $values ),
+                       ], "multival_$valueName" );
                }
  
                if ( is_array( $allowedValues ) ) {
        }
  
        /**
-        * Validate and normalize of parameters of type 'timestamp'
+        * Validate and normalize parameters of type 'timestamp'
         * @param string $value Parameter value
         * @param string $encParamName Parameter name
         * @return string Validated and normalized parameter
                        return wfTimestamp( TS_MW );
                }
  
-               $unixTimestamp = wfTimestamp( TS_UNIX, $value );
-               if ( $unixTimestamp === false ) {
+               $timestamp = wfTimestamp( TS_MW, $value );
+               if ( $timestamp === false ) {
                        $this->dieWithError(
                                [ 'apierror-badtimestamp', $encParamName, wfEscapeWikiText( $value ) ],
                                "badtimestamp_{$encParamName}"
                        );
                }
  
-               return wfTimestamp( TS_MW, $unixTimestamp );
+               return $timestamp;
        }
  
        /**
        }
  
        /**
-        * Validate and normalize of parameters of type 'user'
+        * Validate and normalize parameters of type 'user'
         * @param string $value Parameter value
         * @param string $encParamName Parameter name
         * @return string Validated and normalized parameter
                        return $value;
                }
  
-               $title = Title::makeTitleSafe( NS_USER, $value );
-               if ( $title === null || $title->hasFragment() ) {
+               $titleObj = Title::makeTitleSafe( NS_USER, $value );
+               if ( $titleObj ) {
+                       $value = $titleObj->getText();
+               }
+               if (
+                       !User::isValidUserName( $value ) &&
+                       // We allow ranges as well, for blocks.
+                       !IP::isIPAddress( $value ) &&
+                       // See comment for User::isIP.  We don't just call that function
+                       // here because it also returns true for things like
+                       // 300.300.300.300 that are neither valid usernames nor valid IP
+                       // addresses.
+                       !preg_match(
+                               '/^' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.xxx$/',
+                               $value
+                       )
+               ) {
                        $this->dieWithError(
                                [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $value ) ],
                                "baduser_{$encParamName}"
                        );
                }
  
-               return $title->getText();
+               return $value;
        }
  
        /**@}*/
                return false;
        }
  
 -      /**
 -       * @deprecated since 1.25, always returns empty string
 -       * @param IDatabase|bool $db
 -       * @return string
 -       */
 -      public function getModuleProfileName( $db = false ) {
 -              wfDeprecated( __METHOD__, '1.25' );
 -              return '';
 -      }
 -
        /**
         * @deprecated since 1.25
         */
                wfDeprecated( __METHOD__, '1.25' );
        }
  
 -      /**
 -       * @deprecated since 1.25, always returns 0
 -       * @return float
 -       */
 -      public function getProfileTime() {
 -              wfDeprecated( __METHOD__, '1.25' );
 -              return 0;
 -      }
 -
        /**
         * @deprecated since 1.25
         */
                wfDeprecated( __METHOD__, '1.25' );
        }
  
 -      /**
 -       * @deprecated since 1.25, always returns 0
 -       * @return float
 -       */
 -      public function getProfileDBTime() {
 -              wfDeprecated( __METHOD__, '1.25' );
 -              return 0;
 -      }
 -
        /**
         * Call wfTransactionalTimeLimit() if this request was POSTed
         * @since 1.26
diff --combined includes/api/ApiMain.php
@@@ -368,12 -368,19 +368,12 @@@ class ApiMain extends ApiBase 
         * Set the continuation manager
         * @param ApiContinuationManager|null $manager
         */
 -      public function setContinuationManager( $manager ) {
 -              if ( $manager !== null ) {
 -                      if ( !$manager instanceof ApiContinuationManager ) {
 -                              throw new InvalidArgumentException( __METHOD__ . ': Was passed ' .
 -                                      is_object( $manager ) ? get_class( $manager ) : gettype( $manager )
 -                              );
 -                      }
 -                      if ( $this->mContinuationManager !== null ) {
 -                              throw new UnexpectedValueException(
 -                                      __METHOD__ . ': tried to set manager from ' . $manager->getSource() .
 -                                      ' when a manager is already set from ' . $this->mContinuationManager->getSource()
 -                              );
 -                      }
 +      public function setContinuationManager( ApiContinuationManager $manager = null ) {
 +              if ( $manager !== null && $this->mContinuationManager !== null ) {
 +                      throw new UnexpectedValueException(
 +                              __METHOD__ . ': tried to set manager from ' . $manager->getSource() .
 +                              ' when a manager is already set from ' . $this->mContinuationManager->getSource()
 +                      );
                }
                $this->mContinuationManager = $manager;
        }
                // Instantiate the module requested by the user
                $module = $this->mModuleMgr->getModule( $this->mAction, 'action' );
                if ( $module === null ) {
 +                      // Probably can't happen
 +                      // @codeCoverageIgnoreStart
                        $this->dieWithError(
                                [ 'apierror-unknownaction', wfEscapeWikiText( $this->mAction ) ], 'unknown_action'
                        );
 +                      // @codeCoverageIgnoreEnd
                }
                $moduleParams = $module->extractRequestParams();
  
                        }
  
                        if ( !isset( $moduleParams['token'] ) ) {
 +                              // Probably can't happen
 +                              // @codeCoverageIgnoreStart
                                $module->dieWithError( [ 'apierror-missingparam', 'token' ] );
 +                              // @codeCoverageIgnoreEnd
                        }
  
                        $module->requirePostedParameters( [ 'token' ] );
                }
  
                // Allow extensions to stop execution for arbitrary reasons.
 -              $message = false;
 +              $message = 'hookaborted';
                if ( !Hooks::run( 'ApiCheckCanExecute', [ $module, $user, &$message ] ) ) {
                        $this->dieWithError( $message );
                }
        /**
         * Get a request value, and register the fact that it was used, for logging.
         * @param string $name
-        * @param mixed $default
-        * @return mixed
+        * @param string|null $default
+        * @return string|null
         */
        public function getVal( $name, $default = null ) {
                $this->mParamsUsed[$name] = true;
@@@ -2,8 -2,7 +2,8 @@@
        "@metadata": {
                "authors": [
                        "Anomie",
 -                      "Siebrand"
 +                      "Siebrand",
 +                      "Zoranzoki21"
                ]
        },
  
        "apihelp-query+exturlusage-param-namespace": "The page namespaces to enumerate.",
        "apihelp-query+exturlusage-param-limit": "How many pages to return.",
        "apihelp-query+exturlusage-param-expandurl": "Expand protocol-relative URLs with the canonical protocol.",
 -      "apihelp-query+exturlusage-example-simple": "Show pages linking to <kbd>http://www.mediawiki.org</kbd>.",
 +      "apihelp-query+exturlusage-example-simple": "Show pages linking to <kbd>https://www.mediawiki.org</kbd>.",
  
        "apihelp-query+filearchive-summary": "Enumerate all deleted files sequentially.",
        "apihelp-query+filearchive-param-from": "The image title to start enumerating from.",
        "apihelp-query+info-paramvalue-prop-readable": "Whether the user can read this page.",
        "apihelp-query+info-paramvalue-prop-preload": "Gives the text returned by EditFormPreloadText.",
        "apihelp-query+info-paramvalue-prop-displaytitle": "Gives the manner in which the page title is actually displayed.",
 +      "apihelp-query+info-paramvalue-prop-varianttitles": "Gives the display title in all variants of the site content language.",
        "apihelp-query+info-param-testactions": "Test whether the current user can perform certain actions on the page.",
        "apihelp-query+info-param-token": "Use [[Special:ApiHelp/query+tokens|action=query&meta=tokens]] instead.",
        "apihelp-query+info-example-simple": "Get information about the page <kbd>Main Page</kbd>.",
        "apihelp-query+recentchanges-paramvalue-prop-sizes": "Adds the new and old page length in bytes.",
        "apihelp-query+recentchanges-paramvalue-prop-redirect": "Tags edit if page is a redirect.",
        "apihelp-query+recentchanges-paramvalue-prop-patrolled": "Tags patrollable edits as being patrolled or unpatrolled.",
 +      "apihelp-query+recentchanges-paramvalue-prop-autopatrolled": "Tags patrollable edits as being autopatrolled or not.",
        "apihelp-query+recentchanges-paramvalue-prop-loginfo": "Adds log information (log ID, log type, etc) to log entries.",
        "apihelp-query+recentchanges-paramvalue-prop-tags": "Lists tags for the entry.",
        "apihelp-query+recentchanges-paramvalue-prop-sha1": "Adds the content checksum for entries associated with a revision.",
        "apihelp-query+usercontribs-paramvalue-prop-sizediff": "Adds the size delta of the edit against its parent.",
        "apihelp-query+usercontribs-paramvalue-prop-flags": "Adds flags of the edit.",
        "apihelp-query+usercontribs-paramvalue-prop-patrolled": "Tags patrolled edits.",
 +      "apihelp-query+usercontribs-paramvalue-prop-autopatrolled": "Tags autopatrolled edits.",
        "apihelp-query+usercontribs-paramvalue-prop-tags": "Lists tags for the edit.",
        "apihelp-query+usercontribs-param-show": "Show only items that meet these criteria, e.g. non minor edits only: <kbd>$2show=!minor</kbd>.\n\nIf <kbd>$2show=patrolled</kbd> or <kbd>$2show=!patrolled</kbd> is set, revisions older than <var>[[mw:Special:MyLanguage/Manual:$wgRCMaxAge|$wgRCMaxAge]]</var> ($1 {{PLURAL:$1|second|seconds}}) won't be shown.",
        "apihelp-query+usercontribs-param-tag": "Only list revisions tagged with this tag.",
        "apihelp-query+watchlist-paramvalue-prop-parsedcomment": "Adds parsed comment of the edit.",
        "apihelp-query+watchlist-paramvalue-prop-timestamp": "Adds timestamp of the edit.",
        "apihelp-query+watchlist-paramvalue-prop-patrol": "Tags edits that are patrolled.",
 +      "apihelp-query+watchlist-paramvalue-prop-autopatrol": "Tags edits that are autopatrolled.",
        "apihelp-query+watchlist-paramvalue-prop-sizes": "Adds the old and new lengths of the page.",
        "apihelp-query+watchlist-paramvalue-prop-notificationtimestamp": "Adds timestamp of when the user was last notified about the edit.",
        "apihelp-query+watchlist-paramvalue-prop-loginfo": "Adds log information where appropriate.",
        "apierror-missingtitle-byname": "The page $1 doesn't exist.",
        "apierror-moduledisabled": "The <kbd>$1</kbd> module has been disabled.",
        "apierror-multival-only-one-of": "{{PLURAL:$3|Only|Only one of}} $2 is allowed for parameter <var>$1</var>.",
-       "apierror-multival-only-one": "Only one value is allowed for parameter <var>$1</var>.",
        "apierror-multpages": "<var>$1</var> may only be used with a single page.",
        "apierror-mustbeloggedin-changeauth": "You must be logged in to change authentication data.",
        "apierror-mustbeloggedin-generic": "You must be logged in.",
        "apihelp-query+info-paramvalue-prop-readable": "{{doc-apihelp-paramvalue|query+info|prop|readable}}",
        "apihelp-query+info-paramvalue-prop-preload": "{{doc-apihelp-paramvalue|query+info|prop|preload}}",
        "apihelp-query+info-paramvalue-prop-displaytitle": "{{doc-apihelp-paramvalue|query+info|prop|displaytitle}}",
 +      "apihelp-query+info-paramvalue-prop-varianttitles": "{{doc-apihelp-paramvalue|query+info|prop|varianttitles}}",
        "apihelp-query+info-param-testactions": "{{doc-apihelp-param|query+info|testactions}}",
        "apihelp-query+info-param-token": "{{doc-apihelp-param|query+info|token}}",
        "apihelp-query+info-example-simple": "{{doc-apihelp-example|query+info}}",
        "apihelp-query+recentchanges-paramvalue-prop-sizes": "{{doc-apihelp-paramvalue|query+recentchanges|prop|sizes}}",
        "apihelp-query+recentchanges-paramvalue-prop-redirect": "{{doc-apihelp-paramvalue|query+recentchanges|prop|redirect}}",
        "apihelp-query+recentchanges-paramvalue-prop-patrolled": "{{doc-apihelp-paramvalue|query+recentchanges|prop|patrolled}}",
 +      "apihelp-query+recentchanges-paramvalue-prop-autopatrolled": "{{doc-apihelp-paramvalue|query+recentchanges|prop|autopatrolled}}",
        "apihelp-query+recentchanges-paramvalue-prop-loginfo": "{{doc-apihelp-paramvalue|query+recentchanges|prop|loginfo}}",
        "apihelp-query+recentchanges-paramvalue-prop-tags": "{{doc-apihelp-paramvalue|query+recentchanges|prop|tags}}",
        "apihelp-query+recentchanges-paramvalue-prop-sha1": "{{doc-apihelp-paramvalue|query+recentchanges|prop|sha1}}",
        "apihelp-query+usercontribs-paramvalue-prop-sizediff": "{{doc-apihelp-paramvalue|query+usercontribs|prop|sizediff}}",
        "apihelp-query+usercontribs-paramvalue-prop-flags": "{{doc-apihelp-paramvalue|query+usercontribs|prop|flags}}",
        "apihelp-query+usercontribs-paramvalue-prop-patrolled": "{{doc-apihelp-paramvalue|query+usercontribs|prop|patrolled}}",
 +      "apihelp-query+usercontribs-paramvalue-prop-autopatrolled": "{{doc-apihelp-paramvalue|query+usercontribs|prop|autopatrolled}}",
        "apihelp-query+usercontribs-paramvalue-prop-tags": "{{doc-apihelp-paramvalue|query+usercontribs|prop|tags}}",
        "apihelp-query+usercontribs-param-show": "{{doc-apihelp-param|query+usercontribs|show|params=* $1 - Value of [[mw:Manual:$RCMaxAge|$RCMaxAge]]|paramstart=2}}",
        "apihelp-query+usercontribs-param-tag": "{{doc-apihelp-param|query+usercontribs|tag}}",
        "apihelp-query+watchlist-paramvalue-prop-parsedcomment": "{{doc-apihelp-paramvalue|query+watchlist|prop|parsedcomment}}",
        "apihelp-query+watchlist-paramvalue-prop-timestamp": "{{doc-apihelp-paramvalue|query+watchlist|prop|timestamp}}",
        "apihelp-query+watchlist-paramvalue-prop-patrol": "{{doc-apihelp-paramvalue|query+watchlist|prop|patrol}}",
 +      "apihelp-query+watchlist-paramvalue-prop-autopatrol": "{{doc-apihelp-paramvalue|query+watchlist|prop|autopatrol}}",
        "apihelp-query+watchlist-paramvalue-prop-sizes": "{{doc-apihelp-paramvalue|query+watchlist|prop|sizes}}",
        "apihelp-query+watchlist-paramvalue-prop-notificationtimestamp": "{{doc-apihelp-paramvalue|query+watchlist|prop|notificationtimestamp}}",
        "apihelp-query+watchlist-paramvalue-prop-loginfo": "{{doc-apihelp-paramvalue|query+watchlist|prop|loginfo}}",
        "apierror-missingtitle-byname": "{{doc-apierror}}",
        "apierror-moduledisabled": "{{doc-apierror}}\n\nParameters:\n* $1 - Name of the module.",
        "apierror-multival-only-one-of": "{{doc-apierror}}\n\nParameters:\n* $1 - Parameter name.\n* $2 - Possible values for the parameter.\n* $3 - Number of values.",
-       "apierror-multival-only-one": "{{doc-apierror}}\n\nParameters:\n* $1 - Parameter name.",
        "apierror-multpages": "{{doc-apierror}}\n\nParameters:\n* $1 - Parameter name",
        "apierror-mustbeloggedin-changeauth": "{{doc-apierror}}",
        "apierror-mustbeloggedin-generic": "{{doc-apierror}}",