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
00056 public function getCacheMode( $params ) {
00057 return 'private';
00058 }
00059
00063 protected function resetQueryParams() {
00064 $this->tables = array ();
00065 $this->where = array ();
00066 $this->fields = array ();
00067 $this->options = array ();
00068 $this->join_conds = array ();
00069 }
00070
00077 protected function addTables($tables, $alias = null) {
00078 if (is_array($tables)) {
00079 if (!is_null($alias))
00080 ApiBase :: dieDebug(__METHOD__, 'Multiple table aliases not supported');
00081 $this->tables = array_merge($this->tables, $tables);
00082 } else {
00083 if (!is_null($alias))
00084 $tables = $this->getAliasedName($tables, $alias);
00085 $this->tables[] = $tables;
00086 }
00087 }
00088
00095 protected function getAliasedName($table, $alias) {
00096 return $this->getDB()->tableName($table) . ' ' . $alias;
00097 }
00098
00108 protected function addJoinConds($join_conds) {
00109 if(!is_array($join_conds))
00110 ApiBase::dieDebug(__METHOD__, 'Join conditions have to be arrays');
00111 $this->join_conds = array_merge($this->join_conds, $join_conds);
00112 }
00113
00118 protected function addFields($value) {
00119 if (is_array($value))
00120 $this->fields = array_merge($this->fields, $value);
00121 else
00122 $this->fields[] = $value;
00123 }
00124
00131 protected function addFieldsIf($value, $condition) {
00132 if ($condition) {
00133 $this->addFields($value);
00134 return true;
00135 }
00136 return false;
00137 }
00138
00150 protected function addWhere($value) {
00151 if (is_array($value)) {
00152
00153
00154 if ( count( $value ) )
00155 $this->where = array_merge($this->where, $value);
00156 }
00157 else
00158 $this->where[] = $value;
00159 }
00160
00167 protected function addWhereIf($value, $condition) {
00168 if ($condition) {
00169 $this->addWhere($value);
00170 return true;
00171 }
00172 return false;
00173 }
00174
00180 protected function addWhereFld($field, $value) {
00181
00182
00183 if ( count( $value ) )
00184 $this->where[$field] = $value;
00185 }
00186
00199 protected function addWhereRange($field, $dir, $start, $end, $sort = true) {
00200 $isDirNewer = ($dir === 'newer');
00201 $after = ($isDirNewer ? '>=' : '<=');
00202 $before = ($isDirNewer ? '<=' : '>=');
00203 $db = $this->getDB();
00204
00205 if (!is_null($start))
00206 $this->addWhere($field . $after . $db->addQuotes($start));
00207
00208 if (!is_null($end))
00209 $this->addWhere($field . $before . $db->addQuotes($end));
00210
00211 if ($sort) {
00212 $order = $field . ($isDirNewer ? '' : ' DESC');
00213 if (!isset($this->options['ORDER BY']))
00214 $this->addOption('ORDER BY', $order);
00215 else
00216 $this->addOption('ORDER BY', $this->options['ORDER BY'] . ', ' . $order);
00217 }
00218 }
00219
00226 protected function addOption($name, $value = null) {
00227 if (is_null($value))
00228 $this->options[] = $name;
00229 else
00230 $this->options[$name] = $value;
00231 }
00232
00239 protected function select($method) {
00240
00241
00242 $db = $this->getDB();
00243
00244 $this->profileDBIn();
00245 $res = $db->select($this->tables, $this->fields, $this->where, $method, $this->options, $this->join_conds);
00246 $this->profileDBOut();
00247
00248 return $res;
00249 }
00250
00256 protected function checkRowCount() {
00257 $db = $this->getDB();
00258 $this->profileDBIn();
00259 $rowcount = $db->estimateRowCount($this->tables, $this->fields, $this->where, __METHOD__, $this->options);
00260 $this->profileDBOut();
00261
00262 global $wgAPIMaxDBRows;
00263 if($rowcount > $wgAPIMaxDBRows)
00264 return false;
00265 return true;
00266 }
00267
00275 public static function addTitleInfo(&$arr, $title, $prefix='') {
00276 $arr[$prefix . 'ns'] = intval($title->getNamespace());
00277 $arr[$prefix . 'title'] = $title->getPrefixedText();
00278 }
00279
00285 public function requestExtraData($pageSet) {
00286 }
00287
00292 public function getQuery() {
00293 return $this->mQueryModule;
00294 }
00295
00302 protected function addPageSubItems($pageId, $data) {
00303 $result = $this->getResult();
00304 $result->setIndexedTagName($data, $this->getModulePrefix());
00305 return $result->addValue(array('query', 'pages', intval($pageId)),
00306 $this->getModuleName(),
00307 $data);
00308 }
00309
00318 protected function addPageSubItem($pageId, $item, $elemname = null) {
00319 if(is_null($elemname))
00320 $elemname = $this->getModulePrefix();
00321 $result = $this->getResult();
00322 $fit = $result->addValue(array('query', 'pages', $pageId,
00323 $this->getModuleName()), null, $item);
00324 if(!$fit)
00325 return false;
00326 $result->setIndexedTagName_internal(array('query', 'pages', $pageId,
00327 $this->getModuleName()), $elemname);
00328 return true;
00329 }
00330
00336 protected function setContinueEnumParameter($paramName, $paramValue) {
00337 $paramName = $this->encodeParamName($paramName);
00338 $msg = array( $paramName => $paramValue );
00339 $this->getResult()->disableSizeCheck();
00340 $this->getResult()->addValue('query-continue', $this->getModuleName(), $msg);
00341 $this->getResult()->enableSizeCheck();
00342 }
00343
00348 protected function getDB() {
00349 if (is_null($this->mDb))
00350 $this->mDb = $this->getQuery()->getDB();
00351 return $this->mDb;
00352 }
00353
00362 public function selectNamedDB($name, $db, $groups) {
00363 $this->mDb = $this->getQuery()->getNamedDB($name, $db, $groups);
00364 }
00365
00370 protected function getPageSet() {
00371 return $this->getQuery()->getPageSet();
00372 }
00373
00379 public function titleToKey($title) {
00380 # Don't throw an error if we got an empty string
00381 if(trim($title) == '')
00382 return '';
00383 $t = Title::newFromText($title);
00384 if(!$t)
00385 $this->dieUsageMsg(array('invalidtitle', $title));
00386 return $t->getPrefixedDbKey();
00387 }
00388
00394 public function keyToTitle($key) {
00395 # Don't throw an error if we got an empty string
00396 if(trim($key) == '')
00397 return '';
00398 $t = Title::newFromDbKey($key);
00399 # This really shouldn't happen but we gotta check anyway
00400 if(!$t)
00401 $this->dieUsageMsg(array('invalidtitle', $key));
00402 return $t->getPrefixedText();
00403 }
00404
00410 public function titlePartToKey($titlePart) {
00411 return substr($this->titleToKey($titlePart . 'x'), 0, -1);
00412 }
00413
00419 public function keyPartToTitle($keyPart) {
00420 return substr($this->keyToTitle($keyPart . 'x'), 0, -1);
00421 }
00422
00427 public static function getBaseVersion() {
00428 return __CLASS__ . ': $Id: ApiQueryBase.php 69986 2010-07-27 03:57:39Z tstarling $';
00429 }
00430 }
00431
00435 abstract class ApiQueryGeneratorBase extends ApiQueryBase {
00436
00437 private $mIsGenerator;
00438
00439 public function __construct($query, $moduleName, $paramPrefix = '') {
00440 parent :: __construct($query, $moduleName, $paramPrefix);
00441 $this->mIsGenerator = false;
00442 }
00443
00448 public function setGeneratorMode() {
00449 $this->mIsGenerator = true;
00450 }
00451
00457 public function encodeParamName($paramName) {
00458 if ($this->mIsGenerator)
00459 return 'g' . parent :: encodeParamName($paramName);
00460 else
00461 return parent :: encodeParamName($paramName);
00462 }
00463
00469 public abstract function executeGenerator($resultPageSet);
00470 }