39a6c80ea4062436647df5929009940730ef5180
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 $pageSet = $this->getPageSet();
71 $titles = $pageSet->getGoodTitles();
72 $result = $this->getResult();
74 $pageRestrictions = $pageSet->getCustomField('page_restrictions');
75 $pageIsRedir = $pageSet->getCustomField('page_is_redirect');
76 $pageIsNew = $pageSet->getCustomField('page_is_new');
77 $pageCounter = $pageSet->getCustomField('page_counter');
78 $pageTouched = $pageSet->getCustomField('page_touched');
79 $pageLatest = $pageSet->getCustomField('page_latest');
80 $pageLength = $pageSet->getCustomField('page_len');
82 if ($fld_protection && count($titles) > 0) {
83 $this->addTables('page_restrictions');
84 $this->addFields(array('pr_page', 'pr_type', 'pr_level', 'pr_expiry'));
85 $this->addWhereFld('pr_page', array_keys($titles));
88 $res = $this->select(__METHOD__
);
89 while($row = $db->fetchObject($res)) {
90 $protections[$row->pr_page
][] = array(
91 'type' => $row->pr_type
,
92 'level' => $row->pr_level
,
93 'expiry' => Block
::decodeExpiry( $row->pr_expiry
, TS_ISO_8601
)
96 $db->freeResult($res);
99 foreach ( $titles as $pageid => $title ) {
101 'touched' => wfTimestamp(TS_ISO_8601
, $pageTouched[$pageid]),
102 'lastrevid' => intval($pageLatest[$pageid]),
103 'counter' => intval($pageCounter[$pageid]),
104 'length' => intval($pageLength[$pageid]),
107 if ($pageIsRedir[$pageid])
108 $pageInfo['redirect'] = '';
110 if ($pageIsNew[$pageid])
111 $pageInfo['new'] = '';
113 if (!is_null($token)) {
114 // Currently all tokens are generated the same way, but it might change
116 $pageInfo['edittoken'] = $wgUser->editToken();
118 $pageInfo['deletetoken'] = $wgUser->editToken();
120 $pageInfo['protecttoken'] = $wgUser->editToken();
122 $pageInfo['movetoken'] = $wgUser->editToken();
125 if($fld_protection) {
126 if (isset($protections[$pageid])) {
127 $pageInfo['protection'] = $protections[$pageid];
128 $result->setIndexedTagName($pageInfo['protection'], 'pr');
130 # Also check old restrictions
131 if( $pageRestrictions[$pageid] ) {
132 foreach( explode( ':', trim( $pageRestrictions[$pageid] ) ) as $restrict ) {
133 $temp = explode( '=', trim( $restrict ) );
134 if(count($temp) == 1) {
135 // old old format should be treated as edit/move restriction
136 $restriction = trim( $temp[0] );
137 $pageInfo['protection'][] = array(
139 'level' => $restriction,
140 'expiry' => 'infinity',
142 $pageInfo['protection'][] = array(
144 'level' => $restriction,
145 'expiry' => 'infinity',
148 $restriction = trim( $temp[1] );
149 $pageInfo['protection'][] = array(
151 'level' => $restriction,
152 'expiry' => 'infinity',
156 $result->setIndexedTagName($pageInfo['protection'], 'pr');
158 $pageInfo['protection'] = array();
163 $result->addValue(array (
166 ), $pageid, $pageInfo);
169 // Get edit tokens for missing titles if requested
170 // Delete, protect and move tokens are N/A for missing titles anyway
173 $missing = $pageSet->getMissingTitles();
174 $res = &$result->getData();
175 foreach($missing as $pageid => $title)
176 $res['query']['pages'][$pageid]['edittoken'] = $wgUser->editToken();
180 protected function getAllowedParams() {
183 ApiBase
:: PARAM_DFLT
=> NULL,
184 ApiBase
:: PARAM_ISMULTI
=> true,
185 ApiBase
:: PARAM_TYPE
=> array (
189 ApiBase
:: PARAM_DFLT
=> NULL,
190 ApiBase
:: PARAM_ISMULTI
=> true,
191 ApiBase
:: PARAM_TYPE
=> array (
200 protected function getParamDescription() {
203 'Which additional properties to get:',
204 ' "protection" - List the protection level of each page'
206 'token' => 'Request a token to perform a data-modifying action on a page',
211 protected function getDescription() {
212 return 'Get basic page information such as namespace, title, last touched date, ...';
215 protected function getExamples() {
217 'api.php?action=query&prop=info&titles=Main%20Page',
218 'api.php?action=query&prop=info&inprop=protection&titles=Main%20Page'
222 public function getVersion() {
223 return __CLASS__
. ': $Id$';