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_is_redirect');
44 $pageSet->requestField('page_is_new');
45 $pageSet->requestField('page_counter');
46 $pageSet->requestField('page_touched');
47 $pageSet->requestField('page_latest');
48 $pageSet->requestField('page_len');
51 public function execute() {
55 $params = $this->extractRequestParams();
56 $fld_protection = false;
57 if(!is_null($params['prop'])) {
58 $prop = array_flip($params['prop']);
59 $fld_protection = isset($prop['protection']);
61 if(!is_null($params['token'])) {
62 $token = $params['token'];
63 $tok_edit = $this->getTokenFlag($token, 'edit');
64 $tok_delete = $this->getTokenFlag($token, 'delete');
65 $tok_protect = $this->getTokenFlag($token, 'protect');
66 $tok_move = $this->getTokenFlag($token, 'move');
69 $pageSet = $this->getPageSet();
70 $titles = $pageSet->getGoodTitles();
71 $result = $this->getResult();
73 $pageIsRedir = $pageSet->getCustomField('page_is_redirect');
74 $pageIsNew = $pageSet->getCustomField('page_is_new');
75 $pageCounter = $pageSet->getCustomField('page_counter');
76 $pageTouched = $pageSet->getCustomField('page_touched');
77 $pageLatest = $pageSet->getCustomField('page_latest');
78 $pageLength = $pageSet->getCustomField('page_len');
80 if ($fld_protection && count($titles) > 0) {
81 $this->addTables('page_restrictions');
82 $this->addFields(array('pr_page', 'pr_type', 'pr_level', 'pr_expiry'));
83 $this->addWhereFld('pr_page', array_keys($titles));
86 $res = $this->select(__METHOD__
);
87 while($row = $db->fetchObject($res)) {
88 $protections[$row->pr_page
][] = array(
89 'type' => $row->pr_type
,
90 'level' => $row->pr_level
,
91 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
)
94 $db->freeResult($res);
97 foreach ( $titles as $pageid => $title ) {
99 'touched' => wfTimestamp(TS_ISO_8601
, $pageTouched[$pageid]),
100 'lastrevid' => intval($pageLatest[$pageid]),
101 'counter' => intval($pageCounter[$pageid]),
102 'length' => intval($pageLength[$pageid]),
105 if ($pageIsRedir[$pageid])
106 $pageInfo['redirect'] = '';
108 if ($pageIsNew[$pageid])
109 $pageInfo['new'] = '';
111 if (!is_null($token)) {
112 // Currently all tokens are generated the same way, but it might change
114 $pageInfo['edittoken'] = $wgUser->editToken();
116 $pageInfo['deletetoken'] = $wgUser->editToken();
118 $pageInfo['protecttoken'] = $wgUser->editToken();
120 $pageInfo['movetoken'] = $wgUser->editToken();
123 if($fld_protection) {
124 if (isset($protections[$pageid])) {
125 $pageInfo['protection'] = $protections[$pageid];
126 $result->setIndexedTagName($pageInfo['protection'], 'pr');
128 $pageInfo['protection'] = array();
132 $result->addValue(array (
135 ), $pageid, $pageInfo);
138 // Get edit tokens for missing titles if requested
139 // Delete, protect and move tokens are N/A for missing titles anyway
142 $missing = $pageSet->getMissingTitles();
143 $res = $result->getData();
144 foreach($missing as $pageid => $title)
145 $res['query']['pages'][$pageid]['edittoken'] = $wgUser->editToken();
149 protected function getAllowedParams() {
152 ApiBase
:: PARAM_DFLT
=> NULL,
153 ApiBase
:: PARAM_ISMULTI
=> true,
154 ApiBase
:: PARAM_TYPE
=> array (
158 ApiBase
:: PARAM_DFLT
=> NULL,
159 ApiBase
:: PARAM_ISMULTI
=> true,
160 ApiBase
:: PARAM_TYPE
=> array (
169 protected function getParamDescription() {
172 'Which additional properties to get:',
173 ' "protection" - List the protection level of each page'
175 'token' => 'Request a token to perform a data-modifying action on a page',
180 protected function getDescription() {
181 return 'Get basic page information such as namespace, title, last touched date, ...';
184 protected function getExamples() {
186 'api.php?action=query&prop=info&titles=Main%20Page',
187 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
191 public function getVersion() {
192 return __CLASS__
. ': $Id$';