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