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