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 $db = $this->getDB();
00242
00243 $this->profileDBIn();
00244 $res = $db->select( $this->tables, $this->fields, $this->where, $method, $this->options, $this->join_conds );
00245 $this->profileDBOut();
00246
00247 return $res;
00248 }
00249
00255 protected function checkRowCount() {
00256 $db = $this->getDB();
00257 $this->profileDBIn();
00258 $rowcount = $db->estimateRowCount( $this->tables, $this->fields, $this->where, __METHOD__, $this->options );
00259 $this->profileDBOut();
00260
00261 global $wgAPIMaxDBRows;
00262 if ( $rowcount > $wgAPIMaxDBRows )
00263 return false;
00264 return true;
00265 }
00266
00274 public static function addTitleInfo( &$arr, $title, $prefix = '' ) {
00275 $arr[$prefix . 'ns'] = intval( $title->getNamespace() );
00276 $arr[$prefix . 'title'] = $title->getPrefixedText();
00277 }
00278
00284 public function requestExtraData( $pageSet ) {
00285 }
00286
00291 public function getQuery() {
00292 return $this->mQueryModule;
00293 }
00294
00301 protected function addPageSubItems( $pageId, $data ) {
00302 $result = $this->getResult();
00303 $result->setIndexedTagName( $data, $this->getModulePrefix() );
00304 return $result->addValue( array( 'query', 'pages', intval( $pageId ) ),
00305 $this->getModuleName(),
00306 $data );
00307 }
00308
00317 protected function addPageSubItem( $pageId, $item, $elemname = null ) {
00318 if ( is_null( $elemname ) )
00319 $elemname = $this->getModulePrefix();
00320 $result = $this->getResult();
00321 $fit = $result->addValue( array( 'query', 'pages', $pageId,
00322 $this->getModuleName() ), null, $item );
00323 if ( !$fit )
00324 return false;
00325 $result->setIndexedTagName_internal( array( 'query', 'pages', $pageId,
00326 $this->getModuleName() ), $elemname );
00327 return true;
00328 }
00329
00335 protected function setContinueEnumParameter( $paramName, $paramValue ) {
00336 $paramName = $this->encodeParamName( $paramName );
00337 $msg = array( $paramName => $paramValue );
00338 $this->getResult()->disableSizeCheck();
00339 $this->getResult()->addValue( 'query-continue', $this->getModuleName(), $msg );
00340 $this->getResult()->enableSizeCheck();
00341 }
00342
00347 protected function getDB() {
00348 if ( is_null( $this->mDb ) )
00349 $this->mDb = $this->getQuery()->getDB();
00350 return $this->mDb;
00351 }
00352
00361 public function selectNamedDB( $name, $db, $groups ) {
00362 $this->mDb = $this->getQuery()->getNamedDB( $name, $db, $groups );
00363 }
00364
00369 protected function getPageSet() {
00370 return $this->getQuery()->getPageSet();
00371 }
00372
00378 public function titleToKey( $title ) {
00379
00380 if ( trim( $title ) == '' )
00381 return '';
00382 $t = Title::newFromText( $title );
00383 if ( !$t )
00384 $this->dieUsageMsg( array( 'invalidtitle', $title ) );
00385 return $t->getPrefixedDbKey();
00386 }
00387
00393 public function keyToTitle( $key ) {
00394
00395 if ( trim( $key ) == '' )
00396 return '';
00397 $t = Title::newFromDbKey( $key );
00398
00399 if ( !$t )
00400 $this->dieUsageMsg( array( 'invalidtitle', $key ) );
00401 return $t->getPrefixedText();
00402 }
00403
00409 public function titlePartToKey( $titlePart ) {
00410 return substr( $this->titleToKey( $titlePart . 'x' ), 0, - 1 );
00411 }
00412
00418 public function keyPartToTitle( $keyPart ) {
00419 return substr( $this->keyToTitle( $keyPart . 'x' ), 0, - 1 );
00420 }
00421
00422 public function getPossibleErrors() {
00423 return array_merge( parent::getPossibleErrors(), array(
00424 array( 'invalidtitle', 'title' ),
00425 array( 'invalidtitle', 'key' ),
00426 ) );
00427 }
00428
00433 public static function getBaseVersion() {
00434 return __CLASS__ . ': $Id: ApiQueryBase.php 69932 2010-07-26 08:03:21Z tstarling $';
00435 }
00436 }
00437
00441 abstract class ApiQueryGeneratorBase extends ApiQueryBase {
00442
00443 private $mIsGenerator;
00444
00445 public function __construct( $query, $moduleName, $paramPrefix = '' ) {
00446 parent :: __construct( $query, $moduleName, $paramPrefix );
00447 $this->mIsGenerator = false;
00448 }
00449
00454 public function setGeneratorMode() {
00455 $this->mIsGenerator = true;
00456 }
00457
00463 public function encodeParamName( $paramName ) {
00464 if ( $this->mIsGenerator )
00465 return 'g' . parent :: encodeParamName( $paramName );
00466 else
00467 return parent :: encodeParamName( $paramName );
00468 }
00469
00475 public abstract function executeGenerator( $resultPageSet );
00476 }