Merge "Add padding to popup tag widget"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Tue, 10 Apr 2018 19:09:41 +0000 (19:09 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 10 Apr 2018 19:09:41 +0000 (19:09 +0000)
21 files changed:
RELEASE-NOTES-1.31
docs/hooks.txt
includes/WebRequest.php
includes/api/ApiBase.php
includes/api/ApiFormatJson.php
includes/api/ApiFormatPhp.php
includes/api/ApiMain.php
includes/api/ApiQueryInfo.php
includes/api/i18n/en.json
includes/api/i18n/qqq.json
includes/resourceloader/ResourceLoader.php
includes/specialpage/ChangesListSpecialPage.php
languages/Language.php
languages/i18n/he.json
tests/phpunit/MediaWikiTestCase.php
tests/phpunit/includes/api/ApiBaseTest.php
tests/phpunit/includes/api/ApiMainTest.php
tests/phpunit/includes/specialpage/ChangesListSpecialPageTest.php
tests/phpunit/structure/ApiStructureTest.php
tests/selenium/README.md
tests/selenium/wdio.conf.js

index 051f6d0..ebd9787 100644 (file)
@@ -137,20 +137,11 @@ changes to languages because of Phabricator reports.
 * (T187750) New language support: Spanish formal address (es-formal).
 * (T187824) New language support: Hungarian formal address (hu-formal).
 
-=== Other changes in 1.31 ===
-* Browser support for Internet Explorer 10 was lowered from Grade A to Grade C.
-* Introducing multi-content-revision capability into the storage layer. For details,
-  see <https://www.mediawiki.org/wiki/Requests_for_comment/Multi-Content_Revisions>.
-* The Revision class was deprecated in favor of RevisionStore, BlobStore, and
-  RevisionRecord and its subclasses.
+=== Breaking changes in 1.31 ===
 * MessageBlobStore::insertMessageBlob() (deprecated in 1.27) was removed.
-* The global function wfBCP47 was renamed to LanguageCode::bcp47.
-* The global function wfBCP47 is now deprecated.
-* The global function wfCountDown() is now deprecated in favor of
-  Maintenance::countDown()
 * The OutputPage class constructor now requires a context parameter,
   (instantiating without context was deprecated in 1.18)
-* mw.page (deprecated in 1.30) was removed.
+* The mw.page JavaScript singleton (deprecated in 1.30) was removed.
 * Article::getLastPurgeTimestamp(), WikiPage::getLastPurgeTimestamp(), and the
   related WikiPage::PURGE_* constants, deprecated in 1.29, were removed.
 * The Article::selectFields(), Article::onArticleCreate(),
@@ -158,58 +149,13 @@ changes to languages because of Phabricator reports.
   in 1.24, were removed.
 * Installer::locateExecutable() and Installer::locateExecutableInDefaultPaths()
   were removed, use ExecutableFinder::findInDefaultPaths() instead.
-* Several methods for returning lists of fields to select from the database
-  have been deprecated in favor of similar methods that also return the tables
-  to select from and the join conditions for those tables.
-  * Block::selectFields() → Block::getQueryInfo()
-  * RecentChange::selectFields() → RecentChange::getQueryInfo()
-  * ArchivedFile::selectFields() → ArchivedFile::getQueryInfo()
-  * LocalFile::selectFields() → LocalFile::getQueryInfo()
-  * LocalFile::getCacheFields() with a prefix no longer works
-  * LocalFile::getLazyCacheFields() with a prefix no longer works
-  * OldLocalFile::selectFields() → OldLocalFile::getQueryInfo()
-  * RecentChange::selectFields() → RecentChange::getQueryInfo()
-  * Revision::userJoinCond() → Revision::getQueryInfo( [ 'user' ] )
-  * Revision::selectUserFields() → Revision::getQueryInfo( [ 'user' ] )
-  * Revision::pageJoinCond() → Revision::getQueryInfo( [ 'page' ] )
-  * Revision::selectPageFields() → Revision::getQueryInfo( [ 'page' ] )
-  * Revision::selectTextFields() → Revision::getQueryInfo( [ 'text' ] )
-  * Revision::selectFields() → Revision::getQueryInfo()
-  * Revision::selectArchiveFields() → Revision::getArchiveQueryInfo()
-  * User::selectFields() → User::getQueryInfo()
-  * WikiPage::selectFields() → WikiPage::getQueryInfo()
-* Due to significant refactoring, method ContribsPager::getUserCond() that had
-  no access restriction has been removed.
-* Revision::setUserIdAndName() was deprecated.
-* Access to TitleValue class properties was deprecated, the relevant getters
-  should be used instead.
-* DifferenceEngine::getDiffBodyCacheKey() is deprecated. Subclasses should
-  override DifferenceEngine::getDiffBodyCacheKeyParams() instead.
 * The deprecated MW_DIFF_VERSION constant was removed.
   DifferenceEngine::MW_DIFF_VERSION should be used instead.
-* Use of Maintenance::error( $err, $die ) to exit script was deprecated. Use
-  Maintenance::fatalError() instead.
-* Passing a ParserOptions object to OutputPage::parserOptions() is deprecated.
-* Browser support for Opera 12 and older was removed.
-  Opera 15+ continues at Grade A support.
+* Due to significant refactoring, method ContribsPager::getUserCond() that had
+  no access restriction has been removed.
 * The Block class will no longer accept usable-but-missing usernames for
   'byText' or ->setBlocker(). Callers should either ensure the blocker exists
   locally or use a new interwiki-format username like "iw>Example".
-* The RevisionInsertComplete hook is now deprecated, use RevisionRecordInserted instead.
-  RevisionInsertComplete is still called, but the second and third parameter will always be null.
-  Hard deprecation is scheduled for 1.32.
-* The following methods that get and set ParserOutput state are deprecated.
-  Callers should use the new stateless $options parameter to
-  ParserOutput::getText() instead.
-  * ParserOptions::getEditSection()
-  * ParserOptions::setEditSection()
-  * ParserOutput::getEditSectionTokens()
-  * ParserOutput::setEditSectionTokens()
-  * ParserOutput::getTOCEnabled()
-  * ParserOutput::setTOCEnabled()
-  * OutputPage::enableSectionEditLinks()
-  * OutputPage::sectionEditLinksEnabled()
-  * The public ParserOutput state fields $mTOCEnabled and $mEditSectionTokens are also deprecated.
 * The following methods and constants from the WatchedItem class, which were deprecated in
   1.27, have been removed.
   * WatchedItem::getTitle()
@@ -229,7 +175,6 @@ changes to languages because of Phabricator reports.
   has been deprecated since 1.27 and was removed as well.
 * The HtmlFormatter class was removed (deprecated in 1.27). The namespaced
   HtmlFormatter\HtmlFormatter class should be used instead.
-* License::getLicenses has been deprecated; use License::getLines instead.
 * The driver 'mysql' for MySQL, deprecated in MediaWiki 1.30, has been removed.
   The driver has been deprecated since PHP 5.5 and was removed in PHP 7.0. The
   default driver for MySQL has been 'mysqli' since MediaWiki 1.22.
@@ -237,18 +182,8 @@ changes to languages because of Phabricator reports.
   * PreparedEdit->newText
   * PreparedEdit->oldText
   * PreparedEdit->pst
-* QuickTemplate::setRef() was deprecated in favour of QuickTemplate::set().
-  Setting template variables by reference allowed violating the principle of data being
-  immutable once added to the skin template. In practice, this method was not being
-  used for that. Rather, setRef() existed as memory optimisation for PHP 4.
-* QuickTemplate::setTranslator() was deprecated in favour of Skin::msg() parameters.
-* MediaWikiI18N::set() was deprecated in favour of Skin::msg() parameters.
-* MediaWikiI18N::translate() was deprecated in favour of Skin::msg() or wfMessage().
-* Passing false to ParserOptions::setWrapOutputClass() is deprecated. Use the
-  'unwrap' transform to ParserOutput::getText() instead.
 * ParserOutput objects generated using a non-default value for
-  ParserOptions::setWrapOutputClass() can no longer be added to the parser
-  cache.
+  ParserOptions::setWrapOutputClass() can no longer be added to the parser cache.
 * The following deprecated methods from the OutputPage class have been removed:
   * OutputPage::addExtensionStyle(); deprecated in 1.27
   * OutputPage::getExtStyle(); deprecated in 1.27
@@ -259,23 +194,6 @@ changes to languages because of Phabricator reports.
   * Additionally, the protected OutputPage::$mExtStyles array, only accessed through
     the above and with no known uses, was removed.
 * The no-op method Skin::showIPinHeader(), deprecated in 1.27, was removed.
-* \ObjectFactory (no namespace) is deprecated, the namespaced \Wikimedia\ObjectFactory
-  from the wikimedia/object-factory library should be used instead.
-* CommentStore::newKey is deprecated. Get an instance from MediaWikiServices instead.
-* The following CommentStore methods have had their signatures changed to introduce a $key parameter,
-  usage of the methods on instances retrieved from CommentStore::newKey will remain unchanged but deprecated:
-  * CommentStore::getFields
-  * CommentStore::getJoin
-  * CommentStore::getComment
-  * CommentStore::getCommentLegacy
-  * CommentStore::insert
-  * CommentStore::insertWithTemplate
-* The following methods in Title have been renamed, and the old ones are deprecated:
-  * Title::getSkinFromCssJsSubpage – use ::getSkinFromConfigSubpage
-  * Title::isCssOrJsPage – use ::isSiteConfigPage
-  * Title::isCssJsSubpage – use ::isUserConfigPage
-  * Title::isCssSubpage – use ::isUserCssConfigPage
-  * Title::isJsSubpage – use ::isUserJsConfigPage
 * The following variables and methods in EditPage, deprecated in MediaWiki 1.30, were removed:
   * $isCssJsSubpage — use ::isUserConfigPage()
   * $isCssSubpage — use ::isUserCssConfigPage()
@@ -286,9 +204,6 @@ changes to languages because of Phabricator reports.
   * ::getCheckboxes() – use ::getCheckboxesWidget() or ::getCheckboxesDefinition()
   * ::getCheckboxesOOUI() – use ::getCheckboxesWidget() or ::getCheckboxesDefinition()
 * The method ResourceLoaderModule::getPosition(), deprecated in 1.29, has been removed.
-* The DeferredStringifier class is deprecated, use Message::listParam() instead.
-* The type string for the parameter $lang of DateFormatter::getInstance is
-  deprecated.
 * In User, the cookie-related methods which were wrappers for the functions on the response
   object, and were deprecated in 1.27, have been removed:
   * ::setCookie()
@@ -300,24 +215,12 @@ changes to languages because of Phabricator reports.
   * ::getPasswordFactory() – create a PasswordFactory directly
   * ::passwordChangeInputAttribs()
 * The global functions wfProfileIn and wfProfileOut, deprecated in 1.25, have been removed.
-* The following methods related to caching of half-parsed HTML were deprecated:
-  * Parser::serializeHalfParsedText()
-  * Parser::unserializeHalfParsedText()
-  * Parser::isValidHalfParsedText()
-  * StripState::getSubState()
-  * StripState::merge()
-* The "free" CSS class is now only applied to unbracketed URLs in wikitext. Links
-  written using square brackets will get the class "text" not "free".
 * SpecialPageFactory::getList(), deprecated in 1.24, has been removed. You can
   use ::getNames() instead.
 * OpenSearch::getOpenSearchTemplate(), deprecated in 1.25, has been removed. You
   can use ApiOpenSearch::getOpenSearchTemplate() instead.
 * The global function wfBaseConvert, deprecated in 1.27, has been removed. Use
   Wikimedia\base_convert() directly.
-* RFC 157418: Whitespace is trimmed from wikitext headings, wikitext list items,
-  wikitext table captions, wikitext table headings, wikitext table cells. HTML
-  headings, HTML list items, HTML table captions, HTML table headings, HTML table cells
-  will not have this trimming behavior.
 * Calling Database::begin() explicitly during an implicit transaction or when DBO_TRX
   is set results in an exception. Calling Database::commit() explicitly for an implicit
   transaction also results in an exception. Previously these were logged as errors.
@@ -333,8 +236,108 @@ changes to languages because of Phabricator reports.
   can use MediaWikiTitleCodec::getTitleInvalidRegex() instead.
 * HTMLForm & VFormHTMLForm::isVForm(), deprecated in 1.25, have been removed.
 * The ProfileSection class, deprecated in 1.25 and unused, has been removed.
+* The ResourceLoaderGetLessVars hook, deprecated in 1.30, has been removed.
+  Use ResourceLoaderModule::getLessVars() to expose local variables instead
+  of global ones.
+
+=== Deprecations in 1.31 ===
+* The Revision class was deprecated in favor of RevisionStore, BlobStore, and
+  RevisionRecord and its subclasses.
+* The global function wfBCP47 is deprecated in favour of LanguageCode::bcp47.
+* The global function wfCountDown is now deprecated in favor of Maintenance::countDown.
+* Several methods for returning lists of fields to select from the database
+  have been deprecated in favor of similar methods that also return the tables
+  to select from and the join conditions for those tables.
+  * Block::selectFields() → Block::getQueryInfo()
+  * RecentChange::selectFields() → RecentChange::getQueryInfo()
+  * ArchivedFile::selectFields() → ArchivedFile::getQueryInfo()
+  * LocalFile::selectFields() → LocalFile::getQueryInfo()
+  * LocalFile::getCacheFields() with a prefix no longer works
+  * LocalFile::getLazyCacheFields() with a prefix no longer works
+  * OldLocalFile::selectFields() → OldLocalFile::getQueryInfo()
+  * RecentChange::selectFields() → RecentChange::getQueryInfo()
+  * Revision::userJoinCond() → Revision::getQueryInfo( [ 'user' ] )
+  * Revision::selectUserFields() → Revision::getQueryInfo( [ 'user' ] )
+  * Revision::pageJoinCond() → Revision::getQueryInfo( [ 'page' ] )
+  * Revision::selectPageFields() → Revision::getQueryInfo( [ 'page' ] )
+  * Revision::selectTextFields() → Revision::getQueryInfo( [ 'text' ] )
+  * Revision::selectFields() → Revision::getQueryInfo()
+  * Revision::selectArchiveFields() → Revision::getArchiveQueryInfo()
+  * User::selectFields() → User::getQueryInfo()
+  * WikiPage::selectFields() → WikiPage::getQueryInfo()
+* Revision::setUserIdAndName() was deprecated.
+* Access to TitleValue class properties was deprecated, the relevant getters
+  should be used instead.
+* DifferenceEngine::getDiffBodyCacheKey() is deprecated. Subclasses should
+  override DifferenceEngine::getDiffBodyCacheKeyParams() instead.
+* Use of Maintenance::error( $err, $die ) to exit script was deprecated. Use
+  Maintenance::fatalError() instead.
+* Passing a ParserOptions object to OutputPage::parserOptions() is deprecated.
+* The RevisionInsertComplete hook is now deprecated, use RevisionRecordInserted instead.
+  RevisionInsertComplete is still called, but the second and third parameter will always be null.
+  Hard deprecation is scheduled for 1.32.
+* The following methods that get and set ParserOutput state are deprecated.
+  Callers should use the new stateless $options parameter to
+  ParserOutput::getText() instead.
+  * ParserOptions::getEditSection()
+  * ParserOptions::setEditSection()
+  * ParserOutput::getEditSectionTokens()
+  * ParserOutput::setEditSectionTokens()
+  * ParserOutput::getTOCEnabled()
+  * ParserOutput::setTOCEnabled()
+  * OutputPage::enableSectionEditLinks()
+  * OutputPage::sectionEditLinksEnabled()
+  * The public ParserOutput state fields $mTOCEnabled and $mEditSectionTokens are also deprecated.
+* License::getLicenses has been deprecated; use License::getLines instead.
+* QuickTemplate::setRef() was deprecated in favour of QuickTemplate::set().
+  Setting template variables by reference allowed violating the principle of data being
+  immutable once added to the skin template. In practice, this method was not being
+  used for that. Rather, setRef() existed as memory optimisation for PHP 4.
+* QuickTemplate::setTranslator() was deprecated in favour of Skin::msg() parameters.
+* MediaWikiI18N::set() was deprecated in favour of Skin::msg() parameters.
+* MediaWikiI18N::translate() was deprecated in favour of Skin::msg() or wfMessage().
+* Passing false to ParserOptions::setWrapOutputClass() is deprecated. Use the
+  'unwrap' transform to ParserOutput::getText() instead.
+* \ObjectFactory (no namespace) is deprecated, the namespaced \Wikimedia\ObjectFactory
+  from the wikimedia/object-factory library should be used instead.
+* CommentStore::newKey is deprecated. Get an instance from MediaWikiServices instead.
+* The following CommentStore methods have had their signatures changed to introduce a $key parameter,
+  usage of the methods on instances retrieved from CommentStore::newKey will remain unchanged but deprecated:
+  * CommentStore::getFields
+  * CommentStore::getJoin
+  * CommentStore::getComment
+  * CommentStore::getCommentLegacy
+  * CommentStore::insert
+  * CommentStore::insertWithTemplate
+* The following methods in Title have been renamed, and the old ones are deprecated:
+  * Title::getSkinFromCssJsSubpage – use ::getSkinFromConfigSubpage
+  * Title::isCssOrJsPage – use ::isSiteConfigPage
+  * Title::isCssJsSubpage – use ::isUserConfigPage
+  * Title::isCssSubpage – use ::isUserCssConfigPage
+  * Title::isJsSubpage – use ::isUserJsConfigPage
+* The following methods related to caching of half-parsed HTML were deprecated:
+  * Parser::serializeHalfParsedText()
+  * Parser::unserializeHalfParsedText()
+  * Parser::isValidHalfParsedText()
+  * StripState::getSubState()
+  * StripState::merge()
+* The DeferredStringifier class is deprecated, use Message::listParam() instead.
+* The type string for the parameter $lang of DateFormatter::getInstance is
+  deprecated.
 * Wikimedia\Rdbms\SavepointPostgres is deprecated.
 
+=== Other changes in 1.31 ===
+* Browser support for Internet Explorer 10 was lowered from Grade A to Grade C.
+* Browser support for Opera 12 and older was removed. Opera 15+ continues at Grade A.
+* Introducing multi-content-revision capability into the storage layer. For details,
+  see <https://www.mediawiki.org/wiki/Requests_for_comment/Multi-Content_Revisions>.
+* The "free" CSS class is now only applied to unbracketed URLs in wikitext. Links
+  written using square brackets will get the class "text" not "free".
+* RFC 157418: Whitespace is trimmed from wikitext headings, wikitext list items,
+  wikitext table captions, wikitext table headings, wikitext table cells. HTML
+  headings, HTML list items, HTML table captions, HTML table headings, HTML table cells
+  will not have this trimming behavior.
+
 == Compatibility ==
 MediaWiki 1.31 requires PHP 5.5.9 or later. Although HHVM 3.18.5 or later is supported,
 it is generally advised to use PHP 5.5.9 or later for long term support.
index f35d610..d932148 100644 (file)
@@ -2795,12 +2795,6 @@ configuration variables to JavaScript. Things that depend on the current page
 or request state must be added through MakeGlobalVariablesScript instead.
 &$vars: array( variable name => value )
 
-'ResourceLoaderGetLessVars': DEPRECATED! Called in ResourceLoader::getLessVars
-to add global LESS variables. Loaded after $wgResourceLoaderLESSVars is added.
-Global LESS variables are deprecated. Use ResourceLoaderModule::getLessVars()
-instead to expose variables only in modules that need them.
-&$lessVars: array of variables already added
-
 'ResourceLoaderJqueryMsgModuleMagicWords': Called in
 ResourceLoaderJqueryMsgModule to allow adding magic words for jQueryMsg.
 The value should be a string, and they can depend only on the
index 6f0307d..fa8f84d 100644 (file)
@@ -432,7 +432,7 @@ class WebRequest {
         * selected by a drop-down menu). For freeform input, see getText().
         *
         * @param string $name
-        * @param string $default Optional default (or null)
+        * @param string|null $default Optional default (or null)
         * @return string|null
         */
        public function getVal( $name, $default = null ) {
index 9d6314d..7fafa1f 100644 (file)
@@ -692,7 +692,7 @@ abstract class ApiBase extends ContextSource {
         * 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() ) {
@@ -1129,8 +1129,8 @@ abstract class ApiBase extends ContextSource {
                                ) {
                                        $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' ) {
@@ -1436,22 +1436,15 @@ abstract class ApiBase extends ContextSource {
                                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 ) ) {
@@ -1537,7 +1530,7 @@ abstract class ApiBase extends ContextSource {
        }
 
        /**
-        * 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
@@ -1559,15 +1552,15 @@ abstract class ApiBase extends ContextSource {
                        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;
        }
 
        /**
@@ -1609,7 +1602,7 @@ abstract class ApiBase extends ContextSource {
        }
 
        /**
-        * 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
@@ -1619,15 +1612,32 @@ abstract class ApiBase extends ContextSource {
                        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;
        }
 
        /**@}*/
index 1aa9e15..2f63faf 100644 (file)
@@ -124,8 +124,8 @@ class ApiFormatJson extends ApiFormatBase {
                                ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-ascii',
                        ],
                        'formatversion' => [
-                               ApiBase::PARAM_TYPE => [ 1, 2, 'latest' ],
-                               ApiBase::PARAM_DFLT => 1,
+                               ApiBase::PARAM_TYPE => [ '1', '2', 'latest' ],
+                               ApiBase::PARAM_DFLT => '1',
                                ApiBase::PARAM_HELP_MSG => 'apihelp-json-param-formatversion',
                        ],
                ];
index cc0f159..45bdb6d 100644 (file)
@@ -73,8 +73,8 @@ class ApiFormatPhp extends ApiFormatBase {
        public function getAllowedParams() {
                $ret = parent::getAllowedParams() + [
                        'formatversion' => [
-                               ApiBase::PARAM_TYPE => [ 1, 2, 'latest' ],
-                               ApiBase::PARAM_DFLT => 1,
+                               ApiBase::PARAM_TYPE => [ '1', '2', 'latest' ],
+                               ApiBase::PARAM_DFLT => '1',
                                ApiBase::PARAM_HELP_MSG => 'apihelp-php-param-formatversion',
                        ],
                ];
index a7e3c1b..b7b13c5 100644 (file)
@@ -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;
        }
@@ -1199,9 +1192,12 @@ class ApiMain extends ApiBase {
                // 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();
 
@@ -1220,7 +1216,10 @@ class ApiMain extends ApiBase {
                        }
 
                        if ( !isset( $moduleParams['token'] ) ) {
+                               // Probably can't happen
+                               // @codeCoverageIgnoreStart
                                $module->dieWithError( [ 'apierror-missingparam', 'token' ] );
+                               // @codeCoverageIgnoreEnd
                        }
 
                        $module->requirePostedParameters( [ 'token' ] );
@@ -1433,7 +1432,7 @@ class ApiMain extends ApiBase {
                }
 
                // Allow extensions to stop execution for arbitrary reasons.
-               $message = false;
+               $message = 'hookaborted';
                if ( !Hooks::run( 'ApiCheckCanExecute', [ $module, $user, &$message ] ) ) {
                        $this->dieWithError( $message );
                }
@@ -1720,8 +1719,8 @@ class ApiMain extends ApiBase {
        /**
         * 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;
index 5294b1d..1d3c110 100644 (file)
@@ -34,7 +34,7 @@ class ApiQueryInfo extends ApiQueryBase {
                $fld_readable = false, $fld_watched = false,
                $fld_watchers = false, $fld_visitingwatchers = false,
                $fld_notificationtimestamp = false,
-               $fld_preload = false, $fld_displaytitle = false;
+               $fld_preload = false, $fld_displaytitle = false, $fld_varianttitles = false;
 
        private $params;
 
@@ -49,7 +49,7 @@ class ApiQueryInfo extends ApiQueryBase {
                $pageLatest, $pageLength;
 
        private $protections, $restrictionTypes, $watched, $watchers, $visitingwatchers,
-               $notificationtimestamps, $talkids, $subjectids, $displaytitles;
+               $notificationtimestamps, $talkids, $subjectids, $displaytitles, $variantTitles;
        private $showZeroWatchers = false;
 
        private $tokenFunctions;
@@ -306,6 +306,7 @@ class ApiQueryInfo extends ApiQueryBase {
                        $this->fld_readable = isset( $prop['readable'] );
                        $this->fld_preload = isset( $prop['preload'] );
                        $this->fld_displaytitle = isset( $prop['displaytitle'] );
+                       $this->fld_varianttitles = isset( $prop['varianttitles'] );
                }
 
                $pageSet = $this->getPageSet();
@@ -368,6 +369,10 @@ class ApiQueryInfo extends ApiQueryBase {
                        $this->getDisplayTitle();
                }
 
+               if ( $this->fld_varianttitles ) {
+                       $this->getVariantTitles();
+               }
+
                /** @var Title $title */
                foreach ( $this->everything as $pageid => $title ) {
                        $pageInfo = $this->extractPageInfo( $pageid, $title );
@@ -510,6 +515,12 @@ class ApiQueryInfo extends ApiQueryBase {
                        }
                }
 
+               if ( $this->fld_varianttitles ) {
+                       if ( isset( $this->variantTitles[$pageid] ) ) {
+                               $pageInfo['varianttitles'] = $this->variantTitles[$pageid];
+                       }
+               }
+
                if ( $this->params['testactions'] ) {
                        $limit = $this->getMain()->canApiHighLimits() ? self::LIMIT_SML1 : self::LIMIT_SML2;
                        if ( $this->countTestedActions >= $limit ) {
@@ -740,6 +751,32 @@ class ApiQueryInfo extends ApiQueryBase {
                }
        }
 
+       private function getVariantTitles() {
+               if ( !count( $this->titles ) ) {
+                       return;
+               }
+               $this->variantTitles = [];
+               foreach ( $this->titles as $pageId => $t ) {
+                       $this->variantTitles[$pageId] = isset( $this->displaytitles[$pageId] )
+                               ? $this->getAllVariants( $this->displaytitles[$pageId] )
+                               : $this->getAllVariants( $t->getText(), $t->getNamespace() );
+               }
+       }
+
+       private function getAllVariants( $text, $ns = NS_MAIN ) {
+               global $wgContLang;
+               $result = [];
+               foreach ( $wgContLang->getVariants() as $variant ) {
+                       $convertTitle = $wgContLang->autoConvert( $text, $variant );
+                       if ( $ns !== NS_MAIN ) {
+                               $convertNs = $wgContLang->convertNamespace( $ns, $variant );
+                               $convertTitle = $convertNs . ':' . $convertTitle;
+                       }
+                       $result[$variant] = $convertTitle;
+               }
+               return $result;
+       }
+
        /**
         * Get information about watched status and put it in $this->watched
         * and $this->notificationtimestamps
@@ -879,6 +916,7 @@ class ApiQueryInfo extends ApiQueryBase {
                        'url',
                        'preload',
                        'displaytitle',
+                       'varianttitles',
                ];
                if ( array_diff( (array)$params['prop'], $publicProps ) ) {
                        return 'private';
@@ -912,6 +950,7 @@ class ApiQueryInfo extends ApiQueryBase {
                                        'readable', # private
                                        'preload',
                                        'displaytitle',
+                                       'varianttitles',
                                        // If you add more properties here, please consider whether they
                                        // need to be added to getCacheMode()
                                ],
index 35e164f..6838e54 100644 (file)
        "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>.",
        "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.",
index 96ff10f..594bf8e 100644 (file)
        "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}}",
        "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}}",
index 5ddb99b..90c3140 100644 (file)
@@ -1723,10 +1723,8 @@ MESSAGE;
         * @return array Map of variable names to string CSS values.
         */
        public function getLessVars() {
-               if ( !$this->lessVars ) {
-                       $lessVars = $this->config->get( 'ResourceLoaderLESSVars' );
-                       Hooks::run( 'ResourceLoaderGetLessVars', [ &$lessVars ] );
-                       $this->lessVars = $lessVars;
+               if ( $this->lessVars === null ) {
+                       $this->lessVars = $this->config->get( 'ResourceLoaderLESSVars' );
                }
                return $this->lessVars;
        }
index c38b0da..eb2cada 100644 (file)
@@ -276,7 +276,7 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                                'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
                                                        &$query_options, &$join_conds
                                                ) {
-                                                       $conds[] = 'rc_bot = 0';
+                                                       $conds['rc_bot'] = 0;
                                                },
                                                'cssClassSuffix' => 'bot',
                                                'isRowApplicableCallable' => function ( $ctx, $rc ) {
@@ -291,7 +291,7 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                                'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
                                                        &$query_options, &$join_conds
                                                ) {
-                                                       $conds[] = 'rc_bot = 1';
+                                                       $conds['rc_bot'] = 1;
                                                },
                                                'cssClassSuffix' => 'human',
                                                'isRowApplicableCallable' => function ( $ctx, $rc ) {
index b71defa..d750f7d 100644 (file)
@@ -4148,6 +4148,18 @@ class Language {
                return $this->mConverter;
        }
 
+       /**
+        * convert text to a variant
+        *
+        * @param string $text text to convert
+        * @param string|bool $variant variant to convert to, or false to use the user's preferred
+        *      variant (if logged in), or the project default variant
+        * @return string the converted string
+        */
+       public function autoConvert( $text, $variant = false ) {
+               return $this->mConverter->autoConvert( $text, $variant );
+       }
+
        /**
         * convert text to all supported variants
         *
@@ -4181,11 +4193,13 @@ class Language {
        /**
         * Convert a namespace index to a string in the preferred variant
         *
-        * @param int $ns
-        * @return string
+        * @param int $ns namespace index (https://www.mediawiki.org/wiki/Manual:Namespace)
+        * @param string|null $variant variant to convert to, or null to use the user's preferred
+        *      variant (if logged in), or the project default variant
+        * @return string a string representation of the namespace
         */
-       public function convertNamespace( $ns ) {
-               return $this->mConverter->convertNamespace( $ns );
+       public function convertNamespace( $ns, $variant = null ) {
+               return $this->mConverter->convertNamespace( $ns, $variant );
        }
 
        /**
index 5e9305d..8c6f1e8 100644 (file)
@@ -6,7 +6,6 @@
                        "Crazy Ivan",
                        "Dekel E",
                        "Drorsnir",
-                       "Guycn1",
                        "Guycn2",
                        "Hoo",
                        "Ijon",
index 47b6218..4d42498 100644 (file)
@@ -903,6 +903,36 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                ] );
        }
 
+       /**
+        * Alters $wgGroupPermissions for the duration of the test.  Can be called
+        * with an array, like
+        *   [ '*' => [ 'read' => false ], 'user' => [ 'read' => false ] ]
+        * or three values to set a single permission, like
+        *   $this->setGroupPermissions( '*', 'read', false );
+        *
+        * @since 1.31
+        * @param array|string $newPerms Either an array of permissions to change,
+        *   in which case the next two parameters are ignored; or a single string
+        *   identifying a group, to use with the next two parameters.
+        * @param string|null $newKey
+        * @param mixed $newValue
+        */
+       public function setGroupPermissions( $newPerms, $newKey = null, $newValue = null ) {
+               global $wgGroupPermissions;
+
+               $this->stashMwGlobals( 'wgGroupPermissions' );
+
+               if ( is_string( $newPerms ) ) {
+                       $newPerms = [ $newPerms => [ $newKey => $newValue ] ];
+               }
+
+               foreach ( $newPerms as $group => $permissions ) {
+                       foreach ( $permissions as $key => $value ) {
+                               $wgGroupPermissions[$group][$key] = $value;
+                       }
+               }
+       }
+
        /**
         * Sets the logger for a specified channel, for the duration of the test.
         * @since 1.27
index 3f6cac9..dbedfc5 100644 (file)
@@ -6,12 +6,45 @@ use Wikimedia\TestingAccessWrapper;
  * @group API
  * @group Database
  * @group medium
+ *
+ * @covers ApiBase
  */
 class ApiBaseTest extends ApiTestCase {
-
        /**
-        * @covers ApiBase::requireOnlyOneParameter
+        * This covers a variety of stub methods that return a fixed value.
+        *
+        * @param string|array $method Name of method, or [ name, params... ]
+        * @param string $value Expected value
+        *
+        * @dataProvider provideStubMethods
         */
+       public function testStubMethods( $expected, $method, $args = [] ) {
+               // Some of these are protected
+               $mock = TestingAccessWrapper::newFromObject( new MockApi() );
+               $result = call_user_func_array( [ $mock, $method ], $args );
+               $this->assertSame( $expected, $result );
+       }
+
+       public function provideStubMethods() {
+               return [
+                       [ null, 'getModuleManager' ],
+                       [ null, 'getCustomPrinter' ],
+                       [ [], 'getHelpUrls' ],
+                       // @todo This is actually overriden by MockApi
+                       // [ [], 'getAllowedParams' ],
+                       [ true, 'shouldCheckMaxLag' ],
+                       [ true, 'isReadMode' ],
+                       [ false, 'isWriteMode' ],
+                       [ false, 'mustBePosted' ],
+                       [ false, 'isDeprecated' ],
+                       [ false, 'isInternal' ],
+                       [ false, 'needsToken' ],
+                       [ null, 'getWebUITokenSalt', [ [] ] ],
+                       [ null, 'getConditionalRequestData', [ 'etag' ] ],
+                       [ null, 'dynamicParameterDocumentation' ],
+               ];
+       }
+
        public function testRequireOnlyOneParameterDefault() {
                $mock = new MockApi();
                $mock->requireOnlyOneParameter(
@@ -23,7 +56,6 @@ class ApiBaseTest extends ApiTestCase {
 
        /**
         * @expectedException ApiUsageException
-        * @covers ApiBase::requireOnlyOneParameter
         */
        public function testRequireOnlyOneParameterZero() {
                $mock = new MockApi();
@@ -35,7 +67,6 @@ class ApiBaseTest extends ApiTestCase {
 
        /**
         * @expectedException ApiUsageException
-        * @covers ApiBase::requireOnlyOneParameter
         */
        public function testRequireOnlyOneParameterTrue() {
                $mock = new MockApi();
@@ -45,38 +76,230 @@ class ApiBaseTest extends ApiTestCase {
                );
        }
 
+       public function testRequireOnlyOneParameterMissing() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'One of the parameters "foo" and "bar" is required.' );
+               $mock = new MockApi();
+               $mock->requireOnlyOneParameter(
+                       [ "filename" => "foo.txt", "enablechunks" => false ],
+                       "foo", "bar" );
+       }
+
+       public function testRequireMaxOneParameterZero() {
+               $mock = new MockApi();
+               $mock->requireMaxOneParameter(
+                       [ 'foo' => 'bar', 'baz' => 'quz' ],
+                       'squirrel' );
+               $this->assertTrue( true );
+       }
+
+       public function testRequireMaxOneParameterOne() {
+               $mock = new MockApi();
+               $mock->requireMaxOneParameter(
+                       [ 'foo' => 'bar', 'baz' => 'quz' ],
+                       'foo', 'squirrel' );
+               $this->assertTrue( true );
+       }
+
+       public function testRequireMaxOneParameterTwo() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'The parameters "foo" and "baz" can not be used together.' );
+               $mock = new MockApi();
+               $mock->requireMaxOneParameter(
+                       [ 'foo' => 'bar', 'baz' => 'quz' ],
+                       'foo', 'baz' );
+       }
+
+       public function testRequireAtLeastOneParameterZero() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'At least one of the parameters "foo" and "bar" is required.' );
+               $mock = new MockApi();
+               $mock->requireAtLeastOneParameter(
+                       [ 'a' => 'b', 'c' => 'd' ],
+                       'foo', 'bar' );
+       }
+
+       public function testRequireAtLeastOneParameterOne() {
+               $mock = new MockApi();
+               $mock->requireAtLeastOneParameter(
+                       [ 'a' => 'b', 'c' => 'd' ],
+                       'foo', 'a' );
+               $this->assertTrue( true );
+       }
+
+       public function testRequireAtLeastOneParameterTwo() {
+               $mock = new MockApi();
+               $mock->requireAtLeastOneParameter(
+                       [ 'a' => 'b', 'c' => 'd' ],
+                       'a', 'c' );
+               $this->assertTrue( true );
+       }
+
+       public function testGetTitleOrPageIdBadParams() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'The parameters "title" and "pageid" can not be used together.' );
+               $mock = new MockApi();
+               $mock->getTitleOrPageId( [ 'title' => 'a', 'pageid' => 7 ] );
+       }
+
+       public function testGetTitleOrPageIdTitle() {
+               $mock = new MockApi();
+               $result = $mock->getTitleOrPageId( [ 'title' => 'Foo' ] );
+               $this->assertInstanceOf( WikiPage::class, $result );
+               $this->assertSame( 'Foo', $result->getTitle()->getPrefixedText() );
+       }
+
+       public function testGetTitleOrPageIdInvalidTitle() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'Bad title "|".' );
+               $mock = new MockApi();
+               $mock->getTitleOrPageId( [ 'title' => '|' ] );
+       }
+
+       public function testGetTitleOrPageIdSpecialTitle() {
+               $this->setExpectedException( ApiUsageException::class,
+                       "Namespace doesn't allow actual pages." );
+               $mock = new MockApi();
+               $mock->getTitleOrPageId( [ 'title' => 'Special:RandomPage' ] );
+       }
+
+       public function testGetTitleOrPageIdPageId() {
+               $result = ( new MockApi() )->getTitleOrPageId(
+                       [ 'pageid' => Title::newFromText( 'UTPage' )->getArticleId() ] );
+               $this->assertInstanceOf( WikiPage::class, $result );
+               $this->assertSame( 'UTPage', $result->getTitle()->getPrefixedText() );
+       }
+
+       public function testGetTitleOrPageIdInvalidPageId() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'There is no page with ID 2147483648.' );
+               $mock = new MockApi();
+               $mock->getTitleOrPageId( [ 'pageid' => 2147483648 ] );
+       }
+
+       public function testGetTitleFromTitleOrPageIdBadParams() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'The parameters "title" and "pageid" can not be used together.' );
+               $mock = new MockApi();
+               $mock->getTitleFromTitleOrPageId( [ 'title' => 'a', 'pageid' => 7 ] );
+       }
+
+       public function testGetTitleFromTitleOrPageIdTitle() {
+               $mock = new MockApi();
+               $result = $mock->getTitleFromTitleOrPageId( [ 'title' => 'Foo' ] );
+               $this->assertInstanceOf( Title::class, $result );
+               $this->assertSame( 'Foo', $result->getPrefixedText() );
+       }
+
+       public function testGetTitleFromTitleOrPageIdInvalidTitle() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'Bad title "|".' );
+               $mock = new MockApi();
+               $mock->getTitleFromTitleOrPageId( [ 'title' => '|' ] );
+       }
+
+       public function testGetTitleFromTitleOrPageIdPageId() {
+               $result = ( new MockApi() )->getTitleFromTitleOrPageId(
+                       [ 'pageid' => Title::newFromText( 'UTPage' )->getArticleId() ] );
+               $this->assertInstanceOf( Title::class, $result );
+               $this->assertSame( 'UTPage', $result->getPrefixedText() );
+       }
+
+       public function testGetTitleFromTitleOrPageIdInvalidPageId() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'There is no page with ID 298401643.' );
+               $mock = new MockApi();
+               $mock->getTitleFromTitleOrPageId( [ 'pageid' => 298401643 ] );
+       }
+
        /**
         * @dataProvider provideGetParameterFromSettings
         * @param string|null $input
         * @param array $paramSettings
         * @param mixed $expected
+        * @param array $options Key-value pairs:
+        *   'parseLimits': true|false
+        *   'apihighlimits': true|false
+        *   'internalmode': true|false
         * @param string[] $warnings
-        * @covers ApiBase::getParameterFromSettings
         */
-       public function testGetParameterFromSettings( $input, $paramSettings, $expected, $warnings ) {
+       public function testGetParameterFromSettings(
+               $input, $paramSettings, $expected, $warnings, $options = []
+       ) {
                $mock = new MockApi();
                $wrapper = TestingAccessWrapper::newFromObject( $mock );
 
                $context = new DerivativeContext( $mock );
-               $context->setRequest( new FauxRequest( $input !== null ? [ 'foo' => $input ] : [] ) );
+               $context->setRequest( new FauxRequest(
+                       $input !== null ? [ 'myParam' => $input ] : [] ) );
                $wrapper->mMainModule = new ApiMain( $context );
 
-               if ( $expected instanceof ApiUsageException ) {
+               $parseLimits = isset( $options['parseLimits'] ) ?
+                       $options['parseLimits'] : true;
+
+               if ( !empty( $options['apihighlimits'] ) ) {
+                       $context->setUser( self::$users['sysop']->getUser() );
+               }
+
+               if ( isset( $options['internalmode'] ) && !$options['internalmode'] ) {
+                       $mainWrapper = TestingAccessWrapper::newFromObject( $wrapper->mMainModule );
+                       $mainWrapper->mInternalMode = false;
+               }
+
+               // If we're testing tags, set up some tags
+               if ( isset( $paramSettings[ApiBase::PARAM_TYPE] ) &&
+                       $paramSettings[ApiBase::PARAM_TYPE] === 'tags'
+               ) {
+                       ChangeTags::defineTag( 'tag1' );
+                       ChangeTags::defineTag( 'tag2' );
+               }
+
+               if ( $expected instanceof Exception ) {
                        try {
-                               $wrapper->getParameterFromSettings( 'foo', $paramSettings, true );
-                       } catch ( ApiUsageException $ex ) {
+                               $wrapper->getParameterFromSettings( 'myParam', $paramSettings,
+                                       $parseLimits );
+                               $this->fail( 'No exception thrown' );
+                       } catch ( Exception $ex ) {
                                $this->assertEquals( $expected, $ex );
                        }
                } else {
-                       $result = $wrapper->getParameterFromSettings( 'foo', $paramSettings, true );
-                       $this->assertSame( $expected, $result );
-                       $this->assertSame( $warnings, $mock->warnings );
+                       $result = $wrapper->getParameterFromSettings( 'myParam',
+                               $paramSettings, $parseLimits );
+                       if ( isset( $paramSettings[ApiBase::PARAM_TYPE] ) &&
+                               $paramSettings[ApiBase::PARAM_TYPE] === 'timestamp' &&
+                               $expected === 'now'
+                       ) {
+                               // Allow one second of fuzziness.  Make sure the formats are
+                               // correct!
+                               $this->assertRegExp( '/^\d{14}$/', $result );
+                               $this->assertLessThanOrEqual( 1,
+                                       abs( wfTimestamp( TS_UNIX, $result ) - time() ),
+                                       "Result $result differs from expected $expected by " .
+                                       'more than one second' );
+                       } else {
+                               $this->assertSame( $expected, $result );
+                       }
+                       $actualWarnings = array_map( function ( $warn ) {
+                               return $warn instanceof Message
+                                       ? array_merge( [ $warn->getKey() ], $warn->getParams() )
+                                       : $warn;
+                       }, $mock->warnings );
+                       $this->assertSame( $warnings, $actualWarnings );
+               }
+
+               if ( !empty( $paramSettings[ApiBase::PARAM_SENSITIVE] ) ||
+                       ( isset( $paramSettings[ApiBase::PARAM_TYPE] ) &&
+                       $paramSettings[ApiBase::PARAM_TYPE] === 'password' )
+               ) {
+                       $mainWrapper = TestingAccessWrapper::newFromObject( $wrapper->getMain() );
+                       $this->assertSame( [ 'myParam' ],
+                               $mainWrapper->getSensitiveParams() );
                }
        }
 
        public static function provideGetParameterFromSettings() {
                $warnings = [
-                       [ 'apiwarn-badutf8', 'foo' ],
+                       [ 'apiwarn-badutf8', 'myParam' ],
                ];
 
                $c0 = '';
@@ -88,7 +311,7 @@ class ApiBaseTest extends ApiTestCase {
                                : '�';
                }
 
-               return [
+               $returnArray = [
                        'Basic param' => [ 'bar', null, 'bar', [] ],
                        'Basic param, C0 controls' => [ $c0, null, $enc, $warnings ],
                        'String param' => [ 'bar', '', 'bar', [] ],
@@ -97,7 +320,8 @@ class ApiBaseTest extends ApiTestCase {
                        'String param, required, empty' => [
                                '',
                                [ ApiBase::PARAM_DFLT => 'default', ApiBase::PARAM_REQUIRED => true ],
-                               ApiUsageException::newWithMessage( null, [ 'apierror-missingparam', 'foo' ] ),
+                               ApiUsageException::newWithMessage( null,
+                                       [ 'apierror-missingparam', 'myParam' ] ),
                                []
                        ],
                        'Multi-valued parameter' => [
@@ -124,12 +348,846 @@ class ApiBaseTest extends ApiTestCase {
                                [ substr( $enc, 0, -3 ), '' ],
                                $warnings
                        ],
+                       'Multi-valued parameter with limits' => [
+                               'a|b|c',
+                               [
+                                       ApiBase::PARAM_ISMULTI => true,
+                                       ApiBase::PARAM_ISMULTI_LIMIT1 => 3,
+                               ],
+                               [ 'a', 'b', 'c' ],
+                               [],
+                       ],
+                       'Multi-valued parameter with exceeded limits' => [
+                               'a|b|c',
+                               [
+                                       ApiBase::PARAM_ISMULTI => true,
+                                       ApiBase::PARAM_ISMULTI_LIMIT1 => 2,
+                               ],
+                               [ 'a', 'b' ],
+                               [ [ 'apiwarn-toomanyvalues', 'myParam', 2 ] ],
+                       ],
+                       'Multi-valued parameter with exceeded limits for non-bot' => [
+                               'a|b|c',
+                               [
+                                       ApiBase::PARAM_ISMULTI => true,
+                                       ApiBase::PARAM_ISMULTI_LIMIT1 => 2,
+                                       ApiBase::PARAM_ISMULTI_LIMIT2 => 3,
+                               ],
+                               [ 'a', 'b' ],
+                               [ [ 'apiwarn-toomanyvalues', 'myParam', 2 ] ],
+                       ],
+                       'Multi-valued parameter with non-exceeded limits for bot' => [
+                               'a|b|c',
+                               [
+                                       ApiBase::PARAM_ISMULTI => true,
+                                       ApiBase::PARAM_ISMULTI_LIMIT1 => 2,
+                                       ApiBase::PARAM_ISMULTI_LIMIT2 => 3,
+                               ],
+                               [ 'a', 'b', 'c' ],
+                               [],
+                               [ 'apihighlimits' => true ],
+                       ],
+                       'Multi-valued parameter with prohibited duplicates' => [
+                               'a|b|a|c',
+                               [ ApiBase::PARAM_ISMULTI => true ],
+                               // Note that the keys are not sequential!  This matches
+                               // array_unique, but might be unexpected.
+                               [ 0 => 'a', 1 => 'b', 3 => 'c' ],
+                               [],
+                       ],
+                       'Multi-valued parameter with allowed duplicates' => [
+                               'a|a',
+                               [
+                                       ApiBase::PARAM_ISMULTI => true,
+                                       ApiBase::PARAM_ALLOW_DUPLICATES => true,
+                               ],
+                               [ 'a', 'a' ],
+                               [],
+                       ],
+                       'Empty boolean param' => [
+                               '',
+                               [ ApiBase::PARAM_TYPE => 'boolean' ],
+                               true,
+                               [],
+                       ],
+                       'Boolean param 0' => [
+                               '0',
+                               [ ApiBase::PARAM_TYPE => 'boolean' ],
+                               true,
+                               [],
+                       ],
+                       'Boolean param false' => [
+                               'false',
+                               [ ApiBase::PARAM_TYPE => 'boolean' ],
+                               true,
+                               [],
+                       ],
+                       'Boolean multi-param' => [
+                               'true|false',
+                               [
+                                       ApiBase::PARAM_TYPE => 'boolean',
+                                       ApiBase::PARAM_ISMULTI => true,
+                               ],
+                               new MWException(
+                                       'Internal error in ApiBase::getParameterFromSettings: ' .
+                                       'Multi-values not supported for myParam'
+                               ),
+                               [],
+                       ],
+                       'Empty boolean param with non-false default' => [
+                               '',
+                               [
+                                       ApiBase::PARAM_TYPE => 'boolean',
+                                       ApiBase::PARAM_DFLT => true,
+                               ],
+                               new MWException(
+                                       'Internal error in ApiBase::getParameterFromSettings: ' .
+                                       "Boolean param myParam's default is set to '1'. " .
+                                       'Boolean parameters must default to false.' ),
+                               [],
+                       ],
+                       'Deprecated parameter' => [
+                               'foo',
+                               [ ApiBase::PARAM_DEPRECATED => true ],
+                               'foo',
+                               [ [ 'apiwarn-deprecation-parameter', 'myParam' ] ],
+                       ],
+                       'Deprecated parameter value' => [
+                               'a',
+                               [ ApiBase::PARAM_DEPRECATED_VALUES => [ 'a' => true ] ],
+                               'a',
+                               [ [ 'apiwarn-deprecation-parameter', 'myParam=a' ] ],
+                       ],
+                       'Multiple deprecated parameter values' => [
+                               'a|b|c|d',
+                               [ ApiBase::PARAM_DEPRECATED_VALUES =>
+                                       [ 'b' => true, 'd' => true ],
+                                       ApiBase::PARAM_ISMULTI => true ],
+                               [ 'a', 'b', 'c', 'd' ],
+                               [
+                                       [ 'apiwarn-deprecation-parameter', 'myParam=b' ],
+                                       [ 'apiwarn-deprecation-parameter', 'myParam=d' ],
+                               ],
+                       ],
+                       'Deprecated parameter value with custom warning' => [
+                               'a',
+                               [ ApiBase::PARAM_DEPRECATED_VALUES => [ 'a' => 'my-msg' ] ],
+                               'a',
+                               [ 'my-msg' ],
+                       ],
+                       '"*" when wildcard not allowed' => [
+                               '*',
+                               [ ApiBase::PARAM_ISMULTI => true,
+                                       ApiBase::PARAM_TYPE => [ 'a', 'b', 'c' ] ],
+                               [],
+                               [ [ 'apiwarn-unrecognizedvalues', 'myParam',
+                                       [ 'list' => [ '&#42;' ], 'type' => 'comma' ], 1 ] ],
+                       ],
+                       'Wildcard "*"' => [
+                               '*',
+                               [
+                                       ApiBase::PARAM_ISMULTI => true,
+                                       ApiBase::PARAM_TYPE => [ 'a', 'b', 'c' ],
+                                       ApiBase::PARAM_ALL => true,
+                               ],
+                               [ 'a', 'b', 'c' ],
+                               [],
+                       ],
+                       'Wildcard "*" with multiples not allowed' => [
+                               '*',
+                               [
+                                       ApiBase::PARAM_TYPE => [ 'a', 'b', 'c' ],
+                                       ApiBase::PARAM_ALL => true,
+                               ],
+                               ApiUsageException::newWithMessage( null,
+                                       [ 'apierror-unrecognizedvalue', 'myParam', '&#42;' ],
+                                       'unknown_myParam' ),
+                               [],
+                       ],
+                       'Wildcard "*" with unrestricted type' => [
+                               '*',
+                               [
+                                       ApiBase::PARAM_ISMULTI => true,
+                                       ApiBase::PARAM_ALL => true,
+                               ],
+                               [ '*' ],
+                               [],
+                       ],
+                       'Wildcard "x"' => [
+                               'x',
+                               [
+                                       ApiBase::PARAM_ISMULTI => true,
+                                       ApiBase::PARAM_TYPE => [ 'a', 'b', 'c' ],
+                                       ApiBase::PARAM_ALL => 'x',
+                               ],
+                               [ 'a', 'b', 'c' ],
+                               [],
+                       ],
+                       'Wildcard conflicting with allowed value' => [
+                               'a',
+                               [
+                                       ApiBase::PARAM_ISMULTI => true,
+                                       ApiBase::PARAM_TYPE => [ 'a', 'b', 'c' ],
+                                       ApiBase::PARAM_ALL => 'a',
+                               ],
+                               new MWException(
+                                       'Internal error in ApiBase::getParameterFromSettings: ' .
+                                       'For param myParam, PARAM_ALL collides with a possible ' .
+                                       'value' ),
+                               [],
+                       ],
+                       'Namespace with wildcard' => [
+                               '*',
+                               [
+                                       ApiBase::PARAM_ISMULTI => true,
+                                       ApiBase::PARAM_TYPE => 'namespace',
+                               ],
+                               MWNamespace::getValidNamespaces(),
+                               [],
+                       ],
+                       // PARAM_ALL is ignored with namespace types.
+                       'Namespace with wildcard suppressed' => [
+                               '*',
+                               [
+                                       ApiBase::PARAM_ISMULTI => true,
+                                       ApiBase::PARAM_TYPE => 'namespace',
+                                       ApiBase::PARAM_ALL => false,
+                               ],
+                               MWNamespace::getValidNamespaces(),
+                               [],
+                       ],
+                       'Namespace with wildcard "x"' => [
+                               'x',
+                               [
+                                       ApiBase::PARAM_ISMULTI => true,
+                                       ApiBase::PARAM_TYPE => 'namespace',
+                                       ApiBase::PARAM_ALL => 'x',
+                               ],
+                               [],
+                               [ [ 'apiwarn-unrecognizedvalues', 'myParam',
+                                       [ 'list' => [ 'x' ], 'type' => 'comma' ], 1 ] ],
+                       ],
+                       'Password' => [
+                               'dDy+G?e?txnr.1:(@[Ru',
+                               [ ApiBase::PARAM_TYPE => 'password' ],
+                               'dDy+G?e?txnr.1:(@[Ru',
+                               [],
+                       ],
+                       'Sensitive field' => [
+                               'I am fond of pineapples',
+                               [ ApiBase::PARAM_SENSITIVE => true ],
+                               'I am fond of pineapples',
+                               [],
+                       ],
+                       'Upload with default' => [
+                               '',
+                               [
+                                       ApiBase::PARAM_TYPE => 'upload',
+                                       ApiBase::PARAM_DFLT => '',
+                               ],
+                               new MWException(
+                                       'Internal error in ApiBase::getParameterFromSettings: ' .
+                                       "File upload param myParam's default is set to ''. " .
+                                       'File upload parameters may not have a default.' ),
+                               [],
+                       ],
+                       'Multiple upload' => [
+                               '',
+                               [
+                                       ApiBase::PARAM_TYPE => 'upload',
+                                       ApiBase::PARAM_ISMULTI => true,
+                               ],
+                               new MWException(
+                                       'Internal error in ApiBase::getParameterFromSettings: ' .
+                                       'Multi-values not supported for myParam' ),
+                               [],
+                       ],
+                       // @todo Test actual upload
+                       'Namespace -1' => [
+                               '-1',
+                               [ ApiBase::PARAM_TYPE => 'namespace' ],
+                               ApiUsageException::newWithMessage( null,
+                                       [ 'apierror-unrecognizedvalue', 'myParam', '-1' ],
+                                       'unknown_myParam' ),
+                               [],
+                       ],
+                       'Extra namespace -1' => [
+                               '-1',
+                               [
+                                       ApiBase::PARAM_TYPE => 'namespace',
+                                       ApiBase::PARAM_EXTRA_NAMESPACES => [ '-1' ],
+                               ],
+                               '-1',
+                               [],
+                       ],
+                       // @todo Test with PARAM_SUBMODULE_MAP unset, need
+                       // getModuleManager() to return something real
+                       'Nonexistent module' => [
+                               'not-a-module-name',
+                               [
+                                       ApiBase::PARAM_TYPE => 'submodule',
+                                       ApiBase::PARAM_SUBMODULE_MAP =>
+                                               [ 'foo' => 'foo', 'bar' => 'foo+bar' ],
+                               ],
+                               ApiUsageException::newWithMessage(
+                                       null,
+                                       [
+                                               'apierror-unrecognizedvalue',
+                                               'myParam',
+                                               'not-a-module-name',
+                                       ],
+                                       'unknown_myParam'
+                               ),
+                               [],
+                       ],
+                       '\\x1f with multiples not allowed' => [
+                               "\x1f",
+                               [],
+                               ApiUsageException::newWithMessage( null,
+                                       'apierror-badvalue-notmultivalue',
+                                       'badvalue_notmultivalue' ),
+                               [],
+                       ],
+                       'Integer with unenforced min' => [
+                               '-2',
+                               [
+                                       ApiBase::PARAM_TYPE => 'integer',
+                                       ApiBase::PARAM_MIN => -1,
+                               ],
+                               -1,
+                               [ [ 'apierror-integeroutofrange-belowminimum', 'myParam', -1,
+                                       -2 ] ],
+                       ],
+                       'Integer with enforced min' => [
+                               '-2',
+                               [
+                                       ApiBase::PARAM_TYPE => 'integer',
+                                       ApiBase::PARAM_MIN => -1,
+                                       ApiBase::PARAM_RANGE_ENFORCE => true,
+                               ],
+                               ApiUsageException::newWithMessage( null,
+                                       [ 'apierror-integeroutofrange-belowminimum', 'myParam',
+                                       '-1', '-2' ], 'integeroutofrange',
+                                       [ 'min' => -1, 'max' => null, 'botMax' => null ] ),
+                               [],
+                       ],
+                       'Integer with unenforced max (internal mode)' => [
+                               '8',
+                               [
+                                       ApiBase::PARAM_TYPE => 'integer',
+                                       ApiBase::PARAM_MAX => 7,
+                               ],
+                               8,
+                               [],
+                       ],
+                       'Integer with enforced max (internal mode)' => [
+                               '8',
+                               [
+                                       ApiBase::PARAM_TYPE => 'integer',
+                                       ApiBase::PARAM_MAX => 7,
+                                       ApiBase::PARAM_RANGE_ENFORCE => true,
+                               ],
+                               8,
+                               [],
+                       ],
+                       'Integer with unenforced max (non-internal mode)' => [
+                               '8',
+                               [
+                                       ApiBase::PARAM_TYPE => 'integer',
+                                       ApiBase::PARAM_MAX => 7,
+                               ],
+                               7,
+                               [ [ 'apierror-integeroutofrange-abovemax', 'myParam', 7, 8 ] ],
+                               [ 'internalmode' => false ],
+                       ],
+                       'Integer with enforced max (non-internal mode)' => [
+                               '8',
+                               [
+                                       ApiBase::PARAM_TYPE => 'integer',
+                                       ApiBase::PARAM_MAX => 7,
+                                       ApiBase::PARAM_RANGE_ENFORCE => true,
+                               ],
+                               ApiUsageException::newWithMessage(
+                                       null,
+                                       [ 'apierror-integeroutofrange-abovemax', 'myParam', '7', '8' ],
+                                       'integeroutofrange',
+                                       [ 'min' => null, 'max' => 7, 'botMax' => 7 ]
+                               ),
+                               [],
+                               [ 'internalmode' => false ],
+                       ],
+                       'Array of integers' => [
+                               '3|12|966|-1',
+                               [
+                                       ApiBase::PARAM_ISMULTI => true,
+                                       ApiBase::PARAM_TYPE => 'integer',
+                               ],
+                               [ 3, 12, 966, -1 ],
+                               [],
+                       ],
+                       'Array of integers with unenforced min/max (internal mode)' => [
+                               '3|12|966|-1',
+                               [
+                                       ApiBase::PARAM_ISMULTI => true,
+                                       ApiBase::PARAM_TYPE => 'integer',
+                                       ApiBase::PARAM_MIN => 0,
+                                       ApiBase::PARAM_MAX => 100,
+                               ],
+                               [ 3, 12, 966, 0 ],
+                               [ [ 'apierror-integeroutofrange-belowminimum', 'myParam', 0, -1 ] ],
+                       ],
+                       'Array of integers with enforced min/max (internal mode)' => [
+                               '3|12|966|-1',
+                               [
+                                       ApiBase::PARAM_ISMULTI => true,
+                                       ApiBase::PARAM_TYPE => 'integer',
+                                       ApiBase::PARAM_MIN => 0,
+                                       ApiBase::PARAM_MAX => 100,
+                                       ApiBase::PARAM_RANGE_ENFORCE => true,
+                               ],
+                               ApiUsageException::newWithMessage(
+                                       null,
+                                       [ 'apierror-integeroutofrange-belowminimum', 'myParam', 0, -1 ],
+                                       'integeroutofrange',
+                                       [ 'min' => 0, 'max' => 100, 'botMax' => 100 ]
+                               ),
+                               [],
+                       ],
+                       'Array of integers with unenforced min/max (non-internal mode)' => [
+                               '3|12|966|-1',
+                               [
+                                       ApiBase::PARAM_ISMULTI => true,
+                                       ApiBase::PARAM_TYPE => 'integer',
+                                       ApiBase::PARAM_MIN => 0,
+                                       ApiBase::PARAM_MAX => 100,
+                               ],
+                               [ 3, 12, 100, 0 ],
+                               [
+                                       [ 'apierror-integeroutofrange-abovemax', 'myParam', 100, 966 ],
+                                       [ 'apierror-integeroutofrange-belowminimum', 'myParam', 0, -1 ]
+                               ],
+                               [ 'internalmode' => false ],
+                       ],
+                       'Array of integers with enforced min/max (non-internal mode)' => [
+                               '3|12|966|-1',
+                               [
+                                       ApiBase::PARAM_ISMULTI => true,
+                                       ApiBase::PARAM_TYPE => 'integer',
+                                       ApiBase::PARAM_MIN => 0,
+                                       ApiBase::PARAM_MAX => 100,
+                                       ApiBase::PARAM_RANGE_ENFORCE => true,
+                               ],
+                               ApiUsageException::newWithMessage(
+                                       null,
+                                       [ 'apierror-integeroutofrange-abovemax', 'myParam', 100, 966 ],
+                                       'integeroutofrange',
+                                       [ 'min' => 0, 'max' => 100, 'botMax' => 100 ]
+                               ),
+                               [],
+                               [ 'internalmode' => false ],
+                       ],
+                       'Limit with parseLimits false' => [
+                               '100',
+                               [ ApiBase::PARAM_TYPE => 'limit' ],
+                               '100',
+                               [],
+                               [ 'parseLimits' => false ],
+                       ],
+                       'Limit with no max' => [
+                               '100',
+                               [
+                                       ApiBase::PARAM_TYPE => 'limit',
+                                       ApiBase::PARAM_MAX2 => 10,
+                                       ApiBase::PARAM_ISMULTI => true,
+                               ],
+                               new MWException(
+                                       'Internal error in ApiBase::getParameterFromSettings: ' .
+                                       'MAX1 or MAX2 are not defined for the limit myParam' ),
+                               [],
+                       ],
+                       'Limit with no max2' => [
+                               '100',
+                               [
+                                       ApiBase::PARAM_TYPE => 'limit',
+                                       ApiBase::PARAM_MAX => 10,
+                                       ApiBase::PARAM_ISMULTI => true,
+                               ],
+                               new MWException(
+                                       'Internal error in ApiBase::getParameterFromSettings: ' .
+                                       'MAX1 or MAX2 are not defined for the limit myParam' ),
+                               [],
+                       ],
+                       'Limit with multi-value' => [
+                               '100',
+                               [
+                                       ApiBase::PARAM_TYPE => 'limit',
+                                       ApiBase::PARAM_MAX => 10,
+                                       ApiBase::PARAM_MAX2 => 10,
+                                       ApiBase::PARAM_ISMULTI => true,
+                               ],
+                               new MWException(
+                                       'Internal error in ApiBase::getParameterFromSettings: ' .
+                                       'Multi-values not supported for myParam' ),
+                               [],
+                       ],
+                       'Valid limit' => [
+                               '100',
+                               [
+                                       ApiBase::PARAM_TYPE => 'limit',
+                                       ApiBase::PARAM_MAX => 100,
+                                       ApiBase::PARAM_MAX2 => 100,
+                               ],
+                               100,
+                               [],
+                       ],
+                       'Limit max' => [
+                               'max',
+                               [
+                                       ApiBase::PARAM_TYPE => 'limit',
+                                       ApiBase::PARAM_MAX => 100,
+                                       ApiBase::PARAM_MAX2 => 101,
+                               ],
+                               100,
+                               [],
+                       ],
+                       'Limit max for apihighlimits' => [
+                               'max',
+                               [
+                                       ApiBase::PARAM_TYPE => 'limit',
+                                       ApiBase::PARAM_MAX => 100,
+                                       ApiBase::PARAM_MAX2 => 101,
+                               ],
+                               101,
+                               [],
+                               [ 'apihighlimits' => true ],
+                       ],
+                       'Limit too large (internal mode)' => [
+                               '101',
+                               [
+                                       ApiBase::PARAM_TYPE => 'limit',
+                                       ApiBase::PARAM_MAX => 100,
+                                       ApiBase::PARAM_MAX2 => 101,
+                               ],
+                               101,
+                               [],
+                       ],
+                       'Limit okay for apihighlimits (internal mode)' => [
+                               '101',
+                               [
+                                       ApiBase::PARAM_TYPE => 'limit',
+                                       ApiBase::PARAM_MAX => 100,
+                                       ApiBase::PARAM_MAX2 => 101,
+                               ],
+                               101,
+                               [],
+                               [ 'apihighlimits' => true ],
+                       ],
+                       'Limit too large for apihighlimits (internal mode)' => [
+                               '102',
+                               [
+                                       ApiBase::PARAM_TYPE => 'limit',
+                                       ApiBase::PARAM_MAX => 100,
+                                       ApiBase::PARAM_MAX2 => 101,
+                               ],
+                               102,
+                               [],
+                               [ 'apihighlimits' => true ],
+                       ],
+                       'Limit too large (non-internal mode)' => [
+                               '101',
+                               [
+                                       ApiBase::PARAM_TYPE => 'limit',
+                                       ApiBase::PARAM_MAX => 100,
+                                       ApiBase::PARAM_MAX2 => 101,
+                               ],
+                               100,
+                               [ [ 'apierror-integeroutofrange-abovemax', 'myParam', 100, 101 ] ],
+                               [ 'internalmode' => false ],
+                       ],
+                       'Limit okay for apihighlimits (non-internal mode)' => [
+                               '101',
+                               [
+                                       ApiBase::PARAM_TYPE => 'limit',
+                                       ApiBase::PARAM_MAX => 100,
+                                       ApiBase::PARAM_MAX2 => 101,
+                               ],
+                               101,
+                               [],
+                               [ 'internalmode' => false, 'apihighlimits' => true ],
+                       ],
+                       'Limit too large for apihighlimits (non-internal mode)' => [
+                               '102',
+                               [
+                                       ApiBase::PARAM_TYPE => 'limit',
+                                       ApiBase::PARAM_MAX => 100,
+                                       ApiBase::PARAM_MAX2 => 101,
+                               ],
+                               101,
+                               [ [ 'apierror-integeroutofrange-abovebotmax', 'myParam', 101, 102 ] ],
+                               [ 'internalmode' => false, 'apihighlimits' => true ],
+                       ],
+                       'Limit too small' => [
+                               '-2',
+                               [
+                                       ApiBase::PARAM_TYPE => 'limit',
+                                       ApiBase::PARAM_MIN => -1,
+                                       ApiBase::PARAM_MAX => 100,
+                                       ApiBase::PARAM_MAX2 => 100,
+                               ],
+                               -1,
+                               [ [ 'apierror-integeroutofrange-belowminimum', 'myParam', -1,
+                                       -2 ] ],
+                       ],
+                       'Timestamp' => [
+                               wfTimestamp( TS_UNIX, '20211221122112' ),
+                               [ ApiBase::PARAM_TYPE => 'timestamp' ],
+                               '20211221122112',
+                               [],
+                       ],
+                       'Timestamp 0' => [
+                               '0',
+                               [ ApiBase::PARAM_TYPE => 'timestamp' ],
+                               // Magic keyword
+                               'now',
+                               [ [ 'apiwarn-unclearnowtimestamp', 'myParam', '0' ] ],
+                       ],
+                       'Timestamp empty' => [
+                               '',
+                               [ ApiBase::PARAM_TYPE => 'timestamp' ],
+                               'now',
+                               [ [ 'apiwarn-unclearnowtimestamp', 'myParam', '' ] ],
+                       ],
+                       // wfTimestamp() interprets this as Unix time
+                       'Timestamp 00' => [
+                               '00',
+                               [ ApiBase::PARAM_TYPE => 'timestamp' ],
+                               '19700101000000',
+                               [],
+                       ],
+                       'Timestamp now' => [
+                               'now',
+                               [ ApiBase::PARAM_TYPE => 'timestamp' ],
+                               'now',
+                               [],
+                       ],
+                       'Invalid timestamp' => [
+                               'a potato',
+                               [ ApiBase::PARAM_TYPE => 'timestamp' ],
+                               ApiUsageException::newWithMessage(
+                                       null,
+                                       [ 'apierror-badtimestamp', 'myParam', 'a potato' ],
+                                       'badtimestamp_myParam'
+                               ),
+                               [],
+                       ],
+                       'Timestamp array' => [
+                               '100|101',
+                               [
+                                       ApiBase::PARAM_TYPE => 'timestamp',
+                                       ApiBase::PARAM_ISMULTI => 1,
+                               ],
+                               [ wfTimestamp( TS_MW, 100 ), wfTimestamp( TS_MW, 101 ) ],
+                               [],
+                       ],
+                       'User' => [
+                               'foo_bar',
+                               [ ApiBase::PARAM_TYPE => 'user' ],
+                               'Foo bar',
+                               [],
+                       ],
+                       'Invalid username "|"' => [
+                               '|',
+                               [ ApiBase::PARAM_TYPE => 'user' ],
+                               ApiUsageException::newWithMessage( null,
+                                       [ 'apierror-baduser', 'myParam', '&#124;' ],
+                                       'baduser_myParam' ),
+                               [],
+                       ],
+                       'Invalid username "300.300.300.300"' => [
+                               '300.300.300.300',
+                               [ ApiBase::PARAM_TYPE => 'user' ],
+                               ApiUsageException::newWithMessage( null,
+                                       [ 'apierror-baduser', 'myParam', '300.300.300.300' ],
+                                       'baduser_myParam' ),
+                               [],
+                       ],
+                       'IP range as username' => [
+                               '10.0.0.0/8',
+                               [ ApiBase::PARAM_TYPE => 'user' ],
+                               '10.0.0.0/8',
+                               [],
+                       ],
+                       'IPv6 as username' => [
+                               '::1',
+                               [ ApiBase::PARAM_TYPE => 'user' ],
+                               '0:0:0:0:0:0:0:1',
+                               [],
+                       ],
+                       'Obsolete cloaked usemod IP address as username' => [
+                               '1.2.3.xxx',
+                               [ ApiBase::PARAM_TYPE => 'user' ],
+                               '1.2.3.xxx',
+                               [],
+                       ],
+                       'Invalid username containing IP address' => [
+                               'This is [not] valid 1.2.3.xxx, ha!',
+                               [ ApiBase::PARAM_TYPE => 'user' ],
+                               ApiUsageException::newWithMessage(
+                                       null,
+                                       [ 'apierror-baduser', 'myParam', 'This is &#91;not&#93; valid 1.2.3.xxx, ha!' ],
+                                       'baduser_myParam'
+                               ),
+                               [],
+                       ],
+                       'External username' => [
+                               'M>Foo bar',
+                               [ ApiBase::PARAM_TYPE => 'user' ],
+                               'M>Foo bar',
+                               [],
+                       ],
+                       'Array of usernames' => [
+                               'foo|bar',
+                               [
+                                       ApiBase::PARAM_TYPE => 'user',
+                                       ApiBase::PARAM_ISMULTI => true,
+                               ],
+                               [ 'Foo', 'Bar' ],
+                               [],
+                       ],
+                       'tag' => [
+                               'tag1',
+                               [ ApiBase::PARAM_TYPE => 'tags' ],
+                               [ 'tag1' ],
+                               [],
+                       ],
+                       'Array of one tag' => [
+                               'tag1',
+                               [
+                                       ApiBase::PARAM_TYPE => 'tags',
+                                       ApiBase::PARAM_ISMULTI => true,
+                               ],
+                               [ 'tag1' ],
+                               [],
+                       ],
+                       'Array of tags' => [
+                               'tag1|tag2',
+                               [
+                                       ApiBase::PARAM_TYPE => 'tags',
+                                       ApiBase::PARAM_ISMULTI => true,
+                               ],
+                               [ 'tag1', 'tag2' ],
+                               [],
+                       ],
+                       'Invalid tag' => [
+                               'invalid tag',
+                               [ ApiBase::PARAM_TYPE => 'tags' ],
+                               new ApiUsageException( null,
+                                       Status::newFatal( 'tags-apply-not-allowed-one',
+                                       'invalid tag', 1 ) ),
+                               [],
+                       ],
+                       'Unrecognized type' => [
+                               'foo',
+                               [ ApiBase::PARAM_TYPE => 'nonexistenttype' ],
+                               new MWException(
+                                       'Internal error in ApiBase::getParameterFromSettings: ' .
+                                       "Param myParam's type is unknown - nonexistenttype" ),
+                               [],
+                       ],
+                       'Too many bytes' => [
+                               '1',
+                               [
+                                       ApiBase::PARAM_MAX_BYTES => 0,
+                                       ApiBase::PARAM_MAX_CHARS => 0,
+                               ],
+                               ApiUsageException::newWithMessage( null,
+                                       [ 'apierror-maxbytes', 'myParam', 0 ] ),
+                               [],
+                       ],
+                       'Too many chars' => [
+                               '§§',
+                               [
+                                       ApiBase::PARAM_MAX_BYTES => 4,
+                                       ApiBase::PARAM_MAX_CHARS => 1,
+                               ],
+                               ApiUsageException::newWithMessage( null,
+                                       [ 'apierror-maxchars', 'myParam', 1 ] ),
+                               [],
+                       ],
+                       'Omitted required param' => [
+                               null,
+                               [ ApiBase::PARAM_REQUIRED => true ],
+                               ApiUsageException::newWithMessage( null,
+                                       [ 'apierror-missingparam', 'myParam' ] ),
+                               [],
+                       ],
+                       'Empty multi-value' => [
+                               '',
+                               [ ApiBase::PARAM_ISMULTI => true ],
+                               [],
+                               [],
+                       ],
+                       'Multi-value \x1f' => [
+                               "\x1f",
+                               [ ApiBase::PARAM_ISMULTI => true ],
+                               [],
+                               [],
+                       ],
+                       'Allowed non-multi-value with "|"' => [
+                               'a|b',
+                               [ ApiBase::PARAM_TYPE => [ 'a|b' ] ],
+                               'a|b',
+                               [],
+                       ],
+                       'Prohibited multi-value' => [
+                               'a|b',
+                               [ ApiBase::PARAM_TYPE => [ 'a', 'b' ] ],
+                               ApiUsageException::newWithMessage( null,
+                                       [
+                                               'apierror-multival-only-one-of',
+                                               'myParam',
+                                               Message::listParam( [ '<kbd>a</kbd>', '<kbd>b</kbd>' ] ),
+                                               2
+                                       ],
+                                       'multival_myParam'
+                               ),
+                               [],
+                       ],
+               ];
+
+               // The following really just test PHP's string-to-int conversion.
+               $integerTests = [
+                       [ '+1', 1 ],
+                       [ '-1', -1 ],
+                       [ '1e3', 1 ],
+                       [ '1.5', 1 ],
+                       [ '-1.5', -1 ],
+                       [ '1abc', 1 ],
+                       [ ' 1', 1 ],
+                       [ "\t1", 1, '\t1' ],
+                       [ "\r1", 1, '\r1' ],
+                       [ "\f1", 0, '\f1', 'badutf-8' ],
+                       [ "\n1", 1, '\n1' ],
+                       [ "\v1", 0, '\v1', 'badutf-8' ],
+                       [ "\e1", 0, '\e1', 'badutf-8' ],
+                       [ "\x001", 0, '\x001', 'badutf-8' ],
                ];
+
+               foreach ( $integerTests as $test ) {
+                       $desc = isset( $test[2] ) ? $test[2] : $test[0];
+                       $warnings = isset( $test[3] ) ?
+                               [ [ 'apiwarn-badutf8', 'myParam' ] ] : [];
+                       $returnArray["\"$desc\" as integer"] = [
+                               $test[0],
+                               [ ApiBase::PARAM_TYPE => 'integer' ],
+                               $test[1],
+                               $warnings,
+                       ];
+               }
+
+               return $returnArray;
        }
 
-       /**
-        * @covers ApiBase::errorArrayToStatus
-        */
        public function testErrorArrayToStatus() {
                $mock = new MockApi();
 
@@ -179,9 +1237,6 @@ class ApiBaseTest extends ApiTestCase {
                ], $user ) );
        }
 
-       /**
-        * @covers ApiBase::dieStatus
-        */
        public function testDieStatus() {
                $mock = new MockApi();
 
index 67d323f..8ffe4fc 100644 (file)
@@ -24,6 +24,356 @@ class ApiMainTest extends ApiTestCase {
                $this->assertArrayHasKey( 'query', $data );
        }
 
+       public function testApiNoParam() {
+               $api = new ApiMain();
+               $api->execute();
+               $data = $api->getResult()->getResultData();
+               $this->assertInternalType( 'array', $data );
+       }
+
+       /**
+        * ApiMain behaves differently if passed a FauxRequest (mInternalMode set
+        * to true) or a proper WebRequest (mInternalMode false).  For most tests
+        * we can just set mInternalMode to false using TestingAccessWrapper, but
+        * this doesn't work for the constructor.  This method returns an ApiMain
+        * that's been set up in non-internal mode.
+        *
+        * Note that calling execute() will print to the console.  Wrap it in
+        * ob_start()/ob_end_clean() to prevent this.
+        *
+        * @param array $requestData Query parameters for the WebRequest
+        * @param array $headers Headers for the WebRequest
+        */
+       private function getNonInternalApiMain( array $requestData, array $headers = [] ) {
+               $req = $this->getMockBuilder( WebRequest::class )
+                       ->setMethods( [ 'response', 'getIP' ] )
+                       ->getMock();
+               $response = new FauxResponse();
+               $req->method( 'response' )->willReturn( $response );
+               $req->method( 'getRawIP' )->willReturn( '127.0.0.1' );
+
+               $wrapper = TestingAccessWrapper::newFromObject( $req );
+               $wrapper->data = $requestData;
+               if ( $headers ) {
+                       $wrapper->headers = $headers;
+               }
+
+               return new ApiMain( $req );
+       }
+
+       public function testUselang() {
+               global $wgLang;
+
+               $api = $this->getNonInternalApiMain( [
+                       'action' => 'query',
+                       'meta' => 'siteinfo',
+                       'uselang' => 'fr',
+               ] );
+
+               ob_start();
+               $api->execute();
+               ob_end_clean();
+
+               $this->assertSame( 'fr', $wgLang->getCode() );
+       }
+
+       public function testNonWhitelistedCorsWithCookies() {
+               $logFile = $this->getNewTempFile();
+
+               $this->mergeMwGlobalArrayValue( '_COOKIE', [ 'forceHTTPS' => '1' ] );
+               $logger = new TestLogger( true );
+               $this->setLogger( 'cors', $logger );
+
+               $api = $this->getNonInternalApiMain( [
+                       'action' => 'query',
+                       'meta' => 'siteinfo',
+               // For some reason multiple origins (which are not allowed in the
+               // WHATWG Fetch spec that supersedes the RFC) are always considered to
+               // be problematic.
+               ], [ 'ORIGIN' => 'https://www.example.com https://www.com.example' ] );
+
+               $this->assertSame(
+                       [ [ Psr\Log\LogLevel::WARNING, 'Non-whitelisted CORS request with session cookies' ] ],
+                       $logger->getBuffer()
+               );
+       }
+
+       public function testSuppressedLogin() {
+               global $wgUser;
+               $origUser = $wgUser;
+
+               $api = $this->getNonInternalApiMain( [
+                       'action' => 'query',
+                       'meta' => 'siteinfo',
+                       'origin' => '*',
+               ] );
+
+               ob_start();
+               $api->execute();
+               ob_end_clean();
+
+               $this->assertNotSame( $origUser, $wgUser );
+               $this->assertSame( 'true', $api->getContext()->getRequest()->response()
+                       ->getHeader( 'MediaWiki-Login-Suppressed' ) );
+       }
+
+       public function testSetContinuationManager() {
+               $api = new ApiMain();
+               $manager = $this->createMock( ApiContinuationManager::class );
+               $api->setContinuationManager( $manager );
+               $this->assertTrue( true, 'No exception' );
+               return [ $api, $manager ];
+       }
+
+       /**
+        * @depends testSetContinuationManager
+        */
+       public function testSetContinuationManagerTwice( $args ) {
+               $this->setExpectedException( UnexpectedValueException::class,
+                       'ApiMain::setContinuationManager: tried to set manager from  ' .
+                       'when a manager is already set from ' );
+
+               list( $api, $manager ) = $args;
+               $api->setContinuationManager( $manager );
+       }
+
+       public function testSetCacheModeUnrecognized() {
+               $api = new ApiMain();
+               $api->setCacheMode( 'unrecognized' );
+               $this->assertSame(
+                       'private',
+                       TestingAccessWrapper::newFromObject( $api )->mCacheMode,
+                       'Unrecognized params must be silently ignored'
+               );
+       }
+
+       public function testSetCacheModePrivateWiki() {
+               $this->setGroupPermissions( '*', 'read', false );
+
+               $wrappedApi = TestingAccessWrapper::newFromObject( new ApiMain() );
+               $wrappedApi->setCacheMode( 'public' );
+               $this->assertSame( 'private', $wrappedApi->mCacheMode );
+               $wrappedApi->setCacheMode( 'anon-public-user-private' );
+               $this->assertSame( 'private', $wrappedApi->mCacheMode );
+       }
+
+       public function testAddRequestedFieldsRequestId() {
+               $req = new FauxRequest( [
+                       'action' => 'query',
+                       'meta' => 'siteinfo',
+                       'requestid' => '123456',
+               ] );
+               $api = new ApiMain( $req );
+               $api->execute();
+               $this->assertSame( '123456', $api->getResult()->getResultData()['requestid'] );
+       }
+
+       public function testAddRequestedFieldsCurTimestamp() {
+               $req = new FauxRequest( [
+                       'action' => 'query',
+                       'meta' => 'siteinfo',
+                       'curtimestamp' => '',
+               ] );
+               $api = new ApiMain( $req );
+               $api->execute();
+               $timestamp = $api->getResult()->getResultData()['curtimestamp'];
+               $this->assertLessThanOrEqual( 1, abs( strtotime( $timestamp ) - time() ) );
+       }
+
+       public function testAddRequestedFieldsResponseLangInfo() {
+               $req = new FauxRequest( [
+                       'action' => 'query',
+                       'meta' => 'siteinfo',
+                       // errorlang is ignored if errorformat is not specified
+                       'errorformat' => 'plaintext',
+                       'uselang' => 'FR',
+                       'errorlang' => 'ja',
+                       'responselanginfo' => '',
+               ] );
+               $api = new ApiMain( $req );
+               $api->execute();
+               $data = $api->getResult()->getResultData();
+               $this->assertSame( 'fr', $data['uselang'] );
+               $this->assertSame( 'ja', $data['errorlang'] );
+       }
+
+       public function testSetupModuleUnknown() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'Unrecognized value for parameter "action": unknownaction.' );
+
+               $req = new FauxRequest( [ 'action' => 'unknownaction' ] );
+               $api = new ApiMain( $req );
+               $api->execute();
+       }
+
+       public function testSetupModuleNoTokenProvided() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'The "token" parameter must be set.' );
+
+               $req = new FauxRequest( [
+                       'action' => 'edit',
+                       'title' => 'New page',
+                       'text' => 'Some text',
+               ] );
+               $api = new ApiMain( $req );
+               $api->execute();
+       }
+
+       public function testSetupModuleInvalidTokenProvided() {
+               $this->setExpectedException( ApiUsageException::class, 'Invalid CSRF token.' );
+
+               $req = new FauxRequest( [
+                       'action' => 'edit',
+                       'title' => 'New page',
+                       'text' => 'Some text',
+                       'token' => "This isn't a real token!",
+               ] );
+               $api = new ApiMain( $req );
+               $api->execute();
+       }
+
+       public function testSetupModuleNeedsTokenTrue() {
+               $this->setExpectedException( MWException::class,
+                       "Module 'testmodule' must be updated for the new token handling. " .
+                       "See documentation for ApiBase::needsToken for details." );
+
+               $mock = $this->createMock( ApiBase::class );
+               $mock->method( 'getModuleName' )->willReturn( 'testmodule' );
+               $mock->method( 'needsToken' )->willReturn( true );
+
+               $api = new ApiMain( new FauxRequest( [ 'action' => 'testmodule' ] ) );
+               $api->getModuleManager()->addModule( 'testmodule', 'action', get_class( $mock ),
+                       function () use ( $mock ) {
+                               return $mock;
+                       }
+               );
+               $api->execute();
+       }
+
+       public function testSetupModuleNeedsTokenNeedntBePosted() {
+               $this->setExpectedException( MWException::class,
+                       "Module 'testmodule' must require POST to use tokens." );
+
+               $mock = $this->createMock( ApiBase::class );
+               $mock->method( 'getModuleName' )->willReturn( 'testmodule' );
+               $mock->method( 'needsToken' )->willReturn( 'csrf' );
+               $mock->method( 'mustBePosted' )->willReturn( false );
+
+               $api = new ApiMain( new FauxRequest( [ 'action' => 'testmodule' ] ) );
+               $api->getModuleManager()->addModule( 'testmodule', 'action', get_class( $mock ),
+                       function () use ( $mock ) {
+                               return $mock;
+                       }
+               );
+               $api->execute();
+       }
+
+       public function testCheckMaxLagFailed() {
+               // It's hard to mock the LoadBalancer properly, so instead we'll mock
+               // checkMaxLag (which is tested directly in other tests below).
+               $req = new FauxRequest( [
+                       'action' => 'query',
+                       'meta' => 'siteinfo',
+               ] );
+
+               $mock = $this->getMockBuilder( ApiMain::class )
+                       ->setConstructorArgs( [ $req ] )
+                       ->setMethods( [ 'checkMaxLag' ] )
+                       ->getMock();
+               $mock->method( 'checkMaxLag' )->willReturn( false );
+
+               $mock->execute();
+
+               $this->assertArrayNotHasKey( 'query', $mock->getResult()->getResultData() );
+       }
+
+       public function testCheckConditionalRequestHeadersFailed() {
+               // The detailed checking of all cases of checkConditionalRequestHeaders
+               // is below in testCheckConditionalRequestHeaders(), which calls the
+               // method directly.  Here we just check that it will stop execution if
+               // it does fail.
+               $now = time();
+
+               $this->setMwGlobals( 'wgCacheEpoch', '20030516000000' );
+
+               $mock = $this->createMock( ApiBase::class );
+               $mock->method( 'getModuleName' )->willReturn( 'testmodule' );
+               $mock->method( 'getConditionalRequestData' )
+                       ->willReturn( wfTimestamp( TS_MW, $now - 3600 ) );
+               $mock->expects( $this->exactly( 0 ) )->method( 'execute' );
+
+               $req = new FauxRequest( [
+                       'action' => 'testmodule',
+               ] );
+               $req->setHeader( 'If-Modified-Since', wfTimestamp( TS_RFC2822, $now - 3600 ) );
+               $req->setRequestURL( "http://localhost" );
+
+               $api = new ApiMain( $req );
+               $api->getModuleManager()->addModule( 'testmodule', 'action', get_class( $mock ),
+                       function () use ( $mock ) {
+                               return $mock;
+                       }
+               );
+
+               $wrapper = TestingAccessWrapper::newFromObject( $api );
+               $wrapper->mInternalMode = false;
+
+               ob_start();
+               $api->execute();
+               ob_end_clean();
+       }
+
+       private function doTestCheckMaxLag( $lag ) {
+               $mockLB = $this->getMockBuilder( LoadBalancer::class )
+                       ->disableOriginalConstructor()
+                       ->setMethods( [ 'getMaxLag', '__destruct' ] )
+                       ->getMock();
+               $mockLB->method( 'getMaxLag' )->willReturn( [ 'somehost', $lag ] );
+               $this->setService( 'DBLoadBalancer', $mockLB );
+
+               $req = new FauxRequest();
+
+               $api = new ApiMain( $req );
+               $wrapper = TestingAccessWrapper::newFromObject( $api );
+
+               $mockModule = $this->createMock( ApiBase::class );
+               $mockModule->method( 'shouldCheckMaxLag' )->willReturn( true );
+
+               try {
+                       $wrapper->checkMaxLag( $mockModule, [ 'maxlag' => 3 ] );
+               } finally {
+                       if ( $lag > 3 ) {
+                               $this->assertSame( '5', $req->response()->getHeader( 'Retry-After' ) );
+                               $this->assertSame( (string)$lag, $req->response()->getHeader( 'X-Database-Lag' ) );
+                       }
+               }
+       }
+
+       public function testCheckMaxLagOkay() {
+               $this->doTestCheckMaxLag( 3 );
+
+               // No exception, we're happy
+               $this->assertTrue( true );
+       }
+
+       public function testCheckMaxLagExceeded() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'Waiting for a database server: 4 seconds lagged.' );
+
+               $this->setMwGlobals( 'wgShowHostnames', false );
+
+               $this->doTestCheckMaxLag( 4 );
+       }
+
+       public function testCheckMaxLagExceededWithHostNames() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'Waiting for somehost: 4 seconds lagged.' );
+
+               $this->setMwGlobals( 'wgShowHostnames', true );
+
+               $this->doTestCheckMaxLag( 4 );
+       }
+
        public static function provideAssert() {
                return [
                        [ false, [], 'user', 'assertuserfailed' ],
@@ -37,7 +387,6 @@ class ApiMainTest extends ApiTestCase {
        /**
         * Tests the assert={user|bot} functionality
         *
-        * @covers ApiMain::checkAsserts
         * @dataProvider provideAssert
         * @param bool $registered
         * @param array $rights
@@ -66,8 +415,6 @@ class ApiMainTest extends ApiTestCase {
 
        /**
         * Tests the assertuser= functionality
-        *
-        * @covers ApiMain::checkAsserts
         */
        public function testAssertUser() {
                $user = $this->getTestUser()->getUser();
@@ -107,17 +454,21 @@ class ApiMainTest extends ApiTestCase {
        /**
         * Test HTTP precondition headers
         *
-        * @covers ApiMain::checkConditionalRequestHeaders
         * @dataProvider provideCheckConditionalRequestHeaders
         * @param array $headers HTTP headers
         * @param array $conditions Return data for ApiBase::getConditionalRequestData
         * @param int $status Expected response status
-        * @param bool $post Request is a POST
+        * @param array $options Array of options:
+        *   post => true Request is a POST
+        *   cdn => true CDN is enabled ($wgUseSquid)
         */
        public function testCheckConditionalRequestHeaders(
-               $headers, $conditions, $status, $post = false
+               $headers, $conditions, $status, $options = []
        ) {
-               $request = new FauxRequest( [ 'action' => 'query', 'meta' => 'siteinfo' ], $post );
+               $request = new FauxRequest(
+                       [ 'action' => 'query', 'meta' => 'siteinfo' ],
+                       !empty( $options['post'] )
+               );
                $request->setHeaders( $headers );
                $request->response()->statusHeader( 200 ); // Why doesn't it default?
 
@@ -126,6 +477,13 @@ class ApiMainTest extends ApiTestCase {
                $priv = TestingAccessWrapper::newFromObject( $api );
                $priv->mInternalMode = false;
 
+               if ( !empty( $options['cdn'] ) ) {
+                       $this->setMwGlobals( 'wgUseSquid', true );
+               }
+
+               // Can't do this in TestSetup.php because Setup.php will override it
+               $this->setMwGlobals( 'wgCacheEpoch', '20030516000000' );
+
                $module = $this->getMockBuilder( ApiBase::class )
                        ->setConstructorArgs( [ $api, 'mock' ] )
                        ->setMethods( [ 'getConditionalRequestData' ] )
@@ -143,65 +501,99 @@ class ApiMainTest extends ApiTestCase {
        }
 
        public static function provideCheckConditionalRequestHeaders() {
+               global $wgSquidMaxage;
                $now = time();
 
                return [
                        // Non-existing from module is ignored
-                       [ [ 'If-None-Match' => '"foo", "bar"' ], [], 200 ],
-                       [ [ 'If-Modified-Since' => 'Tue, 18 Aug 2015 00:00:00 GMT' ], [], 200 ],
+                       'If-None-Match' => [ [ 'If-None-Match' => '"foo", "bar"' ], [], 200 ],
+                       'If-Modified-Since' =>
+                               [ [ 'If-Modified-Since' => 'Tue, 18 Aug 2015 00:00:00 GMT' ], [], 200 ],
 
                        // No headers
-                       [
-                               [],
-                               [
-                                       'etag' => '""',
-                                       'last-modified' => '20150815000000',
-                               ],
-                               200
-                       ],
+                       'No headers' => [ [], [ 'etag' => '""', 'last-modified' => '20150815000000', ], 200 ],
 
                        // Basic If-None-Match
-                       [ [ 'If-None-Match' => '"foo", "bar"' ], [ 'etag' => '"bar"' ], 304 ],
-                       [ [ 'If-None-Match' => '"foo", "bar"' ], [ 'etag' => '"baz"' ], 200 ],
-                       [ [ 'If-None-Match' => '"foo"' ], [ 'etag' => 'W/"foo"' ], 304 ],
-                       [ [ 'If-None-Match' => 'W/"foo"' ], [ 'etag' => '"foo"' ], 304 ],
-                       [ [ 'If-None-Match' => 'W/"foo"' ], [ 'etag' => 'W/"foo"' ], 304 ],
+                       'If-None-Match with matching etag' =>
+                               [ [ 'If-None-Match' => '"foo", "bar"' ], [ 'etag' => '"bar"' ], 304 ],
+                       'If-None-Match with non-matching etag' =>
+                               [ [ 'If-None-Match' => '"foo", "bar"' ], [ 'etag' => '"baz"' ], 200 ],
+                       'Strong If-None-Match with weak matching etag' =>
+                               [ [ 'If-None-Match' => '"foo"' ], [ 'etag' => 'W/"foo"' ], 304 ],
+                       'Weak If-None-Match with strong matching etag' =>
+                               [ [ 'If-None-Match' => 'W/"foo"' ], [ 'etag' => '"foo"' ], 304 ],
+                       'Weak If-None-Match with weak matching etag' =>
+                               [ [ 'If-None-Match' => 'W/"foo"' ], [ 'etag' => 'W/"foo"' ], 304 ],
 
-                       // Pointless, but supported
-                       [ [ 'If-None-Match' => '*' ], [], 304 ],
+                       // Pointless for GET, but supported
+                       'If-None-Match: *' => [ [ 'If-None-Match' => '*' ], [], 304 ],
 
                        // Basic If-Modified-Since
-                       [ [ 'If-Modified-Since' => wfTimestamp( TS_RFC2822, $now ) ],
-                               [ 'last-modified' => wfTimestamp( TS_MW, $now - 1 ) ], 304 ],
-                       [ [ 'If-Modified-Since' => wfTimestamp( TS_RFC2822, $now ) ],
-                               [ 'last-modified' => wfTimestamp( TS_MW, $now ) ], 304 ],
-                       [ [ 'If-Modified-Since' => wfTimestamp( TS_RFC2822, $now ) ],
-                               [ 'last-modified' => wfTimestamp( TS_MW, $now + 1 ) ], 200 ],
+                       'If-Modified-Since, modified one second earlier' =>
+                               [ [ 'If-Modified-Since' => wfTimestamp( TS_RFC2822, $now ) ],
+                                       [ 'last-modified' => wfTimestamp( TS_MW, $now - 1 ) ], 304 ],
+                       'If-Modified-Since, modified now' =>
+                               [ [ 'If-Modified-Since' => wfTimestamp( TS_RFC2822, $now ) ],
+                                       [ 'last-modified' => wfTimestamp( TS_MW, $now ) ], 304 ],
+                       'If-Modified-Since, modified one second later' =>
+                               [ [ 'If-Modified-Since' => wfTimestamp( TS_RFC2822, $now ) ],
+                                       [ 'last-modified' => wfTimestamp( TS_MW, $now + 1 ) ], 200 ],
 
                        // If-Modified-Since ignored when If-None-Match is given too
-                       [ [ 'If-None-Match' => '""', 'If-Modified-Since' => wfTimestamp( TS_RFC2822, $now ) ],
-                               [ 'etag' => '"x"', 'last-modified' => wfTimestamp( TS_MW, $now - 1 ) ], 200 ],
-                       [ [ 'If-None-Match' => '""', 'If-Modified-Since' => wfTimestamp( TS_RFC2822, $now ) ],
-                               [ 'last-modified' => wfTimestamp( TS_MW, $now - 1 ) ], 304 ],
+                       'Non-matching If-None-Match and matching If-Modified-Since' =>
+                               [ [ 'If-None-Match' => '""',
+                                       'If-Modified-Since' => wfTimestamp( TS_RFC2822, $now ) ],
+                                       [ 'etag' => '"x"', 'last-modified' => wfTimestamp( TS_MW, $now - 1 ) ], 200 ],
+                       'Non-matching If-None-Match and matching If-Modified-Since with no ETag' =>
+                               [
+                                       [
+                                               'If-None-Match' => '""',
+                                               'If-Modified-Since' => wfTimestamp( TS_RFC2822, $now )
+                                       ],
+                                       [ 'last-modified' => wfTimestamp( TS_MW, $now - 1 ) ],
+                                       304
+                               ],
 
                        // Ignored for POST
-                       [ [ 'If-None-Match' => '"foo", "bar"' ], [ 'etag' => '"bar"' ], 200, true ],
-                       [ [ 'If-Modified-Since' => wfTimestamp( TS_RFC2822, $now ) ],
-                               [ 'last-modified' => wfTimestamp( TS_MW, $now - 1 ) ], 200, true ],
+                       'Matching If-None-Match with POST' =>
+                               [ [ 'If-None-Match' => '"foo", "bar"' ], [ 'etag' => '"bar"' ], 200,
+                                       [ 'post' => true ] ],
+                       'Matching If-Modified-Since with POST' =>
+                               [ [ 'If-Modified-Since' => wfTimestamp( TS_RFC2822, $now ) ],
+                                       [ 'last-modified' => wfTimestamp( TS_MW, $now - 1 ) ], 200,
+                                       [ 'post' => true ] ],
 
                        // Other date formats allowed by the RFC
-                       [ [ 'If-Modified-Since' => gmdate( 'l, d-M-y H:i:s', $now ) . ' GMT' ],
-                               [ 'last-modified' => wfTimestamp( TS_MW, $now - 1 ) ], 304 ],
-                       [ [ 'If-Modified-Since' => gmdate( 'D M j H:i:s Y', $now ) ],
-                               [ 'last-modified' => wfTimestamp( TS_MW, $now - 1 ) ], 304 ],
+                       'If-Modified-Since with alternate date format 1' =>
+                               [ [ 'If-Modified-Since' => gmdate( 'l, d-M-y H:i:s', $now ) . ' GMT' ],
+                                       [ 'last-modified' => wfTimestamp( TS_MW, $now - 1 ) ], 304 ],
+                       'If-Modified-Since with alternate date format 2' =>
+                               [ [ 'If-Modified-Since' => gmdate( 'D M j H:i:s Y', $now ) ],
+                                       [ 'last-modified' => wfTimestamp( TS_MW, $now - 1 ) ], 304 ],
 
                        // Old browser extension to HTTP/1.0
-                       [ [ 'If-Modified-Since' => wfTimestamp( TS_RFC2822, $now ) . '; length=123' ],
-                               [ 'last-modified' => wfTimestamp( TS_MW, $now - 1 ) ], 304 ],
+                       'If-Modified-Since with length' =>
+                               [ [ 'If-Modified-Since' => wfTimestamp( TS_RFC2822, $now ) . '; length=123' ],
+                                       [ 'last-modified' => wfTimestamp( TS_MW, $now - 1 ) ], 304 ],
 
                        // Invalid date formats should be ignored
-                       [ [ 'If-Modified-Since' => gmdate( 'Y-m-d H:i:s', $now ) . ' GMT' ],
-                               [ 'last-modified' => wfTimestamp( TS_MW, $now - 1 ) ], 200 ],
+                       'If-Modified-Since with invalid date format' =>
+                               [ [ 'If-Modified-Since' => gmdate( 'Y-m-d H:i:s', $now ) . ' GMT' ],
+                                       [ 'last-modified' => wfTimestamp( TS_MW, $now - 1 ) ], 200 ],
+                       'If-Modified-Since with entirely unparseable date' =>
+                               [ [ 'If-Modified-Since' => 'a potato' ],
+                                       [ 'last-modified' => wfTimestamp( TS_MW, $now - 1 ) ], 200 ],
+
+                       // Anything before $wgSquidMaxage seconds ago should be considered
+                       // expired.
+                       'If-Modified-Since with CDN post-expiry' =>
+                               [ [ 'If-Modified-Since' => wfTimestamp( TS_RFC2822, $now - $wgSquidMaxage * 2 ) ],
+                                       [ 'last-modified' => wfTimestamp( TS_MW, $now - $wgSquidMaxage * 3 ) ],
+                                       200, [ 'cdn' => true ] ],
+                       'If-Modified-Since with CDN pre-expiry' =>
+                               [ [ 'If-Modified-Since' => wfTimestamp( TS_RFC2822, $now - $wgSquidMaxage / 2 ) ],
+                                       [ 'last-modified' => wfTimestamp( TS_MW, $now - $wgSquidMaxage * 3 ) ],
+                                       304, [ 'cdn' => true ] ],
                ];
        }
 
@@ -277,9 +669,101 @@ class ApiMainTest extends ApiTestCase {
                ];
        }
 
-       /**
-        * @covers ApiMain::lacksSameOriginSecurity
-        */
+       public function testCheckExecutePermissionsReadProhibited() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'You need read permission to use this module.' );
+
+               $this->setGroupPermissions( '*', 'read', false );
+
+               $main = new ApiMain( new FauxRequest( [ 'action' => 'query', 'meta' => 'siteinfo' ] ) );
+               $main->execute();
+       }
+
+       public function testCheckExecutePermissionWriteDisabled() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'Editing of this wiki through the API is disabled. Make sure the ' .
+                       '"$wgEnableWriteAPI=true;" statement is included in the wiki\'s ' .
+                       '"LocalSettings.php" file.' );
+               $main = new ApiMain( new FauxRequest( [
+                       'action' => 'edit',
+                       'title' => 'Some page',
+                       'text' => 'Some text',
+                       'token' => '+\\',
+               ] ) );
+               $main->execute();
+       }
+
+       public function testCheckExecutePermissionWriteApiProhibited() {
+               $this->setExpectedException( ApiUsageException::class,
+                       "You're not allowed to edit this wiki through the API." );
+               $this->setGroupPermissions( '*', 'writeapi', false );
+
+               $main = new ApiMain( new FauxRequest( [
+                       'action' => 'edit',
+                       'title' => 'Some page',
+                       'text' => 'Some text',
+                       'token' => '+\\',
+               ] ), /* enableWrite = */ true );
+               $main->execute();
+       }
+
+       public function testCheckExecutePermissionPromiseNonWrite() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'The "Promise-Non-Write-API-Action" HTTP header cannot be sent ' .
+                       'to write-mode API modules.' );
+
+               $req = new FauxRequest( [
+                       'action' => 'edit',
+                       'title' => 'Some page',
+                       'text' => 'Some text',
+                       'token' => '+\\',
+               ] );
+               $req->setHeaders( [ 'Promise-Non-Write-API-Action' => '1' ] );
+               $main = new ApiMain( $req, /* enableWrite = */ true );
+               $main->execute();
+       }
+
+       public function testCheckExecutePermissionHookAbort() {
+               $this->setExpectedException( ApiUsageException::class, 'Main Page' );
+
+               $this->setTemporaryHook( 'ApiCheckCanExecute', function ( $unused1, $unused2, &$message ) {
+                       $message = 'mainpage';
+                       return false;
+               } );
+
+               $main = new ApiMain( new FauxRequest( [
+                       'action' => 'edit',
+                       'title' => 'Some page',
+                       'text' => 'Some text',
+                       'token' => '+\\',
+               ] ), /* enableWrite = */ true );
+               $main->execute();
+       }
+
+       public function testGetValUnsupportedArray() {
+               $main = new ApiMain( new FauxRequest( [
+                       'action' => 'query',
+                       'meta' => 'siteinfo',
+                       'siprop' => [ 'general', 'namespaces' ],
+               ] ) );
+               $this->assertSame( 'myDefault', $main->getVal( 'siprop', 'myDefault' ) );
+               $main->execute();
+               $this->assertSame( 'Parameter "siprop" uses unsupported PHP array syntax.',
+                       $main->getResult()->getResultData()['warnings']['main']['warnings'] );
+       }
+
+       public function testReportUnusedParams() {
+               $main = new ApiMain( new FauxRequest( [
+                       'action' => 'query',
+                       'meta' => 'siteinfo',
+                       'unusedparam' => 'unusedval',
+                       'anotherunusedparam' => 'anotherval',
+               ] ) );
+               $main->execute();
+               $this->assertSame( 'Unrecognized parameters: unusedparam, anotherunusedparam.',
+                       $main->getResult()->getResultData()['warnings']['main']['warnings'] );
+       }
+
        public function testLacksSameOriginSecurity() {
                // Basic test
                $main = new ApiMain( new FauxRequest( [ 'action' => 'query', 'meta' => 'siteinfo' ] ) );
@@ -309,7 +793,7 @@ class ApiMainTest extends ApiTestCase {
 
        /**
         * Test proper creation of the ApiErrorFormatter
-        * @covers ApiMain::__construct
+        *
         * @dataProvider provideApiErrorFormatterCreation
         * @param array $request Request parameters
         * @param array $expect Expected data
@@ -443,8 +927,6 @@ class ApiMainTest extends ApiTestCase {
        }
 
        /**
-        * @covers ApiMain::errorMessagesFromException
-        * @covers ApiMain::substituteResultWithError
         * @dataProvider provideExceptionErrors
         * @param Exception $exception
         * @param array $expectReturn
index d612b53..5118218 100644 (file)
@@ -105,9 +105,14 @@ class ChangesListSpecialPageTest extends AbstractChangesListSpecialPageTestCase
        }
 
        private static function normalizeCondition( $conds ) {
+               $dbr = wfGetDB( DB_REPLICA );
                $normalized = array_map(
-                       function ( $k, $v ) {
-                               return is_numeric( $k ) ? $v : "$k = $v";
+                       function ( $k, $v ) use ( $dbr ) {
+                               if ( is_array( $v ) ) {
+                                       sort( $v );
+                               }
+                               // (Ab)use makeList() to format only this entry
+                               return $dbr->makeList( [ $k => $v ], Database::LIST_AND );
                        },
                        array_keys( $conds ),
                        $conds
@@ -116,9 +121,9 @@ class ChangesListSpecialPageTest extends AbstractChangesListSpecialPageTestCase
                return $normalized;
        }
 
-       /** return false if condition begin with 'rc_timestamp ' */
+       /** return false if condition begins with 'rc_timestamp ' */
        private static function filterOutRcTimestampCondition( $var ) {
-               return ( false === strpos( $var, 'rc_timestamp ' ) );
+               return ( is_array( $var ) || false === strpos( $var, 'rc_timestamp ' ) );
        }
 
        public function testRcNsFilter() {
@@ -342,7 +347,7 @@ class ChangesListSpecialPageTest extends AbstractChangesListSpecialPageTestCase
                $user = $this->getTestSysop()->getUser();
                $this->assertConditions(
                        [ # expected
-                               "rc_patrolled = 0",
+                               'rc_patrolled = 0',
                        ],
                        [
                                'hidepatrolled' => 1,
@@ -356,7 +361,7 @@ class ChangesListSpecialPageTest extends AbstractChangesListSpecialPageTestCase
                $user = $this->getTestSysop()->getUser();
                $this->assertConditions(
                        [ # expected
-                               "rc_patrolled != 0",
+                               'rc_patrolled != 0',
                        ],
                        [
                                'hideunpatrolled' => 1,
index d0126f2..77d6e74 100644 (file)
@@ -26,6 +26,81 @@ class ApiStructureTest extends MediaWikiTestCase {
                ],
        ];
 
+       /**
+        * Values are an array, where each array value is a permitted type.  A type
+        * can be a string, which is the name of an internal type or a
+        * class/interface.  Or it can be an array, in which case the value must be
+        * an array whose elements are the types given in the array (e.g., [
+        * 'string', integer' ] means an array whose entries are strings and/or
+        * integers).
+        */
+       private static $paramTypes = [
+               // ApiBase::PARAM_DFLT => as appropriate for PARAM_TYPE
+               ApiBase::PARAM_ISMULTI => [ 'boolean' ],
+               ApiBase::PARAM_TYPE => [ 'string', [ 'string' ] ],
+               ApiBase::PARAM_MAX => [ 'integer' ],
+               ApiBase::PARAM_MAX2 => [ 'integer' ],
+               ApiBase::PARAM_MIN => [ 'integer' ],
+               ApiBase::PARAM_ALLOW_DUPLICATES => [ 'boolean' ],
+               ApiBase::PARAM_DEPRECATED => [ 'boolean' ],
+               ApiBase::PARAM_REQUIRED => [ 'boolean' ],
+               ApiBase::PARAM_RANGE_ENFORCE => [ 'boolean' ],
+               ApiBase::PARAM_HELP_MSG => [ 'string', 'array', Message::class ],
+               ApiBase::PARAM_HELP_MSG_APPEND => [ [ 'string', 'array', Message::class ] ],
+               ApiBase::PARAM_HELP_MSG_INFO => [ [ 'array' ] ],
+               ApiBase::PARAM_VALUE_LINKS => [ [ 'string' ] ],
+               ApiBase::PARAM_HELP_MSG_PER_VALUE => [ [ 'string', 'array', Message::class ] ],
+               ApiBase::PARAM_SUBMODULE_MAP => [ [ 'string' ] ],
+               ApiBase::PARAM_SUBMODULE_PARAM_PREFIX => [ 'string' ],
+               ApiBase::PARAM_ALL => [ 'boolean', 'string' ],
+               ApiBase::PARAM_EXTRA_NAMESPACES => [ [ 'integer' ] ],
+               ApiBase::PARAM_SENSITIVE => [ 'boolean' ],
+               ApiBase::PARAM_DEPRECATED_VALUES => [ 'array' ],
+               ApiBase::PARAM_ISMULTI_LIMIT1 => [ 'integer' ],
+               ApiBase::PARAM_ISMULTI_LIMIT2 => [ 'integer' ],
+               ApiBase::PARAM_MAX_BYTES => [ 'integer' ],
+               ApiBase::PARAM_MAX_CHARS => [ 'integer' ],
+       ];
+
+       // param => [ other param that must be present => required value or null ]
+       private static $paramRequirements = [
+               ApiBase::PARAM_ALLOW_DUPLICATES => [ ApiBase::PARAM_ISMULTI => true ],
+               ApiBase::PARAM_ALL => [ ApiBase::PARAM_ISMULTI => true ],
+               ApiBase::PARAM_ISMULTI_LIMIT1 => [
+                       ApiBase::PARAM_ISMULTI => true,
+                       ApiBase::PARAM_ISMULTI_LIMIT2 => null,
+               ],
+               ApiBase::PARAM_ISMULTI_LIMIT2 => [
+                       ApiBase::PARAM_ISMULTI => true,
+                       ApiBase::PARAM_ISMULTI_LIMIT1 => null,
+               ],
+       ];
+
+       // param => type(s) allowed for this param ('array' is any array)
+       private static $paramAllowedTypes = [
+               ApiBase::PARAM_MAX => [ 'integer', 'limit' ],
+               ApiBase::PARAM_MAX2 => 'limit',
+               ApiBase::PARAM_MIN => [ 'integer', 'limit' ],
+               ApiBase::PARAM_RANGE_ENFORCE => 'integer',
+               ApiBase::PARAM_VALUE_LINKS => 'array',
+               ApiBase::PARAM_HELP_MSG_PER_VALUE => 'array',
+               ApiBase::PARAM_SUBMODULE_MAP => 'submodule',
+               ApiBase::PARAM_SUBMODULE_PARAM_PREFIX => 'submodule',
+               ApiBase::PARAM_ALL => 'array',
+               ApiBase::PARAM_EXTRA_NAMESPACES => 'namespace',
+               ApiBase::PARAM_DEPRECATED_VALUES => 'array',
+               ApiBase::PARAM_MAX_BYTES => [ 'NULL', 'string', 'text', 'password' ],
+               ApiBase::PARAM_MAX_CHARS => [ 'NULL', 'string', 'text', 'password' ],
+       ];
+
+       private static $paramProhibitedTypes = [
+               ApiBase::PARAM_ISMULTI => [ 'boolean', 'limit', 'upload' ],
+               ApiBase::PARAM_ALL => 'namespace',
+               ApiBase::PARAM_SENSITIVE => 'password',
+       ];
+
+       private static $constantNames = null;
+
        /**
         * Initialize/fetch the ApiMain instance for testing
         * @return ApiMain
@@ -178,34 +253,327 @@ class ApiStructureTest extends MediaWikiTestCase {
                // avoid warnings about empty tests when no parameter needs to be checked
                $this->assertTrue( true );
 
+               if ( self::$constantNames === null ) {
+                       self::$constantNames = [];
+
+                       foreach ( ( new ReflectionClass( 'ApiBase' ) )->getConstants() as $key => $val ) {
+                               if ( substr( $key, 0, 6 ) === 'PARAM_' ) {
+                                       self::$constantNames[$val] = $key;
+                               }
+                       }
+               }
+
                foreach ( [ $paramsPlain, $paramsForHelp ] as $params ) {
                        foreach ( $params as $param => $config ) {
-                               if ( isset( $config[ApiBase::PARAM_ISMULTI_LIMIT1] )
-                                       || isset( $config[ApiBase::PARAM_ISMULTI_LIMIT2] )
+                               if ( !is_array( $config ) ) {
+                                       $config = [ ApiBase::PARAM_DFLT => $config ];
+                               }
+                               if ( !isset( $config[ApiBase::PARAM_TYPE] ) ) {
+                                       $config[ApiBase::PARAM_TYPE] = isset( $config[ApiBase::PARAM_DFLT] )
+                                               ? gettype( $config[ApiBase::PARAM_DFLT] )
+                                               : 'NULL';
+                               }
+
+                               foreach ( self::$paramTypes as $key => $types ) {
+                                       if ( !isset( $config[$key] ) ) {
+                                               continue;
+                                       }
+                                       $keyName = self::$constantNames[$key];
+                                       $this->validateType( $types, $config[$key], $param, $keyName );
+                               }
+
+                               foreach ( self::$paramRequirements as $key => $required ) {
+                                       if ( !isset( $config[$key] ) ) {
+                                               continue;
+                                       }
+                                       foreach ( $required as $requireKey => $requireVal ) {
+                                               $this->assertArrayHasKey( $requireKey, $config,
+                                                       "$param: When " . self::$constantNames[$key] . " is set, " .
+                                                       self::$constantNames[$requireKey] . " must also be set" );
+                                               if ( $requireVal !== null ) {
+                                                       $this->assertSame( $requireVal, $config[$requireKey],
+                                                               "$param: When " . self::$constantNames[$key] . " is set, " .
+                                                               self::$constantNames[$requireKey] . " must equal " .
+                                                               var_export( $requireVal, true ) );
+                                               }
+                                       }
+                               }
+
+                               foreach ( self::$paramAllowedTypes as $key => $allowedTypes ) {
+                                       if ( !isset( $config[$key] ) ) {
+                                               continue;
+                                       }
+
+                                       $actualType = is_array( $config[ApiBase::PARAM_TYPE] )
+                                               ? 'array' : $config[ApiBase::PARAM_TYPE];
+
+                                       $this->assertContains(
+                                               $actualType,
+                                               (array)$allowedTypes,
+                                               "$param: " . self::$constantNames[$key] .
+                                                       " can only be used with PARAM_TYPE " .
+                                                       implode( ', ', (array)$allowedTypes )
+                                       );
+                               }
+
+                               foreach ( self::$paramProhibitedTypes as $key => $prohibitedTypes ) {
+                                       if ( !isset( $config[$key] ) ) {
+                                               continue;
+                                       }
+
+                                       $actualType = is_array( $config[ApiBase::PARAM_TYPE] )
+                                               ? 'array' : $config[ApiBase::PARAM_TYPE];
+
+                                       $this->assertNotContains(
+                                               $actualType,
+                                               (array)$prohibitedTypes,
+                                               "$param: " . self::$constantNames[$key] .
+                                                       " cannot be used with PARAM_TYPE " .
+                                                       implode( ', ', (array)$prohibitedTypes )
+                                       );
+                               }
+
+                               if ( isset( $config[ApiBase::PARAM_DFLT] ) ) {
+                                       $this->assertFalse(
+                                               isset( $config[ApiBase::PARAM_REQUIRED] ) &&
+                                                       $config[ApiBase::PARAM_REQUIRED],
+                                               "$param: A required parameter cannot have a default" );
+
+                                       $this->validateDefault( $param, $config );
+                               }
+
+                               if ( $config[ApiBase::PARAM_TYPE] === 'limit' ) {
+                                       $this->assertTrue(
+                                               isset( $config[ApiBase::PARAM_MAX] ) &&
+                                                       isset( $config[ApiBase::PARAM_MAX2] ),
+                                               "$param: PARAM_MAX and PARAM_MAX2 are required for limits"
+                                       );
+                                       $this->assertGreaterThanOrEqual(
+                                               $config[ApiBase::PARAM_MAX],
+                                               $config[ApiBase::PARAM_MAX2],
+                                               "$param: PARAM_MAX cannot be greater than PARAM_MAX2"
+                                       );
+                               }
+
+                               if (
+                                       isset( $config[ApiBase::PARAM_MIN] ) &&
+                                       isset( $config[ApiBase::PARAM_MAX] )
                                ) {
-                                       $this->assertTrue( !empty( $config[ApiBase::PARAM_ISMULTI] ), $param
-                                               . ': PARAM_ISMULTI_LIMIT* only makes sense when PARAM_ISMULTI is true' );
-                                       $this->assertTrue( isset( $config[ApiBase::PARAM_ISMULTI_LIMIT1] )
-                                               && isset( $config[ApiBase::PARAM_ISMULTI_LIMIT2] ), $param
-                                               . ': PARAM_ISMULTI_LIMIT1 and PARAM_ISMULTI_LIMIT2 must be used together' );
-                                       $this->assertType( 'int', $config[ApiBase::PARAM_ISMULTI_LIMIT1], $param
-                                               . 'PARAM_ISMULTI_LIMIT1 must be an integer' );
-                                       $this->assertType( 'int', $config[ApiBase::PARAM_ISMULTI_LIMIT2], $param
-                                               . 'PARAM_ISMULTI_LIMIT2 must be an integer' );
-                                       $this->assertGreaterThanOrEqual( $config[ApiBase::PARAM_ISMULTI_LIMIT1],
-                                               $config[ApiBase::PARAM_ISMULTI_LIMIT2], $param
-                                               . 'PARAM_ISMULTI limit cannot be smaller for users with apihighlimits rights' );
-                               }
-                               if ( isset( $config[ApiBase::PARAM_MAX_BYTES] )
-                                       || isset( $config[ApiBase::PARAM_MAX_CHARS] )
+                                       $this->assertGreaterThanOrEqual(
+                                               $config[ApiBase::PARAM_MIN],
+                                               $config[ApiBase::PARAM_MAX],
+                                               "$param: PARAM_MIN cannot be greater than PARAM_MAX"
+                                       );
+                               }
+
+                               if ( isset( $config[ApiBase::PARAM_RANGE_ENFORCE] ) ) {
+                                       $this->assertTrue(
+                                               isset( $config[ApiBase::PARAM_MIN] ) ||
+                                                       isset( $config[ApiBase::PARAM_MAX] ),
+                                               "$param: PARAM_RANGE_ENFORCE can only be set together with " .
+                                                       "PARAM_MIN or PARAM_MAX"
+                                       );
+                               }
+
+                               if ( isset( $config[ApiBase::PARAM_DEPRECATED_VALUES] ) ) {
+                                       foreach ( $config[ApiBase::PARAM_DEPRECATED_VALUES] as $key => $unused ) {
+                                               $this->assertContains( $key, $config[ApiBase::PARAM_TYPE],
+                                                       "$param: Deprecated value \"$key\" is not allowed, " .
+                                                       "how can it be deprecated?" );
+                                       }
+                               }
+
+                               if (
+                                       isset( $config[ApiBase::PARAM_ISMULTI_LIMIT1] ) ||
+                                       isset( $config[ApiBase::PARAM_ISMULTI_LIMIT2] )
+                               ) {
+                                       $this->assertGreaterThanOrEqual( 0, $config[ApiBase::PARAM_ISMULTI_LIMIT1],
+                                               "$param: PARAM_ISMULTI_LIMIT1 cannot be negative" );
+                                       // Zero for both doesn't make sense, but you could have
+                                       // zero for non-bots
+                                       $this->assertGreaterThanOrEqual( 1, $config[ApiBase::PARAM_ISMULTI_LIMIT2],
+                                               "$param: PARAM_ISMULTI_LIMIT2 cannot be negative or zero" );
+                                       $this->assertGreaterThanOrEqual(
+                                               $config[ApiBase::PARAM_ISMULTI_LIMIT1],
+                                               $config[ApiBase::PARAM_ISMULTI_LIMIT2],
+                                               "$param: PARAM_ISMULTI limit cannot be smaller for users with " .
+                                                       "apihighlimits rights" );
+                               }
+
+                               if ( isset( $config[ApiBase::PARAM_MAX_BYTES] ) ) {
+                                       $this->assertGreaterThanOrEqual( 1, $config[ApiBase::PARAM_MAX_BYTES],
+                                               "$param: PARAM_MAX_BYTES cannot be negative or zero" );
+                               }
+
+                               if ( isset( $config[ApiBase::PARAM_MAX_CHARS] ) ) {
+                                       $this->assertGreaterThanOrEqual( 1, $config[ApiBase::PARAM_MAX_CHARS],
+                                               "$param: PARAM_MAX_CHARS cannot be negative or zero" );
+                               }
+
+                               if (
+                                       isset( $config[ApiBase::PARAM_MAX_BYTES] ) &&
+                                       isset( $config[ApiBase::PARAM_MAX_CHARS] )
                                ) {
-                                       $default = isset( $config[ApiBase::PARAM_DFLT] ) ? $config[ApiBase::PARAM_DFLT] : null;
-                                       $type = isset( $config[ApiBase::PARAM_TYPE] ) ? $config[ApiBase::PARAM_TYPE]
-                                               : gettype( $default );
-                                       $this->assertContains( $type, [ 'NULL', 'string', 'text', 'password' ],
-                                               'PARAM_MAX_BYTES/CHARS is only supported for string-like types' );
+                                       // Length of a string in chars is always <= length in bytes,
+                                       // so PARAM_MAX_CHARS is pointless if > PARAM_MAX_BYTES
+                                       $this->assertGreaterThanOrEqual(
+                                               $config[ApiBase::PARAM_MAX_CHARS],
+                                               $config[ApiBase::PARAM_MAX_BYTES],
+                                               "$param: PARAM_MAX_BYTES cannot be less than PARAM_MAX_CHARS"
+                                       );
+                               }
+                       }
+               }
+       }
+
+       /**
+        * Throws if $value does not match one of the types specified in $types.
+        *
+        * @param array $types From self::$paramTypes array
+        * @param mixed $value Value to check
+        * @param string $param Name of param we're checking, for error messages
+        * @param string $desc Description for error messages
+        */
+       private function validateType( $types, $value, $param, $desc ) {
+               if ( count( $types ) === 1 ) {
+                       // Only one type allowed
+                       if ( is_string( $types[0] ) ) {
+                               $this->assertType( $types[0], $value, "$param: $desc type" );
+                       } else {
+                               // Array whose values have specified types, recurse
+                               $this->assertInternalType( 'array', $value, "$param: $desc type" );
+                               foreach ( $value as $subvalue ) {
+                                       $this->validateType( $types[0], $subvalue, $param, "$desc value" );
+                               }
+                       }
+               } else {
+                       // Multiple options
+                       foreach ( $types as $type ) {
+                               if ( is_string( $type ) ) {
+                                       if ( class_exists( $type ) || interface_exists( $type ) ) {
+                                               if ( $value instanceof $type ) {
+                                                       return;
+                                               }
+                                       } else {
+                                               if ( gettype( $value ) === $type ) {
+                                                       return;
+                                               }
+                                       }
+                               } else {
+                                       // Array whose values have specified types, recurse
+                                       try {
+                                               $this->validateType( [ $type ], $value, $param, "$desc type" );
+                                               // Didn't throw, so we're good
+                                               return;
+                                       } catch ( Exception $unused ) {
+                                       }
                                }
                        }
+                       // Doesn't match any of them
+                       $this->fail( "$param: $desc has incorrect type" );
+               }
+       }
+
+       /**
+        * Asserts that $default is a valid default for $type.
+        *
+        * @param string $param Name of param, for error messages
+        * @param array $config Array of configuration options for this parameter
+        */
+       private function validateDefault( $param, $config ) {
+               $type = $config[ApiBase::PARAM_TYPE];
+               $default = $config[ApiBase::PARAM_DFLT];
+
+               if ( !empty( $config[ApiBase::PARAM_ISMULTI] ) ) {
+                       if ( $default === '' ) {
+                               // The empty array is fine
+                               return;
+                       }
+                       $defaults = explode( '|', $default );
+                       $config[ApiBase::PARAM_ISMULTI] = false;
+                       foreach ( $defaults as $defaultValue ) {
+                               // Only allow integers in their simplest form with no leading
+                               // or trailing characters etc.
+                               if ( $type === 'integer' && $defaultValue === (string)(int)$defaultValue ) {
+                                       $defaultValue = (int)$defaultValue;
+                               }
+                               $config[ApiBase::PARAM_DFLT] = $defaultValue;
+                               $this->validateDefault( $param, $config );
+                       }
+                       return;
+               }
+               switch ( $type ) {
+                       case 'boolean':
+                               $this->assertFalse( $default,
+                                       "$param: Boolean params may only default to false" );
+                               break;
+
+                       case 'integer':
+                               $this->assertInternalType( 'integer', $default,
+                                       "$param: Default $default is not an integer" );
+                               break;
+
+                       case 'limit':
+                               if ( $default === 'max' ) {
+                                       break;
+                               }
+                               $this->assertInternalType( 'integer', $default,
+                                       "$param: Default $default is neither an integer nor \"max\"" );
+                               break;
+
+                       case 'namespace':
+                               $validValues = MWNamespace::getValidNamespaces();
+                               if (
+                                       isset( $config[ApiBase::PARAM_EXTRA_NAMESPACES] ) &&
+                                       is_array( $config[ApiBase::PARAM_EXTRA_NAMESPACES] )
+                               ) {
+                                       $validValues = array_merge(
+                                               $validValues,
+                                               $config[ApiBase::PARAM_EXTRA_NAMESPACES]
+                                       );
+                               }
+                               $this->assertContains( $default, $validValues,
+                                       "$param: Default $default is not a valid namespace" );
+                               break;
+
+                       case 'NULL':
+                       case 'password':
+                       case 'string':
+                       case 'submodule':
+                       case 'tags':
+                       case 'text':
+                               $this->assertInternalType( 'string', $default,
+                                       "$param: Default $default is not a string" );
+                               break;
+
+                       case 'timestamp':
+                               if ( $default === 'now' ) {
+                                       return;
+                               }
+                               $this->assertNotFalse( wfTimestamp( TS_MW, $default ),
+                                       "$param: Default $default is not a valid timestamp" );
+                               break;
+
+                       case 'user':
+                               // @todo Should we make user validation a public static method
+                               // in ApiBase() or something so we don't have to resort to
+                               // this?  Or in User for that matter.
+                               $wrapper = TestingAccessWrapper::newFromObject( new ApiMain() );
+                               try {
+                                       $wrapper->validateUser( $default, '' );
+                               } catch ( ApiUsageException $e ) {
+                                       $this->fail( "$param: Default $default is not a valid username/IP address" );
+                               }
+                               break;
+
+                       default:
+                               if ( is_array( $type ) ) {
+                                       $this->assertContains( $default, $type,
+                                               "$param: Default $default is not any of " .
+                                               implode( ', ', $type ) );
+                               } else {
+                                       $this->fail( "Unrecognized type $type" );
+                               }
                }
        }
 
index 2dbf271..b15d407 100644 (file)
@@ -21,6 +21,11 @@ Set up MediaWiki-Vagrant:
 
     npm run selenium
 
+By default, Chrome will run in headless mode. If you want to see Chrome, set DISPLAY
+environment variable to any value:
+
+    DISPLAY=:1 npm run selenium
+
 To run only one file (for example page.js), you first need to spawn the chromedriver:
 
     chromedriver --url-base=wd/hub --port=4444
index 5565bc3..7b74328 100644 (file)
@@ -80,9 +80,10 @@ exports.config = {
                maxInstances: 1,
                //
                browserName: 'chrome',
-               // Since Chrome v57 https://bugs.chromium.org/p/chromedriver/issues/detail?id=1625
                chromeOptions: {
-                       args: [ '--enable-automation' ]
+                       // Run headless when there is no DISPLAY
+                       // --headless: since Chrome 59 https://chromium.googlesource.com/chromium/src/+/59.0.3030.0/headless/README.md
+                       args: process.env.DISPLAY ? [] : [ '--headless' ]
                }
        } ],
        //