2006-10-01 04:38:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Created on Sep 4, 2006
|
|
|
|
|
*
|
|
|
|
|
* API for MediaWiki 1.8+
|
|
|
|
|
*
|
2007-05-20 23:31:44 +00:00
|
|
|
* Copyright (C) 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.,
|
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (!defined('MEDIAWIKI')) {
|
|
|
|
|
// Eclipse helper - will be ignored in production
|
|
|
|
|
require_once ('ApiBase.php');
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
|
|
|
|
*
|
2007-04-20 08:55:14 +00:00
|
|
|
* @addtogroup 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
|
|
|
|
|
*/
|
|
|
|
|
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',
|
|
|
|
|
'feedwatchlist' => 'ApiFeedWatchlist',
|
|
|
|
|
'help' => 'ApiHelp',
|
2008-01-22 21:22:04 +00:00
|
|
|
'paraminfo' => 'ApiParamInfo',
|
2007-12-03 15:52:27 +00:00
|
|
|
);
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-12-03 15:52:27 +00:00
|
|
|
private static $WriteModules = array (
|
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',
|
2006-10-14 07:18:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* List of available formats: format name => format class
|
|
|
|
|
*/
|
|
|
|
|
private static $Formats = array (
|
|
|
|
|
'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',
|
|
|
|
|
'dbgfm' => 'ApiFormatDbg'
|
2006-10-14 07:18:08 +00:00
|
|
|
);
|
|
|
|
|
|
2008-05-18 20:15:56 +00:00
|
|
|
private $mPrinter, $mModules, $mModuleNames, $mFormats, $mFormatNames;
|
2007-06-29 22:05:30 +00:00
|
|
|
private $mResult, $mAction, $mShowVersions, $mEnableWrite, $mRequest, $mInternalMode, $mSquidMaxage;
|
2006-10-01 04:38:31 +00:00
|
|
|
|
|
|
|
|
/**
|
2007-05-22 04:39:49 +00:00
|
|
|
* Constructs an instance of ApiMain that utilizes the module and format specified by $request.
|
|
|
|
|
*
|
2006-10-14 07:18:08 +00:00
|
|
|
* @param $request object - 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
|
2006-10-01 04:38:31 +00:00
|
|
|
*/
|
2006-10-14 07:18:08 +00:00
|
|
|
public function __construct($request, $enableWrite = false) {
|
2006-10-25 03:54:56 +00:00
|
|
|
|
|
|
|
|
$this->mInternalMode = ($request instanceof FauxRequest);
|
|
|
|
|
|
2006-10-01 04:38:31 +00:00
|
|
|
// Special handling for the main module: $parent === $this
|
2006-10-25 03:54:56 +00:00
|
|
|
parent :: __construct($this, $this->mInternalMode ? 'main_int' : 'main');
|
2006-10-01 04:38:31 +00:00
|
|
|
|
2007-07-12 06:54:08 +00:00
|
|
|
if (!$this->mInternalMode) {
|
2008-04-14 07:45:50 +00:00
|
|
|
|
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
|
|
|
|
2008-03-03 05:45:37 +00:00
|
|
|
if( $request->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();
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-07-12 06:54:08 +00:00
|
|
|
if (!$wgUser->isAllowed('read')) {
|
|
|
|
|
self::$Modules = array(
|
2008-01-08 18:10:58 +00:00
|
|
|
'login' => self::$Modules['login'],
|
|
|
|
|
'logout' => self::$Modules['logout'],
|
|
|
|
|
'help' => self::$Modules['help'],
|
2008-04-14 07:45:50 +00:00
|
|
|
);
|
2007-07-12 06:54:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-03 15:52:27 +00:00
|
|
|
global $wgAPIModules, $wgEnableWriteAPI; // extension modules
|
2007-08-31 15:23:48 +00:00
|
|
|
$this->mModules = $wgAPIModules + self :: $Modules;
|
2007-12-03 15:52:27 +00:00
|
|
|
if($wgEnableWriteAPI)
|
|
|
|
|
$this->mModules += self::$WriteModules;
|
2007-08-31 15:23:48 +00:00
|
|
|
|
2006-10-22 23:45:20 +00:00
|
|
|
$this->mModuleNames = array_keys($this->mModules); // todo: optimize
|
2006-10-25 03:54:56 +00:00
|
|
|
$this->mFormats = self :: $Formats;
|
2006-10-22 23:45:20 +00:00
|
|
|
$this->mFormatNames = array_keys($this->mFormats); // todo: optimize
|
|
|
|
|
|
2006-10-01 04:38:31 +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
|
|
|
|
|
|
|
|
$this->mRequest = & $request;
|
2006-10-14 07:18:08 +00:00
|
|
|
|
2006-10-22 23:45:20 +00:00
|
|
|
$this->mSquidMaxage = 0;
|
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
|
|
|
|
|
*/
|
|
|
|
|
public function isInternalMode() {
|
|
|
|
|
return $this->mInternalMode;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
/**
|
|
|
|
|
* Return the request object that contains client's request
|
|
|
|
|
*/
|
|
|
|
|
public function getRequest() {
|
2006-10-14 07:18:08 +00:00
|
|
|
return $this->mRequest;
|
2006-10-01 04:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
/**
|
|
|
|
|
* Get the ApiResult object asscosiated with current request
|
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
/**
|
|
|
|
|
* This method will simply cause an error if the write mode was disabled for this api.
|
|
|
|
|
*/
|
2006-10-02 18:27:06 +00:00
|
|
|
public function requestWriteMode() {
|
|
|
|
|
if (!$this->mEnableWrite)
|
|
|
|
|
$this->dieUsage('Editing of this site is disabled. Make sure the $wgEnableWriteAPI=true; ' .
|
2007-12-02 14:44:35 +00:00
|
|
|
'statement is included in the site\'s LocalSettings.php file', 'noapiwrite');
|
2006-10-02 18:27:06 +00:00
|
|
|
}
|
2006-10-22 23:45:20 +00:00
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
/**
|
|
|
|
|
* Set how long the response should be cached.
|
|
|
|
|
*/
|
2006-10-22 23:45:20 +00:00
|
|
|
public function setCacheMaxAge($maxage) {
|
|
|
|
|
$this->mSquidMaxage = $maxage;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
/**
|
|
|
|
|
* Create an instance of an output formatter by its name
|
|
|
|
|
*/
|
2006-10-15 07:43:52 +00:00
|
|
|
public function createPrinterByName($format) {
|
|
|
|
|
return new $this->mFormats[$format] ($this, $format);
|
|
|
|
|
}
|
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();
|
2006-10-22 23:45:20 +00:00
|
|
|
if ($this->mInternalMode)
|
2006-10-14 07:18:08 +00:00
|
|
|
$this->executeAction();
|
|
|
|
|
else
|
|
|
|
|
$this->executeActionWithErrorHandling();
|
2008-05-17 04:26:26 +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() {
|
2006-10-13 05:21:38 +00:00
|
|
|
|
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();
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
//
|
|
|
|
|
// 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
|
|
|
|
2007-07-15 00:52:35 +00:00
|
|
|
$errCode = $this->substituteResultWithError($e);
|
|
|
|
|
|
2006-10-22 23:45:20 +00:00
|
|
|
// Error results should not be cached
|
|
|
|
|
$this->setCacheMaxAge(0);
|
|
|
|
|
|
2007-07-15 00:52:35 +00:00
|
|
|
$headerStr = 'MediaWiki-API-Error: ' . $errCode;
|
|
|
|
|
if ($e->getCode() === 0)
|
|
|
|
|
header($headerStr, true);
|
|
|
|
|
else
|
|
|
|
|
header($headerStr, true, $e->getCode());
|
|
|
|
|
|
|
|
|
|
// Reset and print just the error message
|
|
|
|
|
ob_clean();
|
|
|
|
|
|
|
|
|
|
// If the error occured during printing, do a printer->profileOut()
|
|
|
|
|
$this->mPrinter->safeProfileOut();
|
|
|
|
|
$this->printResult(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set the cache expiration at the last moment, as any errors may change the expiration.
|
|
|
|
|
// if $this->mSquidMaxage == 0, the expiry time is set to the first second of unix epoch
|
|
|
|
|
$expires = $this->mSquidMaxage == 0 ? 1 : time() + $this->mSquidMaxage;
|
|
|
|
|
header('Expires: ' . wfTimestamp(TS_RFC2822, $expires));
|
|
|
|
|
header('Cache-Control: s-maxage=' . $this->mSquidMaxage . ', must-revalidate, max-age=0');
|
|
|
|
|
|
|
|
|
|
if($this->mPrinter->getIsHtml())
|
|
|
|
|
echo wfReportTime();
|
|
|
|
|
|
|
|
|
|
ob_end_flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Replace the result data with the information about an exception.
|
2008-04-14 07:45:50 +00:00
|
|
|
* Returns the error code
|
2007-07-15 00:52:35 +00:00
|
|
|
*/
|
|
|
|
|
protected function substituteResultWithError($e) {
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-10-14 07:18:08 +00:00
|
|
|
// Printer may not be initialized if the extractRequestParams() fails for the main module
|
|
|
|
|
if (!isset ($this->mPrinter)) {
|
2007-05-28 06:59:19 +00:00
|
|
|
// The printer has not been created yet. Try to manually get formatter value.
|
|
|
|
|
$value = $this->getRequest()->getVal('format', self::API_DEFAULT_FORMAT);
|
|
|
|
|
if (!in_array($value, $this->mFormatNames))
|
|
|
|
|
$value = self::API_DEFAULT_FORMAT;
|
|
|
|
|
|
|
|
|
|
$this->mPrinter = $this->createPrinterByName($value);
|
2006-10-18 05:27:43 +00:00
|
|
|
if ($this->mPrinter->getNeedsRawData())
|
|
|
|
|
$this->getResult()->setRawMode();
|
2006-10-14 07:18:08 +00:00
|
|
|
}
|
2006-10-22 23:45:20 +00:00
|
|
|
|
2006-10-14 07:18:08 +00:00
|
|
|
if ($e instanceof UsageException) {
|
|
|
|
|
//
|
|
|
|
|
// User entered incorrect parameters - print usage screen
|
|
|
|
|
//
|
|
|
|
|
$errMessage = array (
|
2007-07-15 00:52:35 +00:00
|
|
|
'code' => $e->getCodeString(),
|
|
|
|
|
'info' => $e->getMessage());
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-05-28 06:59:19 +00:00
|
|
|
// Only print the help message when this is for the developer, not runtime
|
2007-06-29 22:05:30 +00:00
|
|
|
if ($this->mPrinter->getIsHtml() || $this->mAction == 'help')
|
2007-05-28 06:59:19 +00:00
|
|
|
ApiResult :: setContent($errMessage, $this->makeHelpMsg());
|
2006-10-22 23:45:20 +00:00
|
|
|
|
2006-10-14 07:18:08 +00:00
|
|
|
} else {
|
|
|
|
|
//
|
|
|
|
|
// Something is seriously wrong
|
|
|
|
|
//
|
|
|
|
|
$errMessage = array (
|
2007-09-26 04:28:48 +00:00
|
|
|
'code' => 'internal_api_error_'. get_class($e),
|
2006-10-14 07:18:08 +00:00
|
|
|
'info' => "Exception Caught: {$e->getMessage()}"
|
|
|
|
|
);
|
|
|
|
|
ApiResult :: setContent($errMessage, "\n\n{$e->getTraceAsString()}\n\n");
|
|
|
|
|
}
|
2006-10-22 23:45:20 +00:00
|
|
|
|
2006-10-18 05:27:43 +00:00
|
|
|
$this->getResult()->reset();
|
|
|
|
|
$this->getResult()->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
|
|
|
/**
|
|
|
|
|
* Execute the actual module, without any error handling
|
|
|
|
|
*/
|
|
|
|
|
protected function executeAction() {
|
2008-01-08 18:10:58 +00:00
|
|
|
|
2007-05-28 06:59:19 +00:00
|
|
|
$params = $this->extractRequestParams();
|
2008-01-08 18:10:58 +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'];
|
2006-10-14 07:18:08 +00:00
|
|
|
|
|
|
|
|
// Instantiate the module requested by the user
|
2007-06-29 22:05:30 +00:00
|
|
|
$module = new $this->mModules[$this->mAction] ($this, $this->mAction);
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-11-19 15:57:58 +00:00
|
|
|
if( $module->shouldCheckMaxlag() && isset( $params['maxlag'] ) ) {
|
|
|
|
|
// 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 ) {
|
|
|
|
|
if( $wgShowHostnames ) {
|
|
|
|
|
ApiBase :: dieUsage( "Waiting for $host: $lag seconds lagged", 'maxlag' );
|
|
|
|
|
} else {
|
|
|
|
|
ApiBase :: dieUsage( "Waiting for a database server: $lag seconds lagged", 'maxlag' );
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-10-14 07:18:08 +00:00
|
|
|
if (!$this->mInternalMode) {
|
2008-01-18 20:43:59 +00:00
|
|
|
// Ignore mustBePosted() for internal calls
|
|
|
|
|
if($module->mustBePosted() && !$this->mRequest->wasPosted())
|
|
|
|
|
$this->dieUsage("The {$this->mAction} module requires a POST request", 'mustbeposted');
|
2006-10-22 23:45:20 +00:00
|
|
|
|
2006-10-15 07:43:52 +00:00
|
|
|
// See if custom printer is used
|
2006-10-22 23:45:20 +00:00
|
|
|
$this->mPrinter = $module->getCustomPrinter();
|
2006-10-15 07:43:52 +00:00
|
|
|
if (is_null($this->mPrinter)) {
|
2006-10-14 07:18:08 +00:00
|
|
|
// Create an appropriate printer
|
2007-05-28 06:59:19 +00:00
|
|
|
$this->mPrinter = $this->createPrinterByName($params['format']);
|
2006-10-14 07:18:08 +00:00
|
|
|
}
|
2006-10-22 23:45:20 +00:00
|
|
|
|
2006-10-18 05:27:43 +00:00
|
|
|
if ($this->mPrinter->getNeedsRawData())
|
|
|
|
|
$this->getResult()->setRawMode();
|
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();
|
|
|
|
|
$module->profileOut();
|
2006-10-22 23:45:20 +00:00
|
|
|
|
2006-10-14 07:18:08 +00:00
|
|
|
if (!$this->mInternalMode) {
|
|
|
|
|
// Print result data
|
|
|
|
|
$this->printResult(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
2006-10-01 04:38:31 +00:00
|
|
|
*/
|
2006-10-14 07:18:08 +00:00
|
|
|
protected function printResult($isError) {
|
2006-10-01 04:38:31 +00:00
|
|
|
$printer = $this->mPrinter;
|
|
|
|
|
$printer->profileIn();
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-09-18 22:10:09 +00:00
|
|
|
/* If the help message is requested in the default (xmlfm) format,
|
|
|
|
|
* tell the printer not to escape ampersands so that our links do
|
|
|
|
|
* not break. */
|
2008-04-14 07:45:50 +00:00
|
|
|
$printer->setUnescapeAmps ( ( $this->mAction == 'help' || $isError )
|
2008-04-05 19:32:21 +00:00
|
|
|
&& $this->getParameter('format') == ApiMain::API_DEFAULT_FORMAT );
|
2007-09-18 22:10:09 +00:00
|
|
|
|
2007-10-24 19:34:29 +00:00
|
|
|
$printer->initPrinter($isError);
|
|
|
|
|
|
2006-10-15 07:43:52 +00:00
|
|
|
$printer->execute();
|
2006-10-01 04:38:31 +00:00
|
|
|
$printer->closePrinter();
|
|
|
|
|
$printer->profileOut();
|
|
|
|
|
}
|
2006-10-14 07:18:08 +00:00
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
/**
|
|
|
|
|
* See ApiBase for description.
|
|
|
|
|
*/
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getAllowedParams() {
|
2006-10-14 07:18:08 +00:00
|
|
|
return array (
|
|
|
|
|
'format' => array (
|
|
|
|
|
ApiBase :: PARAM_DFLT => ApiMain :: API_DEFAULT_FORMAT,
|
|
|
|
|
ApiBase :: PARAM_TYPE => $this->mFormatNames
|
|
|
|
|
),
|
|
|
|
|
'action' => array (
|
|
|
|
|
ApiBase :: PARAM_DFLT => 'help',
|
|
|
|
|
ApiBase :: PARAM_TYPE => $this->mModuleNames
|
|
|
|
|
),
|
2007-11-19 15:57:58 +00:00
|
|
|
'version' => false,
|
|
|
|
|
'maxlag' => array (
|
|
|
|
|
ApiBase :: PARAM_TYPE => 'integer'
|
|
|
|
|
),
|
2006-10-14 07:18:08 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
/**
|
|
|
|
|
* See ApiBase for description.
|
|
|
|
|
*/
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getParamDescription() {
|
2006-10-14 07:18:08 +00:00
|
|
|
return array (
|
|
|
|
|
'format' => 'The format of the output',
|
|
|
|
|
'action' => 'What action you would like to perform',
|
2007-11-19 15:57:58 +00:00
|
|
|
'version' => 'When showing help, include version for each module',
|
|
|
|
|
'maxlag' => 'Maximum lag'
|
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.
|
|
|
|
|
*/
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getDescription() {
|
2006-10-01 04:38:31 +00:00
|
|
|
return array (
|
2007-07-08 08:32:00 +00:00
|
|
|
'',
|
2006-10-01 04:38:31 +00:00
|
|
|
'',
|
2007-07-06 02:19:56 +00:00
|
|
|
'******************************************************************',
|
|
|
|
|
'** **',
|
|
|
|
|
'** This is an auto-generated MediaWiki API documentation page **',
|
|
|
|
|
'** **',
|
|
|
|
|
'** Documentation and Examples: **',
|
|
|
|
|
'** http://www.mediawiki.org/wiki/API **',
|
|
|
|
|
'** **',
|
|
|
|
|
'******************************************************************',
|
2006-10-30 00:18:05 +00:00
|
|
|
'',
|
2007-07-08 08:32:00 +00:00
|
|
|
'Status: All features shown on this page should be working, but the API',
|
|
|
|
|
' is still in active development, and may change at any time.',
|
|
|
|
|
' Make sure to monitor our mailing list for any updates.',
|
2007-07-06 02:19:56 +00:00
|
|
|
'',
|
2007-07-08 08:32:00 +00:00
|
|
|
'Documentation: http://www.mediawiki.org/wiki/API',
|
|
|
|
|
'Mailing list: http://lists.wikimedia.org/mailman/listinfo/mediawiki-api',
|
2007-07-06 02:19:56 +00:00
|
|
|
'Bugs & Requests: http://bugzilla.wikimedia.org/buglist.cgi?component=API&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&order=bugs.delta_ts',
|
|
|
|
|
'',
|
|
|
|
|
'',
|
|
|
|
|
'',
|
2007-07-08 08:32:00 +00:00
|
|
|
'',
|
|
|
|
|
'',
|
2006-10-01 04:38:31 +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
|
|
|
|
|
*/
|
2006-10-30 00:18:05 +00:00
|
|
|
protected function getCredits() {
|
|
|
|
|
return array(
|
2008-03-20 06:54:24 +00:00
|
|
|
'API developers:',
|
2008-03-17 20:13:22 +00:00
|
|
|
' Roan Kattouw <Firstname>.<Lastname>@home.nl (lead developer Sep 2007-present)',
|
2008-03-20 06:54:24 +00:00
|
|
|
' Victor Vasiliev - vasilvv at gee mail dot com',
|
|
|
|
|
' 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',
|
|
|
|
|
'or file a bug report at http://bugzilla.wikimedia.org/'
|
2006-10-30 00:18:05 +00:00
|
|
|
);
|
|
|
|
|
}
|
2006-10-01 04:38:31 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Override the parent to generate help messages for all available modules.
|
|
|
|
|
*/
|
|
|
|
|
public function makeHelpMsg() {
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-12-02 15:04:53 +00:00
|
|
|
$this->mPrinter->setHelp();
|
2006-10-01 04:38:31 +00:00
|
|
|
|
|
|
|
|
// Use parent to make default message for the main module
|
|
|
|
|
$msg = parent :: makeHelpMsg();
|
|
|
|
|
|
|
|
|
|
$astriks = str_repeat('*** ', 10);
|
|
|
|
|
$msg .= "\n\n$astriks Modules $astriks\n\n";
|
2006-11-25 17:11:58 +00:00
|
|
|
foreach( $this->mModules as $moduleName => $unused ) {
|
2006-10-01 04:38:31 +00:00
|
|
|
$module = new $this->mModules[$moduleName] ($this, $moduleName);
|
2007-05-21 06:32:32 +00:00
|
|
|
$msg .= self::makeHelpMsgHeader($module, 'action');
|
2006-10-01 04:38:31 +00:00
|
|
|
$msg2 = $module->makeHelpMsg();
|
|
|
|
|
if ($msg2 !== false)
|
|
|
|
|
$msg .= $msg2;
|
|
|
|
|
$msg .= "\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$msg .= "\n$astriks Formats $astriks\n\n";
|
2006-11-25 17:11:58 +00:00
|
|
|
foreach( $this->mFormats as $formatName => $unused ) {
|
2006-10-15 07:43:52 +00:00
|
|
|
$module = $this->createPrinterByName($formatName);
|
2007-05-21 06:32:32 +00:00
|
|
|
$msg .= self::makeHelpMsgHeader($module, 'format');
|
2006-10-01 04:38:31 +00:00
|
|
|
$msg2 = $module->makeHelpMsg();
|
|
|
|
|
if ($msg2 !== false)
|
|
|
|
|
$msg .= $msg2;
|
|
|
|
|
$msg .= "\n";
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-10-30 00:18:05 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-21 06:32:32 +00:00
|
|
|
public static function makeHelpMsgHeader($module, $paramName) {
|
2007-07-07 03:05:09 +00:00
|
|
|
$modulePrefix = $module->getModulePrefix();
|
|
|
|
|
if (!empty($modulePrefix))
|
2008-04-14 07:45:50 +00:00
|
|
|
$modulePrefix = "($modulePrefix) ";
|
|
|
|
|
|
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
|
|
|
|
2006-10-01 04:38:31 +00:00
|
|
|
private $mIsBot = null;
|
2007-05-22 04:39:49 +00:00
|
|
|
private $mIsSysop = null;
|
2007-11-29 14:51:58 +00:00
|
|
|
private $mCanApiHighLimits = null;
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if the currently logged in user is a bot, false otherwise
|
2007-11-29 14:51:58 +00:00
|
|
|
* OBSOLETE, use canApiHighLimits() instead
|
2007-05-20 10:08:40 +00:00
|
|
|
*/
|
2006-10-01 04:38:31 +00:00
|
|
|
public function isBot() {
|
|
|
|
|
if (!isset ($this->mIsBot)) {
|
|
|
|
|
global $wgUser;
|
|
|
|
|
$this->mIsBot = $wgUser->isAllowed('bot');
|
|
|
|
|
}
|
|
|
|
|
return $this->mIsBot;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-05-22 04:39:49 +00:00
|
|
|
/**
|
|
|
|
|
* Similar to isBot(), this method returns true if the logged in user is
|
|
|
|
|
* a sysop, and false if not.
|
2007-11-29 14:51:58 +00:00
|
|
|
* OBSOLETE, use canApiHighLimits() instead
|
2007-05-22 04:39:49 +00:00
|
|
|
*/
|
|
|
|
|
public function isSysop() {
|
|
|
|
|
if (!isset ($this->mIsSysop)) {
|
|
|
|
|
global $wgUser;
|
2007-07-31 17:53:37 +00:00
|
|
|
$this->mIsSysop = in_array( 'sysop', $wgUser->getGroups());
|
2007-05-22 04:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->mIsSysop;
|
|
|
|
|
}
|
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() {
|
2007-11-30 14:41:30 +00:00
|
|
|
if (!isset($this->mCanApiHighLimits)) {
|
2007-11-29 14:51:58 +00:00
|
|
|
global $wgUser;
|
|
|
|
|
$this->mCanApiHighLimits = $wgUser->isAllowed('apihighlimits');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
*/
|
2006-10-01 21:20:55 +00:00
|
|
|
public function getVersion() {
|
2006-10-02 18:27:06 +00:00
|
|
|
$vers = array ();
|
2007-06-28 12:55:12 +00:00
|
|
|
$vers[] = 'MediaWiki ' . SpecialVersion::getVersion();
|
2006-10-02 18:27:06 +00:00
|
|
|
$vers[] = __CLASS__ . ': $Id$';
|
|
|
|
|
$vers[] = ApiBase :: getBaseVersion();
|
|
|
|
|
$vers[] = ApiFormatBase :: getBaseVersion();
|
|
|
|
|
$vers[] = ApiQueryBase :: getBaseVersion();
|
2006-10-22 23:45:20 +00:00
|
|
|
$vers[] = ApiFormatFeedWrapper :: getVersion(); // not accessible with format=xxx
|
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.
|
|
|
|
|
*
|
|
|
|
|
* @access protected
|
|
|
|
|
* @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.
|
|
|
|
|
*
|
|
|
|
|
* @access protected
|
|
|
|
|
* @param $fmtName The identifier for this format.
|
|
|
|
|
* @param $fmtClass The class implementing this format.
|
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
function getModules() {
|
|
|
|
|
return $this->mModules;
|
|
|
|
|
}
|
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
|
|
|
*
|
2007-04-20 08:55:14 +00:00
|
|
|
* @addtogroup 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
|
|
|
*/
|
2006-10-01 04:38:31 +00:00
|
|
|
class UsageException extends Exception {
|
|
|
|
|
|
2006-10-14 07:18:08 +00:00
|
|
|
private $mCodestr;
|
2006-10-01 04:38:31 +00:00
|
|
|
|
2006-10-14 07:18:08 +00:00
|
|
|
public function __construct($message, $codestr, $code = 0) {
|
|
|
|
|
parent :: __construct($message, $code);
|
|
|
|
|
$this->mCodestr = $codestr;
|
|
|
|
|
}
|
|
|
|
|
public function getCodeString() {
|
|
|
|
|
return $this->mCodestr;
|
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
|
|
|
}
|
|
|
|
|
}
|