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 = false;
58 if(!is_null($params['prop'])) {
59 $prop = array_flip($params['prop']);
60 $fld_protection = isset($prop['protection']);
62 if(!is_null($params['token'])) {
63 $token = $params['token'];
64 $tok_edit = $this->getTokenFlag($token, 'edit');
65 $tok_delete = $this->getTokenFlag($token, 'delete');
66 $tok_protect = $this->getTokenFlag($token, 'protect');
67 $tok_move = $this->getTokenFlag($token, 'move');
70 // Fix E_NOTICEs about unset variables
71 $token = $tok_edit = $tok_delete = $tok_protect = $tok_move = null;
73 $pageSet = $this->getPageSet();
74 $titles = $pageSet->getGoodTitles();
75 $missing = $pageSet->getMissingTitles();
76 $result = $this->getResult();
78 $pageRestrictions = $pageSet->getCustomField('page_restrictions');
79 $pageIsRedir = $pageSet->getCustomField('page_is_redirect');
80 $pageIsNew = $pageSet->getCustomField('page_is_new');
81 $pageCounter = $pageSet->getCustomField('page_counter');
82 $pageTouched = $pageSet->getCustomField('page_touched');
83 $pageLatest = $pageSet->getCustomField('page_latest');
84 $pageLength = $pageSet->getCustomField('page_len');
87 if ($fld_protection && !empty($titles)) {
88 $this->addTables('page_restrictions');
89 $this->addFields(array('pr_page', 'pr_type', 'pr_level', 'pr_expiry'));
90 $this->addWhereFld('pr_page', array_keys($titles));
92 $res = $this->select(__METHOD__
);
93 while($row = $db->fetchObject($res)) {
94 $protections[$row->pr_page
][] = array(
95 'type' => $row->pr_type
,
96 'level' => $row->pr_level
,
97 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
)
100 $db->freeResult($res);
102 // We don't need to check for pt stuff if there are no nonexistent titles
103 if($fld_protection && !empty($missing))
105 $this->resetQueryParams();
106 // Construct a custom WHERE clause that matches all titles in $missing
107 $lb = new LinkBatch($missing);
108 $this->addTables('protected_titles');
109 $this->addFields(array('pt_title', 'pt_namespace', 'pt_create_perm', 'pt_expiry'));
110 $this->addWhere($lb->constructSet('pt', $db));
111 $res = $this->select(__METHOD__
);
112 while($row = $db->fetchObject($res)) {
113 $prottitles[$row->pt_namespace
][$row->pt_title
] = array(
115 'level' => $row->pt_create_perm
,
116 'expiry' => Block
::decodeExpiry($row->pt_expiry
, TS_ISO_8601
)
119 $db->freeResult($res);
122 foreach ( $titles as $pageid => $title ) {
124 'touched' => wfTimestamp(TS_ISO_8601
, $pageTouched[$pageid]),
125 'lastrevid' => intval($pageLatest[$pageid]),
126 'counter' => intval($pageCounter[$pageid]),
127 'length' => intval($pageLength[$pageid]),
130 if ($pageIsRedir[$pageid])
131 $pageInfo['redirect'] = '';
133 if ($pageIsNew[$pageid])
134 $pageInfo['new'] = '';
136 if (!is_null($token)) {
137 // Currently all tokens are generated the same way, but it might change
139 $pageInfo['edittoken'] = $wgUser->editToken();
141 $pageInfo['deletetoken'] = $wgUser->editToken();
143 $pageInfo['protecttoken'] = $wgUser->editToken();
145 $pageInfo['movetoken'] = $wgUser->editToken();
148 if($fld_protection) {
149 if (isset($protections[$pageid])) {
150 $pageInfo['protection'] = $protections[$pageid];
151 $result->setIndexedTagName($pageInfo['protection'], 'pr');
153 # Also check old restrictions
154 if( $pageRestrictions[$pageid] ) {
155 foreach( explode( ':', trim( $pageRestrictions[$pageid] ) ) as $restrict ) {
156 $temp = explode( '=', trim( $restrict ) );
157 if(count($temp) == 1) {
158 // old old format should be treated as edit/move restriction
159 $restriction = trim( $temp[0] );
160 $pageInfo['protection'][] = array(
162 'level' => $restriction,
163 'expiry' => 'infinity',
165 $pageInfo['protection'][] = array(
167 'level' => $restriction,
168 'expiry' => 'infinity',
171 $restriction = trim( $temp[1] );
172 $pageInfo['protection'][] = array(
174 'level' => $restriction,
175 'expiry' => 'infinity',
179 $result->setIndexedTagName($pageInfo['protection'], 'pr');
181 $pageInfo['protection'] = array();
186 $result->addValue(array (
189 ), $pageid, $pageInfo);
192 // Get edit/protect tokens and protection data for missing titles if requested
193 // Delete and move tokens are N/A for missing titles anyway
194 if($tok_edit ||
$tok_protect ||
$fld_protection)
196 $res = &$result->getData();
197 foreach($missing as $pageid => $title) {
199 $res['query']['pages'][$pageid]['edittoken'] = $wgUser->editToken();
201 $res['query']['pages'][$pageid]['protecttoken'] = $wgUser->editToken();
203 $res['query']['pages'][$pageid]['protection'][] = $prottitles[$title->getNamespace()][$title->getDbKey()];
204 $result->setIndexedTagName($res['query']['pages'][$pageid]['protection'], 'pr');
209 protected function getAllowedParams() {
212 ApiBase
:: PARAM_DFLT
=> NULL,
213 ApiBase
:: PARAM_ISMULTI
=> true,
214 ApiBase
:: PARAM_TYPE
=> array (
218 ApiBase
:: PARAM_DFLT
=> NULL,
219 ApiBase
:: PARAM_ISMULTI
=> true,
220 ApiBase
:: PARAM_TYPE
=> array (
229 protected function getParamDescription() {
232 'Which additional properties to get:',
233 ' "protection" - List the protection level of each page'
235 'token' => 'Request a token to perform a data-modifying action on a page',
240 protected function getDescription() {
241 return 'Get basic page information such as namespace, title, last touched date, ...';
244 protected function getExamples() {
246 'api.php?action=query&prop=info&titles=Main%20Page',
247 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
251 public function getVersion() {
252 return __CLASS__
. ': $Id$';