5 * Created on Sep 7, 2006
7 * API for MediaWiki 1.8+
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ("ApiBase.php");
32 class ApiQuery
extends ApiBase
{
34 var $mMetaModuleNames, $mPropModuleNames, $mListModuleNames;
36 private $mQueryMetaModules = array (
37 // 'siteinfo' => 'ApiQuerySiteinfo',
38 // 'userinfo' => 'ApiQueryUserinfo'
40 private $mQueryPropModules = array (
41 // 'info' => 'ApiQueryInfo',
42 // 'categories' => 'ApiQueryCategories',
43 // 'imageinfo' => 'ApiQueryImageinfo',
44 // 'langlinks' => 'ApiQueryLanglinks',
45 // 'links' => 'ApiQueryLinks',
46 // 'templates' => 'ApiQueryTemplates',
47 // 'revisions' => 'ApiQueryRevisions',
50 'content' => 'ApiQueryContent'
52 private $mQueryListModules = array (
53 // 'allpages' => 'ApiQueryAllpages',
54 // 'backlinks' => 'ApiQueryBacklinks',
55 // 'categorymembers' => 'ApiQueryCategorymembers',
56 // 'embeddedin' => 'ApiQueryEmbeddedin',
57 // 'imagelinks' => 'ApiQueryImagelinks',
58 // 'logevents' => 'ApiQueryLogevents',
59 // 'recentchanges' => 'ApiQueryRecentchanges',
60 // 'usercontribs' => 'ApiQueryUsercontribs',
61 // 'users' => 'ApiQueryUsers',
62 // 'watchlist' => 'ApiQueryWatchlist',
65 private $mSlaveDB = null;
67 public function __construct($main, $action) {
68 parent
:: __construct($main);
69 $this->mMetaModuleNames
= array_keys($this->mQueryMetaModules
);
70 $this->mPropModuleNames
= array_keys($this->mQueryPropModules
);
71 $this->mListModuleNames
= array_keys($this->mQueryListModules
);
73 $this->mAllowedGenerators
= array_merge( $this->mListModuleNames
, $this->mPropModuleNames
);
76 public function GetDB() {
77 if (!isset ($this->mSlaveDB
))
78 $this->mSlaveDB
= & wfGetDB(DB_SLAVE
);
79 return $this->mSlaveDB
;
82 public function Execute() {
83 $meta = $prop = $list = $generator = $titles = $pageids = $revids = null;
85 extract($this->ExtractRequestParams());
88 // Only one of the titles/pageids/revids is allowed at the same time
92 $dataSource = 'titles';
93 if (isset($pageids)) {
94 if (isset($dataSource))
95 $this->DieUsage("Cannot use 'pageids' at the same time as '$dataSource'", 'multisource');
96 $dataSource = 'pageids';
99 if (isset($dataSource))
100 $this->DieUsage("Cannot use 'revids' at the same time as '$dataSource'", 'multisource');
101 $dataSource = 'revids';
107 if ($dataSource === 'titles') {
108 $linkBatch = new LinkBatch
;
109 foreach ( $titles as &$titleString ) {
110 $titleObj = &Title
::newFromText( $titleString );
114 $this->dieUsage( "bad title $titleString", 'pi_invalidtitle' );
115 if ($titleObj->getNamespace() < 0)
116 $this->dieUsage( "No support for special page $titleString has been implemented", 'pi_unsupportednamespace' );
117 if (!$titleObj->userCanRead())
118 $this->dieUsage( "No read permission for $titleString", 'pi_titleaccessdenied' );
120 $linkBatch->addObj( $titleObj );
122 // Make sure we remember the original title that was given to us
123 // This way the caller can correlate new titles with the originally requested, i.e. namespace is localized or capitalization
124 if( $titleString !== $titleObj->getPrefixedText() ) {
125 $this->GetResult()->AddMessage('query', 'normalized', array($titleString => $titleObj->getPrefixedText()));
131 protected function GetAllowedParams() {
134 GN_ENUM_ISMULTI
=> true,
135 GN_ENUM_CHOICES
=> $this->mMetaModuleNames
138 GN_ENUM_ISMULTI
=> true,
139 GN_ENUM_CHOICES
=> $this->mPropModuleNames
142 GN_ENUM_ISMULTI
=> true,
143 GN_ENUM_CHOICES
=> $this->mListModuleNames
145 'generator' => array (
146 GN_ENUM_CHOICES
=> $this->mAllowedGenerators
149 GN_ENUM_ISMULTI
=> true
152 GN_ENUM_TYPE
=> 'integer',
153 GN_ENUM_ISMULTI
=> true
156 GN_ENUM_TYPE
=> 'integer',
157 GN_ENUM_ISMULTI
=> true
162 protected function GetParamDescription() {
164 'meta' => 'Which meta data to get about the site',
165 'prop' => 'Which properties to get for the titles/revisions/pageids',
166 'list' => 'Which lists to get',
167 'generator' => 'Use the output of a list as the input for other prop/list/meta items',
168 'titles' => 'A list of titles to work on',
169 'pageids' => 'A list of page IDs to work on',
170 'revids' => 'A list of revision IDs to work on'
174 protected function GetDescription() {
176 'Query API module allows applications to get needed pieces of data from the MediaWiki databases,',
177 'and is loosely based on the Query API interface currently available on all MediaWiki servers.',
178 'All data modifications will first have to use query to acquire a token to prevent abuse from malicious sites.');
181 protected function GetExamples() {
183 'api.php ? action=query & what=content & titles=ArticleA|ArticleB'