MediaWiki  REL1_20
ApiSetNotificationTimestamp.php
Go to the documentation of this file.
00001 <?php
00002 
00032 class ApiSetNotificationTimestamp extends ApiBase {
00033 
00034         public function __construct( $main, $action ) {
00035                 parent::__construct( $main, $action );
00036         }
00037 
00038         public function execute() {
00039                 $user = $this->getUser();
00040 
00041                 if ( $user->isAnon() ) {
00042                         $this->dieUsage( 'Anonymous users cannot use watchlist change notifications', 'notloggedin' );
00043                 }
00044 
00045                 $params = $this->extractRequestParams();
00046                 $this->requireMaxOneParameter( $params, 'timestamp', 'torevid', 'newerthanrevid' );
00047 
00048                 $pageSet = new ApiPageSet( $this );
00049                 $args = array_merge( array( $params, 'entirewatchlist' ), array_keys( $pageSet->getAllowedParams() ) );
00050                 call_user_func_array( array( $this, 'requireOnlyOneParameter' ), $args );
00051 
00052                 $dbw = $this->getDB( DB_MASTER );
00053 
00054                 $timestamp = null;
00055                 if ( isset( $params['timestamp'] ) ) {
00056                         $timestamp = $dbw->timestamp( $params['timestamp'] );
00057                 }
00058 
00059                 if ( !$params['entirewatchlist'] ) {
00060                         $pageSet->execute();
00061                 }
00062 
00063                 if ( isset( $params['torevid'] ) ) {
00064                         if ( $params['entirewatchlist'] || $pageSet->getGoodTitleCount() > 1 ) {
00065                                 $this->dieUsage( 'torevid may only be used with a single page', 'multpages' );
00066                         }
00067                         $title = reset( $pageSet->getGoodTitles() );
00068                         $timestamp = Revision::getTimestampFromId( $title, $params['torevid'] );
00069                         if ( $timestamp ) {
00070                                 $timestamp = $dbw->timestamp( $timestamp );
00071                         } else {
00072                                 $timestamp = null;
00073                         }
00074                 } elseif ( isset( $params['newerthanrevid'] ) ) {
00075                         if ( $params['entirewatchlist'] || $pageSet->getGoodTitleCount() > 1 ) {
00076                                 $this->dieUsage( 'newerthanrevid may only be used with a single page', 'multpages' );
00077                         }
00078                         $title = reset( $pageSet->getGoodTitles() );
00079                         $revid = $title->getNextRevisionID( $params['newerthanrevid'] );
00080                         if ( $revid ) {
00081                                 $timestamp = $dbw->timestamp( Revision::getTimestampFromId( $title, $revid ) );
00082                         } else {
00083                                 $timestamp = null;
00084                         }
00085                 }
00086 
00087                 $apiResult = $this->getResult();
00088                 $result = array();
00089                 if ( $params['entirewatchlist'] ) {
00090                         // Entire watchlist mode: Just update the thing and return a success indicator
00091                         $dbw->update( 'watchlist', array( 'wl_notificationtimestamp' => $timestamp ),
00092                                 array( 'wl_user' => $user->getID() ),
00093                                 __METHOD__
00094                         );
00095 
00096                         $result['notificationtimestamp'] = ( is_null( $timestamp ) ? '' : wfTimestamp( TS_ISO_8601, $timestamp ) );
00097                 } else {
00098                         // First, log the invalid titles
00099                         foreach( $pageSet->getInvalidTitles() as $title ) {
00100                                 $r = array();
00101                                 $r['title'] = $title;
00102                                 $r['invalid'] = '';
00103                                 $result[] = $r;
00104                         }
00105                         foreach( $pageSet->getMissingPageIDs() as $p ) {
00106                                 $page = array();
00107                                 $page['pageid'] = $p;
00108                                 $page['missing'] = '';
00109                                 $page['notwatched'] = '';
00110                                 $result[] = $page;
00111                         }
00112                         foreach( $pageSet->getMissingRevisionIDs() as $r ) {
00113                                 $rev = array();
00114                                 $rev['revid'] = $r;
00115                                 $rev['missing'] = '';
00116                                 $rev['notwatched'] = '';
00117                                 $result[] = $rev;
00118                         }
00119 
00120                         // Now process the valid titles
00121                         $lb = new LinkBatch( $pageSet->getTitles() );
00122                         $dbw->update( 'watchlist', array( 'wl_notificationtimestamp' => $timestamp ),
00123                                 array( 'wl_user' => $user->getID(), $lb->constructSet( 'wl', $dbw ) ),
00124                                 __METHOD__
00125                         );
00126 
00127                         // Query the results of our update
00128                         $timestamps = array();
00129                         $res = $dbw->select( 'watchlist', array( 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ),
00130                                 array( 'wl_user' => $user->getID(), $lb->constructSet( 'wl', $dbw ) ),
00131                                 __METHOD__
00132                         );
00133                         foreach ( $res as $row ) {
00134                                 $timestamps[$row->wl_namespace][$row->wl_title] = $row->wl_notificationtimestamp;
00135                         }
00136 
00137                         // Now, put the valid titles into the result
00138                         foreach ( $pageSet->getTitles() as $title ) {
00139                                 $ns = $title->getNamespace();
00140                                 $dbkey = $title->getDBkey();
00141                                 $r = array(
00142                                         'ns' => intval( $ns ),
00143                                         'title' => $title->getPrefixedText(),
00144                                 );
00145                                 if ( !$title->exists() ) {
00146                                         $r['missing'] = '';
00147                                 }
00148                                 if ( isset( $timestamps[$ns] ) && array_key_exists( $dbkey, $timestamps[$ns] ) ) {
00149                                         $r['notificationtimestamp'] = '';
00150                                         if ( $timestamps[$ns][$dbkey] !== null ) {
00151                                                 $r['notificationtimestamp'] = wfTimestamp( TS_ISO_8601, $timestamps[$ns][$dbkey] );
00152                                         }
00153                                 } else {
00154                                         $r['notwatched'] = '';
00155                                 }
00156                                 $result[] = $r;
00157                         }
00158 
00159                         $apiResult->setIndexedTagName( $result, 'page' );
00160                 }
00161                 $apiResult->addValue( null, $this->getModuleName(), $result );
00162         }
00163 
00164         public function mustBePosted() {
00165                 return true;
00166         }
00167 
00168         public function isWriteMode() {
00169                 return true;
00170         }
00171 
00172         public function needsToken() {
00173                 return true;
00174         }
00175 
00176         public function getTokenSalt() {
00177                 return '';
00178         }
00179 
00180         public function getAllowedParams() {
00181                 $psModule = new ApiPageSet( $this );
00182                 return $psModule->getAllowedParams() + array(
00183                         'entirewatchlist' => array(
00184                                 ApiBase::PARAM_TYPE => 'boolean'
00185                         ),
00186                         'token' => null,
00187                         'timestamp' => array(
00188                                 ApiBase::PARAM_TYPE => 'timestamp'
00189                         ),
00190                         'torevid' => array(
00191                                 ApiBase::PARAM_TYPE => 'integer'
00192                         ),
00193                         'newerthanrevid' => array(
00194                                 ApiBase::PARAM_TYPE => 'integer'
00195                         ),
00196                 );
00197         }
00198 
00199         public function getParamDescription() {
00200                 $psModule = new ApiPageSet( $this );
00201                 return $psModule->getParamDescription() + array(
00202                         'entirewatchlist' => 'Work on all watched pages',
00203                         'timestamp' => 'Timestamp to which to set the notification timestamp',
00204                         'torevid' => 'Revision to set the notification timestamp to (one page only)',
00205                         'newerthanrevid' => 'Revision to set the notification timestamp newer than (one page only)',
00206                         'token' => 'A token previously acquired via prop=info',
00207                 );
00208         }
00209 
00210         public function getResultProperties() {
00211                 return array(
00212                         ApiBase::PROP_LIST => true,
00213                         ApiBase::PROP_ROOT => array(
00214                                 'notificationtimestamp' => array(
00215                                         ApiBase::PROP_TYPE => 'timestamp',
00216                                         ApiBase::PROP_NULLABLE => true
00217                                 )
00218                         ),
00219                         '' => array(
00220                                 'ns' => array(
00221                                         ApiBase::PROP_TYPE => 'namespace',
00222                                         ApiBase::PROP_NULLABLE => true
00223                                 ),
00224                                 'title' => array(
00225                                         ApiBase::PROP_TYPE => 'string',
00226                                         ApiBase::PROP_NULLABLE => true
00227                                 ),
00228                                 'pageid' => array(
00229                                         ApiBase::PROP_TYPE => 'integer',
00230                                         ApiBase::PROP_NULLABLE => true
00231                                 ),
00232                                 'revid' => array(
00233                                         ApiBase::PROP_TYPE => 'integer',
00234                                         ApiBase::PROP_NULLABLE => true
00235                                 ),
00236                                 'invalid' => 'boolean',
00237                                 'missing' => 'boolean',
00238                                 'notwatched' => 'boolean',
00239                                 'notificationtimestamp' => array(
00240                                         ApiBase::PROP_TYPE => 'timestamp',
00241                                         ApiBase::PROP_NULLABLE => true
00242                                 )
00243                         )
00244                 );
00245         }
00246 
00247         public function getDescription() {
00248                 return array( 'Update the notification timestamp for watched pages.',
00249                         'This affects the highlighting of changed pages in the watchlist and history,',
00250                         'and the sending of email when the "E-mail me when a page on my watchlist is',
00251                         'changed" preference is enabled.'
00252                 );
00253         }
00254 
00255         public function getPossibleErrors() {
00256                 $psModule = new ApiPageSet( $this );
00257                 return array_merge(
00258                         parent::getPossibleErrors(),
00259                         $psModule->getPossibleErrors(),
00260                         $this->getRequireMaxOneParameterErrorMessages( array( 'timestamp', 'torevid', 'newerthanrevid' ) ),
00261                         $this->getRequireOnlyOneParameterErrorMessages( array_merge( array( 'entirewatchlist' ), array_keys( $psModule->getAllowedParams() ) ) ),
00262                         array(
00263                                 array( 'code' => 'notloggedin', 'info' => 'Anonymous users cannot use watchlist change notifications' ),
00264                                 array( 'code' => 'multpages', 'info' => 'torevid may only be used with a single page' ),
00265                                 array( 'code' => 'multpages', 'info' => 'newerthanrevid may only be used with a single page' ),
00266                         )
00267                 );
00268         }
00269 
00270         public function getExamples() {
00271                 return array(
00272                         'api.php?action=setnotificationtimestamp&entirewatchlist=&token=ABC123' => 'Reset the notification status for the entire watchlist',
00273                         'api.php?action=setnotificationtimestamp&titles=Main_page&token=ABC123' => 'Reset the notification status for "Main page"',
00274                         'api.php?action=setnotificationtimestamp&titles=Main_page&timestamp=2012-01-01T00:00:00Z&token=ABC123' => 'Set the notification timestamp for "Main page" so all edits since 1 January 2012 are unviewed',
00275                 );
00276         }
00277 
00278         public function getHelpUrls() {
00279                 return 'https://www.mediawiki.org/wiki/API:SetNotificationTimestamp';
00280         }
00281 
00282         public function getVersion() {
00283                 return __CLASS__ . ': $Id$';
00284         }
00285 }