* @defgroup API API
*/
-if ( !defined( 'MEDIAWIKI' ) ) {
- // Eclipse helper - will be ignored in production
- require_once( 'ApiBase.php' );
-}
-
/**
* This is the main API class, used for both external and internal processing.
* When executed, it will create the requested formatter object,
'paraminfo' => 'ApiParamInfo',
'rsd' => 'ApiRsd',
'compare' => 'ApiComparePages',
+ 'tokens' => 'ApiTokens',
// Write modules
'purge' => 'ApiPurge',
'patrol' => 'ApiPatrol',
'import' => 'ApiImport',
'userrights' => 'ApiUserrights',
+ 'options' => 'ApiOptions',
);
/**
private $mPrinter;
private $mModules, $mModuleNames, $mFormats, $mFormatNames;
- private $mResult, $mAction, $mShowVersions, $mEnableWrite, $mRequest;
+ private $mResult, $mAction, $mShowVersions, $mEnableWrite;
private $mInternalMode, $mSquidMaxage, $mModule;
private $mCacheMode = 'private';
/**
* Constructs an instance of ApiMain that utilizes the module and format specified by $request.
*
- * @param $request WebRequest - if this is an instance of FauxRequest, errors are thrown and no printing occurs
+ * @param $context IContextSource|WebRequest - if this is an instance of FauxRequest, errors are thrown and no printing occurs
* @param $enableWrite bool should be set to true if the api may modify data
*/
- public function __construct( $request, $enableWrite = false ) {
- $this->mInternalMode = ( $request instanceof FauxRequest );
+ 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 ) );
+
+ if ( isset( $request ) ) {
+ $this->getContext()->setRequest( $request );
+ }
+
+ $this->mInternalMode = ( $this->getRequest() instanceof FauxRequest );
// Special handling for the main module: $parent === $this
parent::__construct( $this, $this->mInternalMode ? 'main_int' : 'main' );
// Remove all modules other than login
global $wgUser;
- if ( $request->getVal( 'callback' ) !== null ) {
+ if ( $this->getRequest()->getVal( 'callback' ) !== null ) {
// JSON callback allows cross-site reads.
// For safety, strip user credentials.
wfDebug( "API: stripping user credentials for JSON callback\n" );
$wgUser = new User();
+ $this->getContext()->setUser( $wgUser );
}
}
$this->mShowVersions = false;
$this->mEnableWrite = $enableWrite;
- $this->mRequest = &$request;
-
$this->mSquidMaxage = - 1; // flag for executeActionWithErrorHandling()
$this->mCommit = false;
}
return $this->mInternalMode;
}
- /**
- * Return the request object that contains client's request
- * @return WebRequest
- */
- public function getRequest() {
- return $this->mRequest;
- }
-
/**
* Get the ApiResult object associated with current request
*
* $this->setCacheMode('private')
*/
public function setCachePrivate() {
+ wfDeprecated( __METHOD__, '1.17' );
$this->setCacheMode( 'private' );
}
* @deprecated since 1.17 Use setCacheMode( 'anon-public-user-private' )
*/
public function setVaryCookie() {
+ wfDeprecated( __METHOD__, '1.17' );
$this->setCacheMode( 'anon-public-user-private' );
}
$this->executeAction();
} catch ( Exception $e ) {
// Log it
- if ( $e instanceof MWException ) {
+ if ( !( $e instanceof UsageException ) ) {
wfDebugLog( 'exception', $e->getLogMessage() );
}
}
protected function sendCacheHeaders() {
- global $wgUseXVO, $wgOut, $wgVaryOnXFP;
+ global $wgUseXVO, $wgVaryOnXFP;
$response = $this->getRequest()->response();
if ( $this->mCacheMode == 'private' ) {
$xfp = $wgVaryOnXFP ? ', X-Forwarded-Proto' : '';
$response->header( 'Vary: Accept-Encoding, Cookie' . $xfp );
if ( $wgUseXVO ) {
+ $out = $this->getOutput();
if ( $wgVaryOnXFP ) {
- $wgOut->addVaryHeader( 'X-Forwarded-Proto' );
+ $out->addVaryHeader( 'X-Forwarded-Proto' );
}
- $response->header( $wgOut->getXVO() );
- if ( $wgOut->haveCacheVaryCookies() ) {
+ $response->header( $out->getXVO() );
+ if ( $out->haveCacheVaryCookies() ) {
// Logged in, mark this request private
$response->header( 'Cache-Control: private' );
return;
$moduleParams = $module->extractRequestParams();
// Die if token required, but not provided (unless there is a gettoken parameter)
+ if ( isset( $moduleParams['gettoken'] ) ) {
+ $gettoken = $moduleParams['gettoken'];
+ } else {
+ $gettoken = false;
+ }
+
$salt = $module->getTokenSalt();
- if ( $salt !== false && !isset( $moduleParams['gettoken'] ) ) {
+ if ( $salt !== false && !$gettoken ) {
if ( !isset( $moduleParams['token'] ) ) {
$this->dieUsageMsg( array( 'missingparam', 'token' ) );
} else {
- global $wgUser;
- if ( !$wgUser->matchEditToken( $moduleParams['token'], $salt, $this->getRequest() ) ) {
+ if ( !$this->getUser()->matchEditToken( $moduleParams['token'], $salt ) ) {
$this->dieUsageMsg( 'sessionfailure' );
}
}
* @param $module ApiBase An Api module
*/
protected function checkExecutePermissions( $module ) {
- global $wgUser;
+ $user = $this->getUser();
if ( $module->isReadMode() && !in_array( 'read', User::getGroupPermissions( array( '*' ) ), true ) &&
- !$wgUser->isAllowed( 'read' ) )
+ !$user->isAllowed( 'read' ) )
{
$this->dieUsageMsg( 'readrequired' );
}
if ( !$this->mEnableWrite ) {
$this->dieUsageMsg( 'writedisabled' );
}
- if ( !$wgUser->isAllowed( 'writeapi' ) ) {
+ if ( !$user->isAllowed( 'writeapi' ) ) {
$this->dieUsageMsg( 'writerequired' );
}
if ( wfReadOnly() ) {
*/
protected function setupExternalResponse( $module, $params ) {
// Ignore mustBePosted() for internal calls
- if ( $module->mustBePosted() && !$this->mRequest->wasPosted() ) {
+ if ( $module->mustBePosted() && !$this->getRequest()->wasPosted() ) {
$this->dieUsageMsg( array( 'mustbeposted', $this->mAction ) );
}
$module->profileOut();
if ( !$this->mInternalMode ) {
+ //append Debug information
+ MWDebug::appendDebugInfoToApiResult( $this->getContext(), $this->getResult() );
+
// Print result data
$this->printResult( false );
}
'wait until the replication lag is less than the specified value.',
'In case of a replag error, a HTTP 503 error is returned, with the message like',
'"Waiting for $host: $lag seconds lagged\n".',
- 'See http://www.mediawiki.org/wiki/Manual:Maxlag_parameter for more information',
+ 'See https://www.mediawiki.org/wiki/Manual:Maxlag_parameter for more information',
),
'smaxage' => 'Set the s-maxage header to this many seconds. Errors are never cached',
'maxage' => 'Set the max-age header to this many seconds. Errors are never cached',
'** **',
'** This is an auto-generated MediaWiki API documentation page **',
'** **',
- '** Documentation and Examples: **',
- '** http://www.mediawiki.org/wiki/API **',
+ '** Documentation and Examples: **',
+ '** https://www.mediawiki.org/wiki/API **',
'** **',
'**********************************************************************************************************',
'',
' In the case of an invalid action being passed, these will have a value',
' of "unknown_action"',
'',
- ' For more information see http://www.mediawiki.org/wiki/API:Errors_and_warnings',
+ ' For more information see https://www.mediawiki.org/wiki/API:Errors_and_warnings',
'',
- 'Documentation: http://www.mediawiki.org/wiki/API:Main_page',
- 'FAQ http://www.mediawiki.org/wiki/API:FAQ',
- 'Mailing list: http://lists.wikimedia.org/mailman/listinfo/mediawiki-api',
- 'Api Announcements: http://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce',
- 'Bugs & Requests: http://bugzilla.wikimedia.org/buglist.cgi?component=API&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&order=bugs.delta_ts',
+ 'Documentation: https://www.mediawiki.org/wiki/API:Main_page',
+ 'FAQ https://www.mediawiki.org/wiki/API:FAQ',
+ 'Mailing list: https://lists.wikimedia.org/mailman/listinfo/mediawiki-api',
+ 'Api Announcements: https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce',
+ 'Bugs & Requests: https://bugzilla.wikimedia.org/buglist.cgi?component=API&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&order=bugs.delta_ts',
'',
'',
'',
' Yuri Astrakhan <Firstname><Lastname>@gmail.com (creator, lead developer Sep 2006-Sep 2007)',
'',
'Please send your comments, suggestions and questions to mediawiki-api@lists.wikimedia.org',
- 'or file a bug report at http://bugzilla.wikimedia.org/'
+ 'or file a bug report at https://bugzilla.wikimedia.org/'
);
}
*/
public function canApiHighLimits() {
if ( !isset( $this->mCanApiHighLimits ) ) {
- global $wgUser;
- $this->mCanApiHighLimits = $wgUser->isAllowed( 'apihighlimits' );
+ $this->mCanApiHighLimits = $this->getUser()->isAllowed( 'apihighlimits' );
}
return $this->mCanApiHighLimits;
*/
public function getVersion() {
$vers = array();
- $vers[] = 'MediaWiki: ' . SpecialVersion::getVersion() . "\n http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/";
+ $vers[] = 'MediaWiki: ' . SpecialVersion::getVersion() . "\n https://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/";
$vers[] = __CLASS__ . ': $Id$';
$vers[] = ApiBase::getBaseVersion();
$vers[] = ApiFormatBase::getBaseVersion();
*
* @ingroup API
*/
-class UsageException extends Exception {
+class UsageException extends MWException {
private $mCodestr;
private $mExtraData;