3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
23 namespace MediaWiki\Api
;
25 use MediaWiki\MediaWikiServices
;
26 use MediaWiki\Title\MalformedTitleException
;
27 use MediaWiki\Title\Title
;
28 use MediaWiki\Title\TitleValue
;
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
;
37 * This is a base class for all Query modules.
38 * It provides some common functionality such as constructing various SQL
45 abstract class ApiQueryBase
extends ApiBase
{
46 use ApiQueryBlockInfoTrait
;
48 private ApiQuery
$mQueryModule;
49 private ?IReadableDatabase
$mDb;
52 * @var SelectQueryBuilder|null
54 private $queryBuilder;
58 * @param ApiQuery $queryModule
59 * @param string $moduleName
60 * @param string $paramPrefix
62 public function __construct( ApiQuery
$queryModule, string $moduleName, $paramPrefix = '' ) {
63 parent
::__construct( $queryModule->getMain(), $moduleName, $paramPrefix );
64 $this->mQueryModule
= $queryModule;
66 $this->resetQueryParams();
69 /***************************************************************************/
70 // region Methods to implement
71 /** @name Methods to implement */
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()
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.
82 * @param array $params
85 public function getCacheMode( $params ) {
90 * Override this method to request extra fields from the pageSet
91 * using $pageSet->requestField('fieldName')
93 * Note this only makes sense for 'prop' modules, as 'list' and 'meta'
94 * modules should not be using the pageset.
97 * @param ApiPageSet $pageSet
99 public function requestExtraData( $pageSet ) {
102 // endregion -- end of methods to implement
104 /***************************************************************************/
105 // region Data access
106 /** @name Data access */
109 * Get the main Query module
112 public function getQuery() {
113 return $this->mQueryModule
;
117 public function getParent() {
118 return $this->getQuery();
122 * Get the Query database connection (read-only)
123 * @stable to override
124 * @return IReadableDatabase
126 protected function getDB() {
127 $this->mDb ??
= $this->getQuery()->getDB();
133 * Get the PageSet object to work on
134 * @stable to override
137 protected function getPageSet() {
138 return $this->getQuery()->getPageSet();
141 // endregion -- end of data access
143 /***************************************************************************/
145 /** @name Querying */
148 * Blank the internal arrays with query parameters
150 protected function resetQueryParams() {
151 $this->queryBuilder
= null;
155 * Get the SelectQueryBuilder.
157 * This is lazy initialised since getDB() fails in ApiQueryAllImages if it
158 * is called before the constructor completes.
160 * @return SelectQueryBuilder
162 protected function getQueryBuilder() {
163 $this->queryBuilder ??
= $this->getDB()->newSelectQueryBuilder();
164 return $this->queryBuilder
;
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
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' );
179 $this->getQueryBuilder()->rawTables( $tables );
181 $this->getQueryBuilder()->table( $tables, $alias );
186 * Add a set of JOIN conditions to the internal array
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
193 protected function addJoinConds( $join_conds ) {
194 if ( !is_array( $join_conds ) ) {
195 ApiBase
::dieDebug( __METHOD__
, 'Join conditions have to be arrays' );
197 $this->getQueryBuilder()->joinConds( $join_conds );
201 * Add a set of fields to select to the internal array
202 * @param array|string $value Field name or array of field names
204 protected function addFields( $value ) {
205 $this->getQueryBuilder()->fields( $value );
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
214 protected function addFieldsIf( $value, $condition ) {
216 $this->addFields( $value );
225 * Add a set of WHERE clauses to the internal array.
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.
231 * When passing `'field' => $arrayOfIDs` where the IDs are taken from user
232 * input, consider using addWhereIDsFld() instead.
234 * @see IDatabase::select()
235 * @param string|array|IExpression $value
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 );
245 $this->getQueryBuilder()->where( $value );
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
255 protected function addWhereIf( $value, $condition ) {
257 $this->addWhere( $value );
266 * Equivalent to addWhere( [ $field => $value ] )
268 * When $value is an array of integer IDs taken from user input,
269 * consider using addWhereIDsFld() instead.
271 * @param string $field Field name
272 * @param int|string|(string|int|null)[] $value Value; ignored if null or empty array
274 protected function addWhereFld( $field, $value ) {
275 if ( $value !== null && !( is_array( $value ) && !$value ) ) {
276 $this->getQueryBuilder()->where( [ $field => $value ] );
281 * Like addWhereFld for an integer list of IDs
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.
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
292 * This should not be used when there is not a suitable index on $field to
293 * quickly retrieve the minimum and maximum values.
296 * @param string $table Table name
297 * @param string $field Field name
299 * @return int Count of IDs actually included
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 );
308 // Return nothing, no IDs are valid
309 $this->getQueryBuilder()->where( '0 = 1' );
311 $this->getQueryBuilder()->where( [ $field => $ids ] );
314 return count( $ids );
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
329 protected function addWhereRange( $field, $dir, $start, $end, $sort = true ) {
330 $isDirNewer = ( $dir === 'newer' );
331 $after = ( $isDirNewer ?
'>=' : '<=' );
332 $before = ( $isDirNewer ?
'<=' : '>=' );
333 $db = $this->getDB();
335 if ( $start !== null ) {
336 $this->addWhere( $db->expr( $field, $after, $start ) );
339 if ( $end !== null ) {
340 $this->addWhere( $db->expr( $field, $before, $end ) );
344 $this->getQueryBuilder()->orderBy( $field, $isDirNewer ?
null : 'DESC' );
349 * Add a WHERE clause corresponding to a range, similar to addWhereRange,
350 * but converts $start and $end to database timestamps.
352 * @param string $field
354 * @param string|int|null $start
355 * @param string|int|null $end
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 );
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
370 protected function addOption( $name, $value = null ) {
371 $this->getQueryBuilder()->option( $name, $value );
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
384 * 'join_conds' => ...
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
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'] );
396 if ( isset( $extraQuery['fields'] ) ) {
397 $queryBuilder->fields( (array)$extraQuery['fields'] );
399 if ( isset( $extraQuery['where'] ) ) {
400 $queryBuilder->where( (array)$extraQuery['where'] );
402 if ( isset( $extraQuery['options'] ) ) {
403 $queryBuilder->options( (array)$extraQuery['options'] );
405 if ( isset( $extraQuery['join_conds'] ) ) {
406 $queryBuilder->joinConds( (array)$extraQuery['join_conds'] );
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
415 $queryBuilder = $this->getDB()->newSelectQueryBuilder()->queryInfo( $info );
418 $queryBuilder->caller( $method );
419 $res = $queryBuilder->fetchResultSet();
421 if ( $hookData !== null ) {
422 $this->getHookRunner()->onApiQueryBaseAfterQuery( $this, $res, $hookData );
429 * Call the ApiQueryBaseProcessRow hook
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().
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
441 protected function processRow( $row, array &$data, array &$hookData ) {
442 return $this->getHookRunner()->onApiQueryBaseProcessRow( $this, $row, $data, $hookData );
445 // endregion -- end of querying
447 /***************************************************************************/
448 // region Utility methods
449 /** @name Utility methods */
452 * Add information (title and namespace) about a Title object to a
454 * @param array &$arr Result array à la ApiResult
455 * @param Title $title
456 * @param string $prefix Module prefix
458 public static function addTitleInfo( &$arr, $title, $prefix = '' ) {
459 $arr[$prefix . 'ns'] = $title->getNamespace();
460 $arr[$prefix . 'title'] = $title->getPrefixedText();
464 * Add a sub-element under the page element with the given page ID
466 * @param array $data Data array à la ApiResult
467 * @return bool Whether the element fit in the result
469 protected function addPageSubItems( $pageId, $data ) {
470 $result = $this->getResult();
471 ApiResult
::setIndexedTagName( $data, $this->getModulePrefix() );
473 return $result->addValue( [ 'query', 'pages', (int)$pageId ],
474 $this->getModuleName(),
479 * Same as addPageSubItems(), but one element of $data at a time
481 * @param mixed $item Data à la ApiResult
482 * @param string|null $elemname XML element name. If null, getModuleName()
484 * @return bool Whether the element fit in the result
486 protected function addPageSubItem( $pageId, $item, $elemname = null ) {
487 $result = $this->getResult();
488 $fit = $result->addValue( [ 'query', 'pages', $pageId,
489 $this->getModuleName() ], null, $item );
493 $result->addIndexedTagName(
494 [ 'query', 'pages', $pageId, $this->getModuleName() ],
495 $elemname ??
$this->getModulePrefix()
502 * Set a query-continue value
503 * @param string $paramName Parameter name
504 * @param int|string|array $paramValue Parameter value
506 protected function setContinueEnumParameter( $paramName, $paramValue ) {
507 $this->getContinuationManager()->addContinueParam( $this, $paramName, $paramValue );
511 * Convert an input title or title prefix into a dbkey.
513 * $namespace should always be specified in order to handle per-namespace
514 * capitalization settings.
516 * @param string $titlePart
517 * @param int $namespace Namespace of the title
518 * @return string DBkey (no namespace prefix)
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 ) ] );
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
532 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $titlePart ) ] );
535 return substr( $t->getDBkey(), 0, -1 );
539 * Convert an input title or title prefix into a TitleValue.
542 * @param string $titlePart
543 * @param int $defaultNamespace Default namespace if none is given
546 protected function parsePrefixedTitlePart( $titlePart, $defaultNamespace = NS_MAIN
) {
548 $titleParser = MediaWikiServices
::getInstance()->getTitleParser();
549 $t = $titleParser->parseTitle( $titlePart . 'X', $defaultNamespace );
550 } catch ( MalformedTitleException
$e ) {
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 ) ] );
559 return new TitleValue( $t->getNamespace(), substr( $t->getDBkey(), 0, -1 ) );
563 * @param string $hash
566 public function validateSha1Hash( $hash ) {
567 return (bool)preg_match( '/^[a-f0-9]{40}$/', $hash );
571 * @param string $hash
574 public function validateSha1Base36Hash( $hash ) {
575 return (bool)preg_match( '/^[a-z0-9]{31}$/', $hash );
579 * Check whether the current user has permission to view revision-deleted
583 public function userCanSeeRevDel() {
584 return $this->getAuthority()->isAllowedAny(
594 * Preprocess the result set to fill the GenderCache with the necessary information
595 * before using self::addTitleInfo
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
602 protected function executeGenderCacheFromResultWrapper(
603 IResultWrapper
$res, $fname = __METHOD__
, $fieldPrefix = 'page'
605 if ( !$res->numRows() ) {
609 $services = MediaWikiServices
::getInstance();
610 if ( !$services->getContentLanguage()->needsGenderDistinction() ) {
614 $nsInfo = $services->getNamespaceInfo();
615 $namespaceField = $fieldPrefix . '_namespace';
616 $titleField = $fieldPrefix . '_title';
619 foreach ( $res as $row ) {
620 if ( $nsInfo->hasGenderDistinction( $row->$namespaceField ) ) {
621 $usernames[] = $row->$titleField;
625 if ( $usernames === [] ) {
629 $genderCache = $services->getGenderCache();
630 $genderCache->doQuery( $usernames, $fname );
633 // endregion -- end of utility methods
636 /** @deprecated class alias since 1.43 */
637 class_alias( ApiQueryBase
::class, 'ApiQueryBase' );