Merge "(bug 43801) add a getter for ICU version to ICUCollation"
[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 /**
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
36 private $mQueryModule, $mDb, $tables, $where, $fields, $options, $join_conds;
37
38 /**
39 * @param $query ApiBase
40 * @param $moduleName string
41 * @param $paramPrefix string
42 */
43 public function __construct( ApiBase $query, $moduleName, $paramPrefix = '' ) {
44 parent::__construct( $query->getMain(), $moduleName, $paramPrefix );
45 $this->mQueryModule = $query;
46 $this->mDb = null;
47 $this->resetQueryParams();
48 }
49
50 /**
51 * Get the cache mode for the data generated by this module. Override
52 * this in the module subclass. For possible return values and other
53 * details about cache modes, see ApiMain::setCacheMode()
54 *
55 * Public caching will only be allowed if *all* the modules that supply
56 * data for a given request return a cache mode of public.
57 *
58 * @param $params
59 * @return string
60 */
61 public function getCacheMode( $params ) {
62 return 'private';
63 }
64
65 /**
66 * Blank the internal arrays with query parameters
67 */
68 protected function resetQueryParams() {
69 $this->tables = array();
70 $this->where = array();
71 $this->fields = array();
72 $this->options = array();
73 $this->join_conds = array();
74 }
75
76 /**
77 * Add a set of tables to the internal array
78 * @param $tables mixed Table name or array of table names
79 * @param $alias mixed Table alias, or null for no alias. Cannot be
80 * used with multiple tables
81 */
82 protected function addTables( $tables, $alias = null ) {
83 if ( is_array( $tables ) ) {
84 if ( !is_null( $alias ) ) {
85 ApiBase::dieDebug( __METHOD__, 'Multiple table aliases not supported' );
86 }
87 $this->tables = array_merge( $this->tables, $tables );
88 } else {
89 if ( !is_null( $alias ) ) {
90 $this->tables[$alias] = $tables;
91 } else {
92 $this->tables[] = $tables;
93 }
94 }
95 }
96
97 /**
98 * Add a set of JOIN conditions to the internal array
99 *
100 * JOIN conditions are formatted as array( tablename => array(jointype,
101 * conditions) e.g. array('page' => array('LEFT JOIN',
102 * 'page_id=rev_page')) . conditions may be a string or an
103 * addWhere()-style array
104 * @param $join_conds array JOIN conditions
105 */
106 protected function addJoinConds( $join_conds ) {
107 if ( !is_array( $join_conds ) ) {
108 ApiBase::dieDebug( __METHOD__, 'Join conditions have to be arrays' );
109 }
110 $this->join_conds = array_merge( $this->join_conds, $join_conds );
111 }
112
113 /**
114 * Add a set of fields to select to the internal array
115 * @param $value array|string Field name or array of field names
116 */
117 protected function addFields( $value ) {
118 if ( is_array( $value ) ) {
119 $this->fields = array_merge( $this->fields, $value );
120 } else {
121 $this->fields[] = $value;
122 }
123 }
124
125 /**
126 * Same as addFields(), but add the fields only if a condition is met
127 * @param $value array|string See addFields()
128 * @param $condition bool If false, do nothing
129 * @return bool $condition
130 */
131 protected function addFieldsIf( $value, $condition ) {
132 if ( $condition ) {
133 $this->addFields( $value );
134 return true;
135 }
136 return false;
137 }
138
139 /**
140 * Add a set of WHERE clauses to the internal array.
141 * Clauses can be formatted as 'foo=bar' or array('foo' => 'bar'),
142 * the latter only works if the value is a constant (i.e. not another field)
143 *
144 * If $value is an empty array, this function does nothing.
145 *
146 * For example, array('foo=bar', 'baz' => 3, 'bla' => 'foo') translates
147 * to "foo=bar AND baz='3' AND bla='foo'"
148 * @param $value mixed String or array
149 */
150 protected function addWhere( $value ) {
151 if ( is_array( $value ) ) {
152 // Sanity check: don't insert empty arrays,
153 // Database::makeList() chokes on them
154 if ( count( $value ) ) {
155 $this->where = array_merge( $this->where, $value );
156 }
157 } else {
158 $this->where[] = $value;
159 }
160 }
161
162 /**
163 * Same as addWhere(), but add the WHERE clauses only if a condition is met
164 * @param $value mixed See addWhere()
165 * @param $condition bool If false, do nothing
166 * @return bool $condition
167 */
168 protected function addWhereIf( $value, $condition ) {
169 if ( $condition ) {
170 $this->addWhere( $value );
171 return true;
172 }
173 return false;
174 }
175
176 /**
177 * Equivalent to addWhere(array($field => $value))
178 * @param $field string Field name
179 * @param $value string Value; ignored if null or empty array;
180 */
181 protected function addWhereFld( $field, $value ) {
182 // Use count() to its full documented capabilities to simultaneously
183 // test for null, empty array or empty countable object
184 if ( count( $value ) ) {
185 $this->where[$field] = $value;
186 }
187 }
188
189 /**
190 * Add a WHERE clause corresponding to a range, and an ORDER BY
191 * clause to sort in the right direction
192 * @param $field string Field name
193 * @param $dir string If 'newer', sort in ascending order, otherwise
194 * sort in descending order
195 * @param $start string Value to start the list at. If $dir == 'newer'
196 * this is the lower boundary, otherwise it's the upper boundary
197 * @param $end string Value to end the list at. If $dir == 'newer' this
198 * is the upper boundary, otherwise it's the lower boundary
199 * @param $sort bool If false, don't add an ORDER BY clause
200 */
201 protected function addWhereRange( $field, $dir, $start, $end, $sort = true ) {
202 $isDirNewer = ( $dir === 'newer' );
203 $after = ( $isDirNewer ? '>=' : '<=' );
204 $before = ( $isDirNewer ? '<=' : '>=' );
205 $db = $this->getDB();
206
207 if ( !is_null( $start ) ) {
208 $this->addWhere( $field . $after . $db->addQuotes( $start ) );
209 }
210
211 if ( !is_null( $end ) ) {
212 $this->addWhere( $field . $before . $db->addQuotes( $end ) );
213 }
214
215 if ( $sort ) {
216 $order = $field . ( $isDirNewer ? '' : ' DESC' );
217 // Append ORDER BY
218 $optionOrderBy = isset( $this->options['ORDER BY'] ) ? (array)$this->options['ORDER BY'] : array();
219 $optionOrderBy[] = $order;
220 $this->addOption( 'ORDER BY', $optionOrderBy );
221 }
222 }
223
224 /**
225 * Add a WHERE clause corresponding to a range, similar to addWhereRange,
226 * but converts $start and $end to database timestamps.
227 * @see addWhereRange
228 * @param $field
229 * @param $dir
230 * @param $start
231 * @param $end
232 * @param $sort bool
233 */
234 protected function addTimestampWhereRange( $field, $dir, $start, $end, $sort = true ) {
235 $db = $this->getDb();
236 $this->addWhereRange( $field, $dir,
237 $db->timestampOrNull( $start ), $db->timestampOrNull( $end ), $sort );
238 }
239
240 /**
241 * Add an option such as LIMIT or USE INDEX. If an option was set
242 * before, the old value will be overwritten
243 * @param $name string Option name
244 * @param $value string Option value
245 */
246 protected function addOption( $name, $value = null ) {
247 if ( is_null( $value ) ) {
248 $this->options[] = $name;
249 } else {
250 $this->options[$name] = $value;
251 }
252 }
253
254 /**
255 * Execute a SELECT query based on the values in the internal arrays
256 * @param $method string Function the query should be attributed to.
257 * You should usually use __METHOD__ here
258 * @param $extraQuery array Query data to add but not store in the object
259 * Format is array( 'tables' => ..., 'fields' => ..., 'where' => ..., 'options' => ..., 'join_conds' => ... )
260 * @return ResultWrapper
261 */
262 protected function select( $method, $extraQuery = array() ) {
263
264 $tables = array_merge( $this->tables, isset( $extraQuery['tables'] ) ? (array)$extraQuery['tables'] : array() );
265 $fields = array_merge( $this->fields, isset( $extraQuery['fields'] ) ? (array)$extraQuery['fields'] : array() );
266 $where = array_merge( $this->where, isset( $extraQuery['where'] ) ? (array)$extraQuery['where'] : array() );
267 $options = array_merge( $this->options, isset( $extraQuery['options'] ) ? (array)$extraQuery['options'] : array() );
268 $join_conds = array_merge( $this->join_conds, isset( $extraQuery['join_conds'] ) ? (array)$extraQuery['join_conds'] : array() );
269
270 // getDB has its own profileDBIn/Out calls
271 $db = $this->getDB();
272
273 $this->profileDBIn();
274 $res = $db->select( $tables, $fields, $where, $method, $options, $join_conds );
275 $this->profileDBOut();
276
277 return $res;
278 }
279
280 /**
281 * Estimate the row count for the SELECT query that would be run if we
282 * called select() right now, and check if it's acceptable.
283 * @return bool true if acceptable, false otherwise
284 */
285 protected function checkRowCount() {
286 $db = $this->getDB();
287 $this->profileDBIn();
288 $rowcount = $db->estimateRowCount( $this->tables, $this->fields, $this->where, __METHOD__, $this->options );
289 $this->profileDBOut();
290
291 global $wgAPIMaxDBRows;
292 if ( $rowcount > $wgAPIMaxDBRows ) {
293 return false;
294 }
295 return true;
296 }
297
298 /**
299 * Add information (title and namespace) about a Title object to a
300 * result array
301 * @param $arr array Result array à la ApiResult
302 * @param $title Title
303 * @param $prefix string Module prefix
304 */
305 public static function addTitleInfo( &$arr, $title, $prefix = '' ) {
306 $arr[$prefix . 'ns'] = intval( $title->getNamespace() );
307 $arr[$prefix . 'title'] = $title->getPrefixedText();
308 }
309
310 /**
311 * Override this method to request extra fields from the pageSet
312 * using $pageSet->requestField('fieldName')
313 * @param $pageSet ApiPageSet
314 */
315 public function requestExtraData( $pageSet ) {
316 }
317
318 /**
319 * Get the main Query module
320 * @return ApiQuery
321 */
322 public function getQuery() {
323 return $this->mQueryModule;
324 }
325
326 /**
327 * Add a sub-element under the page element with the given page ID
328 * @param $pageId int Page ID
329 * @param $data array Data array à la ApiResult
330 * @return bool Whether the element fit in the result
331 */
332 protected function addPageSubItems( $pageId, $data ) {
333 $result = $this->getResult();
334 $result->setIndexedTagName( $data, $this->getModulePrefix() );
335 return $result->addValue( array( 'query', 'pages', intval( $pageId ) ),
336 $this->getModuleName(),
337 $data );
338 }
339
340 /**
341 * Same as addPageSubItems(), but one element of $data at a time
342 * @param $pageId int Page ID
343 * @param $item array Data array à la ApiResult
344 * @param $elemname string XML element name. If null, getModuleName()
345 * is used
346 * @return bool Whether the element fit in the result
347 */
348 protected function addPageSubItem( $pageId, $item, $elemname = null ) {
349 if ( is_null( $elemname ) ) {
350 $elemname = $this->getModulePrefix();
351 }
352 $result = $this->getResult();
353 $fit = $result->addValue( array( 'query', 'pages', $pageId,
354 $this->getModuleName() ), null, $item );
355 if ( !$fit ) {
356 return false;
357 }
358 $result->setIndexedTagName_internal( array( 'query', 'pages', $pageId,
359 $this->getModuleName() ), $elemname );
360 return true;
361 }
362
363 /**
364 * Set a query-continue value
365 * @param $paramName string Parameter name
366 * @param $paramValue string Parameter value
367 */
368 protected function setContinueEnumParameter( $paramName, $paramValue ) {
369 $paramName = $this->encodeParamName( $paramName );
370 $msg = array( $paramName => $paramValue );
371 $result = $this->getResult();
372 $result->disableSizeCheck();
373 $result->addValue( 'query-continue', $this->getModuleName(), $msg );
374 $result->enableSizeCheck();
375 }
376
377 /**
378 * Die with the $prefix.'badcontinue' error. This call is common enough to make it into the base method.
379 * @param $condition boolean will only die if this value is true
380 * @since 1.21
381 */
382 protected function dieContinueUsageIf( $condition ) {
383 if ( $condition ) {
384 $this->dieUsage(
385 'Invalid continue param. You should pass the original value returned by the previous query',
386 'badcontinue' );
387 }
388 }
389
390 /**
391 * Get the Query database connection (read-only)
392 * @return DatabaseBase
393 */
394 protected function getDB() {
395 if ( is_null( $this->mDb ) ) {
396 $apiQuery = $this->getQuery();
397 $this->mDb = $apiQuery->getDB();
398 }
399 return $this->mDb;
400 }
401
402 /**
403 * Selects the query database connection with the given name.
404 * See ApiQuery::getNamedDB() for more information
405 * @param $name string Name to assign to the database connection
406 * @param $db int One of the DB_* constants
407 * @param $groups array Query groups
408 * @return DatabaseBase
409 */
410 public function selectNamedDB( $name, $db, $groups ) {
411 $this->mDb = $this->getQuery()->getNamedDB( $name, $db, $groups );
412 }
413
414 /**
415 * Get the PageSet object to work on
416 * @return ApiPageSet
417 */
418 protected function getPageSet() {
419 return $this->getQuery()->getPageSet();
420 }
421
422 /**
423 * Convert a title to a DB key
424 * @param $title string Page title with spaces
425 * @return string Page title with underscores
426 */
427 public function titleToKey( $title ) {
428 // Don't throw an error if we got an empty string
429 if ( trim( $title ) == '' ) {
430 return '';
431 }
432 $t = Title::newFromText( $title );
433 if ( !$t ) {
434 $this->dieUsageMsg( array( 'invalidtitle', $title ) );
435 }
436 return $t->getPrefixedDbKey();
437 }
438
439 /**
440 * The inverse of titleToKey()
441 * @param $key string Page title with underscores
442 * @return string Page title with spaces
443 */
444 public function keyToTitle( $key ) {
445 // Don't throw an error if we got an empty string
446 if ( trim( $key ) == '' ) {
447 return '';
448 }
449 $t = Title::newFromDbKey( $key );
450 // This really shouldn't happen but we gotta check anyway
451 if ( !$t ) {
452 $this->dieUsageMsg( array( 'invalidtitle', $key ) );
453 }
454 return $t->getPrefixedText();
455 }
456
457 /**
458 * An alternative to titleToKey() that doesn't trim trailing spaces
459 * @param $titlePart string Title part with spaces
460 * @return string Title part with underscores
461 */
462 public function titlePartToKey( $titlePart ) {
463 return substr( $this->titleToKey( $titlePart . 'x' ), 0, - 1 );
464 }
465
466 /**
467 * An alternative to keyToTitle() that doesn't trim trailing spaces
468 * @param $keyPart string Key part with spaces
469 * @return string Key part with underscores
470 */
471 public function keyPartToTitle( $keyPart ) {
472 return substr( $this->keyToTitle( $keyPart . 'x' ), 0, - 1 );
473 }
474
475 /**
476 * Gets the personalised direction parameter description
477 *
478 * @param string $p ModulePrefix
479 * @param string $extraDirText Any extra text to be appended on the description
480 * @return array
481 */
482 public function getDirectionDescription( $p = '', $extraDirText = '' ) {
483 return array(
484 "In which direction to enumerate{$extraDirText}",
485 " newer - List oldest first. Note: {$p}start has to be before {$p}end.",
486 " older - List newest first (default). Note: {$p}start has to be later than {$p}end.",
487 );
488 }
489
490 /**
491 * @param $query String
492 * @param $protocol String
493 * @return null|string
494 */
495 public function prepareUrlQuerySearchString( $query = null, $protocol = null) {
496 $db = $this->getDb();
497 if ( !is_null( $query ) || $query != '' ) {
498 if ( is_null( $protocol ) ) {
499 $protocol = 'http://';
500 }
501
502 $likeQuery = LinkFilter::makeLikeArray( $query, $protocol );
503 if ( !$likeQuery ) {
504 $this->dieUsage( 'Invalid query', 'bad_query' );
505 }
506
507 $likeQuery = LinkFilter::keepOneWildcard( $likeQuery );
508 return 'el_index ' . $db->buildLike( $likeQuery );
509 } elseif ( !is_null( $protocol ) ) {
510 return 'el_index ' . $db->buildLike( "$protocol", $db->anyString() );
511 }
512
513 return null;
514 }
515
516 /**
517 * Filters hidden users (where the user doesn't have the right to view them)
518 * Also adds relevant block information
519 *
520 * @param bool $showBlockInfo
521 * @return void
522 */
523 public function showHiddenUsersAddBlockInfo( $showBlockInfo ) {
524 $userCanViewHiddenUsers = $this->getUser()->isAllowed( 'hideuser' );
525
526 if ( $showBlockInfo || !$userCanViewHiddenUsers ) {
527 $this->addTables( 'ipblocks' );
528 $this->addJoinConds( array(
529 'ipblocks' => array( 'LEFT JOIN', 'ipb_user=user_id' ),
530 ) );
531
532 $this->addFields( 'ipb_deleted' );
533
534 if ( $showBlockInfo ) {
535 $this->addFields( array( 'ipb_id', 'ipb_by', 'ipb_by_text', 'ipb_reason', 'ipb_expiry' ) );
536 }
537
538 // Don't show hidden names
539 if ( !$userCanViewHiddenUsers ) {
540 $this->addWhere( 'ipb_deleted = 0 OR ipb_deleted IS NULL' );
541 }
542 }
543 }
544
545 /**
546 * @param $hash string
547 * @return bool
548 */
549 public function validateSha1Hash( $hash ) {
550 return preg_match( '/^[a-f0-9]{40}$/', $hash );
551 }
552
553 /**
554 * @param $hash string
555 * @return bool
556 */
557 public function validateSha1Base36Hash( $hash ) {
558 return preg_match( '/^[a-z0-9]{31}$/', $hash );
559 }
560
561 /**
562 * @return array
563 */
564 public function getPossibleErrors() {
565 $errors = parent::getPossibleErrors();
566 $errors = array_merge( $errors, array(
567 array( 'invalidtitle', 'title' ),
568 array( 'invalidtitle', 'key' ),
569 ) );
570 $params = $this->getFinalParams();
571 if ( array_key_exists( 'continue', $params ) ) {
572 $errors = array_merge( $errors, array(
573 array(
574 'code' => 'badcontinue',
575 'info' => 'Invalid continue param. You should pass the original value returned by the previous query'
576 ),
577 ) );
578 }
579 return $errors;
580 }
581 }
582
583 /**
584 * @ingroup API
585 */
586 abstract class ApiQueryGeneratorBase extends ApiQueryBase {
587
588 private $mIsGenerator;
589
590 /**
591 * @param $query ApiBase
592 * @param $moduleName string
593 * @param $paramPrefix string
594 */
595 public function __construct( $query, $moduleName, $paramPrefix = '' ) {
596 parent::__construct( $query, $moduleName, $paramPrefix );
597 $this->mIsGenerator = false;
598 }
599
600 /**
601 * Switch this module to generator mode. By default, generator mode is
602 * switched off and the module acts like a normal query module.
603 */
604 public function setGeneratorMode() {
605 $this->mIsGenerator = true;
606 }
607
608 /**
609 * Overrides base class to prepend 'g' to every generator parameter
610 * @param $paramName string Parameter name
611 * @return string Prefixed parameter name
612 */
613 public function encodeParamName( $paramName ) {
614 if ( $this->mIsGenerator ) {
615 return 'g' . parent::encodeParamName( $paramName );
616 } else {
617 return parent::encodeParamName( $paramName );
618 }
619 }
620
621 /**
622 * Execute this module as a generator
623 * @param $resultPageSet ApiPageSet: All output should be appended to
624 * this object
625 */
626 abstract public function executeGenerator( $resultPageSet );
627 }