Merge "Add support for PHP7 random_bytes in favor of mcrypt_create_iv"
[lhc/web/wiklou.git] / includes / api / ApiQueryAllPages.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 25, 2006
6 *
7 * Copyright © 2006 Yuri Astrakhan "<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 use MediaWiki\MediaWikiServices;
27
28 /**
29 * Query module to enumerate all available pages.
30 *
31 * @ingroup API
32 */
33 class ApiQueryAllPages extends ApiQueryGeneratorBase {
34
35 public function __construct( ApiQuery $query, $moduleName ) {
36 parent::__construct( $query, $moduleName, 'ap' );
37 }
38
39 public function execute() {
40 $this->run();
41 }
42
43 public function getCacheMode( $params ) {
44 return 'public';
45 }
46
47 /**
48 * @param ApiPageSet $resultPageSet
49 * @return void
50 */
51 public function executeGenerator( $resultPageSet ) {
52 if ( $resultPageSet->isResolvingRedirects() ) {
53 $this->dieWithError( 'apierror-allpages-generator-redirects', 'params' );
54 }
55
56 $this->run( $resultPageSet );
57 }
58
59 /**
60 * @param ApiPageSet $resultPageSet
61 * @return void
62 */
63 private function run( $resultPageSet = null ) {
64 $db = $this->getDB();
65
66 $params = $this->extractRequestParams();
67
68 // Page filters
69 $this->addTables( 'page' );
70
71 if ( !is_null( $params['continue'] ) ) {
72 $cont = explode( '|', $params['continue'] );
73 $this->dieContinueUsageIf( count( $cont ) != 1 );
74 $op = $params['dir'] == 'descending' ? '<' : '>';
75 $cont_from = $db->addQuotes( $cont[0] );
76 $this->addWhere( "page_title $op= $cont_from" );
77 }
78
79 if ( $params['filterredir'] == 'redirects' ) {
80 $this->addWhereFld( 'page_is_redirect', 1 );
81 } elseif ( $params['filterredir'] == 'nonredirects' ) {
82 $this->addWhereFld( 'page_is_redirect', 0 );
83 }
84
85 $this->addWhereFld( 'page_namespace', $params['namespace'] );
86 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
87 $from = ( $params['from'] === null
88 ? null
89 : $this->titlePartToKey( $params['from'], $params['namespace'] ) );
90 $to = ( $params['to'] === null
91 ? null
92 : $this->titlePartToKey( $params['to'], $params['namespace'] ) );
93 $this->addWhereRange( 'page_title', $dir, $from, $to );
94
95 if ( isset( $params['prefix'] ) ) {
96 $this->addWhere( 'page_title' . $db->buildLike(
97 $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
98 $db->anyString() ) );
99 }
100
101 if ( is_null( $resultPageSet ) ) {
102 $selectFields = [
103 'page_namespace',
104 'page_title',
105 'page_id'
106 ];
107 } else {
108 $selectFields = $resultPageSet->getPageTableFields();
109 }
110
111 $this->addFields( $selectFields );
112 $forceNameTitleIndex = true;
113 if ( isset( $params['minsize'] ) ) {
114 $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
115 $forceNameTitleIndex = false;
116 }
117
118 if ( isset( $params['maxsize'] ) ) {
119 $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
120 $forceNameTitleIndex = false;
121 }
122
123 // Page protection filtering
124 if ( count( $params['prtype'] ) || $params['prexpiry'] != 'all' ) {
125 $this->addTables( 'page_restrictions' );
126 $this->addWhere( 'page_id=pr_page' );
127 $this->addWhere( "pr_expiry > {$db->addQuotes( $db->timestamp() )} OR pr_expiry IS NULL" );
128
129 if ( count( $params['prtype'] ) ) {
130 $this->addWhereFld( 'pr_type', $params['prtype'] );
131
132 if ( isset( $params['prlevel'] ) ) {
133 // Remove the empty string and '*' from the prlevel array
134 $prlevel = array_diff( $params['prlevel'], [ '', '*' ] );
135
136 if ( count( $prlevel ) ) {
137 $this->addWhereFld( 'pr_level', $prlevel );
138 }
139 }
140 if ( $params['prfiltercascade'] == 'cascading' ) {
141 $this->addWhereFld( 'pr_cascade', 1 );
142 } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
143 $this->addWhereFld( 'pr_cascade', 0 );
144 }
145 }
146 $forceNameTitleIndex = false;
147
148 if ( $params['prexpiry'] == 'indefinite' ) {
149 $this->addWhere( "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL" );
150 } elseif ( $params['prexpiry'] == 'definite' ) {
151 $this->addWhere( "pr_expiry != {$db->addQuotes( $db->getInfinity() )}" );
152 }
153
154 $this->addOption( 'DISTINCT' );
155 } elseif ( isset( $params['prlevel'] ) ) {
156 $this->dieWithError(
157 [ 'apierror-invalidparammix-mustusewith', 'prlevel', 'prtype' ], 'invalidparammix'
158 );
159 }
160
161 if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
162 $this->addTables( 'langlinks' );
163 $this->addJoinConds( [ 'langlinks' => [ 'LEFT JOIN', 'page_id=ll_from' ] ] );
164 $this->addWhere( 'll_from IS NULL' );
165 $forceNameTitleIndex = false;
166 } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
167 $this->addTables( 'langlinks' );
168 $this->addWhere( 'page_id=ll_from' );
169 $this->addOption( 'STRAIGHT_JOIN' );
170
171 // MySQL filesorts if we use a GROUP BY that works with the rules
172 // in the 1992 SQL standard (it doesn't like having the
173 // constant-in-WHERE page_namespace column in there). Using the
174 // 1999 rules works fine, but that breaks other DBs. Sigh.
175 /// @todo Once we drop support for 1992-rule DBs, we can simplify this.
176 $dbType = $db->getType();
177 if ( $dbType === 'mysql' || $dbType === 'sqlite' ) {
178 // Ignore the rules, or 1999 rules if you count unique keys
179 // over non-NULL columns as satisfying the requirement for
180 // "functional dependency" and don't require including
181 // constant-in-WHERE columns in the GROUP BY.
182 $this->addOption( 'GROUP BY', [ 'page_title' ] );
183 } elseif ( $dbType === 'postgres' && $db->getServerVersion() >= 9.1 ) {
184 // 1999 rules only counting primary keys
185 $this->addOption( 'GROUP BY', [ 'page_title', 'page_id' ] );
186 } else {
187 // 1992 rules
188 $this->addOption( 'GROUP BY', $selectFields );
189 }
190
191 $forceNameTitleIndex = false;
192 }
193
194 if ( $forceNameTitleIndex ) {
195 $this->addOption( 'USE INDEX', 'name_title' );
196 }
197
198 $limit = $params['limit'];
199 $this->addOption( 'LIMIT', $limit + 1 );
200 $res = $this->select( __METHOD__ );
201
202 // Get gender information
203 if ( MWNamespace::hasGenderDistinction( $params['namespace'] ) ) {
204 $users = [];
205 foreach ( $res as $row ) {
206 $users[] = $row->page_title;
207 }
208 MediaWikiServices::getInstance()->getGenderCache()->doQuery( $users, __METHOD__ );
209 $res->rewind(); // reset
210 }
211
212 $count = 0;
213 $result = $this->getResult();
214 foreach ( $res as $row ) {
215 if ( ++$count > $limit ) {
216 // We've reached the one extra which shows that there are
217 // additional pages to be had. Stop here...
218 $this->setContinueEnumParameter( 'continue', $row->page_title );
219 break;
220 }
221
222 if ( is_null( $resultPageSet ) ) {
223 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
224 $vals = [
225 'pageid' => intval( $row->page_id ),
226 'ns' => intval( $title->getNamespace() ),
227 'title' => $title->getPrefixedText()
228 ];
229 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
230 if ( !$fit ) {
231 $this->setContinueEnumParameter( 'continue', $row->page_title );
232 break;
233 }
234 } else {
235 $resultPageSet->processDbRow( $row );
236 }
237 }
238
239 if ( is_null( $resultPageSet ) ) {
240 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'p' );
241 }
242 }
243
244 public function getAllowedParams() {
245 return [
246 'from' => null,
247 'continue' => [
248 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
249 ],
250 'to' => null,
251 'prefix' => null,
252 'namespace' => [
253 ApiBase::PARAM_DFLT => NS_MAIN,
254 ApiBase::PARAM_TYPE => 'namespace',
255 ],
256 'filterredir' => [
257 ApiBase::PARAM_DFLT => 'all',
258 ApiBase::PARAM_TYPE => [
259 'all',
260 'redirects',
261 'nonredirects'
262 ]
263 ],
264 'minsize' => [
265 ApiBase::PARAM_TYPE => 'integer',
266 ],
267 'maxsize' => [
268 ApiBase::PARAM_TYPE => 'integer',
269 ],
270 'prtype' => [
271 ApiBase::PARAM_TYPE => Title::getFilteredRestrictionTypes( true ),
272 ApiBase::PARAM_ISMULTI => true
273 ],
274 'prlevel' => [
275 ApiBase::PARAM_TYPE => $this->getConfig()->get( 'RestrictionLevels' ),
276 ApiBase::PARAM_ISMULTI => true
277 ],
278 'prfiltercascade' => [
279 ApiBase::PARAM_DFLT => 'all',
280 ApiBase::PARAM_TYPE => [
281 'cascading',
282 'noncascading',
283 'all'
284 ],
285 ],
286 'limit' => [
287 ApiBase::PARAM_DFLT => 10,
288 ApiBase::PARAM_TYPE => 'limit',
289 ApiBase::PARAM_MIN => 1,
290 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
291 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
292 ],
293 'dir' => [
294 ApiBase::PARAM_DFLT => 'ascending',
295 ApiBase::PARAM_TYPE => [
296 'ascending',
297 'descending'
298 ]
299 ],
300 'filterlanglinks' => [
301 ApiBase::PARAM_TYPE => [
302 'withlanglinks',
303 'withoutlanglinks',
304 'all'
305 ],
306 ApiBase::PARAM_DFLT => 'all'
307 ],
308 'prexpiry' => [
309 ApiBase::PARAM_TYPE => [
310 'indefinite',
311 'definite',
312 'all'
313 ],
314 ApiBase::PARAM_DFLT => 'all'
315 ],
316 ];
317 }
318
319 protected function getExamplesMessages() {
320 return [
321 'action=query&list=allpages&apfrom=B'
322 => 'apihelp-query+allpages-example-B',
323 'action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info'
324 => 'apihelp-query+allpages-example-generator',
325 'action=query&generator=allpages&gaplimit=2&' .
326 'gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
327 => 'apihelp-query+allpages-example-generator-revisions',
328 ];
329 }
330
331 public function getHelpUrls() {
332 return 'https://www.mediawiki.org/wiki/API:Allpages';
333 }
334 }