Merge "Add support for PHP7 random_bytes in favor of mcrypt_create_iv"
[lhc/web/wiklou.git] / includes / api / ApiQueryProtectedTitles.php
1 <?php
2 /**
3 *
4 *
5 * Created on Feb 13, 2009
6 *
7 * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * Query module to enumerate all create-protected pages.
29 *
30 * @ingroup API
31 */
32 class ApiQueryProtectedTitles extends ApiQueryGeneratorBase {
33
34 public function __construct( ApiQuery $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'pt' );
36 }
37
38 public function execute() {
39 $this->run();
40 }
41
42 public function executeGenerator( $resultPageSet ) {
43 $this->run( $resultPageSet );
44 }
45
46 /**
47 * @param ApiPageSet $resultPageSet
48 * @return void
49 */
50 private function run( $resultPageSet = null ) {
51 $params = $this->extractRequestParams();
52
53 $this->addTables( 'protected_titles' );
54 $this->addFields( [ 'pt_namespace', 'pt_title', 'pt_timestamp' ] );
55
56 $prop = array_flip( $params['prop'] );
57 $this->addFieldsIf( 'pt_user', isset( $prop['user'] ) || isset( $prop['userid'] ) );
58 $this->addFieldsIf( 'pt_reason', isset( $prop['comment'] ) || isset( $prop['parsedcomment'] ) );
59 $this->addFieldsIf( 'pt_expiry', isset( $prop['expiry'] ) );
60 $this->addFieldsIf( 'pt_create_perm', isset( $prop['level'] ) );
61
62 $this->addTimestampWhereRange( 'pt_timestamp', $params['dir'], $params['start'], $params['end'] );
63 $this->addWhereFld( 'pt_namespace', $params['namespace'] );
64 $this->addWhereFld( 'pt_create_perm', $params['level'] );
65
66 // Include in ORDER BY for uniqueness
67 $this->addWhereRange( 'pt_namespace', $params['dir'], null, null );
68 $this->addWhereRange( 'pt_title', $params['dir'], null, null );
69
70 if ( !is_null( $params['continue'] ) ) {
71 $cont = explode( '|', $params['continue'] );
72 $this->dieContinueUsageIf( count( $cont ) != 3 );
73 $op = ( $params['dir'] === 'newer' ? '>' : '<' );
74 $db = $this->getDB();
75 $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
76 $continueNs = (int)$cont[1];
77 $this->dieContinueUsageIf( $continueNs != $cont[1] );
78 $continueTitle = $db->addQuotes( $cont[2] );
79 $this->addWhere( "pt_timestamp $op $continueTimestamp OR " .
80 "(pt_timestamp = $continueTimestamp AND " .
81 "(pt_namespace $op $continueNs OR " .
82 "(pt_namespace = $continueNs AND " .
83 "pt_title $op= $continueTitle)))"
84 );
85 }
86
87 if ( isset( $prop['user'] ) ) {
88 $this->addTables( 'user' );
89 $this->addFields( 'user_name' );
90 $this->addJoinConds( [ 'user' => [ 'LEFT JOIN',
91 'user_id=pt_user'
92 ] ] );
93 }
94
95 $this->addOption( 'LIMIT', $params['limit'] + 1 );
96 $res = $this->select( __METHOD__ );
97
98 $count = 0;
99 $result = $this->getResult();
100
101 $titles = [];
102
103 foreach ( $res as $row ) {
104 if ( ++$count > $params['limit'] ) {
105 // We've reached the one extra which shows that there are
106 // additional pages to be had. Stop here...
107 $this->setContinueEnumParameter( 'continue',
108 "$row->pt_timestamp|$row->pt_namespace|$row->pt_title"
109 );
110 break;
111 }
112
113 $title = Title::makeTitle( $row->pt_namespace, $row->pt_title );
114 if ( is_null( $resultPageSet ) ) {
115 $vals = [];
116 ApiQueryBase::addTitleInfo( $vals, $title );
117 if ( isset( $prop['timestamp'] ) ) {
118 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->pt_timestamp );
119 }
120
121 if ( isset( $prop['user'] ) && !is_null( $row->user_name ) ) {
122 $vals['user'] = $row->user_name;
123 }
124
125 if ( isset( $prop['userid'] ) || /*B/C*/isset( $prop['user'] ) ) {
126 $vals['userid'] = (int)$row->pt_user;
127 }
128
129 if ( isset( $prop['comment'] ) ) {
130 $vals['comment'] = $row->pt_reason;
131 }
132
133 if ( isset( $prop['parsedcomment'] ) ) {
134 $vals['parsedcomment'] = Linker::formatComment( $row->pt_reason, $title );
135 }
136
137 if ( isset( $prop['expiry'] ) ) {
138 $vals['expiry'] = ApiResult::formatExpiry( $row->pt_expiry );
139 }
140
141 if ( isset( $prop['level'] ) ) {
142 $vals['level'] = $row->pt_create_perm;
143 }
144
145 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
146 if ( !$fit ) {
147 $this->setContinueEnumParameter( 'continue',
148 "$row->pt_timestamp|$row->pt_namespace|$row->pt_title"
149 );
150 break;
151 }
152 } else {
153 $titles[] = $title;
154 }
155 }
156
157 if ( is_null( $resultPageSet ) ) {
158 $result->addIndexedTagName(
159 [ 'query', $this->getModuleName() ],
160 $this->getModulePrefix()
161 );
162 } else {
163 $resultPageSet->populateFromTitles( $titles );
164 }
165 }
166
167 public function getCacheMode( $params ) {
168 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
169 // formatComment() calls wfMessage() among other things
170 return 'anon-public-user-private';
171 } else {
172 return 'public';
173 }
174 }
175
176 public function getAllowedParams() {
177 return [
178 'namespace' => [
179 ApiBase::PARAM_ISMULTI => true,
180 ApiBase::PARAM_TYPE => 'namespace',
181 ],
182 'level' => [
183 ApiBase::PARAM_ISMULTI => true,
184 ApiBase::PARAM_TYPE => array_diff( $this->getConfig()->get( 'RestrictionLevels' ), [ '' ] )
185 ],
186 'limit' => [
187 ApiBase::PARAM_DFLT => 10,
188 ApiBase::PARAM_TYPE => 'limit',
189 ApiBase::PARAM_MIN => 1,
190 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
191 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
192 ],
193 'dir' => [
194 ApiBase::PARAM_DFLT => 'older',
195 ApiBase::PARAM_TYPE => [
196 'newer',
197 'older'
198 ],
199 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
200 ],
201 'start' => [
202 ApiBase::PARAM_TYPE => 'timestamp'
203 ],
204 'end' => [
205 ApiBase::PARAM_TYPE => 'timestamp'
206 ],
207 'prop' => [
208 ApiBase::PARAM_ISMULTI => true,
209 ApiBase::PARAM_DFLT => 'timestamp|level',
210 ApiBase::PARAM_TYPE => [
211 'timestamp',
212 'user',
213 'userid',
214 'comment',
215 'parsedcomment',
216 'expiry',
217 'level'
218 ],
219 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
220 ],
221 'continue' => [
222 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
223 ],
224 ];
225 }
226
227 protected function getExamplesMessages() {
228 return [
229 'action=query&list=protectedtitles'
230 => 'apihelp-query+protectedtitles-example-simple',
231 'action=query&generator=protectedtitles&gptnamespace=0&prop=linkshere'
232 => 'apihelp-query+protectedtitles-example-generator',
233 ];
234 }
235
236 public function getHelpUrls() {
237 return 'https://www.mediawiki.org/wiki/API:Protectedtitles';
238 }
239 }