]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryProtectedTitles.php
MediaWiki 1.15.5
[autoinstalls/mediawiki.git] / includes / api / ApiQueryProtectedTitles.php
1 <?php
2
3 /*
4  * Created on Feb 13, 2009
5  *
6  * API for MediaWiki 1.8+
7  *
8  * Copyright (C) 2009 Roan Kattouw <Firstname>.<Lastname>@home.nl
9  *
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.
14  *
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.
19  *
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
24  */
25
26 if (!defined('MEDIAWIKI')) {
27         // Eclipse helper - will be ignored in production
28         require_once ('ApiQueryBase.php');
29 }
30
31 /**
32  * Query module to enumerate all create-protected pages.
33  *
34  * @ingroup API
35  */
36 class ApiQueryProtectedTitles extends ApiQueryGeneratorBase {
37
38         public function __construct($query, $moduleName) {
39                 parent :: __construct($query, $moduleName, 'pt');
40         }
41
42         public function execute() {
43                 $this->run();
44         }
45
46         public function executeGenerator($resultPageSet) {
47                 $this->run($resultPageSet);
48         }
49
50         private function run($resultPageSet = null) {
51                 $db = $this->getDB();
52                 $params = $this->extractRequestParams();
53
54                 $this->addTables('protected_titles');
55                 $this->addFields(array('pt_namespace', 'pt_title', 'pt_timestamp'));
56
57                 $prop = array_flip($params['prop']);
58                 $this->addFieldsIf('pt_user', isset($prop['user']));
59                 $this->addFieldsIf('pt_reason', isset($prop['comment']));
60                 $this->addFieldsIf('pt_expiry', isset($prop['expiry']));
61                 $this->addFieldsIf('pt_create_perm', isset($prop['level']));
62
63                 $this->addWhereRange('pt_timestamp', $params['dir'], $params['start'], $params['end']);
64                 $this->addWhereFld('pt_namespace', $params['namespace']);
65                 $this->addWhereFld('pt_create_perm', $params['level']);
66                 
67                 if(isset($prop['user']))
68                 {
69                         $this->addTables('user');
70                         $this->addFields('user_name');
71                         $this->addJoinConds(array('user' => array('LEFT JOIN',
72                                 'user_id=pt_user'
73                         )));
74                 }
75
76                 $this->addOption('LIMIT', $params['limit'] + 1);
77                 $res = $this->select(__METHOD__);
78
79                 $count = 0;
80                 $result = $this->getResult();
81                 while ($row = $db->fetchObject($res)) {
82                         if (++ $count > $params['limit']) {
83                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
84                                 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->pt_timestamp));
85                                 break;
86                         }
87
88                         $title = Title::makeTitle($row->pt_namespace, $row->pt_title);
89                         if (is_null($resultPageSet)) {
90                                 $vals = array();
91                                 ApiQueryBase::addTitleInfo($vals, $title);
92                                 if(isset($prop['timestamp']))
93                                         $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->pt_timestamp);
94                                 if(isset($prop['user']) && !is_null($row->user_name))
95                                         $vals['user'] = $row->user_name;
96                                 if(isset($prop['comment']))
97                                         $vals['comment'] = $row->pt_reason;
98                                 if(isset($prop['expiry']))
99                                         $vals['expiry'] = Block::decodeExpiry($row->pt_expiry, TS_ISO_8601);
100                                 if(isset($prop['level']))
101                                         $vals['level'] = $row->pt_create_perm;
102                                 
103                                 $fit = $result->addValue(array('query', $this->getModuleName()), null, $vals);
104                                 if(!$fit)
105                                 {
106                                         $this->setContinueEnumParameter('start',
107                                                 wfTimestamp(TS_ISO_8601, $row->pt_timestamp));
108                                         break;
109                                 }
110                         } else {
111                                 $titles[] = $title;
112                         }
113                 }
114                 $db->freeResult($res);
115                 if(is_null($resultPageSet))
116                         $result->setIndexedTagName_internal(array('query', $this->getModuleName()), $this->getModulePrefix());
117                 else
118                         $resultPageSet->populateFromTitles($titles);
119         }
120
121         public function getCacheMode( $params ) {
122                 return 'public';
123         }
124
125         public function getAllowedParams() {
126                 global $wgRestrictionLevels;
127                 return array (
128                         'namespace' => array (
129                                 ApiBase :: PARAM_ISMULTI => true,
130                                 ApiBase :: PARAM_TYPE => 'namespace',
131                         ),
132                         'level' => array(
133                                 ApiBase :: PARAM_ISMULTI => true,
134                                 ApiBase :: PARAM_TYPE => array_diff($wgRestrictionLevels, array(''))
135                         ),
136                         'limit' => array (
137                                 ApiBase :: PARAM_DFLT => 10,
138                                 ApiBase :: PARAM_TYPE => 'limit',
139                                 ApiBase :: PARAM_MIN => 1,
140                                 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
141                                 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
142                         ),
143                         'dir' => array (
144                                 ApiBase :: PARAM_DFLT => 'older',
145                                 ApiBase :: PARAM_TYPE => array (
146                                         'older',
147                                         'newer'
148                                 )
149                         ),
150                         'start' => array(
151                                 ApiBase :: PARAM_TYPE => 'timestamp'
152                         ),
153                         'end' => array(
154                                 ApiBase :: PARAM_TYPE => 'timestamp'
155                         ),
156                         'prop' => array(
157                                 ApiBase :: PARAM_ISMULTI => true,
158                                 ApiBase :: PARAM_DFLT => 'timestamp|level',
159                                 ApiBase :: PARAM_TYPE => array(
160                                         'timestamp',
161                                         'user',
162                                         'comment',
163                                         'expiry',
164                                         'level'
165                                 )
166                         ),
167                 );
168         }
169
170         public function getParamDescription() {
171                 return array (
172                         'namespace' => 'Only list titles in these namespaces',
173                         'start' => 'Start listing at this protection timestamp',
174                         'end' => 'Stop listing at this protection timestamp',
175                         'dir' => 'The direction in which to list',
176                         'limit' => 'How many total pages to return.',
177                         'prop' => 'Which properties to get',
178                         'level' => 'Only list titles with these protection levels',
179                 );
180         }
181
182         public function getDescription() {
183                 return 'List all titles protected from creation';
184         }
185
186         protected function getExamples() {
187                 return array (
188                         'api.php?action=query&list=protectedtitles',
189                 );
190         }
191
192         public function getVersion() {
193                 return __CLASS__ . ': $Id: ApiQueryProtectedTitles.php 69986 2010-07-27 03:57:39Z tstarling $';
194         }
195 }