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 ('ApiQueryBase.php');
00029 }
00030
00037 class ApiQueryWatchlist extends ApiQueryGeneratorBase {
00038
00039 public function __construct($query, $moduleName) {
00040 parent :: __construct($query, $moduleName, 'wl');
00041 }
00042
00043 public function execute() {
00044 $this->run();
00045 }
00046
00047 public function executeGenerator($resultPageSet) {
00048 $this->run($resultPageSet);
00049 }
00050
00051 private $fld_ids = false, $fld_title = false, $fld_patrol = false, $fld_flags = false,
00052 $fld_timestamp = false, $fld_user = false, $fld_comment = false, $fld_sizes = false;
00053
00054 private function run($resultPageSet = null) {
00055 global $wgUser, $wgDBtype;
00056
00057 $this->selectNamedDB('watchlist', DB_SLAVE, 'watchlist');
00058
00059 if (!$wgUser->isLoggedIn())
00060 $this->dieUsage('You must be logged-in to have a watchlist', 'notloggedin');
00061
00062 $params = $this->extractRequestParams();
00063
00064 if (!is_null($params['prop']) && is_null($resultPageSet)) {
00065
00066 $prop = array_flip($params['prop']);
00067
00068 $this->fld_ids = isset($prop['ids']);
00069 $this->fld_title = isset($prop['title']);
00070 $this->fld_flags = isset($prop['flags']);
00071 $this->fld_user = isset($prop['user']);
00072 $this->fld_comment = isset($prop['comment']);
00073 $this->fld_timestamp = isset($prop['timestamp']);
00074 $this->fld_sizes = isset($prop['sizes']);
00075 $this->fld_patrol = isset($prop['patrol']);
00076
00077 if ($this->fld_patrol) {
00078 global $wgUser;
00079 if (!$wgUser->useRCPatrol() && !$wgUser->useNPPatrol())
00080 $this->dieUsage('patrol property is not available', 'patrol');
00081 }
00082 }
00083
00084 if (is_null($resultPageSet)) {
00085 $this->addFields(array (
00086 'rc_cur_id',
00087 'rc_this_oldid',
00088 'rc_namespace',
00089 'rc_title',
00090 'rc_timestamp'
00091 ));
00092
00093 $this->addFieldsIf('rc_new', $this->fld_flags);
00094 $this->addFieldsIf('rc_minor', $this->fld_flags);
00095 $this->addFieldsIf('rc_bot', $this->fld_flags);
00096 $this->addFieldsIf('rc_user', $this->fld_user);
00097 $this->addFieldsIf('rc_user_text', $this->fld_user);
00098 $this->addFieldsIf('rc_comment', $this->fld_comment);
00099 $this->addFieldsIf('rc_patrolled', $this->fld_patrol);
00100 $this->addFieldsIf('rc_old_len', $this->fld_sizes);
00101 $this->addFieldsIf('rc_new_len', $this->fld_sizes);
00102 }
00103 elseif ($params['allrev']) {
00104 $this->addFields(array (
00105 'rc_this_oldid',
00106 'rc_namespace',
00107 'rc_title',
00108 'rc_timestamp'
00109 ));
00110 } else {
00111 $this->addFields(array (
00112 'rc_cur_id',
00113 'rc_namespace',
00114 'rc_title',
00115 'rc_timestamp'
00116 ));
00117 }
00118
00119 $this->addTables(array (
00120 'watchlist',
00121 'page',
00122 'recentchanges'
00123 ));
00124
00125 $userId = $wgUser->getId();
00126 $this->addWhere(array (
00127 'wl_namespace = rc_namespace',
00128 'wl_title = rc_title',
00129 'rc_cur_id = page_id',
00130 'wl_user' => $userId,
00131 'rc_deleted' => 0,
00132 ));
00133
00134 $this->addWhereRange('rc_timestamp', $params['dir'], $params['start'], $params['end']);
00135 $this->addWhereFld('wl_namespace', $params['namespace']);
00136 $this->addWhereIf('rc_this_oldid=page_latest', !$params['allrev']);
00137
00138 if (!is_null($params['show'])) {
00139 $show = array_flip($params['show']);
00140
00141
00142 if ((isset ($show['minor']) && isset ($show['!minor']))
00143 || (isset ($show['bot']) && isset ($show['!bot']))
00144 || (isset ($show['anon']) && isset ($show['!anon']))
00145 || (isset ($show['patrolled']) && isset ($show['!patrolled']))) {
00146
00147 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
00148 }
00149
00150
00151 global $wgUser;
00152 if((isset($show['patrolled']) || isset($show['!patrolled'])) && !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol())
00153 $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
00154
00155
00156 $this->addWhereIf('rc_minor = 0', isset ($show['!minor']));
00157 $this->addWhereIf('rc_minor != 0', isset ($show['minor']));
00158 $this->addWhereIf('rc_bot = 0', isset ($show['!bot']));
00159 $this->addWhereIf('rc_bot != 0', isset ($show['bot']));
00160 $this->addWhereIf('rc_user = 0', isset ($show['anon']));
00161 $this->addWhereIf('rc_user != 0', isset ($show['!anon']));
00162 $this->addWhereIf('rc_patrolled = 0', isset($show['!patrolled']));
00163 $this->addWhereIf('rc_patrolled != 0', isset($show['patrolled']));
00164 }
00165
00166
00167 # This is an index optimization for mysql, as done in the Special:Watchlist page
00168 $this->addWhereIf("rc_timestamp > ''", !isset ($params['start']) && !isset ($params['end']) && $wgDBtype == 'mysql');
00169
00170 $this->addOption('LIMIT', $params['limit'] +1);
00171
00172 $ids = array ();
00173 $count = 0;
00174 $res = $this->select(__METHOD__);
00175
00176 $db = $this->getDB();
00177 while ($row = $db->fetchObject($res)) {
00178 if (++ $count > $params['limit']) {
00179
00180 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rc_timestamp));
00181 break;
00182 }
00183
00184 if (is_null($resultPageSet)) {
00185 $vals = $this->extractRowInfo($row);
00186 $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals);
00187 if(!$fit)
00188 {
00189 $this->setContinueEnumParameter('start',
00190 wfTimestamp(TS_ISO_8601, $row->rc_timestamp));
00191 break;
00192 }
00193 } else {
00194 if ($params['allrev']) {
00195 $ids[] = intval($row->rc_this_oldid);
00196 } else {
00197 $ids[] = intval($row->rc_cur_id);
00198 }
00199 }
00200 }
00201
00202 $db->freeResult($res);
00203
00204 if (is_null($resultPageSet)) {
00205 $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'item');
00206 }
00207 elseif ($params['allrev']) {
00208 $resultPageSet->populateFromRevisionIDs($ids);
00209 } else {
00210 $resultPageSet->populateFromPageIDs($ids);
00211 }
00212 }
00213
00214 private function extractRowInfo($row) {
00215
00216 $vals = array ();
00217
00218 if ($this->fld_ids) {
00219 $vals['pageid'] = intval($row->rc_cur_id);
00220 $vals['revid'] = intval($row->rc_this_oldid);
00221 }
00222
00223 if ($this->fld_title)
00224 ApiQueryBase :: addTitleInfo($vals, Title :: makeTitle($row->rc_namespace, $row->rc_title));
00225
00226 if ($this->fld_user) {
00227 $vals['user'] = $row->rc_user_text;
00228 if (!$row->rc_user)
00229 $vals['anon'] = '';
00230 }
00231
00232 if ($this->fld_flags) {
00233 if ($row->rc_new)
00234 $vals['new'] = '';
00235 if ($row->rc_minor)
00236 $vals['minor'] = '';
00237 if ($row->rc_bot)
00238 $vals['bot'] = '';
00239 }
00240
00241 if ($this->fld_patrol && isset($row->rc_patrolled))
00242 $vals['patrolled'] = '';
00243
00244 if ($this->fld_timestamp)
00245 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rc_timestamp);
00246
00247 if ($this->fld_sizes) {
00248 $vals['oldlen'] = intval($row->rc_old_len);
00249 $vals['newlen'] = intval($row->rc_new_len);
00250 }
00251
00252 if ($this->fld_comment && isset( $row->rc_comment ))
00253 $vals['comment'] = $row->rc_comment;
00254
00255 return $vals;
00256 }
00257
00258 public function getAllowedParams() {
00259 return array (
00260 'allrev' => false,
00261 'start' => array (
00262 ApiBase :: PARAM_TYPE => 'timestamp'
00263 ),
00264 'end' => array (
00265 ApiBase :: PARAM_TYPE => 'timestamp'
00266 ),
00267 'namespace' => array (
00268 ApiBase :: PARAM_ISMULTI => true,
00269 ApiBase :: PARAM_TYPE => 'namespace'
00270 ),
00271 'dir' => array (
00272 ApiBase :: PARAM_DFLT => 'older',
00273 ApiBase :: PARAM_TYPE => array (
00274 'newer',
00275 'older'
00276 )
00277 ),
00278 'limit' => array (
00279 ApiBase :: PARAM_DFLT => 10,
00280 ApiBase :: PARAM_TYPE => 'limit',
00281 ApiBase :: PARAM_MIN => 1,
00282 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00283 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00284 ),
00285 'prop' => array (
00286 APIBase :: PARAM_ISMULTI => true,
00287 APIBase :: PARAM_DFLT => 'ids|title|flags',
00288 APIBase :: PARAM_TYPE => array (
00289 'ids',
00290 'title',
00291 'flags',
00292 'user',
00293 'comment',
00294 'timestamp',
00295 'patrol',
00296 'sizes',
00297 )
00298 ),
00299 'show' => array (
00300 ApiBase :: PARAM_ISMULTI => true,
00301 ApiBase :: PARAM_TYPE => array (
00302 'minor',
00303 '!minor',
00304 'bot',
00305 '!bot',
00306 'anon',
00307 '!anon',
00308 'patrolled',
00309 '!patrolled',
00310 )
00311 )
00312 );
00313 }
00314
00315 public function getParamDescription() {
00316 return array (
00317 'allrev' => 'Include multiple revisions of the same page within given timeframe.',
00318 'start' => 'The timestamp to start enumerating from.',
00319 'end' => 'The timestamp to end enumerating.',
00320 'namespace' => 'Filter changes to only the given namespace(s).',
00321 'dir' => 'In which direction to enumerate pages.',
00322 'limit' => 'How many total results to return per request.',
00323 'prop' => 'Which additional items to get (non-generator mode only).',
00324 'show' => array (
00325 'Show only items that meet this criteria.',
00326 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
00327 )
00328 );
00329 }
00330
00331 public function getDescription() {
00332 return "Get all recent changes to pages in the logged in user's watchlist";
00333 }
00334
00335 protected function getExamples() {
00336 return array (
00337 'api.php?action=query&list=watchlist',
00338 'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
00339 'api.php?action=query&list=watchlist&wlallrev&wlprop=ids|title|timestamp|user|comment',
00340 'api.php?action=query&generator=watchlist&prop=info',
00341 'api.php?action=query&generator=watchlist&gwlallrev&prop=revisions&rvprop=timestamp|user'
00342 );
00343 }
00344
00345 public function getVersion() {
00346 return __CLASS__ . ': $Id: ApiQueryWatchlist.php 47865 2009-02-27 16:03:01Z catrope $';
00347 }
00348 }