~ubuntu-branches/ubuntu/maverick/mediawiki/maverick

« back to all changes in this revision

Viewing changes to includes/api/ApiQueryBase.php

  • Committer: Bazaar Package Importer
  • Author(s): Romain Beauxis
  • Date: 2009-06-19 01:38:50 UTC
  • mfrom: (16.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20090619013850-dsn4lrxvs90ab4rx
Tags: 1:1.15.0-1
* New upstream release. 
* Upstream added support for OASIS documents.
Closes: #530328
* Refreshed quilt patches
* Bumped standards versions to 3.8.2
* Bumped compat to 7
* Pointed to GPL-2 in debian/copyright
* Added php5-sqlite to possible DB backend dependencies.
Closes: #501569
* Proofread README.Debian, upgrade is documented there.
Closes: #520121

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
 
31
31
/**
32
32
 * This is a base class for all Query modules.
33
 
 * It provides some common functionality such as constructing various SQL queries.
 
33
 * It provides some common functionality such as constructing various SQL
 
34
 * queries.
34
35
 *
35
36
 * @ingroup API
36
37
 */
58
59
 
59
60
        /**
60
61
         * Add a set of tables to the internal array
61
 
         * @param mixed $tables Table name or array of table names
62
 
         * @param mixed $alias Table alias, or null for no alias. Cannot be used with multiple tables
 
62
         * @param $tables mixed Table name or array of table names
 
63
         * @param $alias mixed Table alias, or null for no alias. Cannot be
 
64
         *  used with multiple tables
63
65
         */
64
66
        protected function addTables($tables, $alias = null) {
65
67
                if (is_array($tables)) {
75
77
        
76
78
        /**
77
79
         * Get the SQL for a table name with alias
78
 
         * @param string $table Table name
79
 
         * @param string $alias Alias
 
80
         * @param $table string Table name
 
81
         * @param $alias string Alias
80
82
         * @return string SQL
81
83
         */
82
84
        protected function getAliasedName($table, $alias) {
86
88
        /**
87
89
         * Add a set of JOIN conditions to the internal array
88
90
         *
89
 
         * JOIN conditions are formatted as array( tablename => array(jointype, conditions)
90
 
         * e.g. array('page' => array('LEFT JOIN', 'page_id=rev_page'))
91
 
         * @param array $join_conds JOIN conditions
 
91
         * JOIN conditions are formatted as array( tablename => array(jointype,
 
92
         * conditions) e.g. array('page' => array('LEFT JOIN',
 
93
         * 'page_id=rev_page')) . conditions may be a string or an
 
94
         * addWhere()-style array
 
95
         * @param $join_conds array JOIN conditions
92
96
         */
93
97
        protected function addJoinConds($join_conds) {
94
98
                if(!is_array($join_conds))
98
102
 
99
103
        /**
100
104
         * Add a set of fields to select to the internal array
101
 
         * @param mixed $value Field name or array of field names
 
105
         * @param $value mixed Field name or array of field names
102
106
         */
103
107
        protected function addFields($value) {
104
108
                if (is_array($value))
109
113
 
110
114
        /**
111
115
         * Same as addFields(), but add the fields only if a condition is met
112
 
         * @param mixed $value See addFields()
113
 
         * @param bool $condition If false, do nothing
 
116
         * @param $value mixed See addFields()
 
117
         * @param $condition bool If false, do nothing
114
118
         * @return bool $condition
115
119
         */
116
120
        protected function addFieldsIf($value, $condition) {
130
134
         *
131
135
         * For example, array('foo=bar', 'baz' => 3, 'bla' => 'foo') translates
132
136
         * to "foo=bar AND baz='3' AND bla='foo'"
133
 
         * @param mixed $value String or array
 
137
         * @param $value mixed String or array
134
138
         */
135
139
        protected function addWhere($value) {
136
140
                if (is_array($value)) {
145
149
 
146
150
        /**
147
151
         * Same as addWhere(), but add the WHERE clauses only if a condition is met
148
 
         * @param mixed $value See addWhere()
149
 
         * @param bool $condition If false, do nothing
 
152
         * @param $value mixed See addWhere()
 
153
         * @param $condition boolIf false, do nothing
150
154
         * @return bool $condition
151
155
         */
152
156
        protected function addWhereIf($value, $condition) {
159
163
 
160
164
        /**
161
165
         * Equivalent to addWhere(array($field => $value))
162
 
         * @param string $field Field name
163
 
         * @param string $value Value; ignored if null or empty array;
 
166
         * @param $field string Field name
 
167
         * @param $value string Value; ignored if null or empty array;
164
168
         */
165
169
        protected function addWhereFld($field, $value) {
166
170
                // Use count() to its full documented capabilities to simultaneously 
172
176
        /**
173
177
         * Add a WHERE clause corresponding to a range, and an ORDER BY
174
178
         * clause to sort in the right direction
175
 
         * @param string $field Field name
176
 
         * @param string $dir If 'newer', sort in ascending order, otherwise sort in descending order
177
 
         * @param string $start Value to start the list at. If $dir == 'newer' this is the lower boundary, otherwise it's the upper boundary
178
 
         * @param string $end Value to end the list at. If $dir == 'newer' this is the upper boundary, otherwise it's the lower boundary
 
179
         * @param $field string Field name
 
180
         * @param $dir string If 'newer', sort in ascending order, otherwise
 
181
         *  sort in descending order
 
182
         * @param $start string Value to start the list at. If $dir == 'newer'
 
183
         *  this is the lower boundary, otherwise it's the upper boundary
 
184
         * @param $end string Value to end the list at. If $dir == 'newer' this
 
185
         *  is the upper boundary, otherwise it's the lower boundary
 
186
         * @param $sort bool If false, don't add an ORDER BY clause
179
187
         */
180
 
        protected function addWhereRange($field, $dir, $start, $end) {
 
188
        protected function addWhereRange($field, $dir, $start, $end, $sort = true) {
181
189
                $isDirNewer = ($dir === 'newer');
182
190
                $after = ($isDirNewer ? '>=' : '<=');
183
191
                $before = ($isDirNewer ? '<=' : '>=');
189
197
                if (!is_null($end))
190
198
                        $this->addWhere($field . $before . $db->addQuotes($end));
191
199
 
192
 
                $order = $field . ($isDirNewer ? '' : ' DESC');
193
 
                if (!isset($this->options['ORDER BY']))
194
 
                        $this->addOption('ORDER BY', $order);
195
 
                else
196
 
                        $this->addOption('ORDER BY', $this->options['ORDER BY'] . ', ' . $order);
 
200
                if ($sort) {
 
201
                        $order = $field . ($isDirNewer ? '' : ' DESC');
 
202
                        if (!isset($this->options['ORDER BY']))
 
203
                                $this->addOption('ORDER BY', $order);
 
204
                        else
 
205
                                $this->addOption('ORDER BY', $this->options['ORDER BY'] . ', ' . $order);
 
206
                }
197
207
        }
198
208
 
199
209
        /**
200
 
         * Add an option such as LIMIT or USE INDEX
201
 
         * @param string $name Option name
202
 
         * @param string $value Option value
 
210
         * Add an option such as LIMIT or USE INDEX. If an option was set
 
211
         * before, the old value will be overwritten
 
212
         * @param $name string Option name
 
213
         * @param $value string Option value
203
214
         */
204
215
        protected function addOption($name, $value = null) {
205
216
                if (is_null($value))
210
221
 
211
222
        /**
212
223
         * Execute a SELECT query based on the values in the internal arrays
213
 
         * @param string $method Function the query should be attributed to. You should usually use __METHOD__ here
 
224
         * @param $method string Function the query should be attributed to.
 
225
         *  You should usually use __METHOD__ here
214
226
         * @return ResultWrapper
215
227
         */
216
228
        protected function select($method) {
243
255
        }
244
256
 
245
257
        /**
246
 
         * Add information (title and namespace) about a Title object to a result array
247
 
         * @param array $arr Result array à la ApiResult
248
 
         * @param Title $title Title object
249
 
         * @param string $prefix Module prefix
 
258
         * Add information (title and namespace) about a Title object to a
 
259
         * result array
 
260
         * @param $arr array Result array à la ApiResult
 
261
         * @param $title Title
 
262
         * @param $prefix string Module prefix
250
263
         */
251
264
        public static function addTitleInfo(&$arr, $title, $prefix='') {
252
265
                $arr[$prefix . 'ns'] = intval($title->getNamespace());
256
269
        /**
257
270
         * Override this method to request extra fields from the pageSet
258
271
         * using $pageSet->requestField('fieldName')
259
 
         * @param ApiPageSet $pageSet
 
272
         * @param $pageSet ApiPageSet
260
273
         */
261
274
        public function requestExtraData($pageSet) {
262
275
        }
271
284
 
272
285
        /**
273
286
         * Add a sub-element under the page element with the given page ID
274
 
         * @param int $pageId Page ID
275
 
         * @param array $data Data array à la ApiResult 
 
287
         * @param $pageId int Page ID
 
288
         * @param $data array Data array à la ApiResult 
 
289
         * @return bool Whether the element fit in the result
276
290
         */
277
291
        protected function addPageSubItems($pageId, $data) {
278
292
                $result = $this->getResult();
279
293
                $result->setIndexedTagName($data, $this->getModulePrefix());
280
 
                $result->addValue(array ('query', 'pages', intval($pageId)),
 
294
                return $result->addValue(array('query', 'pages', intval($pageId)),
281
295
                        $this->getModuleName(),
282
296
                        $data);
283
297
        }
 
298
        
 
299
        /**
 
300
         * Same as addPageSubItems(), but one element of $data at a time
 
301
         * @param $pageId int Page ID
 
302
         * @param $data array Data array à la ApiResult
 
303
         * @param $elemname string XML element name. If null, getModuleName()
 
304
         *  is used
 
305
         * @return bool Whether the element fit in the result
 
306
         */
 
307
        protected function addPageSubItem($pageId, $item, $elemname = null) {
 
308
                if(is_null($elemname))
 
309
                        $elemname = $this->getModulePrefix();
 
310
                $result = $this->getResult();
 
311
                $fit = $result->addValue(array('query', 'pages', $pageId,
 
312
                                         $this->getModuleName()), null, $item);
 
313
                if(!$fit)
 
314
                        return false;
 
315
                $result->setIndexedTagName_internal(array('query', 'pages', $pageId,
 
316
                                $this->getModuleName()), $elemname);
 
317
                return true;
 
318
        }
284
319
 
285
320
        /**
286
321
         * Set a query-continue value
287
 
         * @param $paramName Parameter name
288
 
         * @param $paramValue Parameter value
 
322
         * @param $paramName string Parameter name
 
323
         * @param $paramValue string Parameter value
289
324
         */
290
325
        protected function setContinueEnumParameter($paramName, $paramValue) {
291
 
 
292
326
                $paramName = $this->encodeParamName($paramName);
293
327
                $msg = array( $paramName => $paramValue );
 
328
                $this->getResult()->disableSizeCheck();
294
329
                $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
 
330
                $this->getResult()->enableSizeCheck();
295
331
        }
296
332
 
297
333
        /**
298
 
         * Get the Query database connection (readonly)
 
334
         * Get the Query database connection (read-only)
299
335
         * @return Database
300
336
         */
301
337
        protected function getDB() {
306
342
 
307
343
        /**
308
344
         * Selects the query database connection with the given name.
309
 
         * If no such connection has been requested before, it will be created.
310
 
         * Subsequent calls with the same $name will return the same connection
311
 
         * as the first, regardless of $db or $groups new values.
312
 
         * @param string $name Name to assign to the database connection
313
 
         * @param int $db One of the DB_* constants
314
 
         * @param array $groups Query groups
 
345
         * See ApiQuery::getNamedDB() for more information
 
346
         * @param $name string Name to assign to the database connection
 
347
         * @param $db int One of the DB_* constants
 
348
         * @param $groups array Query groups
315
349
         * @return Database 
316
350
         */
317
351
        public function selectNamedDB($name, $db, $groups) {
328
362
 
329
363
        /**
330
364
         * Convert a title to a DB key
331
 
         * @param string $title Page title with spaces
 
365
         * @param $title string Page title with spaces
332
366
         * @return string Page title with underscores
333
367
         */
334
368
        public function titleToKey($title) {
343
377
 
344
378
        /**
345
379
         * The inverse of titleToKey()
346
 
         * @param string $key Page title with underscores
 
380
         * @param $key string Page title with underscores
347
381
         * @return string Page title with spaces
348
382
         */
349
383
        public function keyToTitle($key) {
359
393
        
360
394
        /**
361
395
         * An alternative to titleToKey() that doesn't trim trailing spaces
362
 
         * @param string $titlePart Title part with spaces
 
396
         * @param $titlePart string Title part with spaces
363
397
         * @return string Title part with underscores
364
398
         */
365
399
        public function titlePartToKey($titlePart) {
368
402
        
369
403
        /**
370
404
         * An alternative to keyToTitle() that doesn't trim trailing spaces
371
 
         * @param string $keyPart Key part with spaces
 
405
         * @param $keyPart string Key part with spaces
372
406
         * @return string Key part with underscores
373
407
         */
374
408
        public function keyPartToTitle($keyPart) {
380
414
         * @return string
381
415
         */
382
416
        public static function getBaseVersion() {
383
 
                return __CLASS__ . ': $Id: ApiQueryBase.php 44461 2008-12-11 19:11:11Z ialex $';
 
417
                return __CLASS__ . ': $Id: ApiQueryBase.php 47450 2009-02-18 15:26:09Z catrope $';
384
418
        }
385
419
}
386
420
 
406
440
 
407
441
        /**
408
442
         * Overrides base class to prepend 'g' to every generator parameter
 
443
         * @param $paramNames string Parameter name
 
444
         * @return string Prefixed parameter name
409
445
         */
410
446
        public function encodeParamName($paramName) {
411
447
                if ($this->mIsGenerator)
416
452
 
417
453
        /**
418
454
         * Execute this module as a generator
419
 
         * @param $resultPageSet PageSet: All output should be appended to this object
 
455
         * @param $resultPageSet ApiPageSet: All output should be appended to
 
456
         *  this object
420
457
         */
421
458
        public abstract function executeGenerator($resultPageSet);
422
459
}