MediaWiki
REL1_20
|
00001 <?php 00033 class ApiQueryWatchlistRaw extends ApiQueryGeneratorBase { 00034 00035 public function __construct( $query, $moduleName ) { 00036 parent::__construct( $query, $moduleName, 'wr' ); 00037 } 00038 00039 public function execute() { 00040 $this->run(); 00041 } 00042 00043 public function executeGenerator( $resultPageSet ) { 00044 $this->run( $resultPageSet ); 00045 } 00046 00051 private function run( $resultPageSet = null ) { 00052 $this->selectNamedDB( 'watchlist', DB_SLAVE, 'watchlist' ); 00053 00054 $params = $this->extractRequestParams(); 00055 00056 $user = $this->getWatchlistUser( $params ); 00057 00058 $prop = array_flip( (array)$params['prop'] ); 00059 $show = array_flip( (array)$params['show'] ); 00060 if ( isset( $show['changed'] ) && isset( $show['!changed'] ) ) { 00061 $this->dieUsageMsg( 'show' ); 00062 } 00063 00064 $this->addTables( 'watchlist' ); 00065 $this->addFields( array( 'wl_namespace', 'wl_title' ) ); 00066 $this->addFieldsIf( 'wl_notificationtimestamp', isset( $prop['changed'] ) ); 00067 $this->addWhereFld( 'wl_user', $user->getId() ); 00068 $this->addWhereFld( 'wl_namespace', $params['namespace'] ); 00069 $this->addWhereIf( 'wl_notificationtimestamp IS NOT NULL', isset( $show['changed'] ) ); 00070 $this->addWhereIf( 'wl_notificationtimestamp IS NULL', isset( $show['!changed'] ) ); 00071 00072 if ( isset( $params['continue'] ) ) { 00073 $cont = explode( '|', $params['continue'] ); 00074 if ( count( $cont ) != 2 ) { 00075 $this->dieUsage( "Invalid continue param. You should pass the " . 00076 "original value returned by the previous query", "_badcontinue" ); 00077 } 00078 $ns = intval( $cont[0] ); 00079 $title = $this->getDB()->addQuotes( $cont[1] ); 00080 $op = $params['dir'] == 'ascending' ? '>' : '<'; 00081 $this->addWhere( 00082 "wl_namespace $op $ns OR " . 00083 "(wl_namespace = $ns AND " . 00084 "wl_title $op= $title)" 00085 ); 00086 } 00087 00088 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' ); 00089 // Don't ORDER BY wl_namespace if it's constant in the WHERE clause 00090 if ( count( $params['namespace'] ) == 1 ) { 00091 $this->addOption( 'ORDER BY', 'wl_title' . $sort ); 00092 } else { 00093 $this->addOption( 'ORDER BY', array( 00094 'wl_namespace' . $sort, 00095 'wl_title' . $sort 00096 )); 00097 } 00098 $this->addOption( 'LIMIT', $params['limit'] + 1 ); 00099 $res = $this->select( __METHOD__ ); 00100 00101 $titles = array(); 00102 $count = 0; 00103 foreach ( $res as $row ) { 00104 if ( ++$count > $params['limit'] ) { 00105 // We've reached the one extra which shows that there are additional pages to be had. Stop here... 00106 $this->setContinueEnumParameter( 'continue', $row->wl_namespace . '|' . $row->wl_title ); 00107 break; 00108 } 00109 $t = Title::makeTitle( $row->wl_namespace, $row->wl_title ); 00110 00111 if ( is_null( $resultPageSet ) ) { 00112 $vals = array(); 00113 ApiQueryBase::addTitleInfo( $vals, $t ); 00114 if ( isset( $prop['changed'] ) && !is_null( $row->wl_notificationtimestamp ) ) 00115 { 00116 $vals['changed'] = wfTimestamp( TS_ISO_8601, $row->wl_notificationtimestamp ); 00117 } 00118 $fit = $this->getResult()->addValue( $this->getModuleName(), null, $vals ); 00119 if ( !$fit ) { 00120 $this->setContinueEnumParameter( 'continue', $row->wl_namespace . '|' . $row->wl_title ); 00121 break; 00122 } 00123 } else { 00124 $titles[] = $t; 00125 } 00126 } 00127 if ( is_null( $resultPageSet ) ) { 00128 $this->getResult()->setIndexedTagName_internal( $this->getModuleName(), 'wr' ); 00129 } else { 00130 $resultPageSet->populateFromTitles( $titles ); 00131 } 00132 } 00133 00134 public function getAllowedParams() { 00135 return array( 00136 'continue' => null, 00137 'namespace' => array( 00138 ApiBase::PARAM_ISMULTI => true, 00139 ApiBase::PARAM_TYPE => 'namespace' 00140 ), 00141 'limit' => array( 00142 ApiBase::PARAM_DFLT => 10, 00143 ApiBase::PARAM_TYPE => 'limit', 00144 ApiBase::PARAM_MIN => 1, 00145 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, 00146 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 00147 ), 00148 'prop' => array( 00149 ApiBase::PARAM_ISMULTI => true, 00150 ApiBase::PARAM_TYPE => array( 00151 'changed', 00152 ) 00153 ), 00154 'show' => array( 00155 ApiBase::PARAM_ISMULTI => true, 00156 ApiBase::PARAM_TYPE => array( 00157 'changed', 00158 '!changed', 00159 ) 00160 ), 00161 'owner' => array( 00162 ApiBase::PARAM_TYPE => 'user' 00163 ), 00164 'token' => array( 00165 ApiBase::PARAM_TYPE => 'string' 00166 ), 00167 'dir' => array( 00168 ApiBase::PARAM_DFLT => 'ascending', 00169 ApiBase::PARAM_TYPE => array( 00170 'ascending', 00171 'descending' 00172 ), 00173 ), 00174 ); 00175 } 00176 00177 public function getParamDescription() { 00178 return array( 00179 'continue' => 'When more results are available, use this to continue', 00180 'namespace' => 'Only list pages in the given namespace(s)', 00181 'limit' => 'How many total results to return per request', 00182 'prop' => array( 00183 'Which additional properties to get (non-generator mode only)', 00184 ' changed - Adds timestamp of when the user was last notified about the edit', 00185 ), 00186 'show' => 'Only list items that meet these criteria', 00187 'owner' => 'The name of the user whose watchlist you\'d like to access', 00188 'token' => 'Give a security token (settable in preferences) to allow access to another user\'s watchlist', 00189 'dir' => 'Direction to sort the titles and namespaces in', 00190 ); 00191 } 00192 00193 public function getResultProperties() { 00194 return array( 00195 '' => array( 00196 'ns' => 'namespace', 00197 'title' => 'string' 00198 ), 00199 'changed' => array( 00200 'changed' => array( 00201 ApiBase::PROP_TYPE => 'timestamp', 00202 ApiBase::PROP_NULLABLE => true 00203 ) 00204 ) 00205 ); 00206 } 00207 00208 public function getDescription() { 00209 return "Get all pages on the logged in user's watchlist"; 00210 } 00211 00212 public function getPossibleErrors() { 00213 return array_merge( parent::getPossibleErrors(), array( 00214 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ), 00215 array( 'show' ), 00216 array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ), 00217 array( 'code' => 'bad_wltoken', 'info' => 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences' ), 00218 ) ); 00219 } 00220 00221 public function getExamples() { 00222 return array( 00223 'api.php?action=query&list=watchlistraw', 00224 'api.php?action=query&generator=watchlistraw&gwrshow=changed&prop=revisions', 00225 ); 00226 } 00227 00228 public function getVersion() { 00229 return __CLASS__ . ': $Id$'; 00230 } 00231 }