5 * Created on Sep 7, 2006
7 * API for MediaWiki 1.8+
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiBase.php');
32 abstract class ApiQueryBase
extends ApiBase
{
34 private $mQueryModule, $tables, $where, $fields, $options;
36 public function __construct($query, $moduleName, $paramPrefix = '') {
37 parent
:: __construct($query->getMain(), $moduleName, $paramPrefix);
38 $this->mQueryModule
= $query;
40 $this->tables
= array ();
41 $this->where
= array ();
42 $this->fields
= array();
43 $this->options
= array ();
46 protected function addTables($value) {
47 if(!is_array($this->tables
))
48 $this->dieDebug(__METHOD__
, 'Must not call setTablesAsExpression() before this method');
50 $this->tables
= array_merge($this->tables
, $value);
52 $this->tables
[] = $value;
55 protected function setTablesAsExpression($value) {
56 if(!empty($this->tables
))
57 $this->dieDebug(__METHOD__
, 'Must not call addTables() before this method');
58 $this->tables
= $value;
61 protected function addFields($value) {
63 $this->fields
= array_merge($this->fields
, $value);
65 $this->fields
[] = $value;
68 protected function addFieldsIf($value, $condition) {
70 $this->addFields($value);
73 protected function addWhere($value) {
75 $this->where
= array_merge($this->where
, $value);
77 $this->where
[] = $value;
80 protected function addWhereIf($value, $condition) {
82 $this->addWhere($value);
85 protected function addWhereFld($field, $value) {
87 $this->where
[$field] = $value;
90 protected function addWhereRange($field, $dir, $start, $end) {
91 $isDirNewer = ($dir === 'newer');
92 $after = ($isDirNewer ?
'<=' : '>=');
93 $before = ($isDirNewer ?
'>=' : '<=');
97 $this->addWhere($field . $after . $db->addQuotes($start));
100 $this->addWhere($field . $before . $db->addQuotes($end));
102 $this->addOption('ORDER BY', $field . ($isDirNewer ?
'' : ' DESC'));
105 protected function select($method) {
106 $this->profileDBIn();
107 $res = $this->getDB()->select($this->tables
, $this->fields
, $this->where
, $method, $this->options
);
108 $this->profileDBOut();
112 protected function addOption($name, $value) {
113 $this->options
[$name] = $value;
117 * Override this method to request extra fields from the pageSet
118 * using $this->getPageSet()->requestField('fieldName')
120 public function requestExtraData() {
124 * Get the main Query module
126 public function getQuery() {
127 return $this->mQueryModule
;
130 protected function setContinueEnumParameter($paramName, $paramValue) {
132 $this->encodeParamName($paramName
134 $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
138 * Get the Query database connection (readonly)
140 protected function getDB() {
141 return $this->getQuery()->getDB();
145 * Get the PageSet object to work on
146 * @return ApiPageSet data
148 protected function getPageSet() {
149 return $this->mQueryModule
->getPageSet();
153 * This is a very simplistic utility function
154 * to convert a non-namespaced title string to a db key.
155 * It will replace all ' ' with '_'
157 public static function titleToKey($title) {
158 return str_replace(' ', '_', $title);
161 public static function keyToTitle($key) {
162 return str_replace('_', ' ', $key);
165 public static function getBaseVersion() {
166 return __CLASS__
. ': $Id$';
170 abstract class ApiQueryGeneratorBase
extends ApiQueryBase
{
172 private $mIsGenerator;
174 public function __construct($query, $moduleName, $paramPrefix = '') {
175 parent
:: __construct($query, $moduleName, $paramPrefix);
176 $this->mIsGenerator
= false;
179 public function setGeneratorMode() {
180 $this->mIsGenerator
= true;
184 * Overrides base class to prepend 'g' to every generator parameter
186 public function encodeParamName($paramName) {
187 if ($this->mIsGenerator
)
188 return 'g' . parent
:: encodeParamName($paramName);
190 return parent
:: encodeParamName($paramName);
194 * Execute this module as a generator
195 * @param $resultPageSet PageSet: All output should be appended to this object
197 public abstract function executeGenerator($resultPageSet);