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