4 * API for MediaWiki 1.14+
6 * Copyright © 2012 Wikimedia Foundation and contributors
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
26 use MediaWiki\MediaWikiServices
;
29 * API interface for setting the wl_notificationtimestamp field
32 class ApiSetNotificationTimestamp
extends ApiBase
{
34 private $mPageSet = null;
36 public function execute() {
37 $user = $this->getUser();
39 if ( $user->isAnon() ) {
40 $this->dieWithError( 'watchlistanontext', 'notloggedin' );
42 $this->checkUserRightsAny( 'editmywatchlist' );
44 $params = $this->extractRequestParams();
45 $this->requireMaxOneParameter( $params, 'timestamp', 'torevid', 'newerthanrevid' );
47 $continuationManager = new ApiContinuationManager( $this, [], [] );
48 $this->setContinuationManager( $continuationManager );
50 $pageSet = $this->getPageSet();
51 if ( $params['entirewatchlist'] && $pageSet->getDataSource() !== null ) {
54 'apierror-invalidparammix-cannotusewith',
55 $this->encodeParamName( 'entirewatchlist' ),
56 $pageSet->encodeParamName( $pageSet->getDataSource() )
62 $dbw = wfGetDB( DB_MASTER
, 'api' );
65 if ( isset( $params['timestamp'] ) ) {
66 $timestamp = $dbw->timestamp( $params['timestamp'] );
69 if ( !$params['entirewatchlist'] ) {
73 if ( isset( $params['torevid'] ) ) {
74 if ( $params['entirewatchlist'] ||
$pageSet->getGoodTitleCount() > 1 ) {
75 $this->dieWithError( [ 'apierror-multpages', $this->encodeParamName( 'torevid' ) ] );
77 $titles = $pageSet->getGoodTitles();
78 $title = reset( $titles );
80 // XXX $title isn't actually used, can we just get rid of the previous six lines?
81 $timestamp = MediaWikiServices
::getInstance()->getRevisionStore()
82 ->getTimestampFromId( $params['torevid'], IDBAccessObject
::READ_LATEST
);
84 $timestamp = $dbw->timestamp( $timestamp );
89 } elseif ( isset( $params['newerthanrevid'] ) ) {
90 if ( $params['entirewatchlist'] ||
$pageSet->getGoodTitleCount() > 1 ) {
91 $this->dieWithError( [ 'apierror-multpages', $this->encodeParamName( 'newerthanrevid' ) ] );
93 $titles = $pageSet->getGoodTitles();
94 $title = reset( $titles );
97 $rl = MediaWikiServices
::getInstance()->getRevisionLookup();
98 $currRev = $rl->getRevisionById( $params['newerthanrevid'], Title
::READ_LATEST
);
100 $nextRev = $rl->getNextRevision( $currRev, Title
::READ_LATEST
);
102 $timestamp = $dbw->timestamp( $nextRev->getTimestamp() );
108 $watchedItemStore = MediaWikiServices
::getInstance()->getWatchedItemStore();
109 $apiResult = $this->getResult();
111 if ( $params['entirewatchlist'] ) {
112 // Entire watchlist mode: Just update the thing and return a success indicator
113 $watchedItemStore->resetAllNotificationTimestampsForUser( $user, $timestamp );
115 $result['notificationtimestamp'] = is_null( $timestamp )
117 : wfTimestamp( TS_ISO_8601
, $timestamp );
119 // First, log the invalid titles
120 foreach ( $pageSet->getInvalidTitlesAndReasons() as $r ) {
121 $r['invalid'] = true;
124 foreach ( $pageSet->getMissingPageIDs() as $p ) {
126 $page['pageid'] = $p;
127 $page['missing'] = true;
128 $page['notwatched'] = true;
131 foreach ( $pageSet->getMissingRevisionIDs() as $r ) {
134 $rev['missing'] = true;
135 $rev['notwatched'] = true;
139 if ( $pageSet->getTitles() ) {
140 // Now process the valid titles
141 $watchedItemStore->setNotificationTimestampsForUser(
144 $pageSet->getTitles()
147 // Query the results of our update
148 $timestamps = $watchedItemStore->getNotificationTimestampsBatch(
150 $pageSet->getTitles()
153 // Now, put the valid titles into the result
154 /** @var Title $title */
155 foreach ( $pageSet->getTitles() as $title ) {
156 $ns = $title->getNamespace();
157 $dbkey = $title->getDBkey();
160 'title' => $title->getPrefixedText(),
162 if ( !$title->exists() ) {
163 $r['missing'] = true;
164 if ( $title->isKnown() ) {
168 if ( isset( $timestamps[$ns] ) && array_key_exists( $dbkey, $timestamps[$ns] ) ) {
169 $r['notificationtimestamp'] = '';
170 if ( $timestamps[$ns][$dbkey] !== null ) {
171 $r['notificationtimestamp'] = wfTimestamp( TS_ISO_8601
, $timestamps[$ns][$dbkey] );
174 $r['notwatched'] = true;
180 ApiResult
::setIndexedTagName( $result, 'page' );
182 $apiResult->addValue( null, $this->getModuleName(), $result );
184 $this->setContinuationManager( null );
185 $continuationManager->setContinuationIntoResult( $apiResult );
189 * Get a cached instance of an ApiPageSet object
192 private function getPageSet() {
193 if ( $this->mPageSet
=== null ) {
194 $this->mPageSet
= new ApiPageSet( $this );
197 return $this->mPageSet
;
200 public function mustBePosted() {
204 public function isWriteMode() {
208 public function needsToken() {
212 public function getAllowedParams( $flags = 0 ) {
214 'entirewatchlist' => [
215 ApiBase
::PARAM_TYPE
=> 'boolean'
218 ApiBase
::PARAM_TYPE
=> 'timestamp'
221 ApiBase
::PARAM_TYPE
=> 'integer'
223 'newerthanrevid' => [
224 ApiBase
::PARAM_TYPE
=> 'integer'
227 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
231 $result +
= $this->getPageSet()->getFinalParams( $flags );
237 protected function getExamplesMessages() {
239 'action=setnotificationtimestamp&entirewatchlist=&token=123ABC'
240 => 'apihelp-setnotificationtimestamp-example-all',
241 'action=setnotificationtimestamp&titles=Main_page&token=123ABC'
242 => 'apihelp-setnotificationtimestamp-example-page',
243 'action=setnotificationtimestamp&titles=Main_page&' .
244 'timestamp=2012-01-01T00:00:00Z&token=123ABC'
245 => 'apihelp-setnotificationtimestamp-example-pagetimestamp',
246 'action=setnotificationtimestamp&generator=allpages&gapnamespace=2&token=123ABC'
247 => 'apihelp-setnotificationtimestamp-example-allpages',
251 public function getHelpUrls() {
252 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetNotificationTimestamp';