Debian Codegraph @

includes/api/ApiQueryBase.php

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