4 * API for MediaWiki 1.14+
6 * Created on Jun 18, 2012
8 * Copyright © 2012 Brad Jorsch
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
29 * API interface for setting the wl_notificationtimestamp field
32 class ApiSetNotificationTimestamp
extends ApiBase
{
36 public function execute() {
37 $user = $this->getUser();
39 if ( $user->isAnon() ) {
40 $this->dieUsage( 'Anonymous users cannot use watchlist change notifications', 'notloggedin' );
43 $params = $this->extractRequestParams();
44 $this->requireMaxOneParameter( $params, 'timestamp', 'torevid', 'newerthanrevid' );
46 $pageSet = $this->getPageSet();
47 if ( $params['entirewatchlist'] && $pageSet->getDataSource() !== null ) {
48 $this->dieUsage( "Cannot use 'entirewatchlist' at the same time as '{$pageSet->getDataSource()}'", 'multisource' );
51 $dbw = wfGetDB( DB_MASTER
, 'api' );
54 if ( isset( $params['timestamp'] ) ) {
55 $timestamp = $dbw->timestamp( $params['timestamp'] );
58 if ( !$params['entirewatchlist'] ) {
62 if ( isset( $params['torevid'] ) ) {
63 if ( $params['entirewatchlist'] ||
$pageSet->getGoodTitleCount() > 1 ) {
64 $this->dieUsage( 'torevid may only be used with a single page', 'multpages' );
66 $title = reset( $pageSet->getGoodTitles() );
67 $timestamp = Revision
::getTimestampFromId( $title, $params['torevid'] );
69 $timestamp = $dbw->timestamp( $timestamp );
73 } elseif ( isset( $params['newerthanrevid'] ) ) {
74 if ( $params['entirewatchlist'] ||
$pageSet->getGoodTitleCount() > 1 ) {
75 $this->dieUsage( 'newerthanrevid may only be used with a single page', 'multpages' );
77 $title = reset( $pageSet->getGoodTitles() );
78 $revid = $title->getNextRevisionID( $params['newerthanrevid'] );
80 $timestamp = $dbw->timestamp( Revision
::getTimestampFromId( $title, $revid ) );
86 $apiResult = $this->getResult();
88 if ( $params['entirewatchlist'] ) {
89 // Entire watchlist mode: Just update the thing and return a success indicator
90 $dbw->update( 'watchlist', array( 'wl_notificationtimestamp' => $timestamp ),
91 array( 'wl_user' => $user->getID() ),
95 $result['notificationtimestamp'] = ( is_null( $timestamp ) ?
'' : wfTimestamp( TS_ISO_8601
, $timestamp ) );
97 // First, log the invalid titles
98 foreach ( $pageSet->getInvalidTitles() as $title ) {
100 $r['title'] = $title;
104 foreach ( $pageSet->getMissingPageIDs() as $p ) {
106 $page['pageid'] = $p;
107 $page['missing'] = '';
108 $page['notwatched'] = '';
111 foreach ( $pageSet->getMissingRevisionIDs() as $r ) {
114 $rev['missing'] = '';
115 $rev['notwatched'] = '';
119 // Now process the valid titles
120 $lb = new LinkBatch( $pageSet->getTitles() );
121 $dbw->update( 'watchlist', array( 'wl_notificationtimestamp' => $timestamp ),
122 array( 'wl_user' => $user->getID(), $lb->constructSet( 'wl', $dbw ) ),
126 // Query the results of our update
127 $timestamps = array();
128 $res = $dbw->select( 'watchlist', array( 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ),
129 array( 'wl_user' => $user->getID(), $lb->constructSet( 'wl', $dbw ) ),
132 foreach ( $res as $row ) {
133 $timestamps[$row->wl_namespace
][$row->wl_title
] = $row->wl_notificationtimestamp
;
136 // Now, put the valid titles into the result
137 /** @var $title Title */
138 foreach ( $pageSet->getTitles() as $title ) {
139 $ns = $title->getNamespace();
140 $dbkey = $title->getDBkey();
142 'ns' => intval( $ns ),
143 'title' => $title->getPrefixedText(),
145 if ( !$title->exists() ) {
148 if ( isset( $timestamps[$ns] ) && array_key_exists( $dbkey, $timestamps[$ns] ) ) {
149 $r['notificationtimestamp'] = '';
150 if ( $timestamps[$ns][$dbkey] !== null ) {
151 $r['notificationtimestamp'] = wfTimestamp( TS_ISO_8601
, $timestamps[$ns][$dbkey] );
154 $r['notwatched'] = '';
159 $apiResult->setIndexedTagName( $result, 'page' );
161 $apiResult->addValue( null, $this->getModuleName(), $result );
165 * Get a cached instance of an ApiPageSet object
168 private function getPageSet() {
169 if ( !isset( $this->mPageSet
) ) {
170 $this->mPageSet
= new ApiPageSet( $this );
172 return $this->mPageSet
;
175 public function mustBePosted() {
179 public function isWriteMode() {
183 public function needsToken() {
187 public function getTokenSalt() {
191 public function getAllowedParams( $flags = 0 ) {
193 'entirewatchlist' => array(
194 ApiBase
::PARAM_TYPE
=> 'boolean'
197 'timestamp' => array(
198 ApiBase
::PARAM_TYPE
=> 'timestamp'
201 ApiBase
::PARAM_TYPE
=> 'integer'
203 'newerthanrevid' => array(
204 ApiBase
::PARAM_TYPE
=> 'integer'
208 $result +
= $this->getPageSet()->getFinalParams( $flags );
214 public function getParamDescription() {
215 return $this->getPageSet()->getFinalParamDescription() +
array(
216 'entirewatchlist' => 'Work on all watched pages',
217 'timestamp' => 'Timestamp to which to set the notification timestamp',
218 'torevid' => 'Revision to set the notification timestamp to (one page only)',
219 'newerthanrevid' => 'Revision to set the notification timestamp newer than (one page only)',
220 'token' => 'A token previously acquired via prop=info',
224 public function getResultProperties() {
226 ApiBase
::PROP_LIST
=> true,
227 ApiBase
::PROP_ROOT
=> array(
228 'notificationtimestamp' => array(
229 ApiBase
::PROP_TYPE
=> 'timestamp',
230 ApiBase
::PROP_NULLABLE
=> true
235 ApiBase
::PROP_TYPE
=> 'namespace',
236 ApiBase
::PROP_NULLABLE
=> true
239 ApiBase
::PROP_TYPE
=> 'string',
240 ApiBase
::PROP_NULLABLE
=> true
243 ApiBase
::PROP_TYPE
=> 'integer',
244 ApiBase
::PROP_NULLABLE
=> true
247 ApiBase
::PROP_TYPE
=> 'integer',
248 ApiBase
::PROP_NULLABLE
=> true
250 'invalid' => 'boolean',
251 'missing' => 'boolean',
252 'notwatched' => 'boolean',
253 'notificationtimestamp' => array(
254 ApiBase
::PROP_TYPE
=> 'timestamp',
255 ApiBase
::PROP_NULLABLE
=> true
261 public function getDescription() {
262 return array( 'Update the notification timestamp for watched pages.',
263 'This affects the highlighting of changed pages in the watchlist and history,',
264 'and the sending of email when the "Email me when a page on my watchlist is',
265 'changed" preference is enabled.'
269 public function getPossibleErrors() {
270 $ps = $this->getPageSet();
272 parent
::getPossibleErrors(),
273 $ps->getFinalPossibleErrors(),
274 $this->getRequireMaxOneParameterErrorMessages(
275 array( 'timestamp', 'torevid', 'newerthanrevid' ) ),
276 $this->getRequireOnlyOneParameterErrorMessages(
277 array_merge( array( 'entirewatchlist' ), array_keys( $ps->getFinalParams() ) ) ),
279 array( 'code' => 'notloggedin', 'info' => 'Anonymous users cannot use watchlist change notifications' ),
280 array( 'code' => 'multpages', 'info' => 'torevid may only be used with a single page' ),
281 array( 'code' => 'multpages', 'info' => 'newerthanrevid may only be used with a single page' ),
286 public function getExamples() {
288 'api.php?action=setnotificationtimestamp&entirewatchlist=&token=123ABC' => 'Reset the notification status for the entire watchlist',
289 'api.php?action=setnotificationtimestamp&titles=Main_page&token=123ABC' => 'Reset the notification status for "Main page"',
290 'api.php?action=setnotificationtimestamp&titles=Main_page×tamp=2012-01-01T00:00:00Z&token=123ABC' => 'Set the notification timestamp for "Main page" so all edits since 1 January 2012 are unviewed',
294 public function getHelpUrls() {
295 return 'https://www.mediawiki.org/wiki/API:SetNotificationTimestamp';