2006-10-01 04:38:31 +00:00
< ? php
2010-02-23 18:05:46 +00:00
/**
2010-12-22 20:52:06 +00:00
*
2006-10-01 04:38:31 +00:00
*
2010-08-07 19:59:42 +00:00
* Created on Sep 4 , 2006
*
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
*/
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
*/
const API_DEFAULT_FORMAT = 'xmlfm' ;
/**
* List of available modules : action name => module class
*/
2010-02-23 18:05:46 +00:00
private static $Modules = array (
2007-05-27 23:50:24 +00:00
'login' => 'ApiLogin' ,
2008-01-08 18:10:58 +00:00
'logout' => 'ApiLogout' ,
2007-05-14 05:28:06 +00:00
'query' => 'ApiQuery' ,
2007-10-08 14:24:54 +00:00
'expandtemplates' => 'ApiExpandTemplates' ,
2007-12-01 13:37:02 +00:00
'parse' => 'ApiParse' ,
2007-12-03 15:52:27 +00:00
'opensearch' => 'ApiOpenSearch' ,
2011-06-06 14:50:34 +00:00
'feedcontributions' => 'ApiFeedContributions' ,
2007-12-03 15:52:27 +00:00
'feedwatchlist' => 'ApiFeedWatchlist' ,
'help' => 'ApiHelp' ,
2008-01-22 21:22:04 +00:00
'paraminfo' => 'ApiParamInfo' ,
2010-10-28 19:20:21 +00:00
'rsd' => 'ApiRsd' ,
2011-05-01 21:56:02 +00:00
'compare' => 'ApiComparePages' ,
2012-03-28 22:02:10 +00:00
'tokens' => 'ApiTokens' ,
2008-04-14 07:45:50 +00:00
2009-03-06 13:49:44 +00:00
// Write modules
'purge' => 'ApiPurge' ,
2007-12-02 14:24:07 +00:00
'rollback' => 'ApiRollback' ,
'delete' => 'ApiDelete' ,
'undelete' => 'ApiUndelete' ,
'protect' => 'ApiProtect' ,
'block' => 'ApiBlock' ,
'unblock' => 'ApiUnblock' ,
2008-01-04 22:33:22 +00:00
'move' => 'ApiMove' ,
2008-03-03 18:08:40 +00:00
'edit' => 'ApiEditPage' ,
here it is ... the upload-api, script-server, js2 (javascript phase2) branch merge 1st attempt.
Here is a short overview of changes and associated default configuration variables (most everything is off by default) also see ~soon to be updated~: http://www.mediawiki.org/wiki/Media_Projects_Overview
= Upload Improvements =
==Upload API ==
* Based on the early work of Bryan Tong and others it adds the upload option to the api.
* We rewrite Special:Upload page to include use the new refactoring
* Added in token checks in both the SpecialUpload.php page so avoids DOS / xss copy-by-url JavaScript based cross site POST file submissions
== Copy by URL==
$wgAllowCopyUploads = false;
* http class rewrite includes a new http background download see: includes/HttpFunctions.php
* spins off a php process that calls: maintenance/http_session_download.php
* pushes updates to the session and gives the user a progress bar on http copy uploads from other server progress (using js2 upload interface) (if not using the js2 upload interface it does the request in-place but the download is limited to the php ini timeout time)
== Firefogg ==
* Firefogg enables resumable upload by chunks
* progress indicators and conditional invokation (js2 system)
* and of-course client side transcoding.
= Script Server =
$wgEnableScriptLoader = false;
* off by default if $wgEnableScriptLoader is turned on script files are grouped, gziped, cached etc.
for more info see: http://www.mediawiki.org/wiki/Extension:ScriptLoader
* Includes some early skin js include fixes (skin/script system still lots of love)
* Includes a "javascript class autoloader" this is packaged into mwEmbed so that the mwEmbed library can work in stand alone mode (while retaining localization and script serving) (one such application is the make page for firefogg.org : http://www.firefogg.org/make/index.html )
* The file that contains the autojavascript loading classes is: js2/php/jsAutoloadLocalClasses.php
* One can use this auto class loading dependency system with extensions and add-ons but I need to better document that.
= js2 system / mwEmbed=
$wgEnableJS2system = false
* includes initial rewrite towards more jquery based javascript code
* especially for the Special:Upload page.
* Also the edit page include support for the "add-media-wizard"
* includes dependency loader for javascript that optionally takes advantage of the script-loader
* remote embedding of javascript interfaces (like embedding video, or commons media searching)
* $wgDebugJavaScript = false; .. .this variable lets you always get "always fresh javascript". When used with the script-loader it does not minify the script-loader output.
= mwEmbed =
* Will commit a separate patch to oggHandler that conditionally outputs <video tag> to use the new javascript video player.
** mv_embed player includes: play-head, volume control, remote embedding, oggz-chop support across plugins.
* add-media-wizard adds easy inserts of media to pages (with import)
== jQuery==
* we include a base install of jQuery, jQuery ui and some plugins.
* all the javascript classes are in the scriptloader so its easy to load any set of jquery ui components that you may need using the script-server. You get a callback so you can then execute js with dependencies loaded.
== other stuff ==
there is a bit more code in js2 that pertains to sequence editing, timed text display and basic image editing. We include a base import of pixastic-lib & pixastic-editor... will work with the pixastic developer to try and ensure upstream compatibility on our usage of the library for in-browser photo and sequence manipulation.
2009-07-14 23:52:14 +00:00
'upload' => 'ApiUpload' ,
2011-03-05 17:23:35 +00:00
'filerevert' => 'ApiFileRevert' ,
2008-06-01 17:58:27 +00:00
'emailuser' => 'ApiEmailUser' ,
2008-08-21 15:44:13 +00:00
'watch' => 'ApiWatch' ,
2008-09-04 15:17:51 +00:00
'patrol' => 'ApiPatrol' ,
2009-02-04 20:11:27 +00:00
'import' => 'ApiImport' ,
2009-03-28 19:08:47 +00:00
'userrights' => 'ApiUserrights' ,
2012-04-16 20:02:34 +00:00
'options' => 'ApiOptions' ,
2006-10-14 07:18:08 +00:00
);
/**
* List of available formats : format name => format class
*/
2010-02-23 18:05:46 +00:00
private static $Formats = array (
2006-10-14 07:18:08 +00:00
'json' => 'ApiFormatJson' ,
'jsonfm' => 'ApiFormatJson' ,
2006-10-22 23:45:20 +00:00
'php' => 'ApiFormatPhp' ,
'phpfm' => 'ApiFormatPhp' ,
'wddx' => 'ApiFormatWddx' ,
'wddxfm' => 'ApiFormatWddx' ,
2006-10-14 07:18:08 +00:00
'xml' => 'ApiFormatXml' ,
'xmlfm' => 'ApiFormatXml' ,
'yaml' => 'ApiFormatYaml' ,
2006-10-22 23:45:20 +00:00
'yamlfm' => 'ApiFormatYaml' ,
2008-01-24 13:12:03 +00:00
'rawfm' => 'ApiFormatJson' ,
'txt' => 'ApiFormatTxt' ,
2008-01-24 13:16:34 +00:00
'txtfm' => 'ApiFormatTxt' ,
'dbg' => 'ApiFormatDbg' ,
2010-08-09 00:19:55 +00:00
'dbgfm' => 'ApiFormatDbg' ,
'dump' => 'ApiFormatDump' ,
'dumpfm' => 'ApiFormatDump' ,
2012-04-16 15:45:51 +00:00
'none' => 'ApiFormatNone' ,
2006-10-14 07:18:08 +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 .
* array ( 'right' => array ( 'msg' => 'Some message with a $1' ,
* 'params' => array ( $someVarToSubst ) ),
* );
*/
2010-02-23 18:05:46 +00:00
private static $mRights = array (
'writeapi' => array (
'msg' => 'Use of the write API' ,
'params' => array ()
),
'apihighlimits' => array (
'msg' => 'Use higher limits in API queries (Slow queries: $1 results; Fast queries: $2 results). The limits for slow queries also apply to multivalue parameters.' ,
2010-07-22 21:54:19 +00:00
'params' => array ( ApiBase :: LIMIT_SML2 , ApiBase :: LIMIT_BIG2 )
2010-02-23 18:05:46 +00:00
)
2008-08-05 16:12:52 +00:00
);
2008-08-04 14:26:20 +00:00
2010-12-23 19:53:28 +00:00
/**
* @ var ApiFormatBase
*/
private $mPrinter ;
private $mModules , $mModuleNames , $mFormats , $mFormatNames ;
2011-11-14 19:44:13 +00:00
private $mResult , $mAction , $mShowVersions , $mEnableWrite ;
2010-07-26 07:35:30 +00:00
private $mInternalMode , $mSquidMaxage , $mModule ;
2006-10-01 04:38:31 +00:00
2010-07-23 07:17:56 +00:00
private $mCacheMode = 'private' ;
private $mCacheControl = array ();
2010-02-14 23:52:45 +00:00
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 .
*
2011-10-26 23:27:01 +00:00
* @ param $context IContextSource | WebRequest - if this is an instance of FauxRequest , errors are thrown and no printing occurs
2010-02-23 18:05:46 +00:00
* @ param $enableWrite bool should be set to true if the api may modify data
*/
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 );
2011-10-27 01:13:19 +00:00
}
2011-10-26 23:27:01 +00:00
$this -> mInternalMode = ( $this -> getRequest () 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
2010-01-11 15:55:52 +00:00
if ( ! $this -> mInternalMode ) {
2007-07-12 06:54:08 +00:00
// Impose module restrictions.
2008-04-14 07:45:50 +00:00
// If the current user cannot read,
2007-07-14 19:04:31 +00:00
// Remove all modules other than login
global $wgUser ;
2008-04-14 07:45:50 +00:00
2011-10-26 23:27:01 +00:00
if ( $this -> getRequest () -> getVal ( 'callback' ) !== null ) {
2008-03-03 05:45:37 +00:00
// JSON callback allows cross-site reads.
// For safety, strip user credentials.
wfDebug ( " API: stripping user credentials for JSON callback \n " );
$wgUser = new User ();
2011-10-26 23:27:01 +00:00
$this -> getContext () -> setUser ( $wgUser );
2008-03-03 05:45:37 +00:00
}
2007-07-12 06:54:08 +00:00
}
2009-03-06 13:49:44 +00:00
global $wgAPIModules ; // extension modules
2010-02-23 18:05:46 +00:00
$this -> mModules = $wgAPIModules + self :: $Modules ;
2007-08-31 15:23:48 +00:00
2010-01-11 15:55:52 +00:00
$this -> mModuleNames = array_keys ( $this -> mModules );
2010-02-23 18:05:46 +00:00
$this -> mFormats = self :: $Formats ;
2010-01-11 15:55:52 +00:00
$this -> mFormatNames = array_keys ( $this -> mFormats );
2006-10-22 23:45:20 +00:00
2010-01-11 15:55:52 +00:00
$this -> mResult = new ApiResult ( $this );
2006-10-01 21:20:55 +00:00
$this -> mShowVersions = false ;
2006-10-02 18:27:06 +00:00
$this -> mEnableWrite = $enableWrite ;
2006-10-22 23:45:20 +00:00
2010-04-17 20:58:04 +00:00
$this -> mSquidMaxage = - 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
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
*
* @ param $maxage
2007-05-20 10:08:40 +00:00
*/
2010-01-11 15:55:52 +00:00
public function setCacheMaxAge ( $maxage ) {
2010-02-14 23:52:45 +00:00
$this -> setCacheControl ( array (
'max-age' => $maxage ,
's-maxage' => $maxage
) );
}
2010-07-23 07:17:56 +00:00
/**
* Set the type of caching headers which will be sent .
*
2010-09-25 16:37:39 +00:00
* @ param $mode String 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 ) {
if ( ! in_array ( $mode , array ( 'private' , 'public' , 'anon-public-user-private' ) ) ) {
2010-07-23 07:33:40 +00:00
wfDebug ( __METHOD__ . " : unrecognised cache mode \" $mode\ " \n " );
2010-07-23 07:17:56 +00:00
// Ignore for forwards-compatibility
return ;
}
if ( ! in_array ( 'read' , User :: getGroupPermissions ( array ( '*' ) ), true ) ) {
// 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 " );
2010-07-23 07:17:56 +00:00
return ;
}
}
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-07-14 19:00:54 +00:00
/**
2011-05-06 21:50:18 +00:00
* @ deprecated since 1.17 Private caching is now the default , so there is usually no
2010-07-23 07:33:40 +00:00
* need to call this function . If there is a need , you can use
2010-07-23 07:17:56 +00:00
* $this -> setCacheMode ( 'private' )
2010-07-14 19:00:54 +00:00
*/
public function setCachePrivate () {
2011-12-13 05:19:05 +00:00
wfDeprecated ( __METHOD__ , '1.17' );
2010-07-23 07:17:56 +00:00
$this -> setCacheMode ( 'private' );
2010-07-14 19:00:54 +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
*
* @ param $directives array
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
2010-07-14 19:00:54 +00:00
/**
* Make sure Vary : Cookie and friends are set . Use this when the output of a request
* may be cached for anons but may not be cached for logged - in users .
*
* WARNING : This function must be called CONSISTENTLY for a given URL . This means that a
* given URL must either always or never call this function ; if it sometimes does and
* sometimes doesn ' t , stuff will break .
2010-07-23 07:17:56 +00:00
*
2011-05-06 21:50:18 +00:00
* @ deprecated since 1.17 Use setCacheMode ( 'anon-public-user-private' )
2010-07-14 19:00:54 +00:00
*/
public function setVaryCookie () {
2011-12-13 05:19:05 +00:00
wfDeprecated ( __METHOD__ , '1.17' );
2010-07-23 07:17:56 +00:00
$this -> setCacheMode ( 'anon-public-user-private' );
2010-07-14 19:00:54 +00:00
}
2006-10-22 23:45:20 +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
*
2011-05-08 16:48:30 +00:00
* @ param $format string
*
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 ) {
2010-02-23 18:05:46 +00:00
if ( ! isset ( $this -> mFormats [ $format ] ) ) {
2008-10-25 16:23:09 +00:00
$this -> dieUsage ( " Unrecognized format: { $format } " , 'unknown_format' );
2010-02-23 18:05:46 +00:00
}
2010-01-11 15:55:52 +00:00
return new $this -> mFormats [ $format ] ( $this , $format );
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 () {
$this -> profileIn ();
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
}
2010-01-22 03:14:52 +00:00
2006-10-14 07:18:08 +00:00
$this -> profileOut ();
}
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 ;
}
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
2006-10-13 05:21:38 +00:00
ob_start ();
2006-10-01 04:38:31 +00:00
try {
2006-10-14 07:18:08 +00:00
$this -> executeAction ();
2010-01-11 15:55:52 +00:00
} catch ( Exception $e ) {
2008-09-21 09:38:55 +00:00
// Log it
2012-05-08 01:02:55 +00:00
if ( ! ( $e instanceof UsageException ) ) {
2008-09-21 09:38:55 +00:00
wfDebugLog ( 'exception' , $e -> getLogMessage () );
}
2006-10-14 07:18:08 +00:00
// Handle any kind of exception by outputing properly formatted error message.
// If this fails, an unhandled exception should be thrown so that global error
// handler will process and log it.
2006-10-22 23:45:20 +00:00
2010-01-11 15:55:52 +00:00
$errCode = $this -> substituteResultWithError ( $e );
2007-07-15 00:52:35 +00:00
2006-10-22 23:45:20 +00:00
// Error results should not be cached
2010-07-23 07:17:56 +00:00
$this -> setCacheMode ( 'private' );
2006-10-22 23:45:20 +00:00
2011-06-05 20:29:47 +00:00
$response = $this -> getRequest () -> response ();
2007-07-15 00:52:35 +00:00
$headerStr = 'MediaWiki-API-Error: ' . $errCode ;
2010-02-23 18:05:46 +00:00
if ( $e -> getCode () === 0 ) {
2011-06-05 19:51:31 +00:00
$response -> header ( $headerStr );
2010-02-23 18:05:46 +00:00
} else {
2011-06-05 19:51:31 +00:00
$response -> header ( $headerStr , true , $e -> getCode () );
2010-02-23 18:05:46 +00:00
}
2007-07-15 00:52:35 +00:00
// Reset and print just the error message
ob_clean ();
// If the error occured during printing, do a printer->profileOut()
$this -> mPrinter -> safeProfileOut ();
2010-01-11 15:55:52 +00:00
$this -> printResult ( true );
2007-07-15 00:52:35 +00:00
}
2010-07-23 07:17:56 +00:00
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.
$this -> sendCacheHeaders ();
if ( $this -> mPrinter -> getIsHtml () && ! $this -> mPrinter -> isDisabled () ) {
echo wfReportTime ();
}
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 .
*
* @ return bool False if the caller should abort ( 403 case ), true otherwise ( all other cases )
*/
protected function handleCORS () {
global $wgCrossSiteAJAXdomains , $wgCrossSiteAJAXdomainExceptions ;
$response = $this -> getRequest () -> response ();
$originParam = $this -> getParameter ( 'origin' ); // defaults to null
if ( $originParam === null ) {
// No origin parameter, nothing to do
return true ;
}
// Origin: header is a space-separated list of origins, check all of them
$originHeader = isset ( $_SERVER [ 'HTTP_ORIGIN' ] ) ? $_SERVER [ 'HTTP_ORIGIN' ] : '' ;
$origins = explode ( ' ' , $originHeader );
if ( ! in_array ( $originParam , $origins ) ) {
// origin parameter set but incorrect
// Send a 403 response
$message = HttpStatus :: getMessage ( 403 );
$response -> header ( " HTTP/1.1 403 $message " , true , 403 );
$response -> header ( 'Cache-Control: no-cache' );
echo " 'origin' parameter does not match Origin header \n " ;
return false ;
}
if ( self :: matchOrigin ( $originParam , $wgCrossSiteAJAXdomains , $wgCrossSiteAJAXdomainExceptions ) ) {
$response -> header ( " Access-Control-Allow-Origin: $originParam " );
$response -> header ( 'Access-Control-Allow-Credentials: true' );
$this -> getOutput () -> addVaryHeader ( 'Origin' );
}
return true ;
}
/**
* Attempt to match an Origin header against a set of rules and a set of exceptions
* @ param $value string Origin header
* @ param $rules array Set of wildcard rules
* @ param $exceptions array Set of wildcard rules
* @ return bool True if $value matches a rule in $rules and doesn ' t match any rules in $exceptions , false otherwise
*/
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 ;
}
}
return true ;
}
}
return false ;
}
/**
* Helper function to convert wildcard string into a regex
* '*' => '.*?'
* '?' => '.'
*
* @ param $wildcard string String with wildcards
* @ return string Regular expression
*/
protected static function wildcardToRegex ( $wildcard ) {
$wildcard = preg_quote ( $wildcard , '/' );
$wildcard = str_replace (
array ( '\*' , '\?' ),
array ( '.*?' , '.' ),
$wildcard
);
return " /https?: \ / \ / $wildcard / " ;
}
2010-07-23 07:17:56 +00:00
protected function sendCacheHeaders () {
2011-11-08 16:06:59 +00:00
global $wgUseXVO , $wgVaryOnXFP ;
2011-06-05 20:29:47 +00:00
$response = $this -> getRequest () -> response ();
2012-06-01 11:11:32 +00:00
$out = $this -> getOutput ();
if ( $wgVaryOnXFP ) {
$out -> addVaryHeader ( 'X-Forwarded-Proto' );
}
2011-06-05 19:51:31 +00:00
2010-07-23 07:17:56 +00:00
if ( $this -> mCacheMode == 'private' ) {
2011-06-05 19:51:31 +00:00
$response -> header ( 'Cache-Control: private' );
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 () );
2010-07-23 07:17:56 +00:00
if ( $wgUseXVO ) {
2011-11-08 16:06:59 +00:00
$response -> header ( $out -> getXVO () );
if ( $out -> haveCacheVaryCookies () ) {
2010-07-23 07:17:56 +00:00
// Logged in, mark this request private
2011-06-05 19:51:31 +00:00
$response -> header ( 'Cache-Control: private' );
2010-07-23 07:17:56 +00:00
return ;
}
// Logged out, send normal public headers below
} elseif ( session_id () != '' ) {
// Logged in or otherwise has session (e.g. anonymous users who have edited)
// Mark request private
2011-06-05 19:51:31 +00:00
$response -> header ( 'Cache-Control: private' );
2010-07-23 07:17:56 +00:00
return ;
} // else no XVO and 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 () );
if ( $wgUseXVO ) {
$response -> header ( $out -> getXVO () );
2011-08-03 12:00:47 +00:00
}
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().
2011-06-05 19:51:31 +00:00
$response -> header ( 'Cache-Control: private' );
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
}
/**
* Replace the result data with the information about an exception .
2008-04-14 07:45:50 +00:00
* Returns the error code
2010-10-09 00:01:45 +00:00
* @ param $e Exception
2011-01-30 08:16:13 +00:00
* @ return string
2007-07-15 00:52:35 +00:00
*/
2010-01-11 15:55:52 +00:00
protected function substituteResultWithError ( $e ) {
2011-10-26 19:39:56 +00:00
global $wgShowHostnames ;
2011-06-30 01:06:17 +00:00
$result = $this -> getResult ();
2010-02-15 23:56:09 +00:00
// Printer may not be initialized if the extractRequestParams() fails for the main module
if ( ! isset ( $this -> mPrinter ) ) {
// The printer has not been created yet. Try to manually get formatter value.
$value = $this -> getRequest () -> getVal ( 'format' , self :: API_DEFAULT_FORMAT );
2010-02-23 18:05:46 +00:00
if ( ! in_array ( $value , $this -> mFormatNames ) ) {
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 );
2010-02-23 18:05:46 +00:00
if ( $this -> mPrinter -> getNeedsRawData () ) {
2011-06-30 01:06:17 +00:00
$result -> setRawMode ();
2010-02-23 18:05:46 +00:00
}
2010-02-15 23:56:09 +00:00
}
2006-10-22 23:45:20 +00:00
2010-02-15 23:56:09 +00:00
if ( $e instanceof UsageException ) {
// User entered incorrect parameters - print usage screen
$errMessage = $e -> getMessageArray ();
2008-04-14 07:45:50 +00:00
2010-02-15 23:56:09 +00:00
// Only print the help message when this is for the developer, not runtime
2010-02-23 18:05:46 +00:00
if ( $this -> mPrinter -> getWantsHelp () || $this -> mAction == 'help' ) {
ApiResult :: setContent ( $errMessage , $this -> makeHelpMsg () );
}
2006-10-22 23:45:20 +00:00
2010-02-15 23:56:09 +00:00
} else {
global $wgShowSQLErrors , $wgShowExceptionDetails ;
// Something is seriously wrong
if ( ( $e instanceof DBQueryError ) && ! $wgShowSQLErrors ) {
2010-02-23 18:05:46 +00:00
$info = 'Database query error' ;
2006-10-14 07:18:08 +00:00
} else {
2010-02-15 23:56:09 +00:00
$info = " Exception Caught: { $e -> getMessage () } " ;
2006-10-14 07:18:08 +00:00
}
2006-10-22 23:45:20 +00:00
2010-02-23 18:05:46 +00:00
$errMessage = array (
2010-02-15 23:56:09 +00:00
'code' => 'internal_api_error_' . get_class ( $e ),
'info' => $info ,
);
2010-02-23 18:05:46 +00:00
ApiResult :: setContent ( $errMessage , $wgShowExceptionDetails ? " \n \n { $e -> getTraceAsString () } \n \n " : '' );
2010-02-15 23:56:09 +00:00
}
2011-06-30 01:06:17 +00:00
$result -> reset ();
$result -> disableSizeCheck ();
2010-02-15 23:56:09 +00:00
// Re-add the id
$requestid = $this -> getParameter ( 'requestid' );
2010-02-23 18:05:46 +00:00
if ( ! is_null ( $requestid ) ) {
2011-06-30 01:06:17 +00:00
$result -> addValue ( null , 'requestid' , $requestid );
2010-02-23 18:05:46 +00:00
}
2011-10-26 19:39:56 +00:00
if ( $wgShowHostnames ) {
// servedby is especially useful when debugging errors
$result -> addValue ( null , 'servedby' , wfHostName () );
}
2011-06-30 01:06:17 +00:00
$result -> addValue ( null , 'error' , $errMessage );
2006-10-17 02:01:20 +00:00
2007-07-15 00:52:35 +00:00
return $errMessage [ 'code' ];
2006-10-01 04:38:31 +00:00
}
2006-10-14 07:18:08 +00:00
/**
2010-03-30 17:14:53 +00:00
* Set up for the execution .
2011-01-30 08:16:13 +00:00
* @ return array
2006-10-14 07:18:08 +00:00
*/
2010-03-30 17:14:53 +00:00
protected function setupExecuteAction () {
2011-10-26 19:39:56 +00:00
global $wgShowHostnames ;
2008-08-05 16:12:52 +00:00
// First add the id to the top element
2011-06-30 01:06:17 +00:00
$result = $this -> getResult ();
2010-01-11 15:55:52 +00:00
$requestid = $this -> getParameter ( 'requestid' );
2010-02-23 18:05:46 +00:00
if ( ! is_null ( $requestid ) ) {
2011-06-30 01:06:17 +00:00
$result -> addValue ( null , 'requestid' , $requestid );
2010-02-23 18:05:46 +00:00
}
2011-10-26 19:39:56 +00:00
if ( $wgShowHostnames ) {
$servedby = $this -> getParameter ( 'servedby' );
if ( $servedby ) {
$result -> addValue ( null , 'servedby' , wfHostName () );
}
2010-07-10 10:37:39 +00:00
}
2008-01-08 18:10:58 +00:00
2007-05-28 06:59:19 +00:00
$params = $this -> extractRequestParams ();
2010-02-22 03:34:56 +00:00
2007-05-28 06:59:19 +00:00
$this -> mShowVersions = $params [ 'version' ];
2007-06-29 22:05:30 +00:00
$this -> mAction = $params [ 'action' ];
2008-06-20 10:51:17 +00:00
2010-01-11 15:55:52 +00:00
if ( ! is_string ( $this -> mAction ) ) {
2010-02-23 18:05:46 +00:00
$this -> dieUsage ( 'The API requires a valid action parameter' , 'unknown_action' );
2008-07-30 07:23:22 +00:00
}
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
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
2010-01-11 15:55:52 +00:00
$module = new $this -> mModules [ $this -> mAction ] ( $this , $this -> mAction );
2009-08-27 22:09:28 +00:00
$this -> mModule = $module ;
2008-04-14 07:45:50 +00:00
2010-02-15 23:53:43 +00:00
$moduleParams = $module -> extractRequestParams ();
2010-02-23 18:05:46 +00:00
2010-02-19 01:25:57 +00:00
// Die if token required, but not provided (unless there is a gettoken parameter)
2012-04-15 17:40:20 +00:00
if ( isset ( $moduleParams [ 'gettoken' ] ) ) {
$gettoken = $moduleParams [ 'gettoken' ];
} else {
$gettoken = false ;
}
2010-02-15 23:53:43 +00:00
$salt = $module -> getTokenSalt ();
2012-04-15 17:40:20 +00:00
if ( $salt !== false && ! $gettoken ) {
2010-02-19 19:44:28 +00:00
if ( ! isset ( $moduleParams [ 'token' ] ) ) {
2010-02-15 23:53:43 +00:00
$this -> dieUsageMsg ( array ( 'missingparam' , 'token' ) );
} else {
2012-06-25 11:39:29 +00:00
if ( ! $this -> getUser () -> matchEditToken ( $moduleParams [ 'token' ], $salt , $this -> getContext () -> getRequest () ) ) {
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'sessionfailure' );
2010-02-15 23:53:43 +00:00
}
}
}
2010-03-30 17:14:53 +00:00
return $module ;
}
2010-02-14 22:20:27 +00:00
2010-03-30 17:14:53 +00:00
/**
* Check the max lag if necessary
2010-04-11 19:10:15 +00:00
* @ param $module ApiBase object : Api module being used
2010-03-30 17:14:53 +00:00
* @ param $params Array an array containing the request parameters .
* @ return boolean True on success , false should exit immediately
*/
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
// Check for maxlag
2008-03-30 09:48:15 +00:00
global $wgShowHostnames ;
2007-11-19 15:57:58 +00:00
$maxLag = $params [ 'maxlag' ];
2008-03-30 09:48:15 +00:00
list ( $host , $lag ) = wfGetLB () -> getMaxLag ();
2007-11-19 15:57:58 +00:00
if ( $lag > $maxLag ) {
2011-06-05 20:29:47 +00:00
$response = $this -> getRequest () -> response ();
2011-06-05 19:51:31 +00:00
$response -> header ( 'Retry-After: ' . max ( intval ( $maxLag ), 5 ) );
$response -> header ( 'X-Database-Lag: ' . intval ( $lag ) );
2010-01-11 15:55:52 +00:00
if ( $wgShowHostnames ) {
2009-07-16 08:04:15 +00:00
$this -> dieUsage ( " Waiting for $host : $lag seconds lagged " , 'maxlag' );
2007-11-19 15:57:58 +00:00
} else {
2009-07-16 08:04:15 +00:00
$this -> dieUsage ( " Waiting for a database server: $lag seconds lagged " , 'maxlag' );
2007-11-19 15:57:58 +00:00
}
2010-03-30 18:40:23 +00:00
return false ;
2007-11-19 15:57:58 +00:00
}
}
2010-03-30 17:14:53 +00:00
return true ;
}
/**
* Check for sufficient permissions to execute
2010-09-25 16:56:03 +00:00
* @ param $module ApiBase 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 ();
2010-07-14 19:00:54 +00:00
if ( $module -> isReadMode () && ! in_array ( 'read' , User :: getGroupPermissions ( array ( '*' ) ), true ) &&
2011-10-26 23:27:01 +00:00
! $user -> isAllowed ( 'read' ) )
2010-02-23 18:05:46 +00:00
{
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'readrequired' );
2010-02-23 18:05:46 +00:00
}
2010-01-11 15:55:52 +00:00
if ( $module -> isWriteMode () ) {
2010-02-23 18:05:46 +00:00
if ( ! $this -> mEnableWrite ) {
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'writedisabled' );
2010-02-23 18:05:46 +00:00
}
2011-10-26 23:27:01 +00:00
if ( ! $user -> isAllowed ( 'writeapi' ) ) {
2011-05-19 17:51:16 +00:00
$this -> dieUsageMsg ( 'writerequired' );
2010-02-23 18:05:46 +00:00
}
if ( wfReadOnly () ) {
2009-07-12 12:38:03 +00:00
$this -> dieReadOnly ();
2010-02-23 18:05:46 +00:00
}
2009-03-06 13:49:44 +00:00
}
2010-03-30 17:14:53 +00:00
}
2009-03-06 13:49:44 +00:00
2010-03-30 17:14:53 +00:00
/**
* Check POST for external response and setup result printer
2010-09-25 16:56:03 +00:00
* @ param $module ApiBase An Api module
2010-03-30 17:14:53 +00:00
* @ param $params Array an array with the request parameters
*/
2010-04-17 20:58:04 +00:00
protected function setupExternalResponse ( $module , $params ) {
2010-03-30 17:14:53 +00:00
// Ignore mustBePosted() for internal calls
2011-10-26 23:27:01 +00:00
if ( $module -> mustBePosted () && ! $this -> getRequest () -> wasPosted () ) {
2010-03-30 17:14:53 +00:00
$this -> dieUsageMsg ( array ( 'mustbeposted' , $this -> mAction ) );
}
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' ] );
}
2006-10-22 23:45:20 +00:00
2010-03-30 17:14:53 +00:00
if ( $this -> mPrinter -> getNeedsRawData () ) {
$this -> getResult () -> setRawMode ();
}
}
/**
* Execute the actual module , without any error handling
*/
protected function executeAction () {
$params = $this -> setupExecuteAction ();
$module = $this -> setupModule ();
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
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
// Execute
$module -> profileIn ();
$module -> execute ();
2010-01-11 15:55:52 +00:00
wfRunHooks ( 'APIAfterExecute' , array ( & $module ) );
2006-10-14 07:18:08 +00:00
$module -> profileOut ();
2006-10-22 23:45:20 +00:00
2010-01-11 15:55:52 +00:00
if ( ! $this -> mInternalMode ) {
2012-05-13 09:20:04 +00:00
//append Debug information
MWDebug :: appendDebugInfoToApiResult ( $this -> getContext (), $this -> getResult () );
2006-10-14 07:18:08 +00:00
// Print result data
2010-01-11 15:55:52 +00:00
$this -> printResult ( false );
2006-10-14 07:18:08 +00:00
}
}
2006-10-22 23:45:20 +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
*
* @ param $isError bool
2006-10-01 04:38:31 +00:00
*/
2010-01-11 15:55:52 +00:00
protected function printResult ( $isError ) {
2009-02-10 15:56:34 +00:00
$this -> getResult () -> cleanUpUTF8 ();
2006-10-01 04:38:31 +00:00
$printer = $this -> mPrinter ;
$printer -> profileIn ();
2008-04-14 07:45:50 +00:00
2010-02-23 18:05:46 +00:00
/**
* If the help message is requested in the default ( xmlfm ) format ,
2007-09-18 22:10:09 +00:00
* tell the printer not to escape ampersands so that our links do
2010-02-23 18:05:46 +00:00
* not break .
*/
$printer -> setUnescapeAmps ( ( $this -> mAction == 'help' || $isError )
2008-11-14 00:30:34 +00:00
&& $printer -> getFormat () == 'XML' && $printer -> getIsHtml () );
2007-09-18 22:10:09 +00:00
2010-01-11 15:55:52 +00:00
$printer -> initPrinter ( $isError );
2007-10-24 19:34:29 +00:00
2006-10-15 07:43:52 +00:00
$printer -> execute ();
2006-10-01 04:38:31 +00:00
$printer -> closePrinter ();
$printer -> profileOut ();
}
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 () {
2010-02-23 18:05:46 +00:00
return array (
'format' => array (
ApiBase :: PARAM_DFLT => ApiMain :: API_DEFAULT_FORMAT ,
ApiBase :: PARAM_TYPE => $this -> mFormatNames
2006-10-14 07:18:08 +00:00
),
2010-02-23 18:05:46 +00:00
'action' => array (
ApiBase :: PARAM_DFLT => 'help' ,
ApiBase :: PARAM_TYPE => $this -> mModuleNames
2006-10-14 07:18:08 +00:00
),
2007-11-19 15:57:58 +00:00
'version' => false ,
2010-02-23 18:05:46 +00:00
'maxlag' => array (
ApiBase :: PARAM_TYPE => 'integer'
2007-11-19 15:57:58 +00:00
),
2010-02-23 18:05:46 +00:00
'smaxage' => array (
ApiBase :: PARAM_TYPE => 'integer' ,
ApiBase :: PARAM_DFLT => 0
2008-06-16 19:49:43 +00:00
),
2010-02-23 18:05:46 +00:00
'maxage' => array (
ApiBase :: PARAM_TYPE => 'integer' ,
ApiBase :: PARAM_DFLT => 0
2008-06-16 20:06:23 +00:00
),
2008-08-05 16:12:52 +00:00
'requestid' => null ,
2010-07-10 10:37:39 +00:00
'servedby' => 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 ,
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 getParamDescription () {
2010-02-23 18:05:46 +00:00
return array (
2006-10-14 07:18:08 +00:00
'format' => 'The format of the output' ,
2010-05-15 11:07:31 +00:00
'action' => 'What action you would like to perform. See below for module help' ,
2007-11-19 15:57:58 +00:00
'version' => 'When showing help, include version for each module' ,
2011-06-04 11:31:22 +00:00
'maxlag' => array (
'Maximum lag can be used when MediaWiki is installed on a database replicated cluster.' ,
'To save actions causing any more site replication lag, this parameter can make the client' ,
'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".' ,
2011-11-28 15:43:11 +00:00
'See https://www.mediawiki.org/wiki/Manual:Maxlag_parameter for more information' ,
2011-06-04 11:31:22 +00:00
),
2008-06-16 20:06:23 +00:00
'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' ,
2008-08-05 16:12:52 +00:00
'requestid' => 'Request ID to distinguish requests. This will just be output back to you' ,
2010-07-10 10:37:39 +00:00
'servedby' => 'Include the hostname that served the request in the results. Unconditionally shown on error' ,
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' => array (
'When accessing the API using a cross-domain AJAX request (CORS), set this to the originating domain.' ,
'This must match one of the origins in the Origin: header exactly, so it has to be set to something like http://en.wikipedia.org or https://meta.wikimedia.org .' ,
'If this parameter does not match the Origin: header, a 403 response will be returned.' ,
'If this parameter matches the Origin: header and the origin is whitelisted, an Access-Control-Allow-Origin header will be set.' ,
),
2006-10-14 07:18:08 +00:00
);
2006-10-12 03:15:42 +00:00
}
2006-10-01 04:38:31 +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 getDescription () {
2010-02-23 18:05:46 +00:00
return array (
2007-07-08 08:32:00 +00:00
'' ,
2006-10-01 04:38:31 +00:00
'' ,
2011-01-07 03:12:07 +00:00
'**********************************************************************************************************' ,
'** **' ,
'** This is an auto-generated MediaWiki API documentation page **' ,
'** **' ,
2012-01-30 10:15:20 +00:00
'** Documentation and Examples: **' ,
'** https://www.mediawiki.org/wiki/API **' ,
2011-01-07 03:12:07 +00:00
'** **' ,
'**********************************************************************************************************' ,
2006-10-30 00:18:05 +00:00
'' ,
2010-09-03 01:06:06 +00:00
'Status: All features shown on this page should be working, but the API' ,
2011-06-05 15:10:11 +00:00
' is still in active development, and may change at any time.' ,
2010-09-03 01:06:06 +00:00
' Make sure to monitor our mailing list for any updates' ,
2007-07-06 02:19:56 +00:00
'' ,
2011-06-05 15:12:38 +00:00
'Erroneous requests: When erroneous requests are sent to the API, a HTTP header will be sent' ,
2011-06-05 15:10:11 +00:00
' with the key "MediaWiki-API-Error" and then both the value of the' ,
' header and the error code sent back will be set to the same value' ,
'' ,
' In the case of an invalid action being passed, these will have a value' ,
' of "unknown_action"' ,
'' ,
2011-11-28 15:43:11 +00:00
' For more information see https://www.mediawiki.org/wiki/API:Errors_and_warnings' ,
2011-07-17 17:23:51 +00:00
'' ,
2011-11-28 15:17:46 +00:00
'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' ,
2007-07-06 02:19:56 +00:00
'' ,
'' ,
'' ,
2007-07-08 08:32:00 +00:00
'' ,
'' ,
2006-10-01 04:38:31 +00:00
);
}
2010-02-22 03:34:56 +00:00
2011-05-08 16:48:30 +00:00
/**
* @ return array
*/
2010-02-23 18:05:46 +00:00
public function getPossibleErrors () {
2010-02-13 00:09:05 +00:00
return array_merge ( parent :: getPossibleErrors (), array (
array ( 'readonlytext' ),
2010-02-13 01:05:14 +00:00
array ( 'code' => 'unknown_format' , 'info' => 'Unrecognized format: format' ),
array ( 'code' => 'unknown_action' , 'info' => 'The API requires a valid action parameter' ),
array ( 'code' => 'maxlag' , 'info' => 'Waiting for host: x seconds lagged' ),
array ( 'code' => 'maxlag' , 'info' => 'Waiting for a database server: x seconds lagged' ),
2010-02-23 18:05:46 +00:00
) );
2010-02-13 00:09:05 +00:00
}
2008-04-14 07:45:50 +00:00
2007-05-20 10:08:40 +00:00
/**
* Returns an array of strings with credits for the API
2011-05-08 16:48:30 +00:00
* @ return array
2007-05-20 10:08:40 +00:00
*/
2006-10-30 00:18:05 +00:00
protected function getCredits () {
return array (
2008-03-20 06:54:24 +00:00
'API developers:' ,
2012-07-15 20:13:02 +00:00
' Roan Kattouw "<Firstname>.<Lastname>@gmail.com" (lead developer Sep 2007-present)' ,
2008-03-20 06:54:24 +00:00
' Victor Vasiliev - vasilvv at gee mail dot com' ,
2008-08-19 15:08:14 +00:00
' Bryan Tong Minh - bryan . tongminh @ gmail . com' ,
2010-01-31 23:28:33 +00:00
' Sam Reed - sam @ reedyboy . net' ,
2012-07-15 20:13:02 +00:00
' Yuri Astrakhan "<Firstname><Lastname>@gmail.com" (creator, lead developer Sep 2006-Sep 2007)' ,
2008-03-17 18:16:51 +00:00
'' ,
2008-02-07 15:58:53 +00:00
'Please send your comments, suggestions and questions to mediawiki-api@lists.wikimedia.org' ,
2011-11-28 15:43:11 +00:00
'or file a bug report at https://bugzilla.wikimedia.org/'
2006-10-30 00:18:05 +00:00
);
}
2011-05-08 16:48:30 +00:00
2010-05-08 11:45:41 +00:00
/**
* Sets whether the pretty - printer should format * bold * and $italics $
2011-05-08 16:48:30 +00:00
*
* @ param $help bool
2010-05-08 11:45:41 +00:00
*/
public function setHelp ( $help = true ) {
$this -> mPrinter -> setHelp ( $help );
}
2006-10-01 04:38:31 +00:00
/**
* Override the parent to generate help messages for all available modules .
2011-05-08 16:48:30 +00:00
*
* @ return string
2006-10-01 04:38:31 +00:00
*/
public function makeHelpMsg () {
2011-01-10 18:08:54 +00:00
global $wgMemc , $wgAPICacheHelpTimeout ;
2010-05-08 11:45:41 +00:00
$this -> setHelp ();
2009-08-27 18:25:51 +00:00
// Get help text from cache if present
2010-10-18 09:19:20 +00:00
$key = wfMemcKey ( 'apihelp' , $this -> getModuleName (),
2010-01-11 15:55:52 +00:00
SpecialVersion :: getVersion ( 'nodb' ) .
2011-06-05 20:29:47 +00:00
$this -> getShowVersions () );
2011-01-10 18:08:54 +00:00
if ( $wgAPICacheHelpTimeout > 0 ) {
2009-09-09 12:54:56 +00:00
$cached = $wgMemc -> get ( $key );
2010-02-23 18:05:46 +00:00
if ( $cached ) {
2009-09-09 12:54:56 +00:00
return $cached ;
2010-02-23 18:05:46 +00:00
}
2009-09-09 12:54:56 +00:00
}
2009-08-27 18:25:51 +00:00
$retval = $this -> reallyMakeHelpMsg ();
2011-01-10 18:08:54 +00:00
if ( $wgAPICacheHelpTimeout > 0 ) {
2009-09-17 19:39:34 +00:00
$wgMemc -> set ( $key , $retval , $wgAPICacheHelpTimeout );
2010-02-23 18:05:46 +00:00
}
2009-08-27 18:25:51 +00:00
return $retval ;
}
2010-01-22 03:14:52 +00:00
2011-05-08 16:48:30 +00:00
/**
* @ return mixed | string
*/
2009-08-27 18:25:51 +00:00
public function reallyMakeHelpMsg () {
2010-05-08 11:45:41 +00:00
$this -> setHelp ();
2006-10-01 04:38:31 +00:00
// Use parent to make default message for the main module
2010-02-23 18:05:46 +00:00
$msg = parent :: makeHelpMsg ();
2006-10-01 04:38:31 +00:00
2011-01-07 03:04:03 +00:00
$astriks = str_repeat ( '*** ' , 14 );
2006-10-01 04:38:31 +00:00
$msg .= " \n \n $astriks Modules $astriks\n\n " ;
2010-11-06 16:11:19 +00:00
foreach ( array_keys ( $this -> mModules ) as $moduleName ) {
2010-01-11 15:55:52 +00:00
$module = new $this -> mModules [ $moduleName ] ( $this , $moduleName );
$msg .= self :: makeHelpMsgHeader ( $module , 'action' );
2006-10-01 04:38:31 +00:00
$msg2 = $module -> makeHelpMsg ();
2010-02-23 18:05:46 +00:00
if ( $msg2 !== false ) {
2006-10-01 04:38:31 +00:00
$msg .= $msg2 ;
2010-02-23 18:05:46 +00:00
}
2006-10-01 04:38:31 +00:00
$msg .= " \n " ;
}
2008-08-04 14:26:20 +00:00
$msg .= " \n $astriks Permissions $astriks\n\n " ;
2010-02-23 18:05:46 +00:00
foreach ( self :: $mRights as $right => $rightMsg ) {
2008-08-04 14:26:20 +00:00
$groups = User :: getGroupsWithPermission ( $right );
2010-01-11 15:55:52 +00:00
$msg .= " * " . $right . " * \n " . wfMsgReplaceArgs ( $rightMsg [ 'msg' ], $rightMsg [ 'params' ] ) .
2010-05-16 20:00:25 +00:00
" \n Granted to: \n " . str_replace ( '*' , 'all' , implode ( ', ' , $groups ) ) . " \n \n " ;
2008-08-04 14:26:20 +00:00
}
2006-10-01 04:38:31 +00:00
$msg .= " \n $astriks Formats $astriks\n\n " ;
2010-11-06 16:11:19 +00:00
foreach ( array_keys ( $this -> mFormats ) as $formatName ) {
2010-01-11 15:55:52 +00:00
$module = $this -> createPrinterByName ( $formatName );
$msg .= self :: makeHelpMsgHeader ( $module , 'format' );
2006-10-01 04:38:31 +00:00
$msg2 = $module -> makeHelpMsg ();
2010-02-23 18:05:46 +00:00
if ( $msg2 !== false ) {
2006-10-01 04:38:31 +00:00
$msg .= $msg2 ;
2010-02-23 18:05:46 +00:00
}
2006-10-01 04:38:31 +00:00
$msg .= " \n " ;
}
2008-04-14 07:45:50 +00:00
2010-01-11 15:55:52 +00:00
$msg .= " \n *** Credits: *** \n " . implode ( " \n " , $this -> getCredits () ) . " \n " ;
2008-04-14 07:45:50 +00:00
2006-10-01 04:38:31 +00:00
return $msg ;
}
2010-12-23 19:24:38 +00:00
/**
2011-05-08 16:48:30 +00:00
* @ param $module ApiBase
* @ param $paramName String What type of request is this ? e . g . action , query , list , prop , meta , format
2010-12-23 19:24:38 +00:00
* @ return string
*/
2010-01-11 15:55:52 +00:00
public static function makeHelpMsgHeader ( $module , $paramName ) {
2007-07-07 03:05:09 +00:00
$modulePrefix = $module -> getModulePrefix ();
2010-02-23 18:05:46 +00:00
if ( strval ( $modulePrefix ) !== '' ) {
2008-04-14 07:45:50 +00:00
$modulePrefix = " ( $modulePrefix ) " ;
2010-02-23 18:05:46 +00:00
}
2008-04-14 07:45:50 +00:00
2007-07-07 03:05:09 +00:00
return " * $paramName = { $module -> getModuleName () } $modulePrefix * " ;
2008-04-14 07:45:50 +00:00
}
2007-05-21 06:32:32 +00:00
2007-11-29 14:51:58 +00:00
private $mCanApiHighLimits = null ;
2008-04-14 07:45:50 +00:00
2008-05-10 09:29:34 +00:00
/**
* Check whether the current user is allowed to use high limits
* @ return bool
*/
2007-11-29 14:51:58 +00:00
public function canApiHighLimits () {
2010-01-11 15:55:52 +00:00
if ( ! isset ( $this -> mCanApiHighLimits ) ) {
2011-10-26 23:27:01 +00:00
$this -> mCanApiHighLimits = $this -> getUser () -> isAllowed ( 'apihighlimits' );
2007-11-29 14:51:58 +00:00
}
return $this -> mCanApiHighLimits ;
}
2006-10-01 21:20:55 +00:00
2008-05-10 09:29:34 +00:00
/**
* Check whether the user wants us to show version information in the API help
* @ return bool
*/
2006-10-14 07:18:08 +00:00
public function getShowVersions () {
return $this -> mShowVersions ;
}
2007-05-20 10:08:40 +00:00
/**
* Returns the version information of this file , plus it includes
* the versions for all files that are not callable proper API modules
2011-05-08 16:48:30 +00:00
*
* @ return array
2007-05-20 10:08:40 +00:00
*/
2006-10-01 21:20:55 +00:00
public function getVersion () {
2011-05-08 16:48:30 +00:00
$vers = array ();
2011-11-28 15:43:11 +00:00
$vers [] = 'MediaWiki: ' . SpecialVersion :: getVersion () . " \n https://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/ " ;
2006-10-02 18:27:06 +00:00
$vers [] = __CLASS__ . ': $Id$' ;
2010-02-23 18:05:46 +00:00
$vers [] = ApiBase :: getBaseVersion ();
$vers [] = ApiFormatBase :: getBaseVersion ();
$vers [] = ApiQueryBase :: getBaseVersion ();
2006-10-02 18:27:06 +00:00
return $vers ;
2006-10-01 21:20:55 +00:00
}
2007-07-18 05:25:53 +00:00
/**
* Add or overwrite a module in this ApiMain instance . Intended for use by extending
2008-04-14 07:45:50 +00:00
* classes who wish to add their own modules to their lexicon or override the
2007-07-18 05:25:53 +00:00
* behavior of inherent ones .
*
* @ param $mdlName String The identifier for this module .
* @ param $mdlClass String The class where this module is implemented .
*/
protected function addModule ( $mdlName , $mdlClass ) {
$this -> mModules [ $mdlName ] = $mdlClass ;
}
/**
* Add or overwrite an output format for this ApiMain . Intended for use by extending
* classes who wish to add to or modify current formatters .
*
2011-05-08 16:48:30 +00:00
* @ param $fmtName string The identifier for this format .
* @ param $fmtClass ApiFormatBase The class implementing this format .
2007-07-18 05:25:53 +00:00
*/
protected function addFormat ( $fmtName , $fmtClass ) {
$this -> mFormats [ $fmtName ] = $fmtClass ;
}
2008-04-14 07:45:50 +00:00
2008-01-23 18:40:40 +00:00
/**
* Get the array mapping module names to class names
2011-01-30 08:16:13 +00:00
* @ return array
2008-01-23 18:40:40 +00:00
*/
function getModules () {
return $this -> mModules ;
}
2011-05-03 16:54:16 +00:00
/**
* Returns the list of supported formats in form ( 'format' => 'ClassName' )
*
* @ since 1.18
* @ return array
*/
public function getFormats () {
return $this -> mFormats ;
}
2006-10-01 04:38:31 +00:00
}
/**
Some small doc tweaks to reduce Doxygen warnings, namely:
* @link. You might think @link would surely mean "here comes a web URL" ... but @link is a valid command
in Doxygen, which means an entirely different kind of link (an internal link to somewhere, so that you can separate
documentation and implementation). The result is a mess, and the best solution I can see is to use "@see" instead of "@link".
* Warning: argument `nourl' of command @param is not found in the argument list of Linker::makeMediaLinkObj($title,$text='')
* Moving few class descriptions to right above classes, and/or formatting into Javadoc style.
* "@addtogroup Special Pages" --> "@addtogroup SpecialPage" so that all special pages have the same @addtogroup tag.
* @fixme --> @todo (must have missed these before)
* "@param $specialPage @see" remove the "@" in the "@see" to stop warning.
* @throws wants type, then a brief description, to stop warning.
This last one is for PHPdocumentor only, but it fixes something for PHPDocumentor, and should be neutral for Doxygen:
* WARNING in includes/api/ApiFormatYaml_spyc.php on line 860: docblock template never terminated with /**#@-*/
2007-04-18 09:50:10 +00:00
* This exception will be thrown when dieUsage is called to stop module execution .
2007-05-20 10:08:40 +00:00
* The exception handling code will print a help screen explaining how this API may be used .
2008-04-14 07:45:50 +00:00
*
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
Some small doc tweaks to reduce Doxygen warnings, namely:
* @link. You might think @link would surely mean "here comes a web URL" ... but @link is a valid command
in Doxygen, which means an entirely different kind of link (an internal link to somewhere, so that you can separate
documentation and implementation). The result is a mess, and the best solution I can see is to use "@see" instead of "@link".
* Warning: argument `nourl' of command @param is not found in the argument list of Linker::makeMediaLinkObj($title,$text='')
* Moving few class descriptions to right above classes, and/or formatting into Javadoc style.
* "@addtogroup Special Pages" --> "@addtogroup SpecialPage" so that all special pages have the same @addtogroup tag.
* @fixme --> @todo (must have missed these before)
* "@param $specialPage @see" remove the "@" in the "@see" to stop warning.
* @throws wants type, then a brief description, to stop warning.
This last one is for PHPdocumentor only, but it fixes something for PHPDocumentor, and should be neutral for Doxygen:
* WARNING in includes/api/ApiFormatYaml_spyc.php on line 860: docblock template never terminated with /**#@-*/
2007-04-18 09:50:10 +00:00
*/
2012-03-16 02:02:41 +00:00
class UsageException extends MWException {
2006-10-01 04:38:31 +00:00
2006-10-14 07:18:08 +00:00
private $mCodestr ;
2012-07-12 15:53:00 +00:00
/**
* @ var null | array
*/
2009-07-12 12:38:03 +00:00
private $mExtraData ;
2006-10-01 04:38:31 +00:00
2012-07-12 15:53:00 +00:00
/**
* @ param $message string
* @ param $codestr string
* @ param $code int
* @ param $extradata array | null
*/
2010-01-11 15:55:52 +00:00
public function __construct ( $message , $codestr , $code = 0 , $extradata = null ) {
2010-02-23 18:05:46 +00:00
parent :: __construct ( $message , $code );
2006-10-14 07:18:08 +00:00
$this -> mCodestr = $codestr ;
2009-07-12 12:38:03 +00:00
$this -> mExtraData = $extradata ;
2006-10-14 07:18:08 +00:00
}
2010-02-23 18:05:46 +00:00
2011-06-05 15:10:11 +00:00
/**
* @ return string
*/
2006-10-14 07:18:08 +00:00
public function getCodeString () {
return $this -> mCodestr ;
2006-10-01 04:38:31 +00:00
}
2010-02-23 18:05:46 +00:00
2011-06-05 15:10:11 +00:00
/**
* @ return array
*/
2009-07-12 12:38:03 +00:00
public function getMessageArray () {
2010-02-23 18:05:46 +00:00
$result = array (
'code' => $this -> mCodestr ,
'info' => $this -> getMessage ()
2009-07-12 12:38:03 +00:00
);
2010-02-23 18:05:46 +00:00
if ( is_array ( $this -> mExtraData ) ) {
2009-07-12 12:38:03 +00:00
$result = array_merge ( $result , $this -> mExtraData );
2010-02-23 18:05:46 +00:00
}
2009-07-12 12:38:03 +00:00
return $result ;
}
2010-02-23 18:05:46 +00:00
2011-06-05 15:10:11 +00:00
/**
* @ return string
*/
2006-10-01 04:38:31 +00:00
public function __toString () {
2006-10-14 07:18:08 +00:00
return " { $this -> getCodeString () } : { $this -> getMessage () } " ;
2006-10-01 04:38:31 +00:00
}
2009-05-20 20:07:33 +00:00
}