MediaWiki  REL1_19
ApiQueryWatchlistRaw.php
Go to the documentation of this file.
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()->strencode( $this->titleToKey( $cont[1] ) );
00080                         $this->addWhere(
00081                                 "wl_namespace > '$ns' OR " .
00082                                 "(wl_namespace = '$ns' AND " .
00083                                 "wl_title >= '$title')"
00084                         );
00085                 }
00086 
00087                 // Don't ORDER BY wl_namespace if it's constant in the WHERE clause
00088                 if ( count( $params['namespace'] ) == 1 ) {
00089                         $this->addOption( 'ORDER BY', 'wl_title' );
00090                 } else {
00091                         $this->addOption( 'ORDER BY', 'wl_namespace, wl_title' );
00092                 }
00093                 $this->addOption( 'LIMIT', $params['limit'] + 1 );
00094                 $res = $this->select( __METHOD__ );
00095 
00096                 $titles = array();
00097                 $count = 0;
00098                 foreach ( $res as $row ) {
00099                         if ( ++$count > $params['limit'] ) {
00100                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
00101                                 $this->setContinueEnumParameter( 'continue', $row->wl_namespace . '|' .
00102                                                                         $this->keyToTitle( $row->wl_title ) );
00103                                 break;
00104                         }
00105                         $t = Title::makeTitle( $row->wl_namespace, $row->wl_title );
00106 
00107                         if ( is_null( $resultPageSet ) ) {
00108                                 $vals = array();
00109                                 ApiQueryBase::addTitleInfo( $vals, $t );
00110                                 if ( isset( $prop['changed'] ) && !is_null( $row->wl_notificationtimestamp ) )
00111                                 {
00112                                         $vals['changed'] = wfTimestamp( TS_ISO_8601, $row->wl_notificationtimestamp );
00113                                 }
00114                                 $fit = $this->getResult()->addValue( $this->getModuleName(), null, $vals );
00115                                 if ( !$fit ) {
00116                                         $this->setContinueEnumParameter( 'continue', $row->wl_namespace . '|' .
00117                                                                         $this->keyToTitle( $row->wl_title ) );
00118                                         break;
00119                                 }
00120                         } else {
00121                                 $titles[] = $t;
00122                         }
00123                 }
00124                 if ( is_null( $resultPageSet ) ) {
00125                         $this->getResult()->setIndexedTagName_internal( $this->getModuleName(), 'wr' );
00126                 } else {
00127                         $resultPageSet->populateFromTitles( $titles );
00128                 }
00129         }
00130 
00131         public function getAllowedParams() {
00132                 return array(
00133                         'continue' => null,
00134                         'namespace' => array(
00135                                 ApiBase::PARAM_ISMULTI => true,
00136                                 ApiBase::PARAM_TYPE => 'namespace'
00137                         ),
00138                         'limit' => array(
00139                                 ApiBase::PARAM_DFLT => 10,
00140                                 ApiBase::PARAM_TYPE => 'limit',
00141                                 ApiBase::PARAM_MIN => 1,
00142                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00143                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00144                         ),
00145                         'prop' => array(
00146                                 ApiBase::PARAM_ISMULTI => true,
00147                                 ApiBase::PARAM_TYPE => array(
00148                                         'changed',
00149                                 )
00150                         ),
00151                         'show' => array(
00152                                 ApiBase::PARAM_ISMULTI => true,
00153                                 ApiBase::PARAM_TYPE => array(
00154                                         'changed',
00155                                         '!changed',
00156                                 )
00157                         ),
00158                         'owner' => array(
00159                                 ApiBase::PARAM_TYPE => 'user'
00160                         ),
00161                         'token' => array(
00162                                 ApiBase::PARAM_TYPE => 'string'
00163                         )
00164                 );
00165         }
00166 
00167         public function getParamDescription() {
00168                 return array(
00169                         'continue' => 'When more results are available, use this to continue',
00170                         'namespace' => 'Only list pages in the given namespace(s)',
00171                         'limit' => 'How many total results to return per request',
00172                         'prop' => array(
00173                                 'Which additional properties to get (non-generator mode only)',
00174                                 ' changed  - Adds timestamp of when the user was last notified about the edit',
00175                         ),
00176                         'show' => 'Only list items that meet these criteria',
00177                         'owner' => 'The name of the user whose watchlist you\'d like to access',
00178                         'token' => 'Give a security token (settable in preferences) to allow access to another user\'s watchlist',
00179                 );
00180         }
00181 
00182         public function getDescription() {
00183                 return "Get all pages on the logged in user's watchlist";
00184         }
00185 
00186         public function getPossibleErrors() {
00187                 return array_merge( parent::getPossibleErrors(), array(
00188                         array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
00189                         array( 'show' ),
00190                         array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
00191                         array( 'code' => 'bad_wltoken', 'info' => 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences' ),
00192                 ) );
00193         }
00194 
00195         public function getExamples() {
00196                 return array(
00197                         'api.php?action=query&list=watchlistraw',
00198                         'api.php?action=query&generator=watchlistraw&gwrshow=changed&prop=revisions',
00199                 );
00200         }
00201 
00202         public function getVersion() {
00203                 return __CLASS__ . ': $Id$';
00204         }
00205 }