4 * Created on Sep 25, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
32 * A query module to show basic page information.
36 class ApiQueryInfo
extends ApiQueryBase
{
38 public function __construct($query, $moduleName) {
39 parent
:: __construct($query, $moduleName, 'in');
42 public function requestExtraData($pageSet) {
43 $pageSet->requestField('page_restrictions');
44 $pageSet->requestField('page_is_redirect');
45 $pageSet->requestField('page_is_new');
46 $pageSet->requestField('page_counter');
47 $pageSet->requestField('page_touched');
48 $pageSet->requestField('page_latest');
49 $pageSet->requestField('page_len');
52 public function execute() {
56 $params = $this->extractRequestParams();
57 $fld_protection = $fld_talkid = $fld_subjectid = false;
58 if(!is_null($params['prop'])) {
59 $prop = array_flip($params['prop']);
60 $fld_protection = isset($prop['protection']);
61 $fld_talkid = isset($prop['talkid']);
62 $fld_subjectid = isset($prop['subjectid']);
64 if(!is_null($params['token'])) {
65 $token = $params['token'];
66 $tok_edit = $this->getTokenFlag($token, 'edit');
67 $tok_delete = $this->getTokenFlag($token, 'delete');
68 $tok_protect = $this->getTokenFlag($token, 'protect');
69 $tok_move = $this->getTokenFlag($token, 'move');
72 // Fix E_NOTICEs about unset variables
73 $token = $tok_edit = $tok_delete = $tok_protect = $tok_move = null;
75 $pageSet = $this->getPageSet();
76 $titles = $pageSet->getGoodTitles();
77 $missing = $pageSet->getMissingTitles();
78 $result = $this->getResult();
80 $pageRestrictions = $pageSet->getCustomField('page_restrictions');
81 $pageIsRedir = $pageSet->getCustomField('page_is_redirect');
82 $pageIsNew = $pageSet->getCustomField('page_is_new');
83 $pageCounter = $pageSet->getCustomField('page_counter');
84 $pageTouched = $pageSet->getCustomField('page_touched');
85 $pageLatest = $pageSet->getCustomField('page_latest');
86 $pageLength = $pageSet->getCustomField('page_len');
89 if ($fld_protection && !empty($titles)) {
90 $this->addTables('page_restrictions');
91 $this->addFields(array('pr_page', 'pr_type', 'pr_level', 'pr_expiry', 'pr_cascade'));
92 $this->addWhereFld('pr_page', array_keys($titles));
94 $res = $this->select(__METHOD__
);
95 while($row = $db->fetchObject($res)) {
97 'type' => $row->pr_type
,
98 'level' => $row->pr_level
,
99 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
)
103 $protections[$row->pr_page
][] = $a;
105 $db->freeResult($res);
108 foreach ($titles as $id => $title)
109 if ($title->getNamespace() == NS_IMAGE
)
111 // To avoid code duplication
112 $cascadeTypes = array(
115 'table' => 'templatelinks',
116 'ns' => 'tl_namespace',
117 'title' => 'tl_title',
118 'ids' => array_diff(array_keys($titles), $imageIds)
122 'table' => 'imagelinks',
129 foreach ($cascadeTypes as $type)
131 if (count($type['ids']) != 0) {
132 $this->resetQueryParams();
133 $this->addTables(array('page_restrictions', $type['table']));
134 $this->addTables('page', 'page_source');
135 $this->addTables('page', 'page_target');
136 $this->addFields(array('pr_type', 'pr_level', 'pr_expiry',
137 'page_target.page_id AS page_target_id',
138 'page_source.page_namespace AS page_source_namespace',
139 'page_source.page_title AS page_source_title'));
140 $this->addWhere(array("{$type['prefix']}_from = pr_page",
141 'page_target.page_namespace = '.$type['ns'],
142 'page_target.page_title = '.$type['title'],
143 'page_source.page_id = pr_page'
145 $this->addWhereFld('pr_cascade', 1);
146 $this->addWhereFld('page_target.page_id', $type['ids']);
148 $res = $this->select(__METHOD__
);
149 while($row = $db->fetchObject($res)) {
150 $source = Title
::makeTitle($row->page_source_namespace
, $row->page_source_title
);
152 'type' => $row->pr_type
,
153 'level' => $row->pr_level
,
154 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
),
155 'source' => $source->getPrefixedText()
157 $protections[$row->page_target_id
][] = $a;
159 $db->freeResult($res);
164 // We don't need to check for pt stuff if there are no nonexistent titles
165 if($fld_protection && !empty($missing))
167 $this->resetQueryParams();
168 // Construct a custom WHERE clause that matches all titles in $missing
169 $lb = new LinkBatch($missing);
170 $this->addTables('protected_titles');
171 $this->addFields(array('pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry'));
172 $this->addWhere($lb->constructSet('pt', $db));
173 $res = $this->select(__METHOD__
);
174 $prottitles = array();
175 while($row = $db->fetchObject($res)) {
176 $prottitles[$row->pt_namespace
][$row->pt_title
] = array(
178 'level' => $row->pt_create_perm
,
179 'expiry' => Block
::decodeExpiry($row->pt_expiry
, TS_ISO_8601
)
182 $db->freeResult($res);
185 // Run the talkid/subjectid query
186 if($fld_talkid ||
$fld_subjectid)
188 $talktitles = $subjecttitles =
189 $talkids = $subjectids = array();
190 $everything = array_merge($titles, $missing);
191 foreach($everything as $t)
193 if(MWNamespace
::isTalk($t->getNamespace()))
196 $subjecttitles[] = $t->getSubjectPage();
199 $talktitles[] = $t->getTalkPage();
201 if(!empty($talktitles) ||
!empty($subjecttitles))
203 // Construct a custom WHERE clause that matches
204 // all titles in $talktitles and $subjecttitles
205 $lb = new LinkBatch(array_merge($talktitles, $subjecttitles));
206 $this->resetQueryParams();
207 $this->addTables('page');
208 $this->addFields(array('page_title', 'page_namespace', 'page_id'));
209 $this->addWhere($lb->constructSet('page', $db));
210 $res = $this->select(__METHOD__
);
211 while($row = $db->fetchObject($res))
213 if(MWNamespace
::isTalk($row->page_namespace
))
214 $talkids[MWNamespace
::getSubject($row->page_namespace
)][$row->page_title
] = $row->page_id
;
216 $subjectids[MWNamespace
::getTalk($row->page_namespace
)][$row->page_title
] = $row->page_id
;
221 foreach ( $titles as $pageid => $title ) {
223 'touched' => wfTimestamp(TS_ISO_8601
, $pageTouched[$pageid]),
224 'lastrevid' => intval($pageLatest[$pageid]),
225 'counter' => intval($pageCounter[$pageid]),
226 'length' => intval($pageLength[$pageid]),
229 if ($pageIsRedir[$pageid])
230 $pageInfo['redirect'] = '';
232 if ($pageIsNew[$pageid])
233 $pageInfo['new'] = '';
235 if (!is_null($token)) {
236 // Currently all tokens are generated the same way, but it might change
238 $pageInfo['edittoken'] = $wgUser->editToken();
240 $pageInfo['deletetoken'] = $wgUser->editToken();
242 $pageInfo['protecttoken'] = $wgUser->editToken();
244 $pageInfo['movetoken'] = $wgUser->editToken();
247 if($fld_protection) {
248 if (isset($protections[$pageid])) {
249 $pageInfo['protection'] = $protections[$pageid];
250 $result->setIndexedTagName($pageInfo['protection'], 'pr');
252 # Also check old restrictions
253 if( $pageRestrictions[$pageid] ) {
254 foreach( explode( ':', trim( $pageRestrictions[$pageid] ) ) as $restrict ) {
255 $temp = explode( '=', trim( $restrict ) );
256 if(count($temp) == 1) {
257 // old old format should be treated as edit/move restriction
258 $restriction = trim( $temp[0] );
259 $pageInfo['protection'][] = array(
261 'level' => $restriction,
262 'expiry' => 'infinity',
264 $pageInfo['protection'][] = array(
266 'level' => $restriction,
267 'expiry' => 'infinity',
270 $restriction = trim( $temp[1] );
271 $pageInfo['protection'][] = array(
273 'level' => $restriction,
274 'expiry' => 'infinity',
278 $result->setIndexedTagName($pageInfo['protection'], 'pr');
280 $pageInfo['protection'] = array();
284 if($fld_talkid && isset($talkids[$title->getNamespace()][$title->getDbKey()]))
285 $pageInfo['talkid'] = $talkids[$title->getNamespace()][$title->getDbKey()];
286 if($fld_subjectid && isset($subjectids[$title->getNamespace()][$title->getDbKey()]))
287 $pageInfo['subjectid'] = $subjectids[$title->getNamespace()][$title->getDbKey()];
289 $result->addValue(array (
292 ), $pageid, $pageInfo);
295 // Get edit/protect tokens and protection data for missing titles if requested
296 // Delete and move tokens are N/A for missing titles anyway
297 if($tok_edit ||
$tok_protect ||
$fld_protection ||
$fld_talkid ||
$fld_subjectid)
299 $res = &$result->getData();
300 foreach($missing as $pageid => $title) {
302 $res['query']['pages'][$pageid]['edittoken'] = $wgUser->editToken();
304 $res['query']['pages'][$pageid]['protecttoken'] = $wgUser->editToken();
307 // Apparently the XML formatting code doesn't like array(null)
308 // This is painful to fix, so we'll just work around it
309 if(isset($prottitles[$title->getNamespace()][$title->getDBkey()]))
310 $res['query']['pages'][$pageid]['protection'][] = $prottitles[$title->getNamespace()][$title->getDBkey()];
312 $res['query']['pages'][$pageid]['protection'] = array();
313 $result->setIndexedTagName($res['query']['pages'][$pageid]['protection'], 'pr');
315 if($fld_talkid && isset($talkids[$title->getNamespace()][$title->getDbKey()]))
316 $res['query']['pages'][$pageid]['talkid'] = $talkids[$title->getNamespace()][$title->getDbKey()];
317 if($fld_subjectid && isset($subjectids[$title->getNamespace()][$title->getDbKey()]))
318 $res['query']['pages'][$pageid]['subjectid'] = $subjectids[$title->getNamespace()][$title->getDbKey()];
323 public function getAllowedParams() {
326 ApiBase
:: PARAM_DFLT
=> NULL,
327 ApiBase
:: PARAM_ISMULTI
=> true,
328 ApiBase
:: PARAM_TYPE
=> array (
334 ApiBase
:: PARAM_DFLT
=> NULL,
335 ApiBase
:: PARAM_ISMULTI
=> true,
336 ApiBase
:: PARAM_TYPE
=> array (
345 public function getParamDescription() {
348 'Which additional properties to get:',
349 ' "protection" - List the protection level of each page',
350 ' "talkid" - The page ID of the talk page for each non-talk page',
351 ' "subjectid" - The page ID of the parent page for each talk page'
353 'token' => 'Request a token to perform a data-modifying action on a page',
358 public function getDescription() {
359 return 'Get basic page information such as namespace, title, last touched date, ...';
362 protected function getExamples() {
364 'api.php?action=query&prop=info&titles=Main%20Page',
365 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
369 public function getVersion() {
370 return __CLASS__
. ': $Id$';