501ebb078765428b3b821045228f85c134942251
[lhc/web/wiklou.git] / includes / api / ApiQuery.php
1 <?php
2
3
4 /*
5 * Created on Sep 7, 2006
6 *
7 * API for MediaWiki 1.8+
8 *
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
10 *
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.
15 *
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.
20 *
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
25 */
26
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ("ApiBase.php");
30 }
31
32 class ApiQuery extends ApiBase {
33
34 var $mMetaModuleNames, $mPropModuleNames, $mListModuleNames;
35
36 private $mQueryMetaModules = array (
37 // 'siteinfo' => 'ApiQuerySiteinfo',
38 // 'userinfo' => 'ApiQueryUserinfo'
39 );
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',
48
49 // Should be removed
50 'content' => 'ApiQueryContent'
51 );
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',
63 );
64
65 private $mSlaveDB = null;
66
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);
72
73 $this->mAllowedGenerators = array_merge( $this->mListModuleNames, $this->mPropModuleNames);
74 }
75
76 public function GetDB() {
77 if (!isset ($this->mSlaveDB))
78 $this->mSlaveDB = & wfGetDB(DB_SLAVE);
79 return $this->mSlaveDB;
80 }
81
82 public function Execute() {
83 $meta = $prop = $list = $generator = $titles = $pageids = $revids = null;
84 $redirects = null;
85 extract($this->ExtractRequestParams());
86
87 //
88 // Only one of the titles/pageids/revids is allowed at the same time
89 //
90 $dataSource = null;
91 if (isset($titles))
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';
97 }
98 if (isset($revids)) {
99 if (isset($dataSource))
100 $this->DieUsage("Cannot use 'revids' at the same time as '$dataSource'", 'multisource');
101 $dataSource = 'revids';
102 }
103
104 //
105 // Normalize titles
106 //
107 if ($dataSource === 'titles') {
108 $linkBatch = new LinkBatch;
109 foreach ( $titles as &$titleString ) {
110 $titleObj = &Title::newFromText( $titleString );
111
112 // Validation
113 if (!$titleObj)
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' );
119
120 $linkBatch->addObj( $titleObj );
121
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()));
126 }
127 }
128 }
129 }
130
131 protected function GetAllowedParams() {
132 return array (
133 'meta' => array (
134 GN_ENUM_ISMULTI => true,
135 GN_ENUM_CHOICES => $this->mMetaModuleNames
136 ),
137 'prop' => array (
138 GN_ENUM_ISMULTI => true,
139 GN_ENUM_CHOICES => $this->mPropModuleNames
140 ),
141 'list' => array (
142 GN_ENUM_ISMULTI => true,
143 GN_ENUM_CHOICES => $this->mListModuleNames
144 ),
145 'generator' => array (
146 GN_ENUM_CHOICES => $this->mAllowedGenerators
147 ),
148 'titles' => array (
149 GN_ENUM_ISMULTI => true
150 ),
151 'pageids' => array (
152 GN_ENUM_TYPE => 'integer',
153 GN_ENUM_ISMULTI => true
154 ),
155 'revids' => array (
156 GN_ENUM_TYPE => 'integer',
157 GN_ENUM_ISMULTI => true
158 )
159 );
160 }
161
162 protected function GetParamDescription() {
163 return array (
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'
171 );
172 }
173
174 protected function GetDescription() {
175 return array(
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.');
179 }
180
181 protected function GetExamples() {
182 return array (
183 'api.php ? action=query & what=content & titles=ArticleA|ArticleB'
184 );
185 }
186 }
187 ?>