986bf435618699b219a95d48d5f43723b464de85
[lhc/web/wiklou.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 © 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'] ) || isset( $prop['parsedcomment'] ) );
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 $this->addTables( 'user' );
69 $this->addFields( 'user_name' );
70 $this->addJoinConds( array( 'user' => array( 'LEFT JOIN',
71 'user_id=pt_user'
72 ) ) );
73 }
74
75 $this->addOption( 'LIMIT', $params['limit'] + 1 );
76 $res = $this->select( __METHOD__ );
77
78 $count = 0;
79 $result = $this->getResult();
80 foreach ( $res as $row ) {
81 if ( ++ $count > $params['limit'] ) {
82 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
83 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->pt_timestamp ) );
84 break;
85 }
86
87 $title = Title::makeTitle( $row->pt_namespace, $row->pt_title );
88 if ( is_null( $resultPageSet ) ) {
89 $vals = array();
90 ApiQueryBase::addTitleInfo( $vals, $title );
91 if ( isset( $prop['timestamp'] ) ) {
92 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->pt_timestamp );
93 }
94
95 if ( isset( $prop['user'] ) && !is_null( $row->user_name ) ) {
96 $vals['user'] = $row->user_name;
97 }
98
99 if ( isset( $prop['comment'] ) ) {
100 $vals['comment'] = $row->pt_reason;
101 }
102
103 if ( isset( $prop['parsedcomment'] ) ) {
104 global $wgUser;
105 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->pt_reason, $title );
106 }
107
108 if ( isset( $prop['expiry'] ) ) {
109 $vals['expiry'] = Block::decodeExpiry( $row->pt_expiry, TS_ISO_8601 );
110 }
111
112 if ( isset( $prop['level'] ) ) {
113 $vals['level'] = $row->pt_create_perm;
114 }
115
116 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
117 if ( !$fit ) {
118 $this->setContinueEnumParameter( 'start',
119 wfTimestamp( TS_ISO_8601, $row->pt_timestamp ) );
120 break;
121 }
122 } else {
123 $titles[] = $title;
124 }
125 }
126
127 if ( is_null( $resultPageSet ) ) {
128 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), $this->getModulePrefix() );
129 } else {
130 $resultPageSet->populateFromTitles( $titles );
131 }
132 }
133
134 public function getAllowedParams() {
135 global $wgRestrictionLevels;
136 return array(
137 'namespace' => array(
138 ApiBase::PARAM_ISMULTI => true,
139 ApiBase::PARAM_TYPE => 'namespace',
140 ),
141 'level' => array(
142 ApiBase::PARAM_ISMULTI => true,
143 ApiBase::PARAM_TYPE => array_diff( $wgRestrictionLevels, array( '' ) )
144 ),
145 'limit' => array (
146 ApiBase::PARAM_DFLT => 10,
147 ApiBase::PARAM_TYPE => 'limit',
148 ApiBase::PARAM_MIN => 1,
149 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
150 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
151 ),
152 'dir' => array(
153 ApiBase::PARAM_DFLT => 'older',
154 ApiBase::PARAM_TYPE => array(
155 'older',
156 'newer'
157 )
158 ),
159 'start' => array(
160 ApiBase::PARAM_TYPE => 'timestamp'
161 ),
162 'end' => array(
163 ApiBase::PARAM_TYPE => 'timestamp'
164 ),
165 'prop' => array(
166 ApiBase::PARAM_ISMULTI => true,
167 ApiBase::PARAM_DFLT => 'timestamp|level',
168 ApiBase::PARAM_TYPE => array(
169 'timestamp',
170 'user',
171 'comment',
172 'parsedcomment',
173 'expiry',
174 'level'
175 )
176 ),
177 );
178 }
179
180 public function getParamDescription() {
181 return array(
182 'namespace' => 'Only list titles in these namespaces',
183 'start' => 'Start listing at this protection timestamp',
184 'end' => 'Stop listing at this protection timestamp',
185 'dir' => 'The direction in which to list',
186 'limit' => 'How many total pages to return',
187 'prop' => 'Which properties to get',
188 'level' => 'Only list titles with these protection levels',
189 );
190 }
191
192 public function getDescription() {
193 return 'List all titles protected from creation';
194 }
195
196 protected function getExamples() {
197 return array(
198 'api.php?action=query&list=protectedtitles',
199 );
200 }
201
202 public function getVersion() {
203 return __CLASS__ . ': $Id$';
204 }
205 }