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