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