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);
107 // We don't need to check for pt stuff if there are no nonexistent titles
108 if($fld_protection && !empty($missing))
110 $this->resetQueryParams();
111 // Construct a custom WHERE clause that matches all titles in $missing
112 $lb = new LinkBatch($missing);
113 $this->addTables('protected_titles');
114 $this->addFields(array('pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry'));
115 $this->addWhere($lb->constructSet('pt', $db));
116 $res = $this->select(__METHOD__
);
117 $prottitles = array();
118 while($row = $db->fetchObject($res)) {
119 // Argh, pt_title is case-insensitive
120 $prottitles[$row->pt_namespace
][strtolower($row->pt_title
)] = array(
122 'level' => $row->pt_create_perm
,
123 'expiry' => Block
::decodeExpiry($row->pt_expiry
, TS_ISO_8601
)
126 $db->freeResult($res);
129 // Run the talkid/subjectid query
130 if($fld_talkid ||
$fld_subjectid)
132 $talktitles = $subjecttitles =
133 $talkids = $subjectids = array();
134 $everything = array_merge($titles, $missing);
135 foreach($everything as $t)
137 if(MWNamespace
::isTalk($t->getNamespace()))
140 $subjecttitles[] = $t->getSubjectPage();
143 $talktitles[] = $t->getTalkPage();
145 if(!empty($talktitles) ||
!empty($subjecttitles))
147 // Construct a custom WHERE clause that matches
148 // all titles in $talktitles and $subjecttitles
149 $lb = new LinkBatch(array_merge($talktitles, $subjecttitles));
150 $this->resetQueryParams();
151 $this->addTables('page');
152 $this->addFields(array('page_title', 'page_namespace', 'page_id'));
153 $this->addWhere($lb->constructSet('page', $db));
154 $res = $this->select(__METHOD__
);
155 while($row = $db->fetchObject($res))
157 if(MWNamespace
::isTalk($row->page_namespace
))
158 $talkids[MWNamespace
::getSubject($row->page_namespace
)][$row->page_title
] = $row->page_id
;
160 $subjectids[MWNamespace
::getTalk($row->page_namespace
)][$row->page_title
] = $row->page_id
;
165 foreach ( $titles as $pageid => $title ) {
167 'touched' => wfTimestamp(TS_ISO_8601
, $pageTouched[$pageid]),
168 'lastrevid' => intval($pageLatest[$pageid]),
169 'counter' => intval($pageCounter[$pageid]),
170 'length' => intval($pageLength[$pageid]),
173 if ($pageIsRedir[$pageid])
174 $pageInfo['redirect'] = '';
176 if ($pageIsNew[$pageid])
177 $pageInfo['new'] = '';
179 if (!is_null($token)) {
180 // Currently all tokens are generated the same way, but it might change
182 $pageInfo['edittoken'] = $wgUser->editToken();
184 $pageInfo['deletetoken'] = $wgUser->editToken();
186 $pageInfo['protecttoken'] = $wgUser->editToken();
188 $pageInfo['movetoken'] = $wgUser->editToken();
191 if($fld_protection) {
192 if (isset($protections[$pageid])) {
193 $pageInfo['protection'] = $protections[$pageid];
194 $result->setIndexedTagName($pageInfo['protection'], 'pr');
196 # Also check old restrictions
197 if( $pageRestrictions[$pageid] ) {
198 foreach( explode( ':', trim( $pageRestrictions[$pageid] ) ) as $restrict ) {
199 $temp = explode( '=', trim( $restrict ) );
200 if(count($temp) == 1) {
201 // old old format should be treated as edit/move restriction
202 $restriction = trim( $temp[0] );
203 $pageInfo['protection'][] = array(
205 'level' => $restriction,
206 'expiry' => 'infinity',
208 $pageInfo['protection'][] = array(
210 'level' => $restriction,
211 'expiry' => 'infinity',
214 $restriction = trim( $temp[1] );
215 $pageInfo['protection'][] = array(
217 'level' => $restriction,
218 'expiry' => 'infinity',
222 $result->setIndexedTagName($pageInfo['protection'], 'pr');
224 $pageInfo['protection'] = array();
228 if($fld_talkid && isset($talkids[$title->getNamespace()][$title->getDbKey()]))
229 $pageInfo['talkid'] = $talkids[$title->getNamespace()][$title->getDbKey()];
230 if($fld_subjectid && isset($subjectids[$title->getNamespace()][$title->getDbKey()]))
231 $pageInfo['subjectid'] = $subjectids[$title->getNamespace()][$title->getDbKey()];
233 $result->addValue(array (
236 ), $pageid, $pageInfo);
239 // Get edit/protect tokens and protection data for missing titles if requested
240 // Delete and move tokens are N/A for missing titles anyway
241 if($tok_edit ||
$tok_protect ||
$fld_protection ||
$fld_talkid ||
$fld_subjectid)
243 $res = &$result->getData();
244 foreach($missing as $pageid => $title) {
246 $res['query']['pages'][$pageid]['edittoken'] = $wgUser->editToken();
248 $res['query']['pages'][$pageid]['protecttoken'] = $wgUser->editToken();
251 // Apparently the XML formatting code doesn't like array(null)
252 // This is painful to fix, so we'll just work around it
253 if(isset($prottitles[$title->getNamespace()][strtolower($title->getDBkey())]))
254 $res['query']['pages'][$pageid]['protection'][] = $prottitles[$title->getNamespace()][strtolower($title->getDBkey())];
256 $res['query']['pages'][$pageid]['protection'] = array();
257 $result->setIndexedTagName($res['query']['pages'][$pageid]['protection'], 'pr');
259 if($fld_talkid && isset($talkids[$title->getNamespace()][$title->getDbKey()]))
260 $res['query']['pages'][$pageid]['talkid'] = $talkids[$title->getNamespace()][$title->getDbKey()];
261 if($fld_subjectid && isset($subjectids[$title->getNamespace()][$title->getDbKey()]))
262 $res['query']['pages'][$pageid]['subjectid'] = $subjectids[$title->getNamespace()][$title->getDbKey()];
267 public function getAllowedParams() {
270 ApiBase
:: PARAM_DFLT
=> NULL,
271 ApiBase
:: PARAM_ISMULTI
=> true,
272 ApiBase
:: PARAM_TYPE
=> array (
278 ApiBase
:: PARAM_DFLT
=> NULL,
279 ApiBase
:: PARAM_ISMULTI
=> true,
280 ApiBase
:: PARAM_TYPE
=> array (
289 public function getParamDescription() {
292 'Which additional properties to get:',
293 ' "protection" - List the protection level of each page',
294 ' "talkid" - The page ID of the talk page for each non-talk page',
295 ' "subjectid" - The page ID of the parent page for each talk page'
297 'token' => 'Request a token to perform a data-modifying action on a page',
302 public function getDescription() {
303 return 'Get basic page information such as namespace, title, last touched date, ...';
306 protected function getExamples() {
308 'api.php?action=query&prop=info&titles=Main%20Page',
309 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
313 public function getVersion() {
314 return __CLASS__
. ': $Id$';