2006-10-01 04:38:31 +00:00
|
|
|
<?php
|
2010-02-23 18:05:46 +00:00
|
|
|
/**
|
2012-07-15 20:13:02 +00:00
|
|
|
* Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
|
2006-10-01 04:38:31 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2010-06-21 13:13:32 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2006-10-01 04:38:31 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-07 19:59:42 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @defgroup API API
|
2006-10-01 04:38:31 +00:00
|
|
|
*/
|
|
|
|
|
|
2016-08-16 20:53:57 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2017-03-17 10:57:37 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2016-10-02 04:51:51 +00:00
|
|
|
use Wikimedia\Timestamp\TimestampException;
|
2016-08-16 20:53:57 +00:00
|
|
|
|
2006-10-14 07:18:08 +00:00
|
|
|
/**
|
2007-05-19 20:26:08 +00:00
|
|
|
* This is the main API class, used for both external and internal processing.
|
|
|
|
|
* When executed, it will create the requested formatter object,
|
|
|
|
|
* instantiate and execute an object associated with the needed action,
|
|
|
|
|
* and use formatter to print results.
|
|
|
|
|
* In case of an exception, an error message will be printed using the same formatter.
|
|
|
|
|
*
|
|
|
|
|
* To use API from another application, run it using FauxRequest object, in which
|
|
|
|
|
* case any internal exceptions will not be handled but passed up to the caller.
|
2008-04-14 07:45:50 +00:00
|
|
|
* After successful execution, use getResult() for the resulting data.
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup API
|
2006-10-14 07:18:08 +00:00
|
|
|
*/
|
2006-10-01 04:38:31 +00:00
|
|
|
class ApiMain extends ApiBase {
|
2006-10-14 07:18:08 +00:00
|
|
|
/**
|
|
|
|
|
* When no format parameter is given, this format will be used
|
|
|
|
|
*/
|
2014-09-16 20:45:09 +00:00
|
|
|
const API_DEFAULT_FORMAT = 'jsonfm';
|
2006-10-14 07:18:08 +00:00
|
|
|
|
2016-10-19 16:54:25 +00:00
|
|
|
/**
|
|
|
|
|
* When no uselang parameter is given, this language will be used
|
|
|
|
|
*/
|
|
|
|
|
const API_DEFAULT_USELANG = 'user';
|
|
|
|
|
|
2006-10-14 07:18:08 +00:00
|
|
|
/**
|
|
|
|
|
* List of available modules: action name => module class
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
private static $Modules = [
|
2018-01-13 00:02:09 +00:00
|
|
|
'login' => ApiLogin::class,
|
|
|
|
|
'clientlogin' => ApiClientLogin::class,
|
|
|
|
|
'logout' => ApiLogout::class,
|
|
|
|
|
'createaccount' => ApiAMCreateAccount::class,
|
|
|
|
|
'linkaccount' => ApiLinkAccount::class,
|
|
|
|
|
'unlinkaccount' => ApiRemoveAuthenticationData::class,
|
|
|
|
|
'changeauthenticationdata' => ApiChangeAuthenticationData::class,
|
|
|
|
|
'removeauthenticationdata' => ApiRemoveAuthenticationData::class,
|
|
|
|
|
'resetpassword' => ApiResetPassword::class,
|
|
|
|
|
'query' => ApiQuery::class,
|
|
|
|
|
'expandtemplates' => ApiExpandTemplates::class,
|
|
|
|
|
'parse' => ApiParse::class,
|
|
|
|
|
'stashedit' => ApiStashEdit::class,
|
|
|
|
|
'opensearch' => ApiOpenSearch::class,
|
|
|
|
|
'feedcontributions' => ApiFeedContributions::class,
|
|
|
|
|
'feedrecentchanges' => ApiFeedRecentChanges::class,
|
|
|
|
|
'feedwatchlist' => ApiFeedWatchlist::class,
|
|
|
|
|
'help' => ApiHelp::class,
|
|
|
|
|
'paraminfo' => ApiParamInfo::class,
|
|
|
|
|
'rsd' => ApiRsd::class,
|
|
|
|
|
'compare' => ApiComparePages::class,
|
|
|
|
|
'tokens' => ApiTokens::class,
|
|
|
|
|
'checktoken' => ApiCheckToken::class,
|
|
|
|
|
'cspreport' => ApiCSPReport::class,
|
|
|
|
|
'validatepassword' => ApiValidatePassword::class,
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2009-03-06 13:49:44 +00:00
|
|
|
// Write modules
|
2018-01-13 00:02:09 +00:00
|
|
|
'purge' => ApiPurge::class,
|
|
|
|
|
'setnotificationtimestamp' => ApiSetNotificationTimestamp::class,
|
|
|
|
|
'rollback' => ApiRollback::class,
|
|
|
|
|
'delete' => ApiDelete::class,
|
|
|
|
|
'undelete' => ApiUndelete::class,
|
|
|
|
|
'protect' => ApiProtect::class,
|
|
|
|
|
'block' => ApiBlock::class,
|
|
|
|
|
'unblock' => ApiUnblock::class,
|
|
|
|
|
'move' => ApiMove::class,
|
|
|
|
|
'edit' => ApiEditPage::class,
|
|
|
|
|
'upload' => ApiUpload::class,
|
|
|
|
|
'filerevert' => ApiFileRevert::class,
|
|
|
|
|
'emailuser' => ApiEmailUser::class,
|
|
|
|
|
'watch' => ApiWatch::class,
|
|
|
|
|
'patrol' => ApiPatrol::class,
|
|
|
|
|
'import' => ApiImport::class,
|
|
|
|
|
'clearhasmsg' => ApiClearHasMsg::class,
|
|
|
|
|
'userrights' => ApiUserrights::class,
|
|
|
|
|
'options' => ApiOptions::class,
|
|
|
|
|
'imagerotate' => ApiImageRotate::class,
|
|
|
|
|
'revisiondelete' => ApiRevisionDelete::class,
|
|
|
|
|
'managetags' => ApiManageTags::class,
|
|
|
|
|
'tag' => ApiTag::class,
|
|
|
|
|
'mergehistory' => ApiMergeHistory::class,
|
|
|
|
|
'setpagelanguage' => ApiSetPageLanguage::class,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2006-10-14 07:18:08 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* List of available formats: format name => format class
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
private static $Formats = [
|
2018-01-13 00:02:09 +00:00
|
|
|
'json' => ApiFormatJson::class,
|
|
|
|
|
'jsonfm' => ApiFormatJson::class,
|
|
|
|
|
'php' => ApiFormatPhp::class,
|
|
|
|
|
'phpfm' => ApiFormatPhp::class,
|
|
|
|
|
'xml' => ApiFormatXml::class,
|
|
|
|
|
'xmlfm' => ApiFormatXml::class,
|
|
|
|
|
'rawfm' => ApiFormatJson::class,
|
|
|
|
|
'none' => ApiFormatNone::class,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2010-01-22 03:14:52 +00:00
|
|
|
|
2008-08-04 14:26:20 +00:00
|
|
|
/**
|
|
|
|
|
* List of user roles that are specifically relevant to the API.
|
2016-07-25 01:25:09 +00:00
|
|
|
* [ 'right' => [ 'msg' => 'Some message with a $1',
|
|
|
|
|
* 'params' => [ $someVarToSubst ] ],
|
|
|
|
|
* ];
|
2008-08-04 14:26:20 +00:00
|
|
|
*/
|
2016-02-20 20:16:20 +00:00
|
|
|
private static $mRights = [
|
|
|
|
|
'writeapi' => [
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
'msg' => 'right-writeapi',
|
2016-02-20 20:16:20 +00:00
|
|
|
'params' => []
|
|
|
|
|
],
|
|
|
|
|
'apihighlimits' => [
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
'msg' => 'api-help-right-apihighlimits',
|
2016-02-20 20:16:20 +00:00
|
|
|
'params' => [ ApiBase::LIMIT_SML2, ApiBase::LIMIT_BIG2 ]
|
|
|
|
|
]
|
|
|
|
|
];
|
2008-08-04 14:26:20 +00:00
|
|
|
|
2010-12-23 19:53:28 +00:00
|
|
|
/**
|
|
|
|
|
* @var ApiFormatBase
|
|
|
|
|
*/
|
|
|
|
|
private $mPrinter;
|
|
|
|
|
|
2016-10-19 16:54:25 +00:00
|
|
|
private $mModuleMgr, $mResult, $mErrorFormatter = null;
|
2016-07-01 00:08:44 +00:00
|
|
|
/** @var ApiContinuationManager|null */
|
|
|
|
|
private $mContinuationManager;
|
2013-02-05 06:52:55 +00:00
|
|
|
private $mAction;
|
|
|
|
|
private $mEnableWrite;
|
2017-11-01 20:55:24 +00:00
|
|
|
private $mInternalMode, $mCdnMaxAge;
|
2016-04-29 22:47:11 +00:00
|
|
|
/** @var ApiBase */
|
|
|
|
|
private $mModule;
|
2006-10-01 04:38:31 +00:00
|
|
|
|
2010-07-23 07:17:56 +00:00
|
|
|
private $mCacheMode = 'private';
|
2016-02-17 09:09:32 +00:00
|
|
|
private $mCacheControl = [];
|
|
|
|
|
private $mParamsUsed = [];
|
2016-08-18 17:37:05 +00:00
|
|
|
private $mParamsSensitive = [];
|
2010-02-14 23:52:45 +00:00
|
|
|
|
2015-05-08 14:20:30 +00:00
|
|
|
/** @var bool|null Cached return value from self::lacksSameOriginSecurity() */
|
|
|
|
|
private $lacksSameOriginSecurity = null;
|
|
|
|
|
|
2006-10-01 04:38:31 +00:00
|
|
|
/**
|
2010-02-23 18:05:46 +00:00
|
|
|
* Constructs an instance of ApiMain that utilizes the module and format specified by $request.
|
|
|
|
|
*
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param IContextSource|WebRequest|null $context If this is an instance of
|
2013-11-16 19:09:17 +00:00
|
|
|
* FauxRequest, errors are thrown and no printing occurs
|
2014-04-15 18:12:09 +00:00
|
|
|
* @param bool $enableWrite Should be set to true if the api may modify data
|
2010-02-23 18:05:46 +00:00
|
|
|
*/
|
2011-10-26 23:27:01 +00:00
|
|
|
public function __construct( $context = null, $enableWrite = false ) {
|
|
|
|
|
if ( $context === null ) {
|
|
|
|
|
$context = RequestContext::getMain();
|
|
|
|
|
} elseif ( $context instanceof WebRequest ) {
|
|
|
|
|
// BC for pre-1.19
|
|
|
|
|
$request = $context;
|
|
|
|
|
$context = RequestContext::getMain();
|
|
|
|
|
}
|
|
|
|
|
// We set a derivative context so we can change stuff later
|
|
|
|
|
$this->setContext( new DerivativeContext( $context ) );
|
|
|
|
|
|
2011-10-27 01:13:19 +00:00
|
|
|
if ( isset( $request ) ) {
|
2011-10-29 16:14:11 +00:00
|
|
|
$this->getContext()->setRequest( $request );
|
2016-06-14 16:15:40 +00:00
|
|
|
} else {
|
|
|
|
|
$request = $this->getRequest();
|
2011-10-27 01:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
2016-06-14 16:15:40 +00:00
|
|
|
$this->mInternalMode = ( $request instanceof FauxRequest );
|
2006-10-25 03:54:56 +00:00
|
|
|
|
2006-10-01 04:38:31 +00:00
|
|
|
// Special handling for the main module: $parent === $this
|
2010-02-23 18:05:46 +00:00
|
|
|
parent::__construct( $this, $this->mInternalMode ? 'main_int' : 'main' );
|
2006-10-01 04:38:31 +00:00
|
|
|
|
2016-06-14 16:15:40 +00:00
|
|
|
$config = $this->getConfig();
|
|
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
if ( !$this->mInternalMode ) {
|
2016-06-14 16:15:40 +00:00
|
|
|
// Log if a request with a non-whitelisted Origin header is seen
|
|
|
|
|
// with session cookies.
|
|
|
|
|
$originHeader = $request->getHeader( 'Origin' );
|
|
|
|
|
if ( $originHeader === false ) {
|
|
|
|
|
$origins = [];
|
|
|
|
|
} else {
|
|
|
|
|
$originHeader = trim( $originHeader );
|
|
|
|
|
$origins = preg_split( '/\s+/', $originHeader );
|
|
|
|
|
}
|
|
|
|
|
$sessionCookies = array_intersect(
|
|
|
|
|
array_keys( $_COOKIE ),
|
|
|
|
|
MediaWiki\Session\SessionManager::singleton()->getVaryCookies()
|
|
|
|
|
);
|
|
|
|
|
if ( $origins && $sessionCookies && (
|
|
|
|
|
count( $origins ) !== 1 || !self::matchOrigin(
|
|
|
|
|
$origins[0],
|
|
|
|
|
$config->get( 'CrossSiteAJAXdomains' ),
|
|
|
|
|
$config->get( 'CrossSiteAJAXdomainExceptions' )
|
|
|
|
|
)
|
|
|
|
|
) ) {
|
2016-08-16 20:53:57 +00:00
|
|
|
LoggerFactory::getInstance( 'cors' )->warning(
|
2016-06-14 16:15:40 +00:00
|
|
|
'Non-whitelisted CORS request with session cookies', [
|
|
|
|
|
'origin' => $originHeader,
|
|
|
|
|
'cookies' => $sessionCookies,
|
|
|
|
|
'ip' => $request->getIP(),
|
|
|
|
|
'userAgent' => $this->getUserAgent(),
|
2019-03-29 21:56:18 +00:00
|
|
|
'wiki' => WikiMap::getCurrentWikiDbDomain()->getId(),
|
2016-06-14 16:15:40 +00:00
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2016-06-14 16:15:40 +00:00
|
|
|
// If we're in a mode that breaks the same-origin policy, strip
|
|
|
|
|
// user credentials for security.
|
2014-12-17 11:09:04 +00:00
|
|
|
if ( $this->lacksSameOriginSecurity() ) {
|
2016-06-14 16:15:40 +00:00
|
|
|
global $wgUser;
|
2014-12-17 11:09:04 +00:00
|
|
|
wfDebug( "API: stripping user credentials when the same-origin policy is not applied\n" );
|
2008-03-03 05:45:37 +00:00
|
|
|
$wgUser = new User();
|
2011-10-26 23:27:01 +00:00
|
|
|
$this->getContext()->setUser( $wgUser );
|
2017-05-20 09:40:17 +00:00
|
|
|
$request->response()->header( 'MediaWiki-Login-Suppressed: true' );
|
2008-03-03 05:45:37 +00:00
|
|
|
}
|
2007-07-12 06:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->mResult = new ApiResult( $this->getConfig()->get( 'APIMaxResultSize' ) );
|
|
|
|
|
|
|
|
|
|
// Setup uselang. This doesn't use $this->getParameter()
|
|
|
|
|
// because we're not ready to handle errors yet.
|
|
|
|
|
$uselang = $request->getVal( 'uselang', self::API_DEFAULT_USELANG );
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
if ( $uselang === 'user' ) {
|
2015-01-05 16:59:48 +00:00
|
|
|
// Assume the parent context is going to return the user language
|
|
|
|
|
// for uselang=user (see T85635).
|
|
|
|
|
} else {
|
|
|
|
|
if ( $uselang === 'content' ) {
|
2018-07-29 12:24:54 +00:00
|
|
|
$uselang = MediaWikiServices::getInstance()->getContentLanguage()->getCode();
|
2015-01-05 16:59:48 +00:00
|
|
|
}
|
|
|
|
|
$code = RequestContext::sanitizeLangCode( $uselang );
|
|
|
|
|
$this->getContext()->setLanguage( $code );
|
|
|
|
|
if ( !$this->mInternalMode ) {
|
|
|
|
|
global $wgLang;
|
|
|
|
|
$wgLang = $this->getContext()->getLanguage();
|
|
|
|
|
RequestContext::getMain()->setLanguage( $wgLang );
|
|
|
|
|
}
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-19 16:54:25 +00:00
|
|
|
// Set up the error formatter. This doesn't use $this->getParameter()
|
|
|
|
|
// because we're not ready to handle errors yet.
|
|
|
|
|
$errorFormat = $request->getVal( 'errorformat', 'bc' );
|
|
|
|
|
$errorLangCode = $request->getVal( 'errorlang', 'uselang' );
|
|
|
|
|
$errorsUseDB = $request->getCheck( 'errorsuselocal' );
|
|
|
|
|
if ( in_array( $errorFormat, [ 'plaintext', 'wikitext', 'html', 'raw', 'none' ], true ) ) {
|
|
|
|
|
if ( $errorLangCode === 'uselang' ) {
|
|
|
|
|
$errorLang = $this->getLanguage();
|
|
|
|
|
} elseif ( $errorLangCode === 'content' ) {
|
2018-07-29 12:24:54 +00:00
|
|
|
$errorLang = MediaWikiServices::getInstance()->getContentLanguage();
|
2016-10-19 16:54:25 +00:00
|
|
|
} else {
|
|
|
|
|
$errorLangCode = RequestContext::sanitizeLangCode( $errorLangCode );
|
|
|
|
|
$errorLang = Language::factory( $errorLangCode );
|
|
|
|
|
}
|
|
|
|
|
$this->mErrorFormatter = new ApiErrorFormatter(
|
|
|
|
|
$this->mResult, $errorLang, $errorFormat, $errorsUseDB
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$this->mErrorFormatter = new ApiErrorFormatter_BackCompat( $this->mResult );
|
|
|
|
|
}
|
|
|
|
|
$this->mResult->setErrorFormatter( $this->getErrorFormatter() );
|
|
|
|
|
|
2013-02-05 06:52:55 +00:00
|
|
|
$this->mModuleMgr = new ApiModuleManager( $this );
|
|
|
|
|
$this->mModuleMgr->addModules( self::$Modules, 'action' );
|
2014-01-24 02:51:11 +00:00
|
|
|
$this->mModuleMgr->addModules( $config->get( 'APIModules' ), 'action' );
|
2013-02-05 06:52:55 +00:00
|
|
|
$this->mModuleMgr->addModules( self::$Formats, 'format' );
|
2014-01-24 02:51:11 +00:00
|
|
|
$this->mModuleMgr->addModules( $config->get( 'APIFormatModules' ), 'format' );
|
2006-10-22 23:45:20 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
Hooks::run( 'ApiMain::moduleManager', [ $this->mModuleMgr ] );
|
2015-02-12 01:38:18 +00:00
|
|
|
|
API: Overhaul ApiResult, make format=xml not throw, and add json formatversion
ApiResult was a mess: some methods could only be used with an array
reference instead of manipulating the stored data, methods that had both
array-ref and internal-data versions had names that didn't at all
correspond, some methods that worked on an array reference were
annoyingly non-static, and then the whole mess with setIndexedTagName.
ApiFormatXml is also entirely annoying to deal with, as it liked to
throw exceptions if certain metadata wasn't provided that no other
formatter required. Its legacy also means we have this silly convention
of using empty-string rather than boolean true, annoying restrictions on
keys (leading to things that should be hashes being arrays of key-value
object instead), '*' used as a key all over the place, and so on.
So, changes here:
* ApiResult is no longer an ApiBase or a ContextSource.
* Wherever sensible, ApiResult provides a static method working on an
arrayref and a non-static method working on internal data.
* Metadata is now always added to ApiResult's internal data structure.
Formatters are responsible for stripping it if necessary. "raw mode"
is deprecated.
* New metadata to replace the '*' key, solve the array() => '[]' vs '{}'
question, and so on.
* New class for formatting warnings and errors using i18n messages, and
support for multiple errors and a more machine-readable format for
warnings. For the moment, though, the actual output will not be changing
yet (see T47843 for future plans).
* New formatversion parameter for format=json and format=php, to select
between BC mode and the modern output.
* In BC mode, booleans will be converted to empty-string presence style;
modules currently returning booleans will need to use
ApiResult::META_BC_BOOLS to preserve their current output.
Actual changes to the API modules' output (e.g. actually returning
booleans for the new formatversion) beyond the use of
ApiResult::setContentValue() are left for a future change.
Bug: T76728
Bug: T57371
Bug: T33629
Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678f
2014-12-03 22:14:22 +00:00
|
|
|
$this->mContinuationManager = null;
|
2006-10-02 18:27:06 +00:00
|
|
|
$this->mEnableWrite = $enableWrite;
|
2006-10-22 23:45:20 +00:00
|
|
|
|
2017-11-01 20:55:24 +00:00
|
|
|
$this->mCdnMaxAge = -1; // flag for executeActionWithErrorHandling()
|
2008-04-11 15:20:45 +00:00
|
|
|
$this->mCommit = false;
|
2006-10-01 04:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
2007-07-15 00:52:35 +00:00
|
|
|
/**
|
|
|
|
|
* Return true if the API was started by other PHP code using FauxRequest
|
2011-01-30 08:16:13 +00:00
|
|
|
* @return bool
|
2007-07-15 00:52:35 +00:00
|
|
|
*/
|
|
|
|
|
public function isInternalMode() {
|
|
|
|
|
return $this->mInternalMode;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
/**
|
2010-01-22 03:14:52 +00:00
|
|
|
* Get the ApiResult object associated with current request
|
2010-09-29 18:18:07 +00:00
|
|
|
*
|
|
|
|
|
* @return ApiResult
|
2007-05-20 10:08:40 +00:00
|
|
|
*/
|
2006-11-29 05:45:03 +00:00
|
|
|
public function getResult() {
|
2006-10-14 07:18:08 +00:00
|
|
|
return $this->mResult;
|
2006-10-01 21:20:55 +00:00
|
|
|
}
|
2010-01-22 03:14:52 +00:00
|
|
|
|
2015-05-08 14:20:30 +00:00
|
|
|
/**
|
|
|
|
|
* Get the security flag for the current request
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function lacksSameOriginSecurity() {
|
|
|
|
|
if ( $this->lacksSameOriginSecurity !== null ) {
|
|
|
|
|
return $this->lacksSameOriginSecurity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$request = $this->getRequest();
|
|
|
|
|
|
|
|
|
|
// JSONP mode
|
2019-03-11 08:40:36 +00:00
|
|
|
if ( $request->getCheck( 'callback' ) ) {
|
2015-05-08 14:20:30 +00:00
|
|
|
$this->lacksSameOriginSecurity = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-08 17:05:25 +00:00
|
|
|
// Anonymous CORS
|
|
|
|
|
if ( $request->getVal( 'origin' ) === '*' ) {
|
|
|
|
|
$this->lacksSameOriginSecurity = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-08 14:20:30 +00:00
|
|
|
// Header to be used from XMLHTTPRequest when the request might
|
|
|
|
|
// otherwise be used for XSS.
|
|
|
|
|
if ( $request->getHeader( 'Treat-as-Untrusted' ) !== false ) {
|
|
|
|
|
$this->lacksSameOriginSecurity = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Allow extensions to override.
|
2016-05-20 18:11:58 +00:00
|
|
|
$this->lacksSameOriginSecurity = !Hooks::run( 'RequestHasSameOriginSecurity', [ $request ] );
|
2015-05-08 14:20:30 +00:00
|
|
|
return $this->lacksSameOriginSecurity;
|
|
|
|
|
}
|
|
|
|
|
|
API: Overhaul ApiResult, make format=xml not throw, and add json formatversion
ApiResult was a mess: some methods could only be used with an array
reference instead of manipulating the stored data, methods that had both
array-ref and internal-data versions had names that didn't at all
correspond, some methods that worked on an array reference were
annoyingly non-static, and then the whole mess with setIndexedTagName.
ApiFormatXml is also entirely annoying to deal with, as it liked to
throw exceptions if certain metadata wasn't provided that no other
formatter required. Its legacy also means we have this silly convention
of using empty-string rather than boolean true, annoying restrictions on
keys (leading to things that should be hashes being arrays of key-value
object instead), '*' used as a key all over the place, and so on.
So, changes here:
* ApiResult is no longer an ApiBase or a ContextSource.
* Wherever sensible, ApiResult provides a static method working on an
arrayref and a non-static method working on internal data.
* Metadata is now always added to ApiResult's internal data structure.
Formatters are responsible for stripping it if necessary. "raw mode"
is deprecated.
* New metadata to replace the '*' key, solve the array() => '[]' vs '{}'
question, and so on.
* New class for formatting warnings and errors using i18n messages, and
support for multiple errors and a more machine-readable format for
warnings. For the moment, though, the actual output will not be changing
yet (see T47843 for future plans).
* New formatversion parameter for format=json and format=php, to select
between BC mode and the modern output.
* In BC mode, booleans will be converted to empty-string presence style;
modules currently returning booleans will need to use
ApiResult::META_BC_BOOLS to preserve their current output.
Actual changes to the API modules' output (e.g. actually returning
booleans for the new formatversion) beyond the use of
ApiResult::setContentValue() are left for a future change.
Bug: T76728
Bug: T57371
Bug: T33629
Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678f
2014-12-03 22:14:22 +00:00
|
|
|
/**
|
|
|
|
|
* Get the ApiErrorFormatter object associated with current request
|
|
|
|
|
* @return ApiErrorFormatter
|
|
|
|
|
*/
|
|
|
|
|
public function getErrorFormatter() {
|
|
|
|
|
return $this->mErrorFormatter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the continuation manager
|
|
|
|
|
* @return ApiContinuationManager|null
|
|
|
|
|
*/
|
|
|
|
|
public function getContinuationManager() {
|
|
|
|
|
return $this->mContinuationManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the continuation manager
|
2017-08-11 16:09:41 +00:00
|
|
|
* @param ApiContinuationManager|null $manager
|
API: Overhaul ApiResult, make format=xml not throw, and add json formatversion
ApiResult was a mess: some methods could only be used with an array
reference instead of manipulating the stored data, methods that had both
array-ref and internal-data versions had names that didn't at all
correspond, some methods that worked on an array reference were
annoyingly non-static, and then the whole mess with setIndexedTagName.
ApiFormatXml is also entirely annoying to deal with, as it liked to
throw exceptions if certain metadata wasn't provided that no other
formatter required. Its legacy also means we have this silly convention
of using empty-string rather than boolean true, annoying restrictions on
keys (leading to things that should be hashes being arrays of key-value
object instead), '*' used as a key all over the place, and so on.
So, changes here:
* ApiResult is no longer an ApiBase or a ContextSource.
* Wherever sensible, ApiResult provides a static method working on an
arrayref and a non-static method working on internal data.
* Metadata is now always added to ApiResult's internal data structure.
Formatters are responsible for stripping it if necessary. "raw mode"
is deprecated.
* New metadata to replace the '*' key, solve the array() => '[]' vs '{}'
question, and so on.
* New class for formatting warnings and errors using i18n messages, and
support for multiple errors and a more machine-readable format for
warnings. For the moment, though, the actual output will not be changing
yet (see T47843 for future plans).
* New formatversion parameter for format=json and format=php, to select
between BC mode and the modern output.
* In BC mode, booleans will be converted to empty-string presence style;
modules currently returning booleans will need to use
ApiResult::META_BC_BOOLS to preserve their current output.
Actual changes to the API modules' output (e.g. actually returning
booleans for the new formatversion) beyond the use of
ApiResult::setContentValue() are left for a future change.
Bug: T76728
Bug: T57371
Bug: T33629
Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678f
2014-12-03 22:14:22 +00:00
|
|
|
*/
|
2018-03-26 14:38:41 +00:00
|
|
|
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()
|
|
|
|
|
);
|
API: Overhaul ApiResult, make format=xml not throw, and add json formatversion
ApiResult was a mess: some methods could only be used with an array
reference instead of manipulating the stored data, methods that had both
array-ref and internal-data versions had names that didn't at all
correspond, some methods that worked on an array reference were
annoyingly non-static, and then the whole mess with setIndexedTagName.
ApiFormatXml is also entirely annoying to deal with, as it liked to
throw exceptions if certain metadata wasn't provided that no other
formatter required. Its legacy also means we have this silly convention
of using empty-string rather than boolean true, annoying restrictions on
keys (leading to things that should be hashes being arrays of key-value
object instead), '*' used as a key all over the place, and so on.
So, changes here:
* ApiResult is no longer an ApiBase or a ContextSource.
* Wherever sensible, ApiResult provides a static method working on an
arrayref and a non-static method working on internal data.
* Metadata is now always added to ApiResult's internal data structure.
Formatters are responsible for stripping it if necessary. "raw mode"
is deprecated.
* New metadata to replace the '*' key, solve the array() => '[]' vs '{}'
question, and so on.
* New class for formatting warnings and errors using i18n messages, and
support for multiple errors and a more machine-readable format for
warnings. For the moment, though, the actual output will not be changing
yet (see T47843 for future plans).
* New formatversion parameter for format=json and format=php, to select
between BC mode and the modern output.
* In BC mode, booleans will be converted to empty-string presence style;
modules currently returning booleans will need to use
ApiResult::META_BC_BOOLS to preserve their current output.
Actual changes to the API modules' output (e.g. actually returning
booleans for the new formatversion) beyond the use of
ApiResult::setContentValue() are left for a future change.
Bug: T76728
Bug: T57371
Bug: T33629
Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678f
2014-12-03 22:14:22 +00:00
|
|
|
}
|
|
|
|
|
$this->mContinuationManager = $manager;
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-27 22:09:28 +00:00
|
|
|
/**
|
|
|
|
|
* Get the API module object. Only works after executeAction()
|
2011-01-30 08:16:13 +00:00
|
|
|
*
|
|
|
|
|
* @return ApiBase
|
2009-08-27 22:09:28 +00:00
|
|
|
*/
|
|
|
|
|
public function getModule() {
|
|
|
|
|
return $this->mModule;
|
|
|
|
|
}
|
2010-07-23 07:33:40 +00:00
|
|
|
|
2010-06-03 09:53:28 +00:00
|
|
|
/**
|
|
|
|
|
* Get the result formatter object. Only works after setupExecuteAction()
|
2010-09-29 18:18:07 +00:00
|
|
|
*
|
|
|
|
|
* @return ApiFormatBase
|
2010-06-03 09:53:28 +00:00
|
|
|
*/
|
|
|
|
|
public function getPrinter() {
|
|
|
|
|
return $this->mPrinter;
|
|
|
|
|
}
|
2006-10-01 21:20:55 +00:00
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
/**
|
|
|
|
|
* Set how long the response should be cached.
|
2011-05-08 16:48:30 +00:00
|
|
|
*
|
2014-04-15 18:12:09 +00:00
|
|
|
* @param int $maxage
|
2007-05-20 10:08:40 +00:00
|
|
|
*/
|
2010-01-11 15:55:52 +00:00
|
|
|
public function setCacheMaxAge( $maxage ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setCacheControl( [
|
2010-02-14 23:52:45 +00:00
|
|
|
'max-age' => $maxage,
|
|
|
|
|
's-maxage' => $maxage
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2010-02-14 23:52:45 +00:00
|
|
|
}
|
2010-07-23 07:17:56 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the type of caching headers which will be sent.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $mode One of:
|
2010-07-23 07:33:40 +00:00
|
|
|
* - 'public': Cache this object in public caches, if the maxage or smaxage
|
2010-07-23 07:17:56 +00:00
|
|
|
* parameter is set, or if setCacheMaxAge() was called. If a maximum age is
|
|
|
|
|
* not provided by any of these means, the object will be private.
|
|
|
|
|
* - 'private': Cache this object only in private client-side caches.
|
|
|
|
|
* - 'anon-public-user-private': Make this object cacheable for logged-out
|
2010-07-23 07:33:40 +00:00
|
|
|
* users, but private for logged-in users. IMPORTANT: If this is set, it must be
|
|
|
|
|
* set consistently for a given URL, it cannot be set differently depending on
|
2010-07-23 07:17:56 +00:00
|
|
|
* things like the contents of the database, or whether the user is logged in.
|
|
|
|
|
*
|
|
|
|
|
* If the wiki does not allow anonymous users to read it, the mode set here
|
2010-07-23 07:33:40 +00:00
|
|
|
* will be ignored, and private caching headers will always be sent. In other words,
|
2010-07-23 07:17:56 +00:00
|
|
|
* the "public" mode is equivalent to saying that the data sent is as public as a page
|
|
|
|
|
* view.
|
|
|
|
|
*
|
2010-07-23 07:33:40 +00:00
|
|
|
* For user-dependent data, the private mode should generally be used. The
|
|
|
|
|
* anon-public-user-private mode should only be used where there is a particularly
|
2010-07-23 07:17:56 +00:00
|
|
|
* good performance reason for caching the anonymous response, but where the
|
2010-07-23 07:33:40 +00:00
|
|
|
* response to logged-in users may differ, or may contain private data.
|
2010-07-23 07:17:56 +00:00
|
|
|
*
|
|
|
|
|
* If this function is never called, then the default will be the private mode.
|
|
|
|
|
*/
|
|
|
|
|
public function setCacheMode( $mode ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
if ( !in_array( $mode, [ 'private', 'public', 'anon-public-user-private' ] ) ) {
|
2010-07-23 07:33:40 +00:00
|
|
|
wfDebug( __METHOD__ . ": unrecognised cache mode \"$mode\"\n" );
|
2013-11-14 12:42:04 +00:00
|
|
|
|
2010-07-23 07:17:56 +00:00
|
|
|
// Ignore for forwards-compatibility
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-26 14:58:02 +00:00
|
|
|
if ( !User::isEveryoneAllowed( 'read' ) ) {
|
2010-07-23 07:17:56 +00:00
|
|
|
// Private wiki, only private headers
|
|
|
|
|
if ( $mode !== 'private' ) {
|
2010-07-23 07:33:40 +00:00
|
|
|
wfDebug( __METHOD__ . ": ignoring request for $mode cache mode, private wiki\n" );
|
2013-11-14 12:42:04 +00:00
|
|
|
|
2010-07-23 07:17:56 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
if ( $mode === 'public' && $this->getParameter( 'uselang' ) === 'user' ) {
|
|
|
|
|
// User language is used for i18n, so we don't want to publicly
|
|
|
|
|
// cache. Anons are ok, because if they have non-default language
|
|
|
|
|
// then there's an appropriate Vary header set by whatever set
|
|
|
|
|
// their non-default language.
|
|
|
|
|
wfDebug( __METHOD__ . ": downgrading cache mode 'public' to " .
|
2014-10-30 16:50:19 +00:00
|
|
|
"'anon-public-user-private' due to uselang=user\n" );
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
$mode = 'anon-public-user-private';
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-23 07:33:40 +00:00
|
|
|
wfDebug( __METHOD__ . ": setting cache mode $mode\n" );
|
2010-07-23 07:17:56 +00:00
|
|
|
$this->mCacheMode = $mode;
|
|
|
|
|
}
|
2010-07-23 07:33:40 +00:00
|
|
|
|
2010-02-14 23:52:45 +00:00
|
|
|
/**
|
|
|
|
|
* Set directives (key/value pairs) for the Cache-Control header.
|
|
|
|
|
* Boolean values will be formatted as such, by including or omitting
|
|
|
|
|
* without an equals sign.
|
2010-07-23 07:17:56 +00:00
|
|
|
*
|
2010-07-23 07:33:40 +00:00
|
|
|
* Cache control values set here will only be used if the cache mode is not
|
2010-07-23 07:17:56 +00:00
|
|
|
* private, see setCacheMode().
|
2011-06-05 15:10:11 +00:00
|
|
|
*
|
2014-04-15 18:12:09 +00:00
|
|
|
* @param array $directives
|
2010-02-14 23:52:45 +00:00
|
|
|
*/
|
|
|
|
|
public function setCacheControl( $directives ) {
|
|
|
|
|
$this->mCacheControl = $directives + $this->mCacheControl;
|
2006-10-22 23:45:20 +00:00
|
|
|
}
|
2010-07-23 07:33:40 +00:00
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
/**
|
|
|
|
|
* Create an instance of an output formatter by its name
|
2011-01-30 08:16:13 +00:00
|
|
|
*
|
2014-04-15 18:12:09 +00:00
|
|
|
* @param string $format
|
2011-05-08 16:48:30 +00:00
|
|
|
*
|
2011-01-30 08:16:13 +00:00
|
|
|
* @return ApiFormatBase
|
2007-05-20 10:08:40 +00:00
|
|
|
*/
|
2010-01-11 15:55:52 +00:00
|
|
|
public function createPrinterByName( $format ) {
|
2018-07-19 13:24:48 +00:00
|
|
|
$printer = $this->mModuleMgr->getModule( $format, 'format', /* $ignoreCache */ true );
|
2013-02-05 06:52:55 +00:00
|
|
|
if ( $printer === null ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError(
|
|
|
|
|
[ 'apierror-unknownformat', wfEscapeWikiText( $format ) ], 'unknown_format'
|
|
|
|
|
);
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2013-11-14 12:42:04 +00:00
|
|
|
|
2013-02-05 06:52:55 +00:00
|
|
|
return $printer;
|
2006-10-15 07:43:52 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
/**
|
2008-04-14 07:45:50 +00:00
|
|
|
* Execute api request. Any errors will be handled if the API was called by the remote client.
|
2007-05-20 10:08:40 +00:00
|
|
|
*/
|
2006-10-01 04:38:31 +00:00
|
|
|
public function execute() {
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( $this->mInternalMode ) {
|
2006-10-14 07:18:08 +00:00
|
|
|
$this->executeAction();
|
2010-02-23 18:05:46 +00:00
|
|
|
} else {
|
2006-10-14 07:18:08 +00:00
|
|
|
$this->executeActionWithErrorHandling();
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2006-10-14 07:18:08 +00:00
|
|
|
}
|
2006-10-22 23:45:20 +00:00
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
/**
|
|
|
|
|
* Execute an action, and in case of an error, erase whatever partial results
|
|
|
|
|
* have been accumulated, and replace it with an error message and a help screen.
|
|
|
|
|
*/
|
2006-10-14 07:18:08 +00:00
|
|
|
protected function executeActionWithErrorHandling() {
|
Reimplement CORS properly, addressing Tim's concerns
Tim's concerns (listed at
https://bugzilla.wikimedia.org/show_bug.cgi?id=20814#c6) were:
* Lack of Vary: Origin breaks Squid caching
* Vary: Origin on everything would be disastrous, so add an origin param
* Origin header is space-separated list, wasn't treated as such
This commit:
* Remove CORS code from api.php and reimplement it in ApiMain.php
* Add 'origin' parameter to ApiMain
* If 'origin' parameter doesn't match Origin header, send a 403
* If origin is whitelisted, set CORS headers and set Vary: Origin
* Add https?:// to wildcard matching logic, wasn't there but is needed
CORS now works :) you can test it locally as follows:
Set $wgCrossSiteAJAXdomains[] = '*.wikipedia.org';
Log into MediaWiki on localhost
Go to Wikipedia, open a JS console, and run:
$.ajax( {
'url': 'http://localhost/w/api.php',
'data': {
'action': 'query',
'meta': 'userinfo',
'format': 'json',
'origin': 'https://en.wikipedia.org'
// or whichever domain you're on; must be correct!
},
'xhrFields': {
'withCredentials': true
},
'success': function( data ) {
alert( 'Foreign user ' + data.query.userinfo.name +
' (ID ' + data.query.userinfo.id + ')' );
}
} );
Change-Id: I725ce176866d7c81dd9ad6d7bc4a86b7160f2458
2012-06-01 12:14:47 +00:00
|
|
|
// Verify the CORS header before executing the action
|
|
|
|
|
if ( !$this->handleCORS() ) {
|
|
|
|
|
// handleCORS() has sent a 403, abort
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-26 20:41:00 +00:00
|
|
|
// Exit here if the request method was OPTIONS
|
|
|
|
|
// (assume there will be a followup GET or POST)
|
|
|
|
|
if ( $this->getRequest()->getMethod() === 'OPTIONS' ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-14 07:18:08 +00:00
|
|
|
// In case an error occurs during data output,
|
2007-05-20 10:08:40 +00:00
|
|
|
// clear the output buffer and print just the error information
|
2015-08-17 20:52:09 +00:00
|
|
|
$obLevel = ob_get_level();
|
2006-10-13 05:21:38 +00:00
|
|
|
ob_start();
|
|
|
|
|
|
2012-09-19 10:07:48 +00:00
|
|
|
$t = microtime( true );
|
2016-02-18 21:18:14 +00:00
|
|
|
$isError = false;
|
2006-10-01 04:38:31 +00:00
|
|
|
try {
|
2006-10-14 07:18:08 +00:00
|
|
|
$this->executeAction();
|
2016-04-29 22:47:11 +00:00
|
|
|
$runTime = microtime( true ) - $t;
|
|
|
|
|
$this->logRequest( $runTime );
|
2018-07-20 06:49:49 +00:00
|
|
|
MediaWikiServices::getInstance()->getStatsdDataFactory()->timing(
|
|
|
|
|
'api.' . $this->mModule->getModuleName() . '.executeTiming', 1000 * $runTime
|
|
|
|
|
);
|
2018-08-21 15:19:15 +00:00
|
|
|
} catch ( Exception $e ) { // @todo Remove this block when HHVM is no longer supported
|
|
|
|
|
$this->handleException( $e );
|
|
|
|
|
$this->logRequest( microtime( true ) - $t, $e );
|
|
|
|
|
$isError = true;
|
|
|
|
|
} catch ( Throwable $e ) {
|
2014-03-06 14:37:26 +00:00
|
|
|
$this->handleException( $e );
|
2016-02-18 21:18:14 +00:00
|
|
|
$this->logRequest( microtime( true ) - $t, $e );
|
2015-08-17 20:52:09 +00:00
|
|
|
$isError = true;
|
2007-07-15 00:52:35 +00:00
|
|
|
}
|
2010-07-23 07:17:56 +00:00
|
|
|
|
2015-11-10 06:57:00 +00:00
|
|
|
// Commit DBs and send any related cookies and headers
|
|
|
|
|
MediaWiki::preOutputCommit( $this->getContext() );
|
|
|
|
|
|
2010-07-23 07:33:40 +00:00
|
|
|
// Send cache headers after any code which might generate an error, to
|
2010-07-23 07:17:56 +00:00
|
|
|
// avoid sending public cache headers for errors.
|
2015-08-17 20:52:09 +00:00
|
|
|
$this->sendCacheHeaders( $isError );
|
2010-07-23 07:17:56 +00:00
|
|
|
|
2015-08-17 20:52:09 +00:00
|
|
|
// Executing the action might have already messed with the output
|
|
|
|
|
// buffers.
|
|
|
|
|
while ( ob_get_level() > $obLevel ) {
|
|
|
|
|
ob_end_flush();
|
|
|
|
|
}
|
2010-07-23 07:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
2014-03-06 14:37:26 +00:00
|
|
|
/**
|
|
|
|
|
* Handle an exception as an API response
|
|
|
|
|
*
|
|
|
|
|
* @since 1.23
|
2018-08-21 15:19:15 +00:00
|
|
|
* @param Exception|Throwable $e
|
2014-03-06 14:37:26 +00:00
|
|
|
*/
|
2018-08-21 15:19:15 +00:00
|
|
|
protected function handleException( $e ) {
|
2017-02-20 22:28:10 +00:00
|
|
|
// T65145: Rollback any open database transactions
|
2018-10-10 17:55:09 +00:00
|
|
|
if ( !$e instanceof ApiUsageException ) {
|
|
|
|
|
// ApiUsageExceptions are intentional, so don't rollback if that's the case
|
2017-06-30 22:01:33 +00:00
|
|
|
MWExceptionHandler::rollbackMasterChangesAndLog( $e );
|
2014-04-22 16:56:40 +00:00
|
|
|
}
|
2014-03-27 16:18:38 +00:00
|
|
|
|
2014-03-06 14:37:26 +00:00
|
|
|
// Allow extra cleanup and logging
|
2016-02-17 09:09:32 +00:00
|
|
|
Hooks::run( 'ApiMain::onException', [ $this, $e ] );
|
2014-03-06 14:37:26 +00:00
|
|
|
|
|
|
|
|
// Handle any kind of exception by outputting properly formatted error message.
|
|
|
|
|
// If this fails, an unhandled exception should be thrown so that global error
|
|
|
|
|
// handler will process and log it.
|
|
|
|
|
|
2016-10-19 16:54:25 +00:00
|
|
|
$errCodes = $this->substituteResultWithError( $e );
|
2014-03-06 14:37:26 +00:00
|
|
|
|
|
|
|
|
// Error results should not be cached
|
|
|
|
|
$this->setCacheMode( 'private' );
|
|
|
|
|
|
|
|
|
|
$response = $this->getRequest()->response();
|
2018-02-17 12:29:13 +00:00
|
|
|
$headerStr = 'MediaWiki-API-Error: ' . implode( ', ', $errCodes );
|
2016-11-09 16:59:05 +00:00
|
|
|
$response->header( $headerStr );
|
2014-03-06 14:37:26 +00:00
|
|
|
|
|
|
|
|
// Reset and print just the error message
|
|
|
|
|
ob_clean();
|
|
|
|
|
|
2015-04-20 16:09:24 +00:00
|
|
|
// Printer may not be initialized if the extractRequestParams() fails for the main module
|
|
|
|
|
$this->createErrorPrinter();
|
|
|
|
|
|
2016-10-19 16:54:25 +00:00
|
|
|
$failed = false;
|
2015-04-20 16:09:24 +00:00
|
|
|
try {
|
2016-11-09 16:59:05 +00:00
|
|
|
$this->printResult( $e->getCode() );
|
2016-10-19 16:54:25 +00:00
|
|
|
} catch ( ApiUsageException $ex ) {
|
|
|
|
|
// The error printer itself is failing. Try suppressing its request
|
|
|
|
|
// parameters and redo.
|
|
|
|
|
$failed = true;
|
|
|
|
|
$this->addWarning( 'apiwarn-errorprinterfailed' );
|
|
|
|
|
foreach ( $ex->getStatusValue()->getErrors() as $error ) {
|
|
|
|
|
try {
|
|
|
|
|
$this->mPrinter->addWarning( $error );
|
2018-08-21 15:19:15 +00:00
|
|
|
} catch ( Exception $ex2 ) { // @todo Remove this block when HHVM is no longer supported
|
|
|
|
|
// WTF?
|
|
|
|
|
$this->addWarning( $error );
|
|
|
|
|
} catch ( Throwable $ex2 ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
// WTF?
|
|
|
|
|
$this->addWarning( $error );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $failed ) {
|
2015-04-20 16:09:24 +00:00
|
|
|
$this->mPrinter = null;
|
|
|
|
|
$this->createErrorPrinter();
|
|
|
|
|
$this->mPrinter->forceDefaultParams();
|
2016-11-09 16:59:05 +00:00
|
|
|
if ( $e->getCode() ) {
|
|
|
|
|
$response->statusHeader( 200 ); // Reset in case the fallback doesn't want a non-200
|
|
|
|
|
}
|
|
|
|
|
$this->printResult( $e->getCode() );
|
2015-04-20 16:09:24 +00:00
|
|
|
}
|
2014-03-06 14:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle an exception from the ApiBeforeMain hook.
|
|
|
|
|
*
|
|
|
|
|
* This tries to print the exception as an API response, to be more
|
|
|
|
|
* friendly to clients. If it fails, it will rethrow the exception.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.23
|
2018-08-21 15:19:15 +00:00
|
|
|
* @param Exception|Throwable $e
|
|
|
|
|
* @throws Exception|Throwable
|
2014-03-06 14:37:26 +00:00
|
|
|
*/
|
2018-08-21 15:19:15 +00:00
|
|
|
public static function handleApiBeforeMainException( $e ) {
|
2014-03-06 14:37:26 +00:00
|
|
|
ob_start();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$main = new self( RequestContext::getMain(), false );
|
|
|
|
|
$main->handleException( $e );
|
2016-02-18 21:18:14 +00:00
|
|
|
$main->logRequest( 0, $e );
|
2018-08-21 15:19:15 +00:00
|
|
|
} catch ( Exception $e2 ) { // @todo Remove this block when HHVM is no longer supported
|
|
|
|
|
// Nope, even that didn't work. Punt.
|
|
|
|
|
throw $e;
|
|
|
|
|
} catch ( Throwable $e2 ) {
|
2014-03-06 14:37:26 +00:00
|
|
|
// Nope, even that didn't work. Punt.
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-18 21:18:14 +00:00
|
|
|
// Reset cache headers
|
2015-08-17 20:52:09 +00:00
|
|
|
$main->sendCacheHeaders( true );
|
2014-03-06 14:37:26 +00:00
|
|
|
|
|
|
|
|
ob_end_flush();
|
|
|
|
|
}
|
|
|
|
|
|
Reimplement CORS properly, addressing Tim's concerns
Tim's concerns (listed at
https://bugzilla.wikimedia.org/show_bug.cgi?id=20814#c6) were:
* Lack of Vary: Origin breaks Squid caching
* Vary: Origin on everything would be disastrous, so add an origin param
* Origin header is space-separated list, wasn't treated as such
This commit:
* Remove CORS code from api.php and reimplement it in ApiMain.php
* Add 'origin' parameter to ApiMain
* If 'origin' parameter doesn't match Origin header, send a 403
* If origin is whitelisted, set CORS headers and set Vary: Origin
* Add https?:// to wildcard matching logic, wasn't there but is needed
CORS now works :) you can test it locally as follows:
Set $wgCrossSiteAJAXdomains[] = '*.wikipedia.org';
Log into MediaWiki on localhost
Go to Wikipedia, open a JS console, and run:
$.ajax( {
'url': 'http://localhost/w/api.php',
'data': {
'action': 'query',
'meta': 'userinfo',
'format': 'json',
'origin': 'https://en.wikipedia.org'
// or whichever domain you're on; must be correct!
},
'xhrFields': {
'withCredentials': true
},
'success': function( data ) {
alert( 'Foreign user ' + data.query.userinfo.name +
' (ID ' + data.query.userinfo.id + ')' );
}
} );
Change-Id: I725ce176866d7c81dd9ad6d7bc4a86b7160f2458
2012-06-01 12:14:47 +00:00
|
|
|
/**
|
|
|
|
|
* Check the &origin= query parameter against the Origin: HTTP header and respond appropriately.
|
|
|
|
|
*
|
|
|
|
|
* If no origin parameter is present, nothing happens.
|
|
|
|
|
* If an origin parameter is present but doesn't match the Origin header, a 403 status code
|
|
|
|
|
* is set and false is returned.
|
|
|
|
|
* If the parameter and the header do match, the header is checked against $wgCrossSiteAJAXdomains
|
|
|
|
|
* and $wgCrossSiteAJAXdomainExceptions, and if the origin qualifies, the appropriate CORS
|
|
|
|
|
* headers are set.
|
2016-10-13 05:34:26 +00:00
|
|
|
* https://www.w3.org/TR/cors/#resource-requests
|
|
|
|
|
* https://www.w3.org/TR/cors/#resource-preflight-requests
|
Reimplement CORS properly, addressing Tim's concerns
Tim's concerns (listed at
https://bugzilla.wikimedia.org/show_bug.cgi?id=20814#c6) were:
* Lack of Vary: Origin breaks Squid caching
* Vary: Origin on everything would be disastrous, so add an origin param
* Origin header is space-separated list, wasn't treated as such
This commit:
* Remove CORS code from api.php and reimplement it in ApiMain.php
* Add 'origin' parameter to ApiMain
* If 'origin' parameter doesn't match Origin header, send a 403
* If origin is whitelisted, set CORS headers and set Vary: Origin
* Add https?:// to wildcard matching logic, wasn't there but is needed
CORS now works :) you can test it locally as follows:
Set $wgCrossSiteAJAXdomains[] = '*.wikipedia.org';
Log into MediaWiki on localhost
Go to Wikipedia, open a JS console, and run:
$.ajax( {
'url': 'http://localhost/w/api.php',
'data': {
'action': 'query',
'meta': 'userinfo',
'format': 'json',
'origin': 'https://en.wikipedia.org'
// or whichever domain you're on; must be correct!
},
'xhrFields': {
'withCredentials': true
},
'success': function( data ) {
alert( 'Foreign user ' + data.query.userinfo.name +
' (ID ' + data.query.userinfo.id + ')' );
}
} );
Change-Id: I725ce176866d7c81dd9ad6d7bc4a86b7160f2458
2012-06-01 12:14:47 +00:00
|
|
|
*
|
|
|
|
|
* @return bool False if the caller should abort (403 case), true otherwise (all other cases)
|
|
|
|
|
*/
|
|
|
|
|
protected function handleCORS() {
|
|
|
|
|
$originParam = $this->getParameter( 'origin' ); // defaults to null
|
|
|
|
|
if ( $originParam === null ) {
|
|
|
|
|
// No origin parameter, nothing to do
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-08-20 14:55:28 +00:00
|
|
|
|
|
|
|
|
$request = $this->getRequest();
|
|
|
|
|
$response = $request->response();
|
2014-12-04 14:39:50 +00:00
|
|
|
|
2017-06-21 18:45:56 +00:00
|
|
|
$matchedOrigin = false;
|
2016-04-08 17:05:25 +00:00
|
|
|
$allowTiming = false;
|
|
|
|
|
$varyOrigin = true;
|
|
|
|
|
|
|
|
|
|
if ( $originParam === '*' ) {
|
|
|
|
|
// Request for anonymous CORS
|
2017-06-21 18:45:56 +00:00
|
|
|
// Technically we should check for the presence of an Origin header
|
|
|
|
|
// and not process it as CORS if it's not set, but that would
|
|
|
|
|
// require us to vary on Origin for all 'origin=*' requests which
|
|
|
|
|
// we don't want to do.
|
|
|
|
|
$matchedOrigin = true;
|
2016-04-08 17:05:25 +00:00
|
|
|
$allowOrigin = '*';
|
|
|
|
|
$allowCredentials = 'false';
|
|
|
|
|
$varyOrigin = false; // No need to vary
|
2012-08-20 14:55:28 +00:00
|
|
|
} else {
|
2016-04-08 17:05:25 +00:00
|
|
|
// Non-anonymous CORS, check we allow the domain
|
2013-11-16 19:09:17 +00:00
|
|
|
|
2016-04-08 17:05:25 +00:00
|
|
|
// Origin: header is a space-separated list of origins, check all of them
|
|
|
|
|
$originHeader = $request->getHeader( 'Origin' );
|
|
|
|
|
if ( $originHeader === false ) {
|
|
|
|
|
$origins = [];
|
|
|
|
|
} else {
|
|
|
|
|
$originHeader = trim( $originHeader );
|
|
|
|
|
$origins = preg_split( '/\s+/', $originHeader );
|
|
|
|
|
}
|
2013-11-14 12:42:04 +00:00
|
|
|
|
2016-04-08 17:05:25 +00:00
|
|
|
if ( !in_array( $originParam, $origins ) ) {
|
|
|
|
|
// origin parameter set but incorrect
|
|
|
|
|
// Send a 403 response
|
|
|
|
|
$response->statusHeader( 403 );
|
|
|
|
|
$response->header( 'Cache-Control: no-cache' );
|
|
|
|
|
echo "'origin' parameter does not match Origin header\n";
|
2013-11-16 19:09:17 +00:00
|
|
|
|
2016-04-08 17:05:25 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2013-11-16 19:09:17 +00:00
|
|
|
|
2016-04-08 17:05:25 +00:00
|
|
|
$config = $this->getConfig();
|
2017-06-21 18:45:56 +00:00
|
|
|
$matchedOrigin = count( $origins ) === 1 && self::matchOrigin(
|
2016-04-08 17:05:25 +00:00
|
|
|
$originParam,
|
|
|
|
|
$config->get( 'CrossSiteAJAXdomains' ),
|
|
|
|
|
$config->get( 'CrossSiteAJAXdomainExceptions' )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$allowOrigin = $originHeader;
|
|
|
|
|
$allowCredentials = 'true';
|
|
|
|
|
$allowTiming = $originHeader;
|
|
|
|
|
}
|
2013-11-16 19:09:17 +00:00
|
|
|
|
2017-06-21 18:45:56 +00:00
|
|
|
if ( $matchedOrigin ) {
|
2014-12-04 14:39:50 +00:00
|
|
|
$requestedMethod = $request->getHeader( 'Access-Control-Request-Method' );
|
|
|
|
|
$preflight = $request->getMethod() === 'OPTIONS' && $requestedMethod !== false;
|
|
|
|
|
if ( $preflight ) {
|
|
|
|
|
// This is a CORS preflight request
|
|
|
|
|
if ( $requestedMethod !== 'POST' && $requestedMethod !== 'GET' ) {
|
|
|
|
|
// If method is not a case-sensitive match, do not set any additional headers and terminate.
|
2017-06-21 18:45:56 +00:00
|
|
|
$response->header( 'MediaWiki-CORS-Rejection: Unsupported method requested in preflight' );
|
2014-12-04 14:39:50 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
// We allow the actual request to send the following headers
|
|
|
|
|
$requestedHeaders = $request->getHeader( 'Access-Control-Request-Headers' );
|
|
|
|
|
if ( $requestedHeaders !== false ) {
|
|
|
|
|
if ( !self::matchRequestedHeaders( $requestedHeaders ) ) {
|
2017-06-21 18:45:56 +00:00
|
|
|
$response->header( 'MediaWiki-CORS-Rejection: Unsupported header requested in preflight' );
|
2014-12-04 14:39:50 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
$response->header( 'Access-Control-Allow-Headers: ' . $requestedHeaders );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We only allow the actual request to be GET or POST
|
|
|
|
|
$response->header( 'Access-Control-Allow-Methods: POST, GET' );
|
2017-06-21 18:45:56 +00:00
|
|
|
} elseif ( $request->getMethod() !== 'POST' && $request->getMethod() !== 'GET' ) {
|
|
|
|
|
// Unsupported non-preflight method, don't handle it as CORS
|
|
|
|
|
$response->header(
|
|
|
|
|
'MediaWiki-CORS-Rejection: Unsupported method for simple request or actual request'
|
|
|
|
|
);
|
|
|
|
|
return true;
|
2014-12-04 14:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
2016-04-08 17:05:25 +00:00
|
|
|
$response->header( "Access-Control-Allow-Origin: $allowOrigin" );
|
|
|
|
|
$response->header( "Access-Control-Allow-Credentials: $allowCredentials" );
|
2016-10-13 05:34:26 +00:00
|
|
|
// https://www.w3.org/TR/resource-timing/#timing-allow-origin
|
2016-04-08 17:05:25 +00:00
|
|
|
if ( $allowTiming !== false ) {
|
|
|
|
|
$response->header( "Timing-Allow-Origin: $allowTiming" );
|
|
|
|
|
}
|
2014-12-04 14:39:50 +00:00
|
|
|
|
|
|
|
|
if ( !$preflight ) {
|
2015-09-28 11:15:17 +00:00
|
|
|
$response->header(
|
2017-05-20 09:40:17 +00:00
|
|
|
'Access-Control-Expose-Headers: MediaWiki-API-Error, Retry-After, X-Database-Lag, '
|
|
|
|
|
. 'MediaWiki-Login-Suppressed'
|
2015-09-28 11:15:17 +00:00
|
|
|
);
|
2014-12-04 14:39:50 +00:00
|
|
|
}
|
2017-06-21 18:45:56 +00:00
|
|
|
} else {
|
|
|
|
|
$response->header( 'MediaWiki-CORS-Rejection: Origin mismatch' );
|
Reimplement CORS properly, addressing Tim's concerns
Tim's concerns (listed at
https://bugzilla.wikimedia.org/show_bug.cgi?id=20814#c6) were:
* Lack of Vary: Origin breaks Squid caching
* Vary: Origin on everything would be disastrous, so add an origin param
* Origin header is space-separated list, wasn't treated as such
This commit:
* Remove CORS code from api.php and reimplement it in ApiMain.php
* Add 'origin' parameter to ApiMain
* If 'origin' parameter doesn't match Origin header, send a 403
* If origin is whitelisted, set CORS headers and set Vary: Origin
* Add https?:// to wildcard matching logic, wasn't there but is needed
CORS now works :) you can test it locally as follows:
Set $wgCrossSiteAJAXdomains[] = '*.wikipedia.org';
Log into MediaWiki on localhost
Go to Wikipedia, open a JS console, and run:
$.ajax( {
'url': 'http://localhost/w/api.php',
'data': {
'action': 'query',
'meta': 'userinfo',
'format': 'json',
'origin': 'https://en.wikipedia.org'
// or whichever domain you're on; must be correct!
},
'xhrFields': {
'withCredentials': true
},
'success': function( data ) {
alert( 'Foreign user ' + data.query.userinfo.name +
' (ID ' + data.query.userinfo.id + ')' );
}
} );
Change-Id: I725ce176866d7c81dd9ad6d7bc4a86b7160f2458
2012-06-01 12:14:47 +00:00
|
|
|
}
|
2013-11-14 12:42:04 +00:00
|
|
|
|
2016-04-08 17:05:25 +00:00
|
|
|
if ( $varyOrigin ) {
|
|
|
|
|
$this->getOutput()->addVaryHeader( 'Origin' );
|
|
|
|
|
}
|
|
|
|
|
|
Reimplement CORS properly, addressing Tim's concerns
Tim's concerns (listed at
https://bugzilla.wikimedia.org/show_bug.cgi?id=20814#c6) were:
* Lack of Vary: Origin breaks Squid caching
* Vary: Origin on everything would be disastrous, so add an origin param
* Origin header is space-separated list, wasn't treated as such
This commit:
* Remove CORS code from api.php and reimplement it in ApiMain.php
* Add 'origin' parameter to ApiMain
* If 'origin' parameter doesn't match Origin header, send a 403
* If origin is whitelisted, set CORS headers and set Vary: Origin
* Add https?:// to wildcard matching logic, wasn't there but is needed
CORS now works :) you can test it locally as follows:
Set $wgCrossSiteAJAXdomains[] = '*.wikipedia.org';
Log into MediaWiki on localhost
Go to Wikipedia, open a JS console, and run:
$.ajax( {
'url': 'http://localhost/w/api.php',
'data': {
'action': 'query',
'meta': 'userinfo',
'format': 'json',
'origin': 'https://en.wikipedia.org'
// or whichever domain you're on; must be correct!
},
'xhrFields': {
'withCredentials': true
},
'success': function( data ) {
alert( 'Foreign user ' + data.query.userinfo.name +
' (ID ' + data.query.userinfo.id + ')' );
}
} );
Change-Id: I725ce176866d7c81dd9ad6d7bc4a86b7160f2458
2012-06-01 12:14:47 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Attempt to match an Origin header against a set of rules and a set of exceptions
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $value Origin header
|
|
|
|
|
* @param array $rules Set of wildcard rules
|
|
|
|
|
* @param array $exceptions Set of wildcard rules
|
2013-11-16 19:09:17 +00:00
|
|
|
* @return bool True if $value matches a rule in $rules and doesn't match
|
|
|
|
|
* any rules in $exceptions, false otherwise
|
Reimplement CORS properly, addressing Tim's concerns
Tim's concerns (listed at
https://bugzilla.wikimedia.org/show_bug.cgi?id=20814#c6) were:
* Lack of Vary: Origin breaks Squid caching
* Vary: Origin on everything would be disastrous, so add an origin param
* Origin header is space-separated list, wasn't treated as such
This commit:
* Remove CORS code from api.php and reimplement it in ApiMain.php
* Add 'origin' parameter to ApiMain
* If 'origin' parameter doesn't match Origin header, send a 403
* If origin is whitelisted, set CORS headers and set Vary: Origin
* Add https?:// to wildcard matching logic, wasn't there but is needed
CORS now works :) you can test it locally as follows:
Set $wgCrossSiteAJAXdomains[] = '*.wikipedia.org';
Log into MediaWiki on localhost
Go to Wikipedia, open a JS console, and run:
$.ajax( {
'url': 'http://localhost/w/api.php',
'data': {
'action': 'query',
'meta': 'userinfo',
'format': 'json',
'origin': 'https://en.wikipedia.org'
// or whichever domain you're on; must be correct!
},
'xhrFields': {
'withCredentials': true
},
'success': function( data ) {
alert( 'Foreign user ' + data.query.userinfo.name +
' (ID ' + data.query.userinfo.id + ')' );
}
} );
Change-Id: I725ce176866d7c81dd9ad6d7bc4a86b7160f2458
2012-06-01 12:14:47 +00:00
|
|
|
*/
|
|
|
|
|
protected static function matchOrigin( $value, $rules, $exceptions ) {
|
|
|
|
|
foreach ( $rules as $rule ) {
|
|
|
|
|
if ( preg_match( self::wildcardToRegex( $rule ), $value ) ) {
|
|
|
|
|
// Rule matches, check exceptions
|
|
|
|
|
foreach ( $exceptions as $exc ) {
|
|
|
|
|
if ( preg_match( self::wildcardToRegex( $exc ), $value ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-14 12:42:04 +00:00
|
|
|
|
Reimplement CORS properly, addressing Tim's concerns
Tim's concerns (listed at
https://bugzilla.wikimedia.org/show_bug.cgi?id=20814#c6) were:
* Lack of Vary: Origin breaks Squid caching
* Vary: Origin on everything would be disastrous, so add an origin param
* Origin header is space-separated list, wasn't treated as such
This commit:
* Remove CORS code from api.php and reimplement it in ApiMain.php
* Add 'origin' parameter to ApiMain
* If 'origin' parameter doesn't match Origin header, send a 403
* If origin is whitelisted, set CORS headers and set Vary: Origin
* Add https?:// to wildcard matching logic, wasn't there but is needed
CORS now works :) you can test it locally as follows:
Set $wgCrossSiteAJAXdomains[] = '*.wikipedia.org';
Log into MediaWiki on localhost
Go to Wikipedia, open a JS console, and run:
$.ajax( {
'url': 'http://localhost/w/api.php',
'data': {
'action': 'query',
'meta': 'userinfo',
'format': 'json',
'origin': 'https://en.wikipedia.org'
// or whichever domain you're on; must be correct!
},
'xhrFields': {
'withCredentials': true
},
'success': function( data ) {
alert( 'Foreign user ' + data.query.userinfo.name +
' (ID ' + data.query.userinfo.id + ')' );
}
} );
Change-Id: I725ce176866d7c81dd9ad6d7bc4a86b7160f2458
2012-06-01 12:14:47 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-14 12:42:04 +00:00
|
|
|
|
Reimplement CORS properly, addressing Tim's concerns
Tim's concerns (listed at
https://bugzilla.wikimedia.org/show_bug.cgi?id=20814#c6) were:
* Lack of Vary: Origin breaks Squid caching
* Vary: Origin on everything would be disastrous, so add an origin param
* Origin header is space-separated list, wasn't treated as such
This commit:
* Remove CORS code from api.php and reimplement it in ApiMain.php
* Add 'origin' parameter to ApiMain
* If 'origin' parameter doesn't match Origin header, send a 403
* If origin is whitelisted, set CORS headers and set Vary: Origin
* Add https?:// to wildcard matching logic, wasn't there but is needed
CORS now works :) you can test it locally as follows:
Set $wgCrossSiteAJAXdomains[] = '*.wikipedia.org';
Log into MediaWiki on localhost
Go to Wikipedia, open a JS console, and run:
$.ajax( {
'url': 'http://localhost/w/api.php',
'data': {
'action': 'query',
'meta': 'userinfo',
'format': 'json',
'origin': 'https://en.wikipedia.org'
// or whichever domain you're on; must be correct!
},
'xhrFields': {
'withCredentials': true
},
'success': function( data ) {
alert( 'Foreign user ' + data.query.userinfo.name +
' (ID ' + data.query.userinfo.id + ')' );
}
} );
Change-Id: I725ce176866d7c81dd9ad6d7bc4a86b7160f2458
2012-06-01 12:14:47 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-04 14:39:50 +00:00
|
|
|
/**
|
|
|
|
|
* Attempt to validate the value of Access-Control-Request-Headers against a list
|
|
|
|
|
* of headers that we allow the follow up request to send.
|
|
|
|
|
*
|
2018-08-14 07:56:35 +00:00
|
|
|
* @param string $requestedHeaders Comma separated list of HTTP headers
|
2014-12-04 14:39:50 +00:00
|
|
|
* @return bool True if all requested headers are in the list of allowed headers
|
|
|
|
|
*/
|
|
|
|
|
protected static function matchRequestedHeaders( $requestedHeaders ) {
|
|
|
|
|
if ( trim( $requestedHeaders ) === '' ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
$requestedHeaders = explode( ',', $requestedHeaders );
|
2016-02-17 09:09:32 +00:00
|
|
|
$allowedAuthorHeaders = array_flip( [
|
2014-12-04 14:39:50 +00:00
|
|
|
/* simple headers (see spec) */
|
|
|
|
|
'accept',
|
|
|
|
|
'accept-language',
|
|
|
|
|
'content-language',
|
|
|
|
|
'content-type',
|
|
|
|
|
/* non-authorable headers in XHR, which are however requested by some UAs */
|
|
|
|
|
'accept-encoding',
|
|
|
|
|
'dnt',
|
|
|
|
|
'origin',
|
|
|
|
|
/* MediaWiki whitelist */
|
2018-12-13 17:05:33 +00:00
|
|
|
'user-agent',
|
2014-12-04 14:39:50 +00:00
|
|
|
'api-user-agent',
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2014-12-04 14:39:50 +00:00
|
|
|
foreach ( $requestedHeaders as $rHeader ) {
|
|
|
|
|
$rHeader = strtolower( trim( $rHeader ) );
|
|
|
|
|
if ( !isset( $allowedAuthorHeaders[$rHeader] ) ) {
|
2018-11-07 16:25:40 +00:00
|
|
|
LoggerFactory::getInstance( 'api-warning' )->warning(
|
|
|
|
|
'CORS preflight failed on requested header: {header}', [
|
|
|
|
|
'header' => $rHeader
|
|
|
|
|
]
|
|
|
|
|
);
|
2014-12-04 14:39:50 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
Reimplement CORS properly, addressing Tim's concerns
Tim's concerns (listed at
https://bugzilla.wikimedia.org/show_bug.cgi?id=20814#c6) were:
* Lack of Vary: Origin breaks Squid caching
* Vary: Origin on everything would be disastrous, so add an origin param
* Origin header is space-separated list, wasn't treated as such
This commit:
* Remove CORS code from api.php and reimplement it in ApiMain.php
* Add 'origin' parameter to ApiMain
* If 'origin' parameter doesn't match Origin header, send a 403
* If origin is whitelisted, set CORS headers and set Vary: Origin
* Add https?:// to wildcard matching logic, wasn't there but is needed
CORS now works :) you can test it locally as follows:
Set $wgCrossSiteAJAXdomains[] = '*.wikipedia.org';
Log into MediaWiki on localhost
Go to Wikipedia, open a JS console, and run:
$.ajax( {
'url': 'http://localhost/w/api.php',
'data': {
'action': 'query',
'meta': 'userinfo',
'format': 'json',
'origin': 'https://en.wikipedia.org'
// or whichever domain you're on; must be correct!
},
'xhrFields': {
'withCredentials': true
},
'success': function( data ) {
alert( 'Foreign user ' + data.query.userinfo.name +
' (ID ' + data.query.userinfo.id + ')' );
}
} );
Change-Id: I725ce176866d7c81dd9ad6d7bc4a86b7160f2458
2012-06-01 12:14:47 +00:00
|
|
|
/**
|
|
|
|
|
* Helper function to convert wildcard string into a regex
|
|
|
|
|
* '*' => '.*?'
|
|
|
|
|
* '?' => '.'
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $wildcard String with wildcards
|
Reimplement CORS properly, addressing Tim's concerns
Tim's concerns (listed at
https://bugzilla.wikimedia.org/show_bug.cgi?id=20814#c6) were:
* Lack of Vary: Origin breaks Squid caching
* Vary: Origin on everything would be disastrous, so add an origin param
* Origin header is space-separated list, wasn't treated as such
This commit:
* Remove CORS code from api.php and reimplement it in ApiMain.php
* Add 'origin' parameter to ApiMain
* If 'origin' parameter doesn't match Origin header, send a 403
* If origin is whitelisted, set CORS headers and set Vary: Origin
* Add https?:// to wildcard matching logic, wasn't there but is needed
CORS now works :) you can test it locally as follows:
Set $wgCrossSiteAJAXdomains[] = '*.wikipedia.org';
Log into MediaWiki on localhost
Go to Wikipedia, open a JS console, and run:
$.ajax( {
'url': 'http://localhost/w/api.php',
'data': {
'action': 'query',
'meta': 'userinfo',
'format': 'json',
'origin': 'https://en.wikipedia.org'
// or whichever domain you're on; must be correct!
},
'xhrFields': {
'withCredentials': true
},
'success': function( data ) {
alert( 'Foreign user ' + data.query.userinfo.name +
' (ID ' + data.query.userinfo.id + ')' );
}
} );
Change-Id: I725ce176866d7c81dd9ad6d7bc4a86b7160f2458
2012-06-01 12:14:47 +00:00
|
|
|
* @return string Regular expression
|
|
|
|
|
*/
|
|
|
|
|
protected static function wildcardToRegex( $wildcard ) {
|
|
|
|
|
$wildcard = preg_quote( $wildcard, '/' );
|
|
|
|
|
$wildcard = str_replace(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ '\*', '\?' ],
|
|
|
|
|
[ '.*?', '.' ],
|
Reimplement CORS properly, addressing Tim's concerns
Tim's concerns (listed at
https://bugzilla.wikimedia.org/show_bug.cgi?id=20814#c6) were:
* Lack of Vary: Origin breaks Squid caching
* Vary: Origin on everything would be disastrous, so add an origin param
* Origin header is space-separated list, wasn't treated as such
This commit:
* Remove CORS code from api.php and reimplement it in ApiMain.php
* Add 'origin' parameter to ApiMain
* If 'origin' parameter doesn't match Origin header, send a 403
* If origin is whitelisted, set CORS headers and set Vary: Origin
* Add https?:// to wildcard matching logic, wasn't there but is needed
CORS now works :) you can test it locally as follows:
Set $wgCrossSiteAJAXdomains[] = '*.wikipedia.org';
Log into MediaWiki on localhost
Go to Wikipedia, open a JS console, and run:
$.ajax( {
'url': 'http://localhost/w/api.php',
'data': {
'action': 'query',
'meta': 'userinfo',
'format': 'json',
'origin': 'https://en.wikipedia.org'
// or whichever domain you're on; must be correct!
},
'xhrFields': {
'withCredentials': true
},
'success': function( data ) {
alert( 'Foreign user ' + data.query.userinfo.name +
' (ID ' + data.query.userinfo.id + ')' );
}
} );
Change-Id: I725ce176866d7c81dd9ad6d7bc4a86b7160f2458
2012-06-01 12:14:47 +00:00
|
|
|
$wildcard
|
|
|
|
|
);
|
2013-11-14 12:42:04 +00:00
|
|
|
|
2014-12-08 15:43:50 +00:00
|
|
|
return "/^https?:\/\/$wildcard$/";
|
Reimplement CORS properly, addressing Tim's concerns
Tim's concerns (listed at
https://bugzilla.wikimedia.org/show_bug.cgi?id=20814#c6) were:
* Lack of Vary: Origin breaks Squid caching
* Vary: Origin on everything would be disastrous, so add an origin param
* Origin header is space-separated list, wasn't treated as such
This commit:
* Remove CORS code from api.php and reimplement it in ApiMain.php
* Add 'origin' parameter to ApiMain
* If 'origin' parameter doesn't match Origin header, send a 403
* If origin is whitelisted, set CORS headers and set Vary: Origin
* Add https?:// to wildcard matching logic, wasn't there but is needed
CORS now works :) you can test it locally as follows:
Set $wgCrossSiteAJAXdomains[] = '*.wikipedia.org';
Log into MediaWiki on localhost
Go to Wikipedia, open a JS console, and run:
$.ajax( {
'url': 'http://localhost/w/api.php',
'data': {
'action': 'query',
'meta': 'userinfo',
'format': 'json',
'origin': 'https://en.wikipedia.org'
// or whichever domain you're on; must be correct!
},
'xhrFields': {
'withCredentials': true
},
'success': function( data ) {
alert( 'Foreign user ' + data.query.userinfo.name +
' (ID ' + data.query.userinfo.id + ')' );
}
} );
Change-Id: I725ce176866d7c81dd9ad6d7bc4a86b7160f2458
2012-06-01 12:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-17 20:52:09 +00:00
|
|
|
/**
|
|
|
|
|
* Send caching headers
|
2016-03-24 08:44:09 +00:00
|
|
|
* @param bool $isError Whether an error response is being output
|
2015-08-17 20:52:09 +00:00
|
|
|
* @since 1.26 added $isError parameter
|
|
|
|
|
*/
|
|
|
|
|
protected function sendCacheHeaders( $isError ) {
|
2011-06-05 20:29:47 +00:00
|
|
|
$response = $this->getRequest()->response();
|
2012-06-01 11:11:32 +00:00
|
|
|
$out = $this->getOutput();
|
|
|
|
|
|
2015-05-08 14:20:30 +00:00
|
|
|
$out->addVaryHeader( 'Treat-as-Untrusted' );
|
|
|
|
|
|
2014-01-24 02:51:11 +00:00
|
|
|
$config = $this->getConfig();
|
|
|
|
|
|
|
|
|
|
if ( $config->get( 'VaryOnXFP' ) ) {
|
2012-06-01 11:11:32 +00:00
|
|
|
$out->addVaryHeader( 'X-Forwarded-Proto' );
|
|
|
|
|
}
|
2011-06-05 19:51:31 +00:00
|
|
|
|
2015-08-17 20:52:09 +00:00
|
|
|
if ( !$isError && $this->mModule &&
|
|
|
|
|
( $this->getRequest()->getMethod() === 'GET' || $this->getRequest()->getMethod() === 'HEAD' )
|
|
|
|
|
) {
|
|
|
|
|
$etag = $this->mModule->getConditionalRequestData( 'etag' );
|
|
|
|
|
if ( $etag !== null ) {
|
|
|
|
|
$response->header( "ETag: $etag" );
|
|
|
|
|
}
|
|
|
|
|
$lastMod = $this->mModule->getConditionalRequestData( 'last-modified' );
|
|
|
|
|
if ( $lastMod !== null ) {
|
|
|
|
|
$response->header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $lastMod ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-03 22:17:58 +00:00
|
|
|
// The logic should be:
|
|
|
|
|
// $this->mCacheControl['max-age'] is set?
|
|
|
|
|
// Use it, the module knows better than our guess.
|
|
|
|
|
// !$this->mModule || $this->mModule->isWriteMode(), and mCacheMode is private?
|
|
|
|
|
// Use 0 because we can guess caching is probably the wrong thing to do.
|
|
|
|
|
// Use $this->getParameter( 'maxage' ), which already defaults to 0.
|
|
|
|
|
$maxage = 0;
|
|
|
|
|
if ( isset( $this->mCacheControl['max-age'] ) ) {
|
|
|
|
|
$maxage = $this->mCacheControl['max-age'];
|
|
|
|
|
} elseif ( ( $this->mModule && !$this->mModule->isWriteMode() ) ||
|
|
|
|
|
$this->mCacheMode !== 'private'
|
|
|
|
|
) {
|
|
|
|
|
$maxage = $this->getParameter( 'maxage' );
|
|
|
|
|
}
|
|
|
|
|
$privateCache = 'private, must-revalidate, max-age=' . $maxage;
|
|
|
|
|
|
2010-07-23 07:17:56 +00:00
|
|
|
if ( $this->mCacheMode == 'private' ) {
|
2015-03-03 22:17:58 +00:00
|
|
|
$response->header( "Cache-Control: $privateCache" );
|
2010-07-23 07:17:56 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $this->mCacheMode == 'anon-public-user-private' ) {
|
2012-06-01 11:11:32 +00:00
|
|
|
$out->addVaryHeader( 'Cookie' );
|
|
|
|
|
$response->header( $out->getVaryHeader() );
|
2019-06-19 18:22:42 +00:00
|
|
|
if ( MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent() ) {
|
2010-07-23 07:17:56 +00:00
|
|
|
// Logged in or otherwise has session (e.g. anonymous users who have edited)
|
|
|
|
|
// Mark request private
|
2015-03-03 22:17:58 +00:00
|
|
|
$response->header( "Cache-Control: $privateCache" );
|
2013-11-14 12:42:04 +00:00
|
|
|
|
2010-07-23 07:17:56 +00:00
|
|
|
return;
|
2019-06-19 18:22:42 +00:00
|
|
|
} // else anonymous, send public headers below
|
2010-07-14 19:00:54 +00:00
|
|
|
}
|
2011-09-07 21:14:55 +00:00
|
|
|
|
2011-08-03 12:00:47 +00:00
|
|
|
// Send public headers
|
2012-06-01 11:11:32 +00:00
|
|
|
$response->header( $out->getVaryHeader() );
|
2007-07-15 00:52:35 +00:00
|
|
|
|
2010-02-14 23:52:45 +00:00
|
|
|
// If nobody called setCacheMaxAge(), use the (s)maxage parameters
|
|
|
|
|
if ( !isset( $this->mCacheControl['s-maxage'] ) ) {
|
|
|
|
|
$this->mCacheControl['s-maxage'] = $this->getParameter( 'smaxage' );
|
|
|
|
|
}
|
|
|
|
|
if ( !isset( $this->mCacheControl['max-age'] ) ) {
|
|
|
|
|
$this->mCacheControl['max-age'] = $this->getParameter( 'maxage' );
|
2008-06-16 20:06:23 +00:00
|
|
|
}
|
|
|
|
|
|
2010-07-23 07:17:56 +00:00
|
|
|
if ( !$this->mCacheControl['s-maxage'] && !$this->mCacheControl['max-age'] ) {
|
|
|
|
|
// Public cache not requested
|
|
|
|
|
// Sending a Vary header in this case is harmless, and protects us
|
|
|
|
|
// against conditional calls of setCacheMaxAge().
|
2015-03-03 22:17:58 +00:00
|
|
|
$response->header( "Cache-Control: $privateCache" );
|
2013-11-14 12:42:04 +00:00
|
|
|
|
2010-07-23 07:17:56 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->mCacheControl['public'] = true;
|
|
|
|
|
|
|
|
|
|
// Send an Expires header
|
|
|
|
|
$maxAge = min( $this->mCacheControl['s-maxage'], $this->mCacheControl['max-age'] );
|
|
|
|
|
$expiryUnixTime = ( $maxAge == 0 ? 1 : time() + $maxAge );
|
2011-06-05 19:51:31 +00:00
|
|
|
$response->header( 'Expires: ' . wfTimestamp( TS_RFC2822, $expiryUnixTime ) );
|
2010-02-14 23:52:45 +00:00
|
|
|
|
|
|
|
|
// Construct the Cache-Control header
|
|
|
|
|
$ccHeader = '';
|
|
|
|
|
$separator = '';
|
|
|
|
|
foreach ( $this->mCacheControl as $name => $value ) {
|
|
|
|
|
if ( is_bool( $value ) ) {
|
|
|
|
|
if ( $value ) {
|
|
|
|
|
$ccHeader .= $separator . $name;
|
2010-02-15 21:34:31 +00:00
|
|
|
$separator = ', ';
|
2010-02-14 23:52:45 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$ccHeader .= $separator . "$name=$value";
|
2010-02-15 21:34:31 +00:00
|
|
|
$separator = ', ';
|
2010-02-14 23:52:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-02-23 18:05:46 +00:00
|
|
|
|
2011-06-05 19:51:31 +00:00
|
|
|
$response->header( "Cache-Control: $ccHeader" );
|
2007-07-15 00:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-04-20 16:09:24 +00:00
|
|
|
* Create the printer for error output
|
2007-07-15 00:52:35 +00:00
|
|
|
*/
|
2015-04-20 16:09:24 +00:00
|
|
|
private function createErrorPrinter() {
|
2013-04-26 12:18:06 +00:00
|
|
|
if ( !isset( $this->mPrinter ) ) {
|
2010-02-15 23:56:09 +00:00
|
|
|
$value = $this->getRequest()->getVal( 'format', self::API_DEFAULT_FORMAT );
|
2013-02-05 06:52:55 +00:00
|
|
|
if ( !$this->mModuleMgr->isDefined( $value, 'format' ) ) {
|
2010-02-15 23:56:09 +00:00
|
|
|
$value = self::API_DEFAULT_FORMAT;
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2010-02-15 23:56:09 +00:00
|
|
|
$this->mPrinter = $this->createPrinterByName( $value );
|
|
|
|
|
}
|
2006-10-22 23:45:20 +00:00
|
|
|
|
2014-03-27 15:11:17 +00:00
|
|
|
// Printer may not be able to handle errors. This is particularly
|
|
|
|
|
// likely if the module returns something for getCustomPrinter().
|
|
|
|
|
if ( !$this->mPrinter->canPrintErrors() ) {
|
|
|
|
|
$this->mPrinter = $this->createPrinterByName( self::API_DEFAULT_FORMAT );
|
|
|
|
|
}
|
2015-04-20 16:09:24 +00:00
|
|
|
}
|
2014-03-27 15:11:17 +00:00
|
|
|
|
2015-04-20 16:09:24 +00:00
|
|
|
/**
|
2016-02-18 21:18:14 +00:00
|
|
|
* Create an error message for the given exception.
|
|
|
|
|
*
|
2016-10-19 16:54:25 +00:00
|
|
|
* If an ApiUsageException, errors/warnings will be extracted from the
|
|
|
|
|
* embedded StatusValue.
|
|
|
|
|
*
|
|
|
|
|
* Any other exception will be returned with a generic code and wrapper
|
|
|
|
|
* text around the exception's (presumably English) message as a single
|
|
|
|
|
* error (no warnings).
|
2016-02-18 21:18:14 +00:00
|
|
|
*
|
2018-08-21 15:19:15 +00:00
|
|
|
* @param Exception|Throwable $e
|
2016-10-19 16:54:25 +00:00
|
|
|
* @param string $type 'error' or 'warning'
|
|
|
|
|
* @return ApiMessage[]
|
2016-02-18 21:18:14 +00:00
|
|
|
* @since 1.27
|
2015-04-20 16:09:24 +00:00
|
|
|
*/
|
2016-10-19 16:54:25 +00:00
|
|
|
protected function errorMessagesFromException( $e, $type = 'error' ) {
|
|
|
|
|
$messages = [];
|
|
|
|
|
if ( $e instanceof ApiUsageException ) {
|
|
|
|
|
foreach ( $e->getStatusValue()->getErrorsByType( $type ) as $error ) {
|
|
|
|
|
$messages[] = ApiMessage::create( $error );
|
|
|
|
|
}
|
|
|
|
|
} elseif ( $type !== 'error' ) {
|
|
|
|
|
// None of the rest have any messages for non-error types
|
2010-02-15 23:56:09 +00:00
|
|
|
} else {
|
|
|
|
|
// Something is seriously wrong
|
2016-10-19 16:54:25 +00:00
|
|
|
$config = $this->getConfig();
|
2018-11-07 16:25:40 +00:00
|
|
|
// TODO: Avoid embedding arbitrary class names in the error code.
|
2017-02-24 16:17:16 +00:00
|
|
|
$class = preg_replace( '#^Wikimedia\\\Rdbms\\\#', '', get_class( $e ) );
|
|
|
|
|
$code = 'internal_api_error_' . $class;
|
2018-11-26 18:31:41 +00:00
|
|
|
$data = [ 'errorclass' => get_class( $e ) ];
|
2018-07-17 16:51:36 +00:00
|
|
|
if ( $config->get( 'ShowExceptionDetails' ) ) {
|
2017-02-24 16:20:07 +00:00
|
|
|
if ( $e instanceof ILocalizedException ) {
|
|
|
|
|
$msg = $e->getMessageObject();
|
|
|
|
|
} elseif ( $e instanceof MessageSpecifier ) {
|
|
|
|
|
$msg = Message::newFromSpecifier( $e );
|
|
|
|
|
} else {
|
|
|
|
|
$msg = wfEscapeWikiText( $e->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
$params = [ 'apierror-exceptioncaught', WebRequest::getRequestId(), $msg ];
|
2018-07-17 16:51:36 +00:00
|
|
|
} else {
|
|
|
|
|
$params = [ 'apierror-exceptioncaughttype', WebRequest::getRequestId(), get_class( $e ) ];
|
2006-10-14 07:18:08 +00:00
|
|
|
}
|
2018-07-17 16:51:36 +00:00
|
|
|
|
2018-11-26 18:31:41 +00:00
|
|
|
$messages[] = ApiMessage::create( $params, $code, $data );
|
2016-02-18 21:18:14 +00:00
|
|
|
}
|
2016-10-19 16:54:25 +00:00
|
|
|
return $messages;
|
2016-02-18 21:18:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Replace the result data with the information about an exception.
|
2018-08-21 15:19:15 +00:00
|
|
|
* @param Exception|Throwable $e
|
2016-10-19 16:54:25 +00:00
|
|
|
* @return string[] Error codes
|
2016-02-18 21:18:14 +00:00
|
|
|
*/
|
|
|
|
|
protected function substituteResultWithError( $e ) {
|
|
|
|
|
$result = $this->getResult();
|
2016-10-19 16:54:25 +00:00
|
|
|
$formatter = $this->getErrorFormatter();
|
2016-02-18 21:18:14 +00:00
|
|
|
$config = $this->getConfig();
|
2016-10-19 16:54:25 +00:00
|
|
|
$errorCodes = [];
|
2016-02-18 21:18:14 +00:00
|
|
|
|
2016-10-19 16:54:25 +00:00
|
|
|
// Remember existing warnings and errors across the reset
|
|
|
|
|
$errors = $result->getResultData( [ 'errors' ] );
|
|
|
|
|
$warnings = $result->getResultData( [ 'warnings' ] );
|
|
|
|
|
$result->reset();
|
|
|
|
|
if ( $warnings !== null ) {
|
|
|
|
|
$result->addValue( null, 'warnings', $warnings, ApiResult::NO_SIZE_CHECK );
|
|
|
|
|
}
|
|
|
|
|
if ( $errors !== null ) {
|
|
|
|
|
$result->addValue( null, 'errors', $errors, ApiResult::NO_SIZE_CHECK );
|
|
|
|
|
|
|
|
|
|
// Collect the copied error codes for the return value
|
|
|
|
|
foreach ( $errors as $error ) {
|
|
|
|
|
if ( isset( $error['code'] ) ) {
|
|
|
|
|
$errorCodes[$error['code']] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add errors from the exception
|
|
|
|
|
$modulePath = $e instanceof ApiUsageException ? $e->getModulePath() : null;
|
|
|
|
|
foreach ( $this->errorMessagesFromException( $e, 'error' ) as $msg ) {
|
2018-11-07 16:25:40 +00:00
|
|
|
if ( ApiErrorFormatter::isValidApiCode( $msg->getApiCode() ) ) {
|
|
|
|
|
$errorCodes[$msg->getApiCode()] = true;
|
|
|
|
|
} else {
|
|
|
|
|
LoggerFactory::getInstance( 'api-warning' )->error( 'Invalid API error code "{code}"', [
|
|
|
|
|
'code' => $msg->getApiCode(),
|
|
|
|
|
'exception' => $e,
|
|
|
|
|
] );
|
|
|
|
|
$errorCodes['<invalid-code>'] = true;
|
|
|
|
|
}
|
2016-10-19 16:54:25 +00:00
|
|
|
$formatter->addError( $modulePath, $msg );
|
|
|
|
|
}
|
|
|
|
|
foreach ( $this->errorMessagesFromException( $e, 'warning' ) as $msg ) {
|
|
|
|
|
$formatter->addWarning( $modulePath, $msg );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add additional data. Path depends on whether we're in BC mode or not.
|
|
|
|
|
// Data depends on the type of exception.
|
|
|
|
|
if ( $formatter instanceof ApiErrorFormatter_BackCompat ) {
|
|
|
|
|
$path = [ 'error' ];
|
|
|
|
|
} else {
|
|
|
|
|
$path = null;
|
|
|
|
|
}
|
2018-10-10 17:55:09 +00:00
|
|
|
if ( $e instanceof ApiUsageException ) {
|
2016-02-18 21:18:14 +00:00
|
|
|
$link = wfExpandUrl( wfScript( 'api' ) );
|
2016-10-19 16:54:25 +00:00
|
|
|
$result->addContentValue(
|
|
|
|
|
$path,
|
|
|
|
|
'docref',
|
2017-01-09 23:38:36 +00:00
|
|
|
trim(
|
|
|
|
|
$this->msg( 'api-usage-docref', $link )->inLanguage( $formatter->getLanguage() )->text()
|
|
|
|
|
. ' '
|
|
|
|
|
. $this->msg( 'api-usage-mailinglist-ref' )->inLanguage( $formatter->getLanguage() )->text()
|
|
|
|
|
)
|
2016-10-19 16:54:25 +00:00
|
|
|
);
|
2019-03-29 20:12:24 +00:00
|
|
|
} elseif ( $config->get( 'ShowExceptionDetails' ) ) {
|
|
|
|
|
$result->addContentValue(
|
|
|
|
|
$path,
|
|
|
|
|
'trace',
|
|
|
|
|
$this->msg( 'api-exception-trace',
|
|
|
|
|
get_class( $e ),
|
|
|
|
|
$e->getFile(),
|
|
|
|
|
$e->getLine(),
|
|
|
|
|
MWExceptionHandler::getRedactedTraceAsString( $e )
|
|
|
|
|
)->inLanguage( $formatter->getLanguage() )->text()
|
|
|
|
|
);
|
2010-02-15 23:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-19 16:54:25 +00:00
|
|
|
// Add the id and such
|
|
|
|
|
$this->addRequestedFields( [ 'servedby' ] );
|
2013-01-18 06:45:43 +00:00
|
|
|
|
2016-10-19 16:54:25 +00:00
|
|
|
return array_keys( $errorCodes );
|
2006-10-01 04:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
2006-10-14 07:18:08 +00:00
|
|
|
/**
|
2016-10-19 16:54:25 +00:00
|
|
|
* Add requested fields to the result
|
|
|
|
|
* @param string[] $force Which fields to force even if not requested. Accepted values are:
|
|
|
|
|
* - servedby
|
2006-10-14 07:18:08 +00:00
|
|
|
*/
|
2016-10-19 16:54:25 +00:00
|
|
|
protected function addRequestedFields( $force = [] ) {
|
2011-06-30 01:06:17 +00:00
|
|
|
$result = $this->getResult();
|
2016-10-19 16:54:25 +00:00
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
$requestid = $this->getParameter( 'requestid' );
|
2016-10-19 16:54:25 +00:00
|
|
|
if ( $requestid !== null ) {
|
|
|
|
|
$result->addValue( null, 'requestid', $requestid, ApiResult::NO_SIZE_CHECK );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2011-10-26 19:39:56 +00:00
|
|
|
|
2016-10-19 16:54:25 +00:00
|
|
|
if ( $this->getConfig()->get( 'ShowHostnames' ) && (
|
|
|
|
|
in_array( 'servedby', $force, true ) || $this->getParameter( 'servedby' )
|
|
|
|
|
) ) {
|
|
|
|
|
$result->addValue( null, 'servedby', wfHostname(), ApiResult::NO_SIZE_CHECK );
|
2010-07-10 10:37:39 +00:00
|
|
|
}
|
2008-01-08 18:10:58 +00:00
|
|
|
|
2014-08-08 16:56:07 +00:00
|
|
|
if ( $this->getParameter( 'curtimestamp' ) ) {
|
|
|
|
|
$result->addValue( null, 'curtimestamp', wfTimestamp( TS_ISO_8601, time() ),
|
|
|
|
|
ApiResult::NO_SIZE_CHECK );
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-19 16:54:25 +00:00
|
|
|
if ( $this->getParameter( 'responselanginfo' ) ) {
|
|
|
|
|
$result->addValue( null, 'uselang', $this->getLanguage()->getCode(),
|
|
|
|
|
ApiResult::NO_SIZE_CHECK );
|
|
|
|
|
$result->addValue( null, 'errorlang', $this->getErrorFormatter()->getLanguage()->getCode(),
|
|
|
|
|
ApiResult::NO_SIZE_CHECK );
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-02-22 03:34:56 +00:00
|
|
|
|
2016-10-19 16:54:25 +00:00
|
|
|
/**
|
|
|
|
|
* Set up for the execution.
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function setupExecuteAction() {
|
|
|
|
|
$this->addRequestedFields();
|
2008-06-20 10:51:17 +00:00
|
|
|
|
2016-10-19 16:54:25 +00:00
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
$this->mAction = $params['action'];
|
2010-02-23 18:05:46 +00:00
|
|
|
|
2010-03-30 17:14:53 +00:00
|
|
|
return $params;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set up the module for response
|
2010-09-28 01:33:11 +00:00
|
|
|
* @return ApiBase The module that will handle this action
|
2014-12-24 13:49:20 +00:00
|
|
|
* @throws MWException
|
2016-10-19 16:54:25 +00:00
|
|
|
* @throws ApiUsageException
|
2010-03-30 17:14:53 +00:00
|
|
|
*/
|
|
|
|
|
protected function setupModule() {
|
2006-10-14 07:18:08 +00:00
|
|
|
// Instantiate the module requested by the user
|
2013-02-05 06:52:55 +00:00
|
|
|
$module = $this->mModuleMgr->getModule( $this->mAction, 'action' );
|
|
|
|
|
if ( $module === null ) {
|
2018-03-26 14:38:41 +00:00
|
|
|
// Probably can't happen
|
|
|
|
|
// @codeCoverageIgnoreStart
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError(
|
|
|
|
|
[ 'apierror-unknownaction', wfEscapeWikiText( $this->mAction ) ], 'unknown_action'
|
|
|
|
|
);
|
2018-03-26 14:38:41 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2013-02-05 06:52:55 +00:00
|
|
|
}
|
2010-02-15 23:53:43 +00:00
|
|
|
$moduleParams = $module->extractRequestParams();
|
2010-02-23 18:05:46 +00:00
|
|
|
|
2014-08-08 16:56:07 +00:00
|
|
|
// Check token, if necessary
|
|
|
|
|
if ( $module->needsToken() === true ) {
|
|
|
|
|
throw new MWException(
|
|
|
|
|
"Module '{$module->getModuleName()}' must be updated for the new token handling. " .
|
2016-03-08 08:04:45 +00:00
|
|
|
'See documentation for ApiBase::needsToken for details.'
|
2014-08-08 16:56:07 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if ( $module->needsToken() ) {
|
|
|
|
|
if ( !$module->mustBePosted() ) {
|
|
|
|
|
throw new MWException(
|
|
|
|
|
"Module '{$module->getModuleName()}' must require POST to use tokens."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-19 19:44:28 +00:00
|
|
|
if ( !isset( $moduleParams['token'] ) ) {
|
2018-03-26 14:38:41 +00:00
|
|
|
// Probably can't happen
|
|
|
|
|
// @codeCoverageIgnoreStart
|
2016-10-19 16:54:25 +00:00
|
|
|
$module->dieWithError( [ 'apierror-missingparam', 'token' ] );
|
2018-03-26 14:38:41 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2013-11-17 19:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-18 17:36:11 +00:00
|
|
|
$module->requirePostedParameters( [ 'token' ] );
|
2014-08-08 16:56:07 +00:00
|
|
|
|
|
|
|
|
if ( !$module->validateToken( $moduleParams['token'], $moduleParams ) ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$module->dieWithError( 'apierror-badtoken' );
|
2010-02-15 23:53:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-11-14 12:42:04 +00:00
|
|
|
|
2010-03-30 17:14:53 +00:00
|
|
|
return $module;
|
|
|
|
|
}
|
2010-02-14 22:20:27 +00:00
|
|
|
|
2017-04-10 06:54:01 +00:00
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function getMaxLag() {
|
|
|
|
|
$dbLag = MediaWikiServices::getInstance()->getDBLoadBalancer()->getMaxLag();
|
|
|
|
|
$lagInfo = [
|
|
|
|
|
'host' => $dbLag[0],
|
|
|
|
|
'lag' => $dbLag[1],
|
|
|
|
|
'type' => 'db'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$jobQueueLagFactor = $this->getConfig()->get( 'JobQueueIncludeInMaxLagFactor' );
|
|
|
|
|
if ( $jobQueueLagFactor ) {
|
|
|
|
|
// Turn total number of jobs into seconds by using the configured value
|
|
|
|
|
$totalJobs = array_sum( JobQueueGroup::singleton()->getQueueSizes() );
|
|
|
|
|
$jobQueueLag = $totalJobs / (float)$jobQueueLagFactor;
|
|
|
|
|
if ( $jobQueueLag > $lagInfo['lag'] ) {
|
|
|
|
|
$lagInfo = [
|
|
|
|
|
'host' => wfHostname(), // XXX: Is there a better value that could be used?
|
|
|
|
|
'lag' => $jobQueueLag,
|
|
|
|
|
'type' => 'jobqueue',
|
|
|
|
|
'jobs' => $totalJobs,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-31 14:03:47 +00:00
|
|
|
Hooks::runWithoutAbort( 'ApiMaxLagInfo', [ &$lagInfo ] );
|
|
|
|
|
|
2017-04-10 06:54:01 +00:00
|
|
|
return $lagInfo;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-30 17:14:53 +00:00
|
|
|
/**
|
|
|
|
|
* Check the max lag if necessary
|
2014-04-15 18:12:09 +00:00
|
|
|
* @param ApiBase $module Api module being used
|
|
|
|
|
* @param array $params Array an array containing the request parameters.
|
|
|
|
|
* @return bool True on success, false should exit immediately
|
2010-03-30 17:14:53 +00:00
|
|
|
*/
|
2010-04-17 20:58:04 +00:00
|
|
|
protected function checkMaxLag( $module, $params ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
if ( $module->shouldCheckMaxlag() && isset( $params['maxlag'] ) ) {
|
2007-11-19 15:57:58 +00:00
|
|
|
$maxLag = $params['maxlag'];
|
2017-04-10 06:54:01 +00:00
|
|
|
$lagInfo = $this->getMaxLag();
|
|
|
|
|
if ( $lagInfo['lag'] > $maxLag ) {
|
2011-06-05 20:29:47 +00:00
|
|
|
$response = $this->getRequest()->response();
|
2011-06-05 19:51:31 +00:00
|
|
|
|
2019-02-25 00:38:33 +00:00
|
|
|
$response->header( 'Retry-After: ' . max( (int)$maxLag, 5 ) );
|
|
|
|
|
$response->header( 'X-Database-Lag: ' . (int)$lagInfo['lag'] );
|
2011-06-05 19:51:31 +00:00
|
|
|
|
2014-01-24 02:51:11 +00:00
|
|
|
if ( $this->getConfig()->get( 'ShowHostnames' ) ) {
|
2017-04-10 06:54:01 +00:00
|
|
|
$this->dieWithError(
|
|
|
|
|
[ 'apierror-maxlag', $lagInfo['lag'], $lagInfo['host'] ],
|
|
|
|
|
'maxlag',
|
|
|
|
|
$lagInfo
|
|
|
|
|
);
|
2007-11-19 15:57:58 +00:00
|
|
|
}
|
2013-11-14 12:42:04 +00:00
|
|
|
|
2017-04-10 06:54:01 +00:00
|
|
|
$this->dieWithError( [ 'apierror-maxlag-generic', $lagInfo['lag'] ], 'maxlag', $lagInfo );
|
2007-11-19 15:57:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-11-14 12:42:04 +00:00
|
|
|
|
2010-03-30 17:14:53 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-17 20:52:09 +00:00
|
|
|
/**
|
|
|
|
|
* Check selected RFC 7232 precondition headers
|
|
|
|
|
*
|
|
|
|
|
* RFC 7232 envisions a particular model where you send your request to "a
|
|
|
|
|
* resource", and for write requests that you can read "the resource" by
|
|
|
|
|
* changing the method to GET. When the API receives a GET request, it
|
|
|
|
|
* works out even though "the resource" from RFC 7232's perspective might
|
|
|
|
|
* be many resources from MediaWiki's perspective. But it totally fails for
|
|
|
|
|
* a POST, since what HTTP sees as "the resource" is probably just
|
|
|
|
|
* "/api.php" with all the interesting bits in the body.
|
|
|
|
|
*
|
|
|
|
|
* Therefore, we only support RFC 7232 precondition headers for GET (and
|
|
|
|
|
* HEAD). That means we don't need to bother with If-Match and
|
|
|
|
|
* If-Unmodified-Since since they only apply to modification requests.
|
|
|
|
|
*
|
|
|
|
|
* And since we don't support Range, If-Range is ignored too.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.26
|
|
|
|
|
* @param ApiBase $module Api module being used
|
|
|
|
|
* @return bool True on success, false should exit immediately
|
|
|
|
|
*/
|
|
|
|
|
protected function checkConditionalRequestHeaders( $module ) {
|
|
|
|
|
if ( $this->mInternalMode ) {
|
|
|
|
|
// No headers to check in internal mode
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $this->getRequest()->getMethod() !== 'GET' && $this->getRequest()->getMethod() !== 'HEAD' ) {
|
|
|
|
|
// Don't check POSTs
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$return304 = false;
|
|
|
|
|
|
|
|
|
|
$ifNoneMatch = array_diff(
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->getRequest()->getHeader( 'If-None-Match', WebRequest::GETHEADER_LIST ) ?: [],
|
|
|
|
|
[ '' ]
|
2015-08-17 20:52:09 +00:00
|
|
|
);
|
|
|
|
|
if ( $ifNoneMatch ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
if ( $ifNoneMatch === [ '*' ] ) {
|
2015-08-17 20:52:09 +00:00
|
|
|
// API responses always "exist"
|
|
|
|
|
$etag = '*';
|
|
|
|
|
} else {
|
|
|
|
|
$etag = $module->getConditionalRequestData( 'etag' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $ifNoneMatch && $etag !== null ) {
|
|
|
|
|
$test = substr( $etag, 0, 2 ) === 'W/' ? substr( $etag, 2 ) : $etag;
|
|
|
|
|
$match = array_map( function ( $s ) {
|
|
|
|
|
return substr( $s, 0, 2 ) === 'W/' ? substr( $s, 2 ) : $s;
|
|
|
|
|
}, $ifNoneMatch );
|
|
|
|
|
$return304 = in_array( $test, $match, true );
|
|
|
|
|
} else {
|
|
|
|
|
$value = trim( $this->getRequest()->getHeader( 'If-Modified-Since' ) );
|
|
|
|
|
|
|
|
|
|
// Some old browsers sends sizes after the date, like this:
|
|
|
|
|
// Wed, 20 Aug 2003 06:51:19 GMT; length=5202
|
|
|
|
|
// Ignore that.
|
|
|
|
|
$i = strpos( $value, ';' );
|
|
|
|
|
if ( $i !== false ) {
|
|
|
|
|
$value = trim( substr( $value, 0, $i ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $value !== '' ) {
|
|
|
|
|
try {
|
|
|
|
|
$ts = new MWTimestamp( $value );
|
|
|
|
|
if (
|
|
|
|
|
// RFC 7231 IMF-fixdate
|
|
|
|
|
$ts->getTimestamp( TS_RFC2822 ) === $value ||
|
|
|
|
|
// RFC 850
|
|
|
|
|
$ts->format( 'l, d-M-y H:i:s' ) . ' GMT' === $value ||
|
|
|
|
|
// asctime (with and without space-padded day)
|
|
|
|
|
$ts->format( 'D M j H:i:s Y' ) === $value ||
|
|
|
|
|
$ts->format( 'D M j H:i:s Y' ) === $value
|
|
|
|
|
) {
|
2017-11-01 20:55:24 +00:00
|
|
|
$config = $this->getConfig();
|
2015-08-17 20:52:09 +00:00
|
|
|
$lastMod = $module->getConditionalRequestData( 'last-modified' );
|
|
|
|
|
if ( $lastMod !== null ) {
|
|
|
|
|
// Mix in some MediaWiki modification times
|
2016-02-17 09:09:32 +00:00
|
|
|
$modifiedTimes = [
|
2015-08-17 20:52:09 +00:00
|
|
|
'page' => $lastMod,
|
|
|
|
|
'user' => $this->getUser()->getTouched(),
|
2017-11-01 20:55:24 +00:00
|
|
|
'epoch' => $config->get( 'CacheEpoch' ),
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2017-11-01 20:55:24 +00:00
|
|
|
|
|
|
|
|
if ( $config->get( 'UseCdn' ) ) {
|
2015-08-17 20:52:09 +00:00
|
|
|
// T46570: the core page itself may not change, but resources might
|
|
|
|
|
$modifiedTimes['sepoch'] = wfTimestamp(
|
2017-11-01 20:55:24 +00:00
|
|
|
TS_MW, time() - $config->get( 'CdnMaxAge' )
|
2015-08-17 20:52:09 +00:00
|
|
|
);
|
|
|
|
|
}
|
2016-05-13 23:01:26 +00:00
|
|
|
Hooks::run( 'OutputPageCheckLastModified', [ &$modifiedTimes, $this->getOutput() ] );
|
2015-08-17 20:52:09 +00:00
|
|
|
$lastMod = max( $modifiedTimes );
|
|
|
|
|
$return304 = wfTimestamp( TS_MW, $lastMod ) <= $ts->getTimestamp( TS_MW );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch ( TimestampException $e ) {
|
|
|
|
|
// Invalid timestamp, ignore it
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $return304 ) {
|
|
|
|
|
$this->getRequest()->response()->statusHeader( 304 );
|
|
|
|
|
|
|
|
|
|
// Avoid outputting the compressed representation of a zero-length body
|
2018-02-10 07:52:26 +00:00
|
|
|
Wikimedia\suppressWarnings();
|
2015-08-17 20:52:09 +00:00
|
|
|
ini_set( 'zlib.output_compression', 0 );
|
2018-02-10 07:52:26 +00:00
|
|
|
Wikimedia\restoreWarnings();
|
2015-08-17 20:52:09 +00:00
|
|
|
wfClearOutputBuffers();
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-30 17:14:53 +00:00
|
|
|
/**
|
|
|
|
|
* Check for sufficient permissions to execute
|
2014-04-15 18:12:09 +00:00
|
|
|
* @param ApiBase $module An Api module
|
2010-03-30 17:14:53 +00:00
|
|
|
*/
|
2010-04-17 20:58:04 +00:00
|
|
|
protected function checkExecutePermissions( $module ) {
|
2011-10-26 23:27:01 +00:00
|
|
|
$user = $this->getUser();
|
2013-07-12 15:06:41 +00:00
|
|
|
if ( $module->isReadMode() && !User::isEveryoneAllowed( 'read' ) &&
|
2013-11-14 12:42:04 +00:00
|
|
|
!$user->isAllowed( 'read' )
|
|
|
|
|
) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( 'apierror-readapidenied' );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2015-10-02 00:57:07 +00:00
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
if ( $module->isWriteMode() ) {
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( !$this->mEnableWrite ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( 'apierror-noapiwrite' );
|
2015-10-25 06:45:02 +00:00
|
|
|
} elseif ( !$user->isAllowed( 'writeapi' ) ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( 'apierror-writeapidenied' );
|
2015-10-02 00:57:07 +00:00
|
|
|
} elseif ( $this->getRequest()->getHeader( 'Promise-Non-Write-API-Action' ) ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( 'apierror-promised-nonwrite-api' );
|
2015-10-25 06:45:02 +00:00
|
|
|
}
|
2015-10-02 00:57:07 +00:00
|
|
|
|
|
|
|
|
$this->checkReadOnly( $module );
|
2009-03-06 13:49:44 +00:00
|
|
|
}
|
2012-08-21 15:52:47 +00:00
|
|
|
|
|
|
|
|
// Allow extensions to stop execution for arbitrary reasons.
|
2018-03-26 14:38:41 +00:00
|
|
|
$message = 'hookaborted';
|
2016-02-17 09:09:32 +00:00
|
|
|
if ( !Hooks::run( 'ApiCheckCanExecute', [ $module, $user, &$message ] ) ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( $message );
|
2012-08-21 15:52:47 +00:00
|
|
|
}
|
2010-03-30 17:14:53 +00:00
|
|
|
}
|
2009-03-06 13:49:44 +00:00
|
|
|
|
2015-10-02 00:57:07 +00:00
|
|
|
/**
|
|
|
|
|
* Check if the DB is read-only for this user
|
|
|
|
|
* @param ApiBase $module An Api module
|
|
|
|
|
*/
|
|
|
|
|
protected function checkReadOnly( $module ) {
|
|
|
|
|
if ( wfReadOnly() ) {
|
|
|
|
|
$this->dieReadOnly();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $module->isWriteMode()
|
2016-09-18 21:01:42 +00:00
|
|
|
&& $this->getUser()->isBot()
|
2018-01-04 16:38:22 +00:00
|
|
|
&& MediaWikiServices::getInstance()->getDBLoadBalancer()->getServerCount() > 1
|
2015-10-02 00:57:07 +00:00
|
|
|
) {
|
2016-01-17 17:03:25 +00:00
|
|
|
$this->checkBotReadOnly();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check whether we are readonly for bots
|
|
|
|
|
*/
|
|
|
|
|
private function checkBotReadOnly() {
|
|
|
|
|
// Figure out how many servers have passed the lag threshold
|
|
|
|
|
$numLagged = 0;
|
|
|
|
|
$lagLimit = $this->getConfig()->get( 'APIMaxLagThreshold' );
|
2016-02-17 09:09:32 +00:00
|
|
|
$laggedServers = [];
|
2018-01-04 16:38:22 +00:00
|
|
|
$loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer();
|
2016-01-17 17:03:25 +00:00
|
|
|
foreach ( $loadBalancer->getLagTimes() as $serverIndex => $lag ) {
|
|
|
|
|
if ( $lag > $lagLimit ) {
|
|
|
|
|
++$numLagged;
|
|
|
|
|
$laggedServers[] = $loadBalancer->getServerName( $serverIndex ) . " ({$lag}s)";
|
2015-10-02 00:57:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-01-17 17:03:25 +00:00
|
|
|
|
2016-09-05 20:21:26 +00:00
|
|
|
// If a majority of replica DBs are too lagged then disallow writes
|
2018-01-04 16:38:22 +00:00
|
|
|
$replicaCount = $loadBalancer->getServerCount() - 1;
|
2016-09-06 00:09:08 +00:00
|
|
|
if ( $numLagged >= ceil( $replicaCount / 2 ) ) {
|
2016-03-08 08:13:12 +00:00
|
|
|
$laggedServers = implode( ', ', $laggedServers );
|
2016-01-17 17:03:25 +00:00
|
|
|
wfDebugLog(
|
2018-11-07 16:25:40 +00:00
|
|
|
'api-readonly', // Deprecate this channel in favor of api-warning?
|
2016-01-17 17:03:25 +00:00
|
|
|
"Api request failed as read only because the following DBs are lagged: $laggedServers"
|
|
|
|
|
);
|
2018-11-07 16:25:40 +00:00
|
|
|
LoggerFactory::getInstance( 'api-warning' )->warning(
|
|
|
|
|
"Api request failed as read only because the following DBs are lagged: {laggeddbs}", [
|
|
|
|
|
'laggeddbs' => $laggedServers,
|
|
|
|
|
]
|
|
|
|
|
);
|
2016-01-17 17:03:25 +00:00
|
|
|
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError(
|
|
|
|
|
'readonly_lag',
|
|
|
|
|
'readonly',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'readonlyreason' => "Waiting for $numLagged lagged database(s)" ]
|
2016-01-17 17:03:25 +00:00
|
|
|
);
|
|
|
|
|
}
|
2015-10-02 00:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-16 22:27:59 +00:00
|
|
|
/**
|
|
|
|
|
* Check asserts of the user's rights
|
2014-04-15 18:12:09 +00:00
|
|
|
* @param array $params
|
2013-10-16 22:27:59 +00:00
|
|
|
*/
|
|
|
|
|
protected function checkAsserts( $params ) {
|
|
|
|
|
if ( isset( $params['assert'] ) ) {
|
|
|
|
|
$user = $this->getUser();
|
|
|
|
|
switch ( $params['assert'] ) {
|
|
|
|
|
case 'user':
|
|
|
|
|
if ( $user->isAnon() ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( 'apierror-assertuserfailed' );
|
2013-10-16 22:27:59 +00:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'bot':
|
|
|
|
|
if ( !$user->isAllowed( 'bot' ) ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( 'apierror-assertbotfailed' );
|
2013-10-16 22:27:59 +00:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-05 14:35:47 +00:00
|
|
|
if ( isset( $params['assertuser'] ) ) {
|
|
|
|
|
$assertUser = User::newFromName( $params['assertuser'], false );
|
|
|
|
|
if ( !$assertUser || !$this->getUser()->equals( $assertUser ) ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError(
|
|
|
|
|
[ 'apierror-assertnameduserfailed', wfEscapeWikiText( $params['assertuser'] ) ]
|
2016-10-05 14:35:47 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-16 22:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
2010-03-30 17:14:53 +00:00
|
|
|
/**
|
|
|
|
|
* Check POST for external response and setup result printer
|
2014-04-15 18:12:09 +00:00
|
|
|
* @param ApiBase $module An Api module
|
2014-07-24 17:42:45 +00:00
|
|
|
* @param array $params An array with the request parameters
|
2010-03-30 17:14:53 +00:00
|
|
|
*/
|
2010-04-17 20:58:04 +00:00
|
|
|
protected function setupExternalResponse( $module, $params ) {
|
2018-11-17 07:47:16 +00:00
|
|
|
$validMethods = [ 'GET', 'HEAD', 'POST', 'OPTIONS' ];
|
2016-01-28 00:53:54 +00:00
|
|
|
$request = $this->getRequest();
|
2018-11-17 07:47:16 +00:00
|
|
|
|
|
|
|
|
if ( !in_array( $request->getMethod(), $validMethods ) ) {
|
|
|
|
|
$this->dieWithError( 'apierror-invalidmethod', null, null, 405 );
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-28 00:53:54 +00:00
|
|
|
if ( !$request->wasPosted() && $module->mustBePosted() ) {
|
2013-01-18 15:23:17 +00:00
|
|
|
// Module requires POST. GET request might still be allowed
|
|
|
|
|
// if $wgDebugApi is true, otherwise fail.
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithErrorOrDebug( [ 'apierror-mustbeposted', $this->mAction ] );
|
2010-03-30 17:14:53 +00:00
|
|
|
}
|
2006-10-22 23:45:20 +00:00
|
|
|
|
2010-03-30 17:14:53 +00:00
|
|
|
// See if custom printer is used
|
|
|
|
|
$this->mPrinter = $module->getCustomPrinter();
|
|
|
|
|
if ( is_null( $this->mPrinter ) ) {
|
|
|
|
|
// Create an appropriate printer
|
|
|
|
|
$this->mPrinter = $this->createPrinterByName( $params['format'] );
|
|
|
|
|
}
|
2016-01-28 00:53:54 +00:00
|
|
|
|
|
|
|
|
if ( $request->getProtocol() === 'http' && (
|
2016-02-01 20:44:03 +00:00
|
|
|
$request->getSession()->shouldForceHTTPS() ||
|
2016-01-28 00:53:54 +00:00
|
|
|
( $this->getUser()->isLoggedIn() &&
|
|
|
|
|
$this->getUser()->requiresHTTPS() )
|
|
|
|
|
) ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->addDeprecation( 'apiwarn-deprecation-httpsexpected', 'https-expected' );
|
2016-01-28 00:53:54 +00:00
|
|
|
}
|
2010-03-30 17:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the actual module, without any error handling
|
|
|
|
|
*/
|
|
|
|
|
protected function executeAction() {
|
|
|
|
|
$params = $this->setupExecuteAction();
|
2018-06-20 14:32:03 +00:00
|
|
|
|
|
|
|
|
// Check asserts early so e.g. errors in parsing a module's parameters due to being
|
|
|
|
|
// logged out don't override the client's intended "am I logged in?" check.
|
|
|
|
|
$this->checkAsserts( $params );
|
|
|
|
|
|
2010-03-30 17:14:53 +00:00
|
|
|
$module = $this->setupModule();
|
2013-02-05 06:52:55 +00:00
|
|
|
$this->mModule = $module;
|
2010-03-30 17:14:53 +00:00
|
|
|
|
2016-02-08 20:26:39 +00:00
|
|
|
if ( !$this->mInternalMode ) {
|
|
|
|
|
$this->setRequestExpectations( $module );
|
|
|
|
|
}
|
2015-12-16 23:46:43 +00:00
|
|
|
|
2010-04-17 20:58:04 +00:00
|
|
|
$this->checkExecutePermissions( $module );
|
2010-03-30 17:14:53 +00:00
|
|
|
|
2010-09-25 16:45:41 +00:00
|
|
|
if ( !$this->checkMaxLag( $module, $params ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2010-03-30 17:14:53 +00:00
|
|
|
|
2015-08-17 20:52:09 +00:00
|
|
|
if ( !$this->checkConditionalRequestHeaders( $module ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-30 17:14:53 +00:00
|
|
|
if ( !$this->mInternalMode ) {
|
2010-04-17 20:58:04 +00:00
|
|
|
$this->setupExternalResponse( $module, $params );
|
2006-10-14 07:18:08 +00:00
|
|
|
}
|
2006-10-22 23:45:20 +00:00
|
|
|
|
2006-10-14 07:18:08 +00:00
|
|
|
$module->execute();
|
2016-02-17 09:09:32 +00:00
|
|
|
Hooks::run( 'APIAfterExecute', [ &$module ] );
|
2006-10-22 23:45:20 +00:00
|
|
|
|
2013-03-02 00:06:46 +00:00
|
|
|
$this->reportUnusedParams();
|
2012-09-19 10:07:48 +00:00
|
|
|
|
2013-03-02 00:06:46 +00:00
|
|
|
if ( !$this->mInternalMode ) {
|
2012-05-13 09:20:04 +00:00
|
|
|
MWDebug::appendDebugInfoToApiResult( $this->getContext(), $this->getResult() );
|
|
|
|
|
|
2016-11-09 16:59:05 +00:00
|
|
|
$this->printResult();
|
2006-10-14 07:18:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-10-22 23:45:20 +00:00
|
|
|
|
2015-12-16 23:46:43 +00:00
|
|
|
/**
|
|
|
|
|
* Set database connection, query, and write expectations given this module request
|
|
|
|
|
* @param ApiBase $module
|
|
|
|
|
*/
|
|
|
|
|
protected function setRequestExpectations( ApiBase $module ) {
|
|
|
|
|
$limits = $this->getConfig()->get( 'TrxProfilerLimits' );
|
|
|
|
|
$trxProfiler = Profiler::instance()->getTransactionProfiler();
|
2016-08-16 20:53:57 +00:00
|
|
|
$trxProfiler->setLogger( LoggerFactory::getInstance( 'DBPerformance' ) );
|
2016-05-20 03:31:19 +00:00
|
|
|
if ( $this->getRequest()->hasSafeMethod() ) {
|
2015-12-16 23:46:43 +00:00
|
|
|
$trxProfiler->setExpectations( $limits['GET'], __METHOD__ );
|
2016-05-20 03:31:19 +00:00
|
|
|
} elseif ( $this->getRequest()->wasPosted() && !$module->isWriteMode() ) {
|
|
|
|
|
$trxProfiler->setExpectations( $limits['POST-nonwrite'], __METHOD__ );
|
|
|
|
|
$this->getRequest()->markAsSafeRequest();
|
|
|
|
|
} else {
|
|
|
|
|
$trxProfiler->setExpectations( $limits['POST'], __METHOD__ );
|
2015-12-16 23:46:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-19 10:07:48 +00:00
|
|
|
/**
|
|
|
|
|
* Log the preceding request
|
Add structured API request debug logging
Add a new "ApiRequest" PSR-3 logging channel for messages that describe
an Action API request as structured data. This logging channel can be
routed to a storage service to facilitate analysis of the requests.
The logging context is designed to match the following avro schema:
{
"type": "record",
"name": "ApiRequest",
"namespace": "org.wikimedia.mediawiki.api",
"doc": "Describes an API request made via mediawiki ApiMain",
"fields": [
{ "name": "dt", "type": "string" },
{ "name": "client_ip", "type": "string" },
{ "name": "user_agent", "type": "string" },
{ "name": "wiki", "type": "string" },
{ "name": "time_backend_ms", "type": "int" },
{ "name": "params", "type": {
"type": "map", "values": "string"
} }
]
}
Co-Author: Bryan Davis <bd808@wikimedia.org>
Bug: T108618
Change-Id: I38f5cdb288f332f75adca8a2d03fbe0fc36ab936
2015-09-23 23:37:06 +00:00
|
|
|
* @param float $time Time in seconds
|
2018-08-21 15:19:15 +00:00
|
|
|
* @param Exception|Throwable|null $e Exception caught while processing the request
|
2012-09-19 10:07:48 +00:00
|
|
|
*/
|
2016-02-18 21:18:14 +00:00
|
|
|
protected function logRequest( $time, $e = null ) {
|
2012-09-19 10:07:48 +00:00
|
|
|
$request = $this->getRequest();
|
Add structured API request debug logging
Add a new "ApiRequest" PSR-3 logging channel for messages that describe
an Action API request as structured data. This logging channel can be
routed to a storage service to facilitate analysis of the requests.
The logging context is designed to match the following avro schema:
{
"type": "record",
"name": "ApiRequest",
"namespace": "org.wikimedia.mediawiki.api",
"doc": "Describes an API request made via mediawiki ApiMain",
"fields": [
{ "name": "dt", "type": "string" },
{ "name": "client_ip", "type": "string" },
{ "name": "user_agent", "type": "string" },
{ "name": "wiki", "type": "string" },
{ "name": "time_backend_ms", "type": "int" },
{ "name": "params", "type": {
"type": "map", "values": "string"
} }
]
}
Co-Author: Bryan Davis <bd808@wikimedia.org>
Bug: T108618
Change-Id: I38f5cdb288f332f75adca8a2d03fbe0fc36ab936
2015-09-23 23:37:06 +00:00
|
|
|
|
2019-02-21 02:09:41 +00:00
|
|
|
$logCtx = [
|
2019-06-19 14:05:50 +00:00
|
|
|
// https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/event-schemas/+/master/jsonschema/mediawiki/api/request
|
2019-02-21 02:09:41 +00:00
|
|
|
'$schema' => '/mediawiki/api/request/0.0.1',
|
|
|
|
|
'meta' => [
|
2019-03-08 21:50:34 +00:00
|
|
|
'request_id' => WebRequest::getRequestId(),
|
2019-05-15 05:06:09 +00:00
|
|
|
'id' => UIDGenerator::newUUIDv4(),
|
2019-03-08 21:50:34 +00:00
|
|
|
'dt' => wfTimestamp( TS_ISO_8601 ),
|
2019-02-21 02:09:41 +00:00
|
|
|
'domain' => $this->getConfig()->get( 'ServerName' ),
|
2019-06-19 14:05:50 +00:00
|
|
|
// If using the EventBus extension (as intended) with this log channel,
|
|
|
|
|
// this stream name will map to a Kafka topic.
|
2019-02-21 02:09:41 +00:00
|
|
|
'stream' => 'mediawiki.api-request'
|
|
|
|
|
],
|
|
|
|
|
'http' => [
|
|
|
|
|
'method' => $request->getMethod(),
|
2019-03-08 21:50:34 +00:00
|
|
|
'client_ip' => $request->getIP()
|
2019-02-21 02:09:41 +00:00
|
|
|
],
|
2019-03-29 21:56:18 +00:00
|
|
|
'database' => WikiMap::getCurrentWikiDbDomain()->getId(),
|
2019-02-21 02:09:41 +00:00
|
|
|
'backend_time_ms' => (int)round( $time * 1000 ),
|
|
|
|
|
];
|
|
|
|
|
|
2019-03-04 20:47:01 +00:00
|
|
|
// If set, these headers will be logged in http.request_headers.
|
2019-03-08 21:50:34 +00:00
|
|
|
$httpRequestHeadersToLog = [ 'accept-language', 'referer', 'user-agent' ];
|
|
|
|
|
foreach ( $httpRequestHeadersToLog as $header ) {
|
|
|
|
|
if ( $request->getHeader( $header ) ) {
|
|
|
|
|
// Set the header in http.request_headers
|
|
|
|
|
$logCtx['http']['request_headers'][$header] = $request->getHeader( $header );
|
|
|
|
|
}
|
2019-03-04 20:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-18 21:18:14 +00:00
|
|
|
if ( $e ) {
|
2019-02-21 02:09:41 +00:00
|
|
|
$logCtx['api_error_codes'] = [];
|
2016-10-19 16:54:25 +00:00
|
|
|
foreach ( $this->errorMessagesFromException( $e ) as $msg ) {
|
2019-02-21 02:09:41 +00:00
|
|
|
$logCtx['api_error_codes'][] = $msg->getApiCode();
|
2016-10-19 16:54:25 +00:00
|
|
|
}
|
2016-02-18 21:18:14 +00:00
|
|
|
}
|
|
|
|
|
|
Add structured API request debug logging
Add a new "ApiRequest" PSR-3 logging channel for messages that describe
an Action API request as structured data. This logging channel can be
routed to a storage service to facilitate analysis of the requests.
The logging context is designed to match the following avro schema:
{
"type": "record",
"name": "ApiRequest",
"namespace": "org.wikimedia.mediawiki.api",
"doc": "Describes an API request made via mediawiki ApiMain",
"fields": [
{ "name": "dt", "type": "string" },
{ "name": "client_ip", "type": "string" },
{ "name": "user_agent", "type": "string" },
{ "name": "wiki", "type": "string" },
{ "name": "time_backend_ms", "type": "int" },
{ "name": "params", "type": {
"type": "map", "values": "string"
} }
]
}
Co-Author: Bryan Davis <bd808@wikimedia.org>
Bug: T108618
Change-Id: I38f5cdb288f332f75adca8a2d03fbe0fc36ab936
2015-09-23 23:37:06 +00:00
|
|
|
// Construct space separated message for 'api' log channel
|
|
|
|
|
$msg = "API {$request->getMethod()} " .
|
|
|
|
|
wfUrlencode( str_replace( ' ', '_', $this->getUser()->getName() ) ) .
|
2019-06-19 14:05:50 +00:00
|
|
|
" {$logCtx['http']['client_ip']} " .
|
|
|
|
|
"T={$logCtx['backend_time_ms']}ms";
|
Add structured API request debug logging
Add a new "ApiRequest" PSR-3 logging channel for messages that describe
an Action API request as structured data. This logging channel can be
routed to a storage service to facilitate analysis of the requests.
The logging context is designed to match the following avro schema:
{
"type": "record",
"name": "ApiRequest",
"namespace": "org.wikimedia.mediawiki.api",
"doc": "Describes an API request made via mediawiki ApiMain",
"fields": [
{ "name": "dt", "type": "string" },
{ "name": "client_ip", "type": "string" },
{ "name": "user_agent", "type": "string" },
{ "name": "wiki", "type": "string" },
{ "name": "time_backend_ms", "type": "int" },
{ "name": "params", "type": {
"type": "map", "values": "string"
} }
]
}
Co-Author: Bryan Davis <bd808@wikimedia.org>
Bug: T108618
Change-Id: I38f5cdb288f332f75adca8a2d03fbe0fc36ab936
2015-09-23 23:37:06 +00:00
|
|
|
|
2016-08-18 17:37:05 +00:00
|
|
|
$sensitive = array_flip( $this->getSensitiveParams() );
|
2012-09-19 10:07:48 +00:00
|
|
|
foreach ( $this->getParamsUsed() as $name ) {
|
|
|
|
|
$value = $request->getVal( $name );
|
|
|
|
|
if ( $value === null ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
Add structured API request debug logging
Add a new "ApiRequest" PSR-3 logging channel for messages that describe
an Action API request as structured data. This logging channel can be
routed to a storage service to facilitate analysis of the requests.
The logging context is designed to match the following avro schema:
{
"type": "record",
"name": "ApiRequest",
"namespace": "org.wikimedia.mediawiki.api",
"doc": "Describes an API request made via mediawiki ApiMain",
"fields": [
{ "name": "dt", "type": "string" },
{ "name": "client_ip", "type": "string" },
{ "name": "user_agent", "type": "string" },
{ "name": "wiki", "type": "string" },
{ "name": "time_backend_ms", "type": "int" },
{ "name": "params", "type": {
"type": "map", "values": "string"
} }
]
}
Co-Author: Bryan Davis <bd808@wikimedia.org>
Bug: T108618
Change-Id: I38f5cdb288f332f75adca8a2d03fbe0fc36ab936
2015-09-23 23:37:06 +00:00
|
|
|
|
2016-08-18 17:37:05 +00:00
|
|
|
if ( isset( $sensitive[$name] ) ) {
|
|
|
|
|
$value = '[redacted]';
|
|
|
|
|
$encValue = '[redacted]';
|
|
|
|
|
} elseif ( strlen( $value ) > 256 ) {
|
Add structured API request debug logging
Add a new "ApiRequest" PSR-3 logging channel for messages that describe
an Action API request as structured data. This logging channel can be
routed to a storage service to facilitate analysis of the requests.
The logging context is designed to match the following avro schema:
{
"type": "record",
"name": "ApiRequest",
"namespace": "org.wikimedia.mediawiki.api",
"doc": "Describes an API request made via mediawiki ApiMain",
"fields": [
{ "name": "dt", "type": "string" },
{ "name": "client_ip", "type": "string" },
{ "name": "user_agent", "type": "string" },
{ "name": "wiki", "type": "string" },
{ "name": "time_backend_ms", "type": "int" },
{ "name": "params", "type": {
"type": "map", "values": "string"
} }
]
}
Co-Author: Bryan Davis <bd808@wikimedia.org>
Bug: T108618
Change-Id: I38f5cdb288f332f75adca8a2d03fbe0fc36ab936
2015-09-23 23:37:06 +00:00
|
|
|
$value = substr( $value, 0, 256 );
|
|
|
|
|
$encValue = $this->encodeRequestLogValue( $value ) . '[...]';
|
2012-09-19 10:07:48 +00:00
|
|
|
} else {
|
Add structured API request debug logging
Add a new "ApiRequest" PSR-3 logging channel for messages that describe
an Action API request as structured data. This logging channel can be
routed to a storage service to facilitate analysis of the requests.
The logging context is designed to match the following avro schema:
{
"type": "record",
"name": "ApiRequest",
"namespace": "org.wikimedia.mediawiki.api",
"doc": "Describes an API request made via mediawiki ApiMain",
"fields": [
{ "name": "dt", "type": "string" },
{ "name": "client_ip", "type": "string" },
{ "name": "user_agent", "type": "string" },
{ "name": "wiki", "type": "string" },
{ "name": "time_backend_ms", "type": "int" },
{ "name": "params", "type": {
"type": "map", "values": "string"
} }
]
}
Co-Author: Bryan Davis <bd808@wikimedia.org>
Bug: T108618
Change-Id: I38f5cdb288f332f75adca8a2d03fbe0fc36ab936
2015-09-23 23:37:06 +00:00
|
|
|
$encValue = $this->encodeRequestLogValue( $value );
|
2012-09-19 10:07:48 +00:00
|
|
|
}
|
Add structured API request debug logging
Add a new "ApiRequest" PSR-3 logging channel for messages that describe
an Action API request as structured data. This logging channel can be
routed to a storage service to facilitate analysis of the requests.
The logging context is designed to match the following avro schema:
{
"type": "record",
"name": "ApiRequest",
"namespace": "org.wikimedia.mediawiki.api",
"doc": "Describes an API request made via mediawiki ApiMain",
"fields": [
{ "name": "dt", "type": "string" },
{ "name": "client_ip", "type": "string" },
{ "name": "user_agent", "type": "string" },
{ "name": "wiki", "type": "string" },
{ "name": "time_backend_ms", "type": "int" },
{ "name": "params", "type": {
"type": "map", "values": "string"
} }
]
}
Co-Author: Bryan Davis <bd808@wikimedia.org>
Bug: T108618
Change-Id: I38f5cdb288f332f75adca8a2d03fbe0fc36ab936
2015-09-23 23:37:06 +00:00
|
|
|
|
|
|
|
|
$logCtx['params'][$name] = $value;
|
|
|
|
|
$msg .= " {$name}={$encValue}";
|
2012-09-19 10:07:48 +00:00
|
|
|
}
|
Add structured API request debug logging
Add a new "ApiRequest" PSR-3 logging channel for messages that describe
an Action API request as structured data. This logging channel can be
routed to a storage service to facilitate analysis of the requests.
The logging context is designed to match the following avro schema:
{
"type": "record",
"name": "ApiRequest",
"namespace": "org.wikimedia.mediawiki.api",
"doc": "Describes an API request made via mediawiki ApiMain",
"fields": [
{ "name": "dt", "type": "string" },
{ "name": "client_ip", "type": "string" },
{ "name": "user_agent", "type": "string" },
{ "name": "wiki", "type": "string" },
{ "name": "time_backend_ms", "type": "int" },
{ "name": "params", "type": {
"type": "map", "values": "string"
} }
]
}
Co-Author: Bryan Davis <bd808@wikimedia.org>
Bug: T108618
Change-Id: I38f5cdb288f332f75adca8a2d03fbe0fc36ab936
2015-09-23 23:37:06 +00:00
|
|
|
|
2019-06-19 14:05:50 +00:00
|
|
|
// Log an unstructured message to the api channel.
|
Add structured API request debug logging
Add a new "ApiRequest" PSR-3 logging channel for messages that describe
an Action API request as structured data. This logging channel can be
routed to a storage service to facilitate analysis of the requests.
The logging context is designed to match the following avro schema:
{
"type": "record",
"name": "ApiRequest",
"namespace": "org.wikimedia.mediawiki.api",
"doc": "Describes an API request made via mediawiki ApiMain",
"fields": [
{ "name": "dt", "type": "string" },
{ "name": "client_ip", "type": "string" },
{ "name": "user_agent", "type": "string" },
{ "name": "wiki", "type": "string" },
{ "name": "time_backend_ms", "type": "int" },
{ "name": "params", "type": {
"type": "map", "values": "string"
} }
]
}
Co-Author: Bryan Davis <bd808@wikimedia.org>
Bug: T108618
Change-Id: I38f5cdb288f332f75adca8a2d03fbe0fc36ab936
2015-09-23 23:37:06 +00:00
|
|
|
wfDebugLog( 'api', $msg, 'private' );
|
2019-06-19 14:05:50 +00:00
|
|
|
|
|
|
|
|
// The api-request channel a structured data log channel.
|
2019-02-21 02:09:41 +00:00
|
|
|
wfDebugLog( 'api-request', '', 'private', $logCtx );
|
2012-09-19 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Encode a value in a format suitable for a space-separated log line.
|
2014-04-15 18:12:09 +00:00
|
|
|
* @param string $s
|
2014-05-24 20:06:01 +00:00
|
|
|
* @return string
|
2012-09-19 10:07:48 +00:00
|
|
|
*/
|
|
|
|
|
protected function encodeRequestLogValue( $s ) {
|
|
|
|
|
static $table;
|
|
|
|
|
if ( !$table ) {
|
|
|
|
|
$chars = ';@$!*(),/:';
|
2013-11-16 19:09:17 +00:00
|
|
|
$numChars = strlen( $chars );
|
|
|
|
|
for ( $i = 0; $i < $numChars; $i++ ) {
|
2013-01-12 06:50:48 +00:00
|
|
|
$table[rawurlencode( $chars[$i] )] = $chars[$i];
|
2012-09-19 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-11-14 12:42:04 +00:00
|
|
|
|
2012-09-19 10:07:48 +00:00
|
|
|
return strtr( rawurlencode( $s ), $table );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the request parameters used in the course of the preceding execute() request
|
2014-04-15 18:12:09 +00:00
|
|
|
* @return array
|
2012-09-19 10:07:48 +00:00
|
|
|
*/
|
|
|
|
|
protected function getParamsUsed() {
|
|
|
|
|
return array_keys( $this->mParamsUsed );
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-11 18:20:22 +00:00
|
|
|
/**
|
|
|
|
|
* Mark parameters as used
|
|
|
|
|
* @param string|string[] $params
|
|
|
|
|
*/
|
|
|
|
|
public function markParamsUsed( $params ) {
|
|
|
|
|
$this->mParamsUsed += array_fill_keys( (array)$params, true );
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-18 17:37:05 +00:00
|
|
|
/**
|
|
|
|
|
* Get the request parameters that should be considered sensitive
|
|
|
|
|
* @since 1.29
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function getSensitiveParams() {
|
|
|
|
|
return array_keys( $this->mParamsSensitive );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mark parameters as sensitive
|
|
|
|
|
* @since 1.29
|
|
|
|
|
* @param string|string[] $params
|
|
|
|
|
*/
|
|
|
|
|
public function markParamsSensitive( $params ) {
|
|
|
|
|
$this->mParamsSensitive += array_fill_keys( (array)$params, true );
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-19 10:07:48 +00:00
|
|
|
/**
|
|
|
|
|
* Get a request value, and register the fact that it was used, for logging.
|
2014-04-15 18:12:09 +00:00
|
|
|
* @param string $name
|
2018-03-20 17:34:03 +00:00
|
|
|
* @param string|null $default
|
|
|
|
|
* @return string|null
|
2012-09-19 10:07:48 +00:00
|
|
|
*/
|
|
|
|
|
public function getVal( $name, $default = null ) {
|
|
|
|
|
$this->mParamsUsed[$name] = true;
|
2013-11-14 12:42:04 +00:00
|
|
|
|
2014-04-17 14:24:30 +00:00
|
|
|
$ret = $this->getRequest()->getVal( $name );
|
|
|
|
|
if ( $ret === null ) {
|
|
|
|
|
if ( $this->getRequest()->getArray( $name ) !== null ) {
|
2017-02-20 22:28:10 +00:00
|
|
|
// See T12262 for why we don't just implode( '|', ... ) the
|
2014-04-17 14:24:30 +00:00
|
|
|
// array.
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->addWarning( [ 'apiwarn-unsupportedarray', $name ] );
|
2014-04-17 14:24:30 +00:00
|
|
|
}
|
|
|
|
|
$ret = $default;
|
|
|
|
|
}
|
|
|
|
|
return $ret;
|
2012-09-19 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a boolean request value, and register the fact that the parameter
|
|
|
|
|
* was used, for logging.
|
2014-04-15 18:12:09 +00:00
|
|
|
* @param string $name
|
|
|
|
|
* @return bool
|
2012-09-19 10:07:48 +00:00
|
|
|
*/
|
|
|
|
|
public function getCheck( $name ) {
|
2014-04-17 14:24:30 +00:00
|
|
|
return $this->getVal( $name, null ) !== null;
|
2012-09-19 10:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
2013-02-26 21:45:37 +00:00
|
|
|
/**
|
|
|
|
|
* Get a request upload, and register the fact that it was used, for logging.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.21
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $name Parameter name
|
2013-02-26 21:45:37 +00:00
|
|
|
* @return WebRequestUpload
|
|
|
|
|
*/
|
|
|
|
|
public function getUpload( $name ) {
|
|
|
|
|
$this->mParamsUsed[$name] = true;
|
2013-11-14 12:42:04 +00:00
|
|
|
|
2013-02-26 21:45:37 +00:00
|
|
|
return $this->getRequest()->getUpload( $name );
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-26 08:30:53 +00:00
|
|
|
/**
|
|
|
|
|
* Report unused parameters, so the client gets a hint in case it gave us parameters we don't know,
|
|
|
|
|
* for example in case of spelling mistakes or a missing 'g' prefix for generators.
|
|
|
|
|
*/
|
|
|
|
|
protected function reportUnusedParams() {
|
|
|
|
|
$paramsUsed = $this->getParamsUsed();
|
|
|
|
|
$allParams = $this->getRequest()->getValueNames();
|
|
|
|
|
|
2013-03-02 00:06:46 +00:00
|
|
|
if ( !$this->mInternalMode ) {
|
|
|
|
|
// Printer has not yet executed; don't warn that its parameters are unused
|
2016-10-19 16:54:25 +00:00
|
|
|
$printerParams = $this->mPrinter->encodeParamName(
|
2016-02-17 09:09:32 +00:00
|
|
|
array_keys( $this->mPrinter->getFinalParams() ?: [] )
|
2013-03-02 00:06:46 +00:00
|
|
|
);
|
|
|
|
|
$unusedParams = array_diff( $allParams, $paramsUsed, $printerParams );
|
|
|
|
|
} else {
|
|
|
|
|
$unusedParams = array_diff( $allParams, $paramsUsed );
|
|
|
|
|
}
|
2013-02-23 21:19:29 +00:00
|
|
|
|
2013-04-19 18:03:05 +00:00
|
|
|
if ( count( $unusedParams ) ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->addWarning( [
|
|
|
|
|
'apierror-unrecognizedparams',
|
|
|
|
|
Message::listParam( array_map( 'wfEscapeWikiText', $unusedParams ), 'comma' ),
|
|
|
|
|
count( $unusedParams )
|
|
|
|
|
] );
|
2012-09-26 08:30:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-01 04:38:31 +00:00
|
|
|
/**
|
2007-05-20 10:08:40 +00:00
|
|
|
* Print results using the current printer
|
2011-05-08 16:48:30 +00:00
|
|
|
*
|
2016-11-09 16:59:05 +00:00
|
|
|
* @param int $httpCode HTTP status code, or 0 to not change
|
2006-10-01 04:38:31 +00:00
|
|
|
*/
|
2016-11-09 16:59:05 +00:00
|
|
|
protected function printResult( $httpCode = 0 ) {
|
2014-01-24 02:51:11 +00:00
|
|
|
if ( $this->getConfig()->get( 'DebugAPI' ) !== false ) {
|
2019-04-30 20:41:50 +00:00
|
|
|
$this->addWarning( 'apiwarn-wgdebugapi' );
|
2013-01-18 15:23:17 +00:00
|
|
|
}
|
|
|
|
|
|
2006-10-01 04:38:31 +00:00
|
|
|
$printer = $this->mPrinter;
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
$printer->initPrinter( false );
|
2016-11-09 16:59:05 +00:00
|
|
|
if ( $httpCode ) {
|
|
|
|
|
$printer->setHttpStatus( $httpCode );
|
|
|
|
|
}
|
2006-10-15 07:43:52 +00:00
|
|
|
$printer->execute();
|
2006-10-01 04:38:31 +00:00
|
|
|
$printer->closePrinter();
|
|
|
|
|
}
|
2010-01-22 03:14:52 +00:00
|
|
|
|
2011-01-30 08:16:13 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2009-03-06 13:49:44 +00:00
|
|
|
public function isReadMode() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2006-10-14 07:18:08 +00:00
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
/**
|
|
|
|
|
* See ApiBase for description.
|
2011-05-08 16:48:30 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
2007-05-20 10:08:40 +00:00
|
|
|
*/
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getAllowedParams() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
'action' => [
|
2010-02-23 18:05:46 +00:00
|
|
|
ApiBase::PARAM_DFLT => 'help',
|
2014-08-14 20:12:58 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'submodule',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'format' => [
|
2017-07-23 01:24:09 +00:00
|
|
|
ApiBase::PARAM_DFLT => self::API_DEFAULT_FORMAT,
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'submodule',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'maxlag' => [
|
2010-02-23 18:05:46 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'integer'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'smaxage' => [
|
2010-02-23 18:05:46 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'integer',
|
|
|
|
|
ApiBase::PARAM_DFLT => 0
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'maxage' => [
|
2010-02-23 18:05:46 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'integer',
|
|
|
|
|
ApiBase::PARAM_DFLT => 0
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'assert' => [
|
|
|
|
|
ApiBase::PARAM_TYPE => [ 'user', 'bot' ]
|
|
|
|
|
],
|
2016-10-05 14:35:47 +00:00
|
|
|
'assertuser' => [
|
|
|
|
|
ApiBase::PARAM_TYPE => 'user',
|
|
|
|
|
],
|
2008-08-05 16:12:52 +00:00
|
|
|
'requestid' => null,
|
2013-04-19 18:03:05 +00:00
|
|
|
'servedby' => false,
|
2014-08-08 16:56:07 +00:00
|
|
|
'curtimestamp' => false,
|
2016-10-19 16:54:25 +00:00
|
|
|
'responselanginfo' => false,
|
Reimplement CORS properly, addressing Tim's concerns
Tim's concerns (listed at
https://bugzilla.wikimedia.org/show_bug.cgi?id=20814#c6) were:
* Lack of Vary: Origin breaks Squid caching
* Vary: Origin on everything would be disastrous, so add an origin param
* Origin header is space-separated list, wasn't treated as such
This commit:
* Remove CORS code from api.php and reimplement it in ApiMain.php
* Add 'origin' parameter to ApiMain
* If 'origin' parameter doesn't match Origin header, send a 403
* If origin is whitelisted, set CORS headers and set Vary: Origin
* Add https?:// to wildcard matching logic, wasn't there but is needed
CORS now works :) you can test it locally as follows:
Set $wgCrossSiteAJAXdomains[] = '*.wikipedia.org';
Log into MediaWiki on localhost
Go to Wikipedia, open a JS console, and run:
$.ajax( {
'url': 'http://localhost/w/api.php',
'data': {
'action': 'query',
'meta': 'userinfo',
'format': 'json',
'origin': 'https://en.wikipedia.org'
// or whichever domain you're on; must be correct!
},
'xhrFields': {
'withCredentials': true
},
'success': function( data ) {
alert( 'Foreign user ' + data.query.userinfo.name +
' (ID ' + data.query.userinfo.id + ')' );
}
} );
Change-Id: I725ce176866d7c81dd9ad6d7bc4a86b7160f2458
2012-06-01 12:14:47 +00:00
|
|
|
'origin' => null,
|
2016-02-17 09:09:32 +00:00
|
|
|
'uselang' => [
|
2016-10-19 16:54:25 +00:00
|
|
|
ApiBase::PARAM_DFLT => self::API_DEFAULT_USELANG,
|
|
|
|
|
],
|
|
|
|
|
'errorformat' => [
|
|
|
|
|
ApiBase::PARAM_TYPE => [ 'plaintext', 'wikitext', 'html', 'raw', 'none', 'bc' ],
|
|
|
|
|
ApiBase::PARAM_DFLT => 'bc',
|
|
|
|
|
],
|
|
|
|
|
'errorlang' => [
|
|
|
|
|
ApiBase::PARAM_DFLT => 'uselang',
|
|
|
|
|
],
|
|
|
|
|
'errorsuselocal' => [
|
|
|
|
|
ApiBase::PARAM_DFLT => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2006-10-14 07:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
2017-09-09 20:47:04 +00:00
|
|
|
/** @inheritDoc */
|
2014-10-28 17:17:02 +00:00
|
|
|
protected function getExamplesMessages() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2014-09-18 17:38:23 +00:00
|
|
|
'action=help'
|
|
|
|
|
=> 'apihelp-help-example-main',
|
|
|
|
|
'action=help&recursivesubmodules=1'
|
|
|
|
|
=> 'apihelp-help-example-recursive',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
2015-05-06 17:37:41 +00:00
|
|
|
public function modifyHelp( array &$help, array $options, array &$tocData ) {
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
// Wish PHP had an "array_insert_before". Instead, we have to manually
|
|
|
|
|
// reindex the array to get 'permissions' in the right place.
|
|
|
|
|
$oldHelp = $help;
|
2016-02-17 09:09:32 +00:00
|
|
|
$help = [];
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
foreach ( $oldHelp as $k => $v ) {
|
|
|
|
|
if ( $k === 'submodules' ) {
|
|
|
|
|
$help['permissions'] = '';
|
|
|
|
|
}
|
|
|
|
|
$help[$k] = $v;
|
|
|
|
|
}
|
2015-03-26 19:34:55 +00:00
|
|
|
$help['datatypes'] = '';
|
2018-04-04 20:22:01 +00:00
|
|
|
$help['templatedparams'] = '';
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
$help['credits'] = '';
|
|
|
|
|
|
|
|
|
|
// Fill 'permissions'
|
|
|
|
|
$help['permissions'] .= Html::openElement( 'div',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'class' => 'apihelp-block apihelp-permissions' ] );
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
$m = $this->msg( 'api-help-permissions' );
|
|
|
|
|
if ( !$m->isDisabled() ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$help['permissions'] .= Html::rawElement( 'div', [ 'class' => 'apihelp-block-head' ],
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
$m->numParams( count( self::$mRights ) )->parse()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$help['permissions'] .= Html::openElement( 'dl' );
|
|
|
|
|
foreach ( self::$mRights as $right => $rightMsg ) {
|
|
|
|
|
$help['permissions'] .= Html::element( 'dt', null, $right );
|
|
|
|
|
|
|
|
|
|
$rightMsg = $this->msg( $rightMsg['msg'], $rightMsg['params'] )->parse();
|
|
|
|
|
$help['permissions'] .= Html::rawElement( 'dd', null, $rightMsg );
|
|
|
|
|
|
|
|
|
|
$groups = array_map( function ( $group ) {
|
|
|
|
|
return $group == '*' ? 'all' : $group;
|
|
|
|
|
}, User::getGroupsWithPermission( $right ) );
|
|
|
|
|
|
|
|
|
|
$help['permissions'] .= Html::rawElement( 'dd', null,
|
|
|
|
|
$this->msg( 'api-help-permissions-granted-to' )
|
|
|
|
|
->numParams( count( $groups ) )
|
2016-10-19 16:54:25 +00:00
|
|
|
->params( Message::listParam( $groups ) )
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
->parse()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$help['permissions'] .= Html::closeElement( 'dl' );
|
|
|
|
|
$help['permissions'] .= Html::closeElement( 'div' );
|
|
|
|
|
|
2018-04-04 20:22:01 +00:00
|
|
|
// Fill 'datatypes', 'templatedparams', and 'credits', if applicable
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
if ( empty( $options['nolead'] ) ) {
|
2015-05-06 17:37:41 +00:00
|
|
|
$level = $options['headerlevel'];
|
|
|
|
|
$tocnumber = &$options['tocnumber'];
|
|
|
|
|
|
|
|
|
|
$header = $this->msg( 'api-help-datatypes-header' )->parse();
|
2016-05-02 14:09:06 +00:00
|
|
|
|
2017-06-30 00:13:12 +00:00
|
|
|
$id = Sanitizer::escapeIdForAttribute( 'main/datatypes', Sanitizer::ID_PRIMARY );
|
|
|
|
|
$idFallback = Sanitizer::escapeIdForAttribute( 'main/datatypes', Sanitizer::ID_FALLBACK );
|
2017-08-02 22:45:01 +00:00
|
|
|
$headline = Linker::makeHeadline( min( 6, $level ),
|
2017-12-18 14:53:27 +00:00
|
|
|
' class="apihelp-header">',
|
2017-06-30 00:13:12 +00:00
|
|
|
$id,
|
|
|
|
|
$header,
|
|
|
|
|
'',
|
|
|
|
|
$idFallback
|
2015-03-26 19:34:55 +00:00
|
|
|
);
|
2017-08-02 22:45:01 +00:00
|
|
|
// Ensure we have a sane anchor
|
|
|
|
|
if ( $id !== 'main/datatypes' && $idFallback !== 'main/datatypes' ) {
|
|
|
|
|
$headline = '<div id="main/datatypes"></div>' . $headline;
|
|
|
|
|
}
|
|
|
|
|
$help['datatypes'] .= $headline;
|
2015-03-26 19:34:55 +00:00
|
|
|
$help['datatypes'] .= $this->msg( 'api-help-datatypes' )->parseAsBlock();
|
2015-05-06 17:37:41 +00:00
|
|
|
if ( !isset( $tocData['main/datatypes'] ) ) {
|
|
|
|
|
$tocnumber[$level]++;
|
2016-02-17 09:09:32 +00:00
|
|
|
$tocData['main/datatypes'] = [
|
2015-05-06 17:37:41 +00:00
|
|
|
'toclevel' => count( $tocnumber ),
|
|
|
|
|
'level' => $level,
|
|
|
|
|
'anchor' => 'main/datatypes',
|
|
|
|
|
'line' => $header,
|
2016-03-08 08:13:12 +00:00
|
|
|
'number' => implode( '.', $tocnumber ),
|
2018-04-04 20:22:01 +00:00
|
|
|
'index' => false,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$header = $this->msg( 'api-help-templatedparams-header' )->parse();
|
|
|
|
|
|
|
|
|
|
$id = Sanitizer::escapeIdForAttribute( 'main/templatedparams', Sanitizer::ID_PRIMARY );
|
|
|
|
|
$idFallback = Sanitizer::escapeIdForAttribute( 'main/templatedparams', Sanitizer::ID_FALLBACK );
|
|
|
|
|
$headline = Linker::makeHeadline( min( 6, $level ),
|
|
|
|
|
' class="apihelp-header">',
|
|
|
|
|
$id,
|
|
|
|
|
$header,
|
|
|
|
|
'',
|
|
|
|
|
$idFallback
|
|
|
|
|
);
|
|
|
|
|
// Ensure we have a sane anchor
|
|
|
|
|
if ( $id !== 'main/templatedparams' && $idFallback !== 'main/templatedparams' ) {
|
|
|
|
|
$headline = '<div id="main/templatedparams"></div>' . $headline;
|
|
|
|
|
}
|
|
|
|
|
$help['templatedparams'] .= $headline;
|
|
|
|
|
$help['templatedparams'] .= $this->msg( 'api-help-templatedparams' )->parseAsBlock();
|
|
|
|
|
if ( !isset( $tocData['main/templatedparams'] ) ) {
|
|
|
|
|
$tocnumber[$level]++;
|
|
|
|
|
$tocData['main/templatedparams'] = [
|
|
|
|
|
'toclevel' => count( $tocnumber ),
|
|
|
|
|
'level' => $level,
|
|
|
|
|
'anchor' => 'main/templatedparams',
|
|
|
|
|
'line' => $header,
|
|
|
|
|
'number' => implode( '.', $tocnumber ),
|
2015-05-06 17:37:41 +00:00
|
|
|
'index' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2015-05-06 17:37:41 +00:00
|
|
|
}
|
2015-03-26 19:34:55 +00:00
|
|
|
|
2015-05-06 17:37:41 +00:00
|
|
|
$header = $this->msg( 'api-credits-header' )->parse();
|
2017-06-30 00:13:12 +00:00
|
|
|
$id = Sanitizer::escapeIdForAttribute( 'main/credits', Sanitizer::ID_PRIMARY );
|
|
|
|
|
$idFallback = Sanitizer::escapeIdForAttribute( 'main/credits', Sanitizer::ID_FALLBACK );
|
2017-08-02 22:45:01 +00:00
|
|
|
$headline = Linker::makeHeadline( min( 6, $level ),
|
2017-12-18 14:53:27 +00:00
|
|
|
' class="apihelp-header">',
|
2017-06-30 00:13:12 +00:00
|
|
|
$id,
|
|
|
|
|
$header,
|
|
|
|
|
'',
|
|
|
|
|
$idFallback
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
);
|
2017-08-02 22:45:01 +00:00
|
|
|
// Ensure we have a sane anchor
|
|
|
|
|
if ( $id !== 'main/credits' && $idFallback !== 'main/credits' ) {
|
|
|
|
|
$headline = '<div id="main/credits"></div>' . $headline;
|
|
|
|
|
}
|
|
|
|
|
$help['credits'] .= $headline;
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
$help['credits'] .= $this->msg( 'api-credits' )->useDatabase( false )->parseAsBlock();
|
2015-05-06 17:37:41 +00:00
|
|
|
if ( !isset( $tocData['main/credits'] ) ) {
|
|
|
|
|
$tocnumber[$level]++;
|
2016-02-17 09:09:32 +00:00
|
|
|
$tocData['main/credits'] = [
|
2015-05-06 17:37:41 +00:00
|
|
|
'toclevel' => count( $tocnumber ),
|
|
|
|
|
'level' => $level,
|
|
|
|
|
'anchor' => 'main/credits',
|
|
|
|
|
'line' => $header,
|
2016-03-08 08:13:12 +00:00
|
|
|
'number' => implode( '.', $tocnumber ),
|
2015-05-06 17:37:41 +00:00
|
|
|
'index' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2015-05-06 17:37:41 +00:00
|
|
|
}
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private $mCanApiHighLimits = null;
|
|
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
/**
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
* Check whether the current user is allowed to use high limits
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function canApiHighLimits() {
|
|
|
|
|
if ( !isset( $this->mCanApiHighLimits ) ) {
|
|
|
|
|
$this->mCanApiHighLimits = $this->getUser()->isAllowed( 'apihighlimits' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->mCanApiHighLimits;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Overrides to return this instance's module manager.
|
|
|
|
|
* @return ApiModuleManager
|
|
|
|
|
*/
|
|
|
|
|
public function getModuleManager() {
|
|
|
|
|
return $this->mModuleMgr;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-18 19:33:09 +00:00
|
|
|
/**
|
|
|
|
|
* Fetches the user agent used for this request
|
|
|
|
|
*
|
|
|
|
|
* The value will be the combination of the 'Api-User-Agent' header (if
|
|
|
|
|
* any) and the standard User-Agent header (if any).
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getUserAgent() {
|
|
|
|
|
return trim(
|
|
|
|
|
$this->getRequest()->getHeader( 'Api-user-agent' ) . ' ' .
|
|
|
|
|
$this->getRequest()->getHeader( 'User-agent' )
|
|
|
|
|
);
|
|
|
|
|
}
|
2006-10-01 04:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
API: HTMLize and internationalize the help, add Special:ApiHelp
The existing API help, formatted as basically a plain-text document
embedded in XML and with a little bolding and a few links
syntax-highlighted in after the fact, works ok for experienced programmers
but isn't at all newbie-friendly. Further, all the help is hard-coded in
English, which isn't very friendly to non-English speakers.
So let's rewrite it. The help text is now obtained from i18n messages
and output in HTML, with the default display consisting of help for a
single module with links to help for other modules. This, of course,
necessitates deprecating many of the existing help-related methods and
hooks and replacing them with new ones, but backwards compatibility is
maintained for almost everything.
At the same time, action=paraminfo also needs to support the
'description' and other help-related fields being output in wikitext or
HTML, and I11cb063d (to access all modules via the 'modules' parameter
instead of having 'modules', 'formatmodules', 'querymodules', and so on)
is folded in.
And we also add Special:ApiHelp. When directly accessed, it simply
redirects to api.php with appropriate parameters. But it's also
transcludable to allow up-to-date API help text to be included within
the on-wiki documentation.
Note this patch doesn't actually add i18n messages for any API modules
besides ApiMain and ApiHelp. That will come in a followup patch, but for
the moment the backwards-compatibility code handles them nicely.
While we're messing with the documentation, we may as well add the
"internal" flag requested in bug 62905 (although the 'includeinternal'
parameter it also requests doesn't make much sense anymore) and a
"deprecated" flag that's needed by several modules now.
Bug: 30936
Bug: 38126
Bug: 42343
Bug: 45641
Bug: 62905
Bug: 63211
Change-Id: Ib14c00df06d85c2f6364d83b2b10ce34c7f513cc
2014-09-16 17:54:01 +00:00
|
|
|
/**
|
|
|
|
|
* For really cool vim folding this needs to be at the end:
|
|
|
|
|
* vim: foldmarker=@{,@} foldmethod=marker
|
|
|
|
|
*/
|