Revert "Made LCStoreDB try to use a separate DB connection"
[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 ApiQuery $queryModule
40 * @param string $moduleName
41 * @param string $paramPrefix
42 */
43 public function __construct( ApiQuery $queryModule, $moduleName, $paramPrefix = '' ) {
44 parent::__construct( $queryModule->getMain(), $moduleName, $paramPrefix );
45 $this->mQueryModule = $queryModule;
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 array $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 string|string[] $tables Table name or array of table names
79 * @param string|null $alias 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 array $join_conds 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 array|string $value 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 array|string $value See addFields()
128 * @param bool $condition If false, do nothing
129 * @return bool $condition
130 */
131 protected function addFieldsIf( $value, $condition ) {
132 if ( $condition ) {
133 $this->addFields( $value );
134
135 return true;
136 }
137
138 return false;
139 }
140
141 /**
142 * Add a set of WHERE clauses to the internal array.
143 * Clauses can be formatted as 'foo=bar' or array('foo' => 'bar'),
144 * the latter only works if the value is a constant (i.e. not another field)
145 *
146 * If $value is an empty array, this function does nothing.
147 *
148 * For example, array('foo=bar', 'baz' => 3, 'bla' => 'foo') translates
149 * to "foo=bar AND baz='3' AND bla='foo'"
150 * @param string|array $value
151 */
152 protected function addWhere( $value ) {
153 if ( is_array( $value ) ) {
154 // Sanity check: don't insert empty arrays,
155 // Database::makeList() chokes on them
156 if ( count( $value ) ) {
157 $this->where = array_merge( $this->where, $value );
158 }
159 } else {
160 $this->where[] = $value;
161 }
162 }
163
164 /**
165 * Same as addWhere(), but add the WHERE clauses only if a condition is met
166 * @param string|array $value
167 * @param bool $condition If false, do nothing
168 * @return bool $condition
169 */
170 protected function addWhereIf( $value, $condition ) {
171 if ( $condition ) {
172 $this->addWhere( $value );
173
174 return true;
175 }
176
177 return false;
178 }
179
180 /**
181 * Equivalent to addWhere(array($field => $value))
182 * @param string $field Field name
183 * @param string $value Value; ignored if null or empty array;
184 */
185 protected function addWhereFld( $field, $value ) {
186 // Use count() to its full documented capabilities to simultaneously
187 // test for null, empty array or empty countable object
188 if ( count( $value ) ) {
189 $this->where[$field] = $value;
190 }
191 }
192
193 /**
194 * Add a WHERE clause corresponding to a range, and an ORDER BY
195 * clause to sort in the right direction
196 * @param string $field Field name
197 * @param string $dir If 'newer', sort in ascending order, otherwise
198 * sort in descending order
199 * @param string $start Value to start the list at. If $dir == 'newer'
200 * this is the lower boundary, otherwise it's the upper boundary
201 * @param string $end Value to end the list at. If $dir == 'newer' this
202 * is the upper boundary, otherwise it's the lower boundary
203 * @param bool $sort If false, don't add an ORDER BY clause
204 */
205 protected function addWhereRange( $field, $dir, $start, $end, $sort = true ) {
206 $isDirNewer = ( $dir === 'newer' );
207 $after = ( $isDirNewer ? '>=' : '<=' );
208 $before = ( $isDirNewer ? '<=' : '>=' );
209 $db = $this->getDB();
210
211 if ( !is_null( $start ) ) {
212 $this->addWhere( $field . $after . $db->addQuotes( $start ) );
213 }
214
215 if ( !is_null( $end ) ) {
216 $this->addWhere( $field . $before . $db->addQuotes( $end ) );
217 }
218
219 if ( $sort ) {
220 $order = $field . ( $isDirNewer ? '' : ' DESC' );
221 // Append ORDER BY
222 $optionOrderBy = isset( $this->options['ORDER BY'] )
223 ? (array)$this->options['ORDER BY']
224 : array();
225 $optionOrderBy[] = $order;
226 $this->addOption( 'ORDER BY', $optionOrderBy );
227 }
228 }
229
230 /**
231 * Add a WHERE clause corresponding to a range, similar to addWhereRange,
232 * but converts $start and $end to database timestamps.
233 * @see addWhereRange
234 * @param string $field
235 * @param string $dir
236 * @param string $start
237 * @param string $end
238 * @param bool $sort
239 */
240 protected function addTimestampWhereRange( $field, $dir, $start, $end, $sort = true ) {
241 $db = $this->getDb();
242 $this->addWhereRange( $field, $dir,
243 $db->timestampOrNull( $start ), $db->timestampOrNull( $end ), $sort );
244 }
245
246 /**
247 * Add an option such as LIMIT or USE INDEX. If an option was set
248 * before, the old value will be overwritten
249 * @param string $name Option name
250 * @param string $value Option value
251 */
252 protected function addOption( $name, $value = null ) {
253 if ( is_null( $value ) ) {
254 $this->options[] = $name;
255 } else {
256 $this->options[$name] = $value;
257 }
258 }
259
260 /**
261 * Execute a SELECT query based on the values in the internal arrays
262 * @param string $method Function the query should be attributed to.
263 * You should usually use __METHOD__ here
264 * @param array $extraQuery Query data to add but not store in the object
265 * Format is array(
266 * 'tables' => ...,
267 * 'fields' => ...,
268 * 'where' => ...,
269 * 'options' => ...,
270 * 'join_conds' => ...
271 * )
272 * @return ResultWrapper
273 */
274 protected function select( $method, $extraQuery = array() ) {
275
276 $tables = array_merge(
277 $this->tables,
278 isset( $extraQuery['tables'] ) ? (array)$extraQuery['tables'] : array()
279 );
280 $fields = array_merge(
281 $this->fields,
282 isset( $extraQuery['fields'] ) ? (array)$extraQuery['fields'] : array()
283 );
284 $where = array_merge(
285 $this->where,
286 isset( $extraQuery['where'] ) ? (array)$extraQuery['where'] : array()
287 );
288 $options = array_merge(
289 $this->options,
290 isset( $extraQuery['options'] ) ? (array)$extraQuery['options'] : array()
291 );
292 $join_conds = array_merge(
293 $this->join_conds,
294 isset( $extraQuery['join_conds'] ) ? (array)$extraQuery['join_conds'] : array()
295 );
296
297 // getDB has its own profileDBIn/Out calls
298 $db = $this->getDB();
299
300 $this->profileDBIn();
301 $res = $db->select( $tables, $fields, $where, $method, $options, $join_conds );
302 $this->profileDBOut();
303
304 return $res;
305 }
306
307 /**
308 * Estimate the row count for the SELECT query that would be run if we
309 * called select() right now, and check if it's acceptable.
310 * @return bool True if acceptable, false otherwise
311 */
312 protected function checkRowCount() {
313 $db = $this->getDB();
314 $this->profileDBIn();
315 $rowcount = $db->estimateRowCount(
316 $this->tables,
317 $this->fields,
318 $this->where,
319 __METHOD__,
320 $this->options
321 );
322 $this->profileDBOut();
323
324 if ( $rowcount > $this->getConfig()->get( 'APIMaxDBRows' ) ) {
325 return false;
326 }
327
328 return true;
329 }
330
331 /**
332 * Add information (title and namespace) about a Title object to a
333 * result array
334 * @param array $arr Result array à la ApiResult
335 * @param Title $title
336 * @param string $prefix Module prefix
337 */
338 public static function addTitleInfo( &$arr, $title, $prefix = '' ) {
339 $arr[$prefix . 'ns'] = intval( $title->getNamespace() );
340 $arr[$prefix . 'title'] = $title->getPrefixedText();
341 }
342
343 /**
344 * Override this method to request extra fields from the pageSet
345 * using $pageSet->requestField('fieldName')
346 * @param ApiPageSet $pageSet
347 */
348 public function requestExtraData( $pageSet ) {
349 }
350
351 /**
352 * Get the main Query module
353 * @return ApiQuery
354 */
355 public function getQuery() {
356 return $this->mQueryModule;
357 }
358
359 /**
360 * Add a sub-element under the page element with the given page ID
361 * @param int $pageId Page ID
362 * @param array $data Data array à la ApiResult
363 * @return bool Whether the element fit in the result
364 */
365 protected function addPageSubItems( $pageId, $data ) {
366 $result = $this->getResult();
367 $result->setIndexedTagName( $data, $this->getModulePrefix() );
368
369 return $result->addValue( array( 'query', 'pages', intval( $pageId ) ),
370 $this->getModuleName(),
371 $data );
372 }
373
374 /**
375 * Same as addPageSubItems(), but one element of $data at a time
376 * @param int $pageId Page ID
377 * @param array $item Data array à la ApiResult
378 * @param string $elemname XML element name. If null, getModuleName()
379 * is used
380 * @return bool Whether the element fit in the result
381 */
382 protected function addPageSubItem( $pageId, $item, $elemname = null ) {
383 if ( is_null( $elemname ) ) {
384 $elemname = $this->getModulePrefix();
385 }
386 $result = $this->getResult();
387 $fit = $result->addValue( array( 'query', 'pages', $pageId,
388 $this->getModuleName() ), null, $item );
389 if ( !$fit ) {
390 return false;
391 }
392 $result->setIndexedTagName_internal( array( 'query', 'pages', $pageId,
393 $this->getModuleName() ), $elemname );
394
395 return true;
396 }
397
398 /**
399 * Set a query-continue value
400 * @param string $paramName Parameter name
401 * @param string|array $paramValue Parameter value
402 */
403 protected function setContinueEnumParameter( $paramName, $paramValue ) {
404 $this->getResult()->setContinueParam( $this, $paramName, $paramValue );
405 }
406
407 /**
408 * Get the Query database connection (read-only)
409 * @return DatabaseBase
410 */
411 protected function getDB() {
412 if ( is_null( $this->mDb ) ) {
413 $this->mDb = $this->getQuery()->getDB();
414 }
415
416 return $this->mDb;
417 }
418
419 /**
420 * Selects the query database connection with the given name.
421 * See ApiQuery::getNamedDB() for more information
422 * @param string $name Name to assign to the database connection
423 * @param int $db One of the DB_* constants
424 * @param array $groups Query groups
425 * @return DatabaseBase
426 */
427 public function selectNamedDB( $name, $db, $groups ) {
428 $this->mDb = $this->getQuery()->getNamedDB( $name, $db, $groups );
429 }
430
431 /**
432 * Get the PageSet object to work on
433 * @return ApiPageSet
434 */
435 protected function getPageSet() {
436 return $this->getQuery()->getPageSet();
437 }
438
439 /**
440 * Convert a title to a DB key
441 * @param string $title Page title with spaces
442 * @return string Page title with underscores
443 */
444 public function titleToKey( $title ) {
445 // Don't throw an error if we got an empty string
446 if ( trim( $title ) == '' ) {
447 return '';
448 }
449 $t = Title::newFromText( $title );
450 if ( !$t ) {
451 $this->dieUsageMsg( array( 'invalidtitle', $title ) );
452 }
453
454 return $t->getPrefixedDBkey();
455 }
456
457 /**
458 * The inverse of titleToKey()
459 * @param string $key Page title with underscores
460 * @return string Page title with spaces
461 */
462 public function keyToTitle( $key ) {
463 // Don't throw an error if we got an empty string
464 if ( trim( $key ) == '' ) {
465 return '';
466 }
467 $t = Title::newFromDBkey( $key );
468 // This really shouldn't happen but we gotta check anyway
469 if ( !$t ) {
470 $this->dieUsageMsg( array( 'invalidtitle', $key ) );
471 }
472
473 return $t->getPrefixedText();
474 }
475
476 /**
477 * An alternative to titleToKey() that doesn't trim trailing spaces, and
478 * does not mangle the input if starts with something that looks like a
479 * namespace. It is advisable to pass the namespace parameter in order to
480 * handle per-namespace capitalization settings.
481 * @param string $titlePart Title part with spaces
482 * @param int $defaultNamespace Namespace to assume
483 * @return string Title part with underscores
484 */
485 public function titlePartToKey( $titlePart, $defaultNamespace = NS_MAIN ) {
486 $t = Title::makeTitleSafe( $defaultNamespace, $titlePart . 'x' );
487 if ( !$t ) {
488 $this->dieUsageMsg( array( 'invalidtitle', $titlePart ) );
489 }
490 if ( $defaultNamespace != $t->getNamespace() || $t->isExternal() ) {
491 // This can happen in two cases. First, if you call titlePartToKey with a title part
492 // that looks like a namespace, but with $defaultNamespace = NS_MAIN. It would be very
493 // difficult to handle such a case. Such cases cannot exist and are therefore treated
494 // as invalid user input. The second case is when somebody specifies a title interwiki
495 // prefix.
496 $this->dieUsageMsg( array( 'invalidtitle', $titlePart ) );
497 }
498
499 return substr( $t->getDbKey(), 0, -1 );
500 }
501
502 /**
503 * An alternative to keyToTitle() that doesn't trim trailing spaces
504 * @param string $keyPart Key part with spaces
505 * @return string Key part with underscores
506 */
507 public function keyPartToTitle( $keyPart ) {
508 return substr( $this->keyToTitle( $keyPart . 'x' ), 0, -1 );
509 }
510
511 /**
512 * Gets the personalised direction parameter description
513 *
514 * @param string $p ModulePrefix
515 * @param string $extraDirText Any extra text to be appended on the description
516 * @return array
517 */
518 public function getDirectionDescription( $p = '', $extraDirText = '' ) {
519 return array(
520 "In which direction to enumerate{$extraDirText}",
521 " newer - List oldest first. Note: {$p}start has to be before {$p}end.",
522 " older - List newest first (default). Note: {$p}start has to be later than {$p}end.",
523 );
524 }
525
526 /**
527 * @param string $query
528 * @param string $protocol
529 * @return null|string
530 */
531 public function prepareUrlQuerySearchString( $query = null, $protocol = null ) {
532 $db = $this->getDb();
533 if ( !is_null( $query ) || $query != '' ) {
534 if ( is_null( $protocol ) ) {
535 $protocol = 'http://';
536 }
537
538 $likeQuery = LinkFilter::makeLikeArray( $query, $protocol );
539 if ( !$likeQuery ) {
540 $this->dieUsage( 'Invalid query', 'bad_query' );
541 }
542
543 $likeQuery = LinkFilter::keepOneWildcard( $likeQuery );
544
545 return 'el_index ' . $db->buildLike( $likeQuery );
546 } elseif ( !is_null( $protocol ) ) {
547 return 'el_index ' . $db->buildLike( "$protocol", $db->anyString() );
548 }
549
550 return null;
551 }
552
553 /**
554 * Filters hidden users (where the user doesn't have the right to view them)
555 * Also adds relevant block information
556 *
557 * @param bool $showBlockInfo
558 * @return void
559 */
560 public function showHiddenUsersAddBlockInfo( $showBlockInfo ) {
561 $this->addTables( 'ipblocks' );
562 $this->addJoinConds( array(
563 'ipblocks' => array( 'LEFT JOIN', 'ipb_user=user_id' ),
564 ) );
565
566 $this->addFields( 'ipb_deleted' );
567
568 if ( $showBlockInfo ) {
569 $this->addFields( array( 'ipb_id', 'ipb_by', 'ipb_by_text', 'ipb_reason', 'ipb_expiry' ) );
570 }
571
572 // Don't show hidden names
573 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
574 $this->addWhere( 'ipb_deleted = 0 OR ipb_deleted IS NULL' );
575 }
576 }
577
578 /**
579 * @param string $hash
580 * @return bool
581 */
582 public function validateSha1Hash( $hash ) {
583 return preg_match( '/^[a-f0-9]{40}$/', $hash );
584 }
585
586 /**
587 * @param string $hash
588 * @return bool
589 */
590 public function validateSha1Base36Hash( $hash ) {
591 return preg_match( '/^[a-z0-9]{31}$/', $hash );
592 }
593
594 /**
595 * @return array
596 */
597 public function getPossibleErrors() {
598 $errors = parent::getPossibleErrors();
599 $errors = array_merge( $errors, array(
600 array( 'invalidtitle', 'title' ),
601 array( 'invalidtitle', 'key' ),
602 ) );
603
604 return $errors;
605 }
606
607 /**
608 * Check whether the current user has permission to view revision-deleted
609 * fields.
610 * @return bool
611 */
612 public function userCanSeeRevDel() {
613 return $this->getUser()->isAllowedAny(
614 'deletedhistory',
615 'deletedtext',
616 'suppressrevision',
617 'viewsuppressed'
618 );
619 }
620 }
621
622 /**
623 * @ingroup API
624 */
625 abstract class ApiQueryGeneratorBase extends ApiQueryBase {
626
627 private $mGeneratorPageSet = null;
628
629 /**
630 * Switch this module to generator mode. By default, generator mode is
631 * switched off and the module acts like a normal query module.
632 * @since 1.21 requires pageset parameter
633 * @param ApiPageSet $generatorPageSet ApiPageSet object that the module will get
634 * by calling getPageSet() when in generator mode.
635 */
636 public function setGeneratorMode( ApiPageSet $generatorPageSet ) {
637 if ( $generatorPageSet === null ) {
638 ApiBase::dieDebug( __METHOD__, 'Required parameter missing - $generatorPageSet' );
639 }
640 $this->mGeneratorPageSet = $generatorPageSet;
641 }
642
643 /**
644 * Get the PageSet object to work on.
645 * If this module is generator, the pageSet object is different from other module's
646 * @return ApiPageSet
647 */
648 protected function getPageSet() {
649 if ( $this->mGeneratorPageSet !== null ) {
650 return $this->mGeneratorPageSet;
651 }
652
653 return parent::getPageSet();
654 }
655
656 /**
657 * Overrides base class to prepend 'g' to every generator parameter
658 * @param string $paramName Parameter name
659 * @return string Prefixed parameter name
660 */
661 public function encodeParamName( $paramName ) {
662 if ( $this->mGeneratorPageSet !== null ) {
663 return 'g' . parent::encodeParamName( $paramName );
664 } else {
665 return parent::encodeParamName( $paramName );
666 }
667 }
668
669 /**
670 * Overridden to set the generator param if in generator mode
671 * @param string $paramName Parameter name
672 * @param string|array $paramValue Parameter value
673 */
674 protected function setContinueEnumParameter( $paramName, $paramValue ) {
675 if ( $this->mGeneratorPageSet !== null ) {
676 $this->getResult()->setGeneratorContinueParam( $this, $paramName, $paramValue );
677 } else {
678 parent::setContinueEnumParameter( $paramName, $paramValue );
679 }
680 }
681
682 /**
683 * Execute this module as a generator
684 * @param ApiPageSet $resultPageSet All output should be appended to this object
685 */
686 abstract public function executeGenerator( $resultPageSet );
687 }