Merge "generatePhpCharToUpperMappings: Die if fopen fails"
[lhc/web/wiklou.git] / includes / api / ApiQueryBase.php
1 <?php
2 /**
3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 use Wikimedia\Rdbms\IDatabase;
24 use Wikimedia\Rdbms\IResultWrapper;
25
26 /**
27 * This is a base class for all Query modules.
28 * It provides some common functionality such as constructing various SQL
29 * queries.
30 *
31 * @ingroup API
32 */
33 abstract class ApiQueryBase extends ApiBase {
34 use ApiQueryBlockInfoTrait;
35
36 private $mQueryModule, $mDb, $tables, $where, $fields, $options, $join_conds;
37
38 /**
39 * @param ApiQuery $queryModule
40 * @param string $moduleName
41 * @param string $paramPrefix
42 */
43 public function __construct( ApiQuery $queryModule, $moduleName, $paramPrefix = '' ) {
44 parent::__construct( $queryModule->getMain(), $moduleName, $paramPrefix );
45 $this->mQueryModule = $queryModule;
46 $this->mDb = null;
47 $this->resetQueryParams();
48 }
49
50 /************************************************************************//**
51 * @name Methods to implement
52 * @{
53 */
54
55 /**
56 * Get the cache mode for the data generated by this module. Override
57 * this in the module subclass. For possible return values and other
58 * details about cache modes, see ApiMain::setCacheMode()
59 *
60 * Public caching will only be allowed if *all* the modules that supply
61 * data for a given request return a cache mode of public.
62 *
63 * @param array $params
64 * @return string
65 */
66 public function getCacheMode( $params ) {
67 return 'private';
68 }
69
70 /**
71 * Override this method to request extra fields from the pageSet
72 * using $pageSet->requestField('fieldName')
73 *
74 * Note this only makes sense for 'prop' modules, as 'list' and 'meta'
75 * modules should not be using the pageset.
76 *
77 * @param ApiPageSet $pageSet
78 */
79 public function requestExtraData( $pageSet ) {
80 }
81
82 /** @} */
83
84 /************************************************************************//**
85 * @name Data access
86 * @{
87 */
88
89 /**
90 * Get the main Query module
91 * @return ApiQuery
92 */
93 public function getQuery() {
94 return $this->mQueryModule;
95 }
96
97 /** @inheritDoc */
98 public function getParent() {
99 return $this->getQuery();
100 }
101
102 /**
103 * Get the Query database connection (read-only)
104 * @return IDatabase
105 */
106 protected function getDB() {
107 if ( is_null( $this->mDb ) ) {
108 $this->mDb = $this->getQuery()->getDB();
109 }
110
111 return $this->mDb;
112 }
113
114 /**
115 * Selects the query database connection with the given name.
116 * See ApiQuery::getNamedDB() for more information
117 * @param string $name Name to assign to the database connection
118 * @param int $db One of the DB_* constants
119 * @param string|string[] $groups Query groups
120 * @return IDatabase
121 */
122 public function selectNamedDB( $name, $db, $groups ) {
123 $this->mDb = $this->getQuery()->getNamedDB( $name, $db, $groups );
124 return $this->mDb;
125 }
126
127 /**
128 * Get the PageSet object to work on
129 * @return ApiPageSet
130 */
131 protected function getPageSet() {
132 return $this->getQuery()->getPageSet();
133 }
134
135 /** @} */
136
137 /************************************************************************//**
138 * @name Querying
139 * @{
140 */
141
142 /**
143 * Blank the internal arrays with query parameters
144 */
145 protected function resetQueryParams() {
146 $this->tables = [];
147 $this->where = [];
148 $this->fields = [];
149 $this->options = [];
150 $this->join_conds = [];
151 }
152
153 /**
154 * Add a set of tables to the internal array
155 * @param string|array $tables Table name or array of table names
156 * or nested arrays for joins using parentheses for grouping
157 * @param string|null $alias Table alias, or null for no alias. Cannot be
158 * used with multiple tables
159 */
160 protected function addTables( $tables, $alias = null ) {
161 if ( is_array( $tables ) ) {
162 if ( $alias !== null ) {
163 ApiBase::dieDebug( __METHOD__, 'Multiple table aliases not supported' );
164 }
165 $this->tables = array_merge( $this->tables, $tables );
166 } elseif ( $alias !== null ) {
167 $this->tables[$alias] = $tables;
168 } else {
169 $this->tables[] = $tables;
170 }
171 }
172
173 /**
174 * Add a set of JOIN conditions to the internal array
175 *
176 * JOIN conditions are formatted as [ tablename => [ jointype, conditions ] ]
177 * e.g. [ 'page' => [ 'LEFT JOIN', 'page_id=rev_page' ] ].
178 * Conditions may be a string or an addWhere()-style array.
179 * @param array $join_conds JOIN conditions
180 */
181 protected function addJoinConds( $join_conds ) {
182 if ( !is_array( $join_conds ) ) {
183 ApiBase::dieDebug( __METHOD__, 'Join conditions have to be arrays' );
184 }
185 $this->join_conds = array_merge( $this->join_conds, $join_conds );
186 }
187
188 /**
189 * Add a set of fields to select to the internal array
190 * @param array|string $value Field name or array of field names
191 */
192 protected function addFields( $value ) {
193 if ( is_array( $value ) ) {
194 $this->fields = array_merge( $this->fields, $value );
195 } else {
196 $this->fields[] = $value;
197 }
198 }
199
200 /**
201 * Same as addFields(), but add the fields only if a condition is met
202 * @param array|string $value See addFields()
203 * @param bool $condition If false, do nothing
204 * @return bool $condition
205 */
206 protected function addFieldsIf( $value, $condition ) {
207 if ( $condition ) {
208 $this->addFields( $value );
209
210 return true;
211 }
212
213 return false;
214 }
215
216 /**
217 * Add a set of WHERE clauses to the internal array.
218 * Clauses can be formatted as 'foo=bar' or [ 'foo' => 'bar' ],
219 * the latter only works if the value is a constant (i.e. not another field)
220 *
221 * If $value is an empty array, this function does nothing.
222 *
223 * For example, [ 'foo=bar', 'baz' => 3, 'bla' => 'foo' ] translates
224 * to "foo=bar AND baz='3' AND bla='foo'"
225 * @param string|array $value
226 */
227 protected function addWhere( $value ) {
228 if ( is_array( $value ) ) {
229 // Sanity check: don't insert empty arrays,
230 // Database::makeList() chokes on them
231 if ( count( $value ) ) {
232 $this->where = array_merge( $this->where, $value );
233 }
234 } else {
235 $this->where[] = $value;
236 }
237 }
238
239 /**
240 * Same as addWhere(), but add the WHERE clauses only if a condition is met
241 * @param string|array $value
242 * @param bool $condition If false, do nothing
243 * @return bool $condition
244 */
245 protected function addWhereIf( $value, $condition ) {
246 if ( $condition ) {
247 $this->addWhere( $value );
248
249 return true;
250 }
251
252 return false;
253 }
254
255 /**
256 * Equivalent to addWhere( [ $field => $value ] )
257 * @param string $field Field name
258 * @param string|string[] $value Value; ignored if null or empty array
259 */
260 protected function addWhereFld( $field, $value ) {
261 if ( $value !== null && !( is_array( $value ) && !$value ) ) {
262 $this->where[$field] = $value;
263 }
264 }
265
266 /**
267 * Like addWhereFld for an integer list of IDs
268 * @since 1.33
269 * @param string $table Table name
270 * @param string $field Field name
271 * @param int[] $ids IDs
272 * @return int Count of IDs actually included
273 */
274 protected function addWhereIDsFld( $table, $field, $ids ) {
275 // Use count() to its full documented capabilities to simultaneously
276 // test for null, empty array or empty countable object
277 if ( count( $ids ) ) {
278 $ids = $this->filterIDs( [ [ $table, $field ] ], $ids );
279
280 if ( $ids === [] ) {
281 // Return nothing, no IDs are valid
282 $this->where[] = '0 = 1';
283 } else {
284 $this->where[$field] = $ids;
285 }
286 }
287 return count( $ids );
288 }
289
290 /**
291 * Add a WHERE clause corresponding to a range, and an ORDER BY
292 * clause to sort in the right direction
293 * @param string $field Field name
294 * @param string $dir If 'newer', sort in ascending order, otherwise
295 * sort in descending order
296 * @param string $start Value to start the list at. If $dir == 'newer'
297 * this is the lower boundary, otherwise it's the upper boundary
298 * @param string $end Value to end the list at. If $dir == 'newer' this
299 * is the upper boundary, otherwise it's the lower boundary
300 * @param bool $sort If false, don't add an ORDER BY clause
301 */
302 protected function addWhereRange( $field, $dir, $start, $end, $sort = true ) {
303 $isDirNewer = ( $dir === 'newer' );
304 $after = ( $isDirNewer ? '>=' : '<=' );
305 $before = ( $isDirNewer ? '<=' : '>=' );
306 $db = $this->getDB();
307
308 if ( !is_null( $start ) ) {
309 $this->addWhere( $field . $after . $db->addQuotes( $start ) );
310 }
311
312 if ( !is_null( $end ) ) {
313 $this->addWhere( $field . $before . $db->addQuotes( $end ) );
314 }
315
316 if ( $sort ) {
317 $order = $field . ( $isDirNewer ? '' : ' DESC' );
318 // Append ORDER BY
319 $optionOrderBy = isset( $this->options['ORDER BY'] )
320 ? (array)$this->options['ORDER BY']
321 : [];
322 $optionOrderBy[] = $order;
323 $this->addOption( 'ORDER BY', $optionOrderBy );
324 }
325 }
326
327 /**
328 * Add a WHERE clause corresponding to a range, similar to addWhereRange,
329 * but converts $start and $end to database timestamps.
330 * @see addWhereRange
331 * @param string $field
332 * @param string $dir
333 * @param string $start
334 * @param string $end
335 * @param bool $sort
336 */
337 protected function addTimestampWhereRange( $field, $dir, $start, $end, $sort = true ) {
338 $db = $this->getDB();
339 $this->addWhereRange( $field, $dir,
340 $db->timestampOrNull( $start ), $db->timestampOrNull( $end ), $sort );
341 }
342
343 /**
344 * Add an option such as LIMIT or USE INDEX. If an option was set
345 * before, the old value will be overwritten
346 * @param string $name Option name
347 * @param string|string[]|null $value Option value
348 */
349 protected function addOption( $name, $value = null ) {
350 if ( is_null( $value ) ) {
351 $this->options[] = $name;
352 } else {
353 $this->options[$name] = $value;
354 }
355 }
356
357 /**
358 * Execute a SELECT query based on the values in the internal arrays
359 * @param string $method Function the query should be attributed to.
360 * You should usually use __METHOD__ here
361 * @param array $extraQuery Query data to add but not store in the object
362 * Format is [
363 * 'tables' => ...,
364 * 'fields' => ...,
365 * 'where' => ...,
366 * 'options' => ...,
367 * 'join_conds' => ...
368 * ]
369 * @param array|null &$hookData If set, the ApiQueryBaseBeforeQuery and
370 * ApiQueryBaseAfterQuery hooks will be called, and the
371 * ApiQueryBaseProcessRow hook will be expected.
372 * @return IResultWrapper
373 */
374 protected function select( $method, $extraQuery = [], array &$hookData = null ) {
375 $tables = array_merge(
376 $this->tables,
377 isset( $extraQuery['tables'] ) ? (array)$extraQuery['tables'] : []
378 );
379 $fields = array_merge(
380 $this->fields,
381 isset( $extraQuery['fields'] ) ? (array)$extraQuery['fields'] : []
382 );
383 $where = array_merge(
384 $this->where,
385 isset( $extraQuery['where'] ) ? (array)$extraQuery['where'] : []
386 );
387 $options = array_merge(
388 $this->options,
389 isset( $extraQuery['options'] ) ? (array)$extraQuery['options'] : []
390 );
391 $join_conds = array_merge(
392 $this->join_conds,
393 isset( $extraQuery['join_conds'] ) ? (array)$extraQuery['join_conds'] : []
394 );
395
396 if ( $hookData !== null ) {
397 Hooks::run( 'ApiQueryBaseBeforeQuery',
398 [ $this, &$tables, &$fields, &$where, &$options, &$join_conds, &$hookData ]
399 );
400 }
401
402 $res = $this->getDB()->select( $tables, $fields, $where, $method, $options, $join_conds );
403
404 if ( $hookData !== null ) {
405 Hooks::run( 'ApiQueryBaseAfterQuery', [ $this, $res, &$hookData ] );
406 }
407
408 return $res;
409 }
410
411 /**
412 * Call the ApiQueryBaseProcessRow hook
413 *
414 * Generally, a module that passed $hookData to self::select() will call
415 * this just before calling ApiResult::addValue(), and treat a false return
416 * here in the same way it treats a false return from addValue().
417 *
418 * @since 1.28
419 * @param object $row Database row
420 * @param array &$data Data to be added to the result
421 * @param array &$hookData Hook data from ApiQueryBase::select()
422 * @return bool Return false if row processing should end with continuation
423 */
424 protected function processRow( $row, array &$data, array &$hookData ) {
425 return Hooks::run( 'ApiQueryBaseProcessRow', [ $this, $row, &$data, &$hookData ] );
426 }
427
428 /** @} */
429
430 /************************************************************************//**
431 * @name Utility methods
432 * @{
433 */
434
435 /**
436 * Add information (title and namespace) about a Title object to a
437 * result array
438 * @param array &$arr Result array à la ApiResult
439 * @param Title $title
440 * @param string $prefix Module prefix
441 */
442 public static function addTitleInfo( &$arr, $title, $prefix = '' ) {
443 $arr[$prefix . 'ns'] = (int)$title->getNamespace();
444 $arr[$prefix . 'title'] = $title->getPrefixedText();
445 }
446
447 /**
448 * Add a sub-element under the page element with the given page ID
449 * @param int $pageId Page ID
450 * @param array $data Data array à la ApiResult
451 * @return bool Whether the element fit in the result
452 */
453 protected function addPageSubItems( $pageId, $data ) {
454 $result = $this->getResult();
455 ApiResult::setIndexedTagName( $data, $this->getModulePrefix() );
456
457 return $result->addValue( [ 'query', 'pages', (int)$pageId ],
458 $this->getModuleName(),
459 $data );
460 }
461
462 /**
463 * Same as addPageSubItems(), but one element of $data at a time
464 * @param int $pageId Page ID
465 * @param mixed $item Data à la ApiResult
466 * @param string|null $elemname XML element name. If null, getModuleName()
467 * is used
468 * @return bool Whether the element fit in the result
469 */
470 protected function addPageSubItem( $pageId, $item, $elemname = null ) {
471 if ( is_null( $elemname ) ) {
472 $elemname = $this->getModulePrefix();
473 }
474 $result = $this->getResult();
475 $fit = $result->addValue( [ 'query', 'pages', $pageId,
476 $this->getModuleName() ], null, $item );
477 if ( !$fit ) {
478 return false;
479 }
480 $result->addIndexedTagName( [ 'query', 'pages', $pageId,
481 $this->getModuleName() ], $elemname );
482
483 return true;
484 }
485
486 /**
487 * Set a query-continue value
488 * @param string $paramName Parameter name
489 * @param string|array $paramValue Parameter value
490 */
491 protected function setContinueEnumParameter( $paramName, $paramValue ) {
492 $this->getContinuationManager()->addContinueParam( $this, $paramName, $paramValue );
493 }
494
495 /**
496 * Convert an input title or title prefix into a dbkey.
497 *
498 * $namespace should always be specified in order to handle per-namespace
499 * capitalization settings.
500 *
501 * @param string $titlePart Title part
502 * @param int $namespace Namespace of the title
503 * @return string DBkey (no namespace prefix)
504 */
505 public function titlePartToKey( $titlePart, $namespace = NS_MAIN ) {
506 $t = Title::makeTitleSafe( $namespace, $titlePart . 'x' );
507 if ( !$t || $t->hasFragment() ) {
508 // Invalid title (e.g. bad chars) or contained a '#'.
509 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $titlePart ) ] );
510 }
511 if ( $namespace != $t->getNamespace() || $t->isExternal() ) {
512 // This can happen in two cases. First, if you call titlePartToKey with a title part
513 // that looks like a namespace, but with $defaultNamespace = NS_MAIN. It would be very
514 // difficult to handle such a case. Such cases cannot exist and are therefore treated
515 // as invalid user input. The second case is when somebody specifies a title interwiki
516 // prefix.
517 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $titlePart ) ] );
518 }
519
520 return substr( $t->getDBkey(), 0, -1 );
521 }
522
523 /**
524 * Convert an input title or title prefix into a namespace constant and dbkey.
525 *
526 * @since 1.26
527 * @param string $titlePart Title part
528 * @param int $defaultNamespace Default namespace if none is given
529 * @return array (int, string) Namespace number and DBkey
530 */
531 public function prefixedTitlePartToKey( $titlePart, $defaultNamespace = NS_MAIN ) {
532 $t = Title::newFromText( $titlePart . 'x', $defaultNamespace );
533 if ( !$t || $t->hasFragment() || $t->isExternal() ) {
534 // Invalid title (e.g. bad chars) or contained a '#'.
535 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $titlePart ) ] );
536 }
537
538 return [ $t->getNamespace(), substr( $t->getDBkey(), 0, -1 ) ];
539 }
540
541 /**
542 * @param string $hash
543 * @return bool
544 */
545 public function validateSha1Hash( $hash ) {
546 return (bool)preg_match( '/^[a-f0-9]{40}$/', $hash );
547 }
548
549 /**
550 * @param string $hash
551 * @return bool
552 */
553 public function validateSha1Base36Hash( $hash ) {
554 return (bool)preg_match( '/^[a-z0-9]{31}$/', $hash );
555 }
556
557 /**
558 * Check whether the current user has permission to view revision-deleted
559 * fields.
560 * @return bool
561 */
562 public function userCanSeeRevDel() {
563 return $this->getPermissionManager()->userHasAnyRight(
564 $this->getUser(),
565 'deletedhistory',
566 'deletedtext',
567 'suppressrevision',
568 'viewsuppressed'
569 );
570 }
571
572 /** @} */
573
574 /************************************************************************//**
575 * @name Deprecated methods
576 * @{
577 */
578
579 /**
580 * Filters hidden users (where the user doesn't have the right to view them)
581 * Also adds relevant block information
582 *
583 * @deprecated since 1.34, use ApiQueryBlockInfoTrait instead
584 * @param bool $showBlockInfo
585 * @return void
586 */
587 public function showHiddenUsersAddBlockInfo( $showBlockInfo ) {
588 wfDeprecated( __METHOD__, '1.34' );
589 return $this->addBlockInfoToQuery( $showBlockInfo );
590 }
591
592 /** @} */
593 }