00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if (!defined('MEDIAWIKI')) {
00027
00028 require_once ('ApiBase.php');
00029 }
00030
00038 abstract class ApiQueryBase extends ApiBase {
00039
00040 private $mQueryModule, $mDb, $tables, $where, $fields, $options, $join_conds;
00041
00042 public function __construct($query, $moduleName, $paramPrefix = '') {
00043 parent :: __construct($query->getMain(), $moduleName, $paramPrefix);
00044 $this->mQueryModule = $query;
00045 $this->mDb = null;
00046 $this->resetQueryParams();
00047 }
00048
00052 protected function resetQueryParams() {
00053 $this->tables = array ();
00054 $this->where = array ();
00055 $this->fields = array ();
00056 $this->options = array ();
00057 $this->join_conds = array ();
00058 }
00059
00066 protected function addTables($tables, $alias = null) {
00067 if (is_array($tables)) {
00068 if (!is_null($alias))
00069 ApiBase :: dieDebug(__METHOD__, 'Multiple table aliases not supported');
00070 $this->tables = array_merge($this->tables, $tables);
00071 } else {
00072 if (!is_null($alias))
00073 $tables = $this->getAliasedName($tables, $alias);
00074 $this->tables[] = $tables;
00075 }
00076 }
00077
00084 protected function getAliasedName($table, $alias) {
00085 return $this->getDB()->tableName($table) . ' ' . $alias;
00086 }
00087
00097 protected function addJoinConds($join_conds) {
00098 if(!is_array($join_conds))
00099 ApiBase::dieDebug(__METHOD__, 'Join conditions have to be arrays');
00100 $this->join_conds = array_merge($this->join_conds, $join_conds);
00101 }
00102
00107 protected function addFields($value) {
00108 if (is_array($value))
00109 $this->fields = array_merge($this->fields, $value);
00110 else
00111 $this->fields[] = $value;
00112 }
00113
00120 protected function addFieldsIf($value, $condition) {
00121 if ($condition) {
00122 $this->addFields($value);
00123 return true;
00124 }
00125 return false;
00126 }
00127
00139 protected function addWhere($value) {
00140 if (is_array($value)) {
00141
00142
00143 if ( count( $value ) )
00144 $this->where = array_merge($this->where, $value);
00145 }
00146 else
00147 $this->where[] = $value;
00148 }
00149
00156 protected function addWhereIf($value, $condition) {
00157 if ($condition) {
00158 $this->addWhere($value);
00159 return true;
00160 }
00161 return false;
00162 }
00163
00169 protected function addWhereFld($field, $value) {
00170
00171
00172 if ( count( $value ) )
00173 $this->where[$field] = $value;
00174 }
00175
00188 protected function addWhereRange($field, $dir, $start, $end, $sort = true) {
00189 $isDirNewer = ($dir === 'newer');
00190 $after = ($isDirNewer ? '>=' : '<=');
00191 $before = ($isDirNewer ? '<=' : '>=');
00192 $db = $this->getDB();
00193
00194 if (!is_null($start))
00195 $this->addWhere($field . $after . $db->addQuotes($start));
00196
00197 if (!is_null($end))
00198 $this->addWhere($field . $before . $db->addQuotes($end));
00199
00200 if ($sort) {
00201 $order = $field . ($isDirNewer ? '' : ' DESC');
00202 if (!isset($this->options['ORDER BY']))
00203 $this->addOption('ORDER BY', $order);
00204 else
00205 $this->addOption('ORDER BY', $this->options['ORDER BY'] . ', ' . $order);
00206 }
00207 }
00208
00215 protected function addOption($name, $value = null) {
00216 if (is_null($value))
00217 $this->options[] = $name;
00218 else
00219 $this->options[$name] = $value;
00220 }
00221
00228 protected function select($method) {
00229
00230
00231 $db = $this->getDB();
00232
00233 $this->profileDBIn();
00234 $res = $db->select($this->tables, $this->fields, $this->where, $method, $this->options, $this->join_conds);
00235 $this->profileDBOut();
00236
00237 return $res;
00238 }
00239
00245 protected function checkRowCount() {
00246 $db = $this->getDB();
00247 $this->profileDBIn();
00248 $rowcount = $db->estimateRowCount($this->tables, $this->fields, $this->where, __METHOD__, $this->options);
00249 $this->profileDBOut();
00250
00251 global $wgAPIMaxDBRows;
00252 if($rowcount > $wgAPIMaxDBRows)
00253 return false;
00254 return true;
00255 }
00256
00264 public static function addTitleInfo(&$arr, $title, $prefix='') {
00265 $arr[$prefix . 'ns'] = intval($title->getNamespace());
00266 $arr[$prefix . 'title'] = $title->getPrefixedText();
00267 }
00268
00274 public function requestExtraData($pageSet) {
00275 }
00276
00281 public function getQuery() {
00282 return $this->mQueryModule;
00283 }
00284
00291 protected function addPageSubItems($pageId, $data) {
00292 $result = $this->getResult();
00293 $result->setIndexedTagName($data, $this->getModulePrefix());
00294 return $result->addValue(array('query', 'pages', intval($pageId)),
00295 $this->getModuleName(),
00296 $data);
00297 }
00298
00307 protected function addPageSubItem($pageId, $item, $elemname = null) {
00308 if(is_null($elemname))
00309 $elemname = $this->getModulePrefix();
00310 $result = $this->getResult();
00311 $fit = $result->addValue(array('query', 'pages', $pageId,
00312 $this->getModuleName()), null, $item);
00313 if(!$fit)
00314 return false;
00315 $result->setIndexedTagName_internal(array('query', 'pages', $pageId,
00316 $this->getModuleName()), $elemname);
00317 return true;
00318 }
00319
00325 protected function setContinueEnumParameter($paramName, $paramValue) {
00326 $paramName = $this->encodeParamName($paramName);
00327 $msg = array( $paramName => $paramValue );
00328 $this->getResult()->disableSizeCheck();
00329 $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
00330 $this->getResult()->enableSizeCheck();
00331 }
00332
00337 protected function getDB() {
00338 if (is_null($this->mDb))
00339 $this->mDb = $this->getQuery()->getDB();
00340 return $this->mDb;
00341 }
00342
00351 public function selectNamedDB($name, $db, $groups) {
00352 $this->mDb = $this->getQuery()->getNamedDB($name, $db, $groups);
00353 }
00354
00359 protected function getPageSet() {
00360 return $this->getQuery()->getPageSet();
00361 }
00362
00368 public function titleToKey($title) {
00369 # Don't throw an error if we got an empty string
00370 if(trim($title) == '')
00371 return '';
00372 $t = Title::newFromText($title);
00373 if(!$t)
00374 $this->dieUsageMsg(array('invalidtitle', $title));
00375 return $t->getPrefixedDbKey();
00376 }
00377
00383 public function keyToTitle($key) {
00384 # Don't throw an error if we got an empty string
00385 if(trim($key) == '')
00386 return '';
00387 $t = Title::newFromDbKey($key);
00388 # This really shouldn't happen but we gotta check anyway
00389 if(!$t)
00390 $this->dieUsageMsg(array('invalidtitle', $key));
00391 return $t->getPrefixedText();
00392 }
00393
00399 public function titlePartToKey($titlePart) {
00400 return substr($this->titleToKey($titlePart . 'x'), 0, -1);
00401 }
00402
00408 public function keyPartToTitle($keyPart) {
00409 return substr($this->keyToTitle($keyPart . 'x'), 0, -1);
00410 }
00411
00416 public static function getBaseVersion() {
00417 return __CLASS__ . ': $Id: ApiQueryBase.php 47450 2009-02-18 15:26:09Z catrope $';
00418 }
00419 }
00420
00424 abstract class ApiQueryGeneratorBase extends ApiQueryBase {
00425
00426 private $mIsGenerator;
00427
00428 public function __construct($query, $moduleName, $paramPrefix = '') {
00429 parent :: __construct($query, $moduleName, $paramPrefix);
00430 $this->mIsGenerator = false;
00431 }
00432
00437 public function setGeneratorMode() {
00438 $this->mIsGenerator = true;
00439 }
00440
00446 public function encodeParamName($paramName) {
00447 if ($this->mIsGenerator)
00448 return 'g' . parent :: encodeParamName($paramName);
00449 else
00450 return parent :: encodeParamName($paramName);
00451 }
00452
00458 public abstract function executeGenerator($resultPageSet);
00459 }