Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiSetNotificationTimestamp.php
blob5d37e20dc54ad860c37e54c067c4c3005ad17c10
1 <?php
3 /**
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
25 * @file
28 /**
29 * API interface for setting the wl_notificationtimestamp field
30 * @ingroup API
32 class ApiSetNotificationTimestamp extends ApiBase {
34 private $mPageSet;
36 public function execute() {
37 $user = $this->getUser();
39 if ( $user->isAnon() ) {
40 $this->dieUsage( 'Anonymous users cannot use watchlist change notifications', 'notloggedin' );
42 if ( !$user->isAllowed( 'editmywatchlist' ) ) {
43 $this->dieUsage( 'You don\'t have permission to edit your watchlist', 'permissiondenied' );
46 $params = $this->extractRequestParams();
47 $this->requireMaxOneParameter( $params, 'timestamp', 'torevid', 'newerthanrevid' );
49 $this->getResult()->beginContinuation( $params['continue'], array(), array() );
51 $pageSet = $this->getPageSet();
52 if ( $params['entirewatchlist'] && $pageSet->getDataSource() !== null ) {
53 $this->dieUsage(
54 "Cannot use 'entirewatchlist' at the same time as '{$pageSet->getDataSource()}'",
55 'multisource'
59 $dbw = wfGetDB( DB_MASTER, 'api' );
61 $timestamp = null;
62 if ( isset( $params['timestamp'] ) ) {
63 $timestamp = $dbw->timestamp( $params['timestamp'] );
66 if ( !$params['entirewatchlist'] ) {
67 $pageSet->execute();
70 if ( isset( $params['torevid'] ) ) {
71 if ( $params['entirewatchlist'] || $pageSet->getGoodTitleCount() > 1 ) {
72 $this->dieUsage( 'torevid may only be used with a single page', 'multpages' );
74 $title = reset( $pageSet->getGoodTitles() );
75 if ( $title ) {
76 $timestamp = Revision::getTimestampFromId( $title, $params['torevid'] );
77 if ( $timestamp ) {
78 $timestamp = $dbw->timestamp( $timestamp );
79 } else {
80 $timestamp = null;
83 } elseif ( isset( $params['newerthanrevid'] ) ) {
84 if ( $params['entirewatchlist'] || $pageSet->getGoodTitleCount() > 1 ) {
85 $this->dieUsage( 'newerthanrevid may only be used with a single page', 'multpages' );
87 $title = reset( $pageSet->getGoodTitles() );
88 if ( $title ) {
89 $revid = $title->getNextRevisionID( $params['newerthanrevid'] );
90 if ( $revid ) {
91 $timestamp = $dbw->timestamp( Revision::getTimestampFromId( $title, $revid ) );
92 } else {
93 $timestamp = null;
98 $apiResult = $this->getResult();
99 $result = array();
100 if ( $params['entirewatchlist'] ) {
101 // Entire watchlist mode: Just update the thing and return a success indicator
102 $dbw->update( 'watchlist', array( 'wl_notificationtimestamp' => $timestamp ),
103 array( 'wl_user' => $user->getID() ),
104 __METHOD__
107 $result['notificationtimestamp'] = is_null( $timestamp )
108 ? ''
109 : wfTimestamp( TS_ISO_8601, $timestamp );
110 } else {
111 // First, log the invalid titles
112 foreach ( $pageSet->getInvalidTitles() as $title ) {
113 $r = array();
114 $r['title'] = $title;
115 $r['invalid'] = '';
116 $result[] = $r;
118 foreach ( $pageSet->getMissingPageIDs() as $p ) {
119 $page = array();
120 $page['pageid'] = $p;
121 $page['missing'] = '';
122 $page['notwatched'] = '';
123 $result[] = $page;
125 foreach ( $pageSet->getMissingRevisionIDs() as $r ) {
126 $rev = array();
127 $rev['revid'] = $r;
128 $rev['missing'] = '';
129 $rev['notwatched'] = '';
130 $result[] = $rev;
133 if ( $pageSet->getTitles() ) {
134 // Now process the valid titles
135 $lb = new LinkBatch( $pageSet->getTitles() );
136 $dbw->update( 'watchlist', array( 'wl_notificationtimestamp' => $timestamp ),
137 array( 'wl_user' => $user->getID(), $lb->constructSet( 'wl', $dbw ) ),
138 __METHOD__
141 // Query the results of our update
142 $timestamps = array();
143 $res = $dbw->select(
144 'watchlist',
145 array( 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ),
146 array( 'wl_user' => $user->getID(), $lb->constructSet( 'wl', $dbw ) ),
147 __METHOD__
149 foreach ( $res as $row ) {
150 $timestamps[$row->wl_namespace][$row->wl_title] = $row->wl_notificationtimestamp;
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();
158 $r = array(
159 'ns' => intval( $ns ),
160 'title' => $title->getPrefixedText(),
162 if ( !$title->exists() ) {
163 $r['missing'] = '';
165 if ( isset( $timestamps[$ns] ) && array_key_exists( $dbkey, $timestamps[$ns] ) ) {
166 $r['notificationtimestamp'] = '';
167 if ( $timestamps[$ns][$dbkey] !== null ) {
168 $r['notificationtimestamp'] = wfTimestamp( TS_ISO_8601, $timestamps[$ns][$dbkey] );
170 } else {
171 $r['notwatched'] = '';
173 $result[] = $r;
177 $apiResult->setIndexedTagName( $result, 'page' );
179 $apiResult->addValue( null, $this->getModuleName(), $result );
181 $apiResult->endContinuation();
185 * Get a cached instance of an ApiPageSet object
186 * @return ApiPageSet
188 private function getPageSet() {
189 if ( !isset( $this->mPageSet ) ) {
190 $this->mPageSet = new ApiPageSet( $this );
193 return $this->mPageSet;
196 public function mustBePosted() {
197 return true;
200 public function isWriteMode() {
201 return true;
204 public function needsToken() {
205 return 'csrf';
208 public function getAllowedParams( $flags = 0 ) {
209 $result = array(
210 'entirewatchlist' => array(
211 ApiBase::PARAM_TYPE => 'boolean'
213 'timestamp' => array(
214 ApiBase::PARAM_TYPE => 'timestamp'
216 'torevid' => array(
217 ApiBase::PARAM_TYPE => 'integer'
219 'newerthanrevid' => array(
220 ApiBase::PARAM_TYPE => 'integer'
222 'continue' => array(
223 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
226 if ( $flags ) {
227 $result += $this->getPageSet()->getFinalParams( $flags );
230 return $result;
233 protected function getExamplesMessages() {
234 return array(
235 'action=setnotificationtimestamp&entirewatchlist=&token=123ABC'
236 => 'apihelp-setnotificationtimestamp-example-all',
237 'action=setnotificationtimestamp&titles=Main_page&token=123ABC'
238 => 'apihelp-setnotificationtimestamp-example-page',
239 'action=setnotificationtimestamp&titles=Main_page&' .
240 'timestamp=2012-01-01T00:00:00Z&token=123ABC'
241 => 'apihelp-setnotificationtimestamp-example-pagetimestamp',
242 'action=setnotificationtimestamp&generator=allpages&gapnamespace=2&token=123ABC'
243 => 'apihelp-setnotificationtimestamp-example-allpages',
247 public function getHelpUrls() {
248 return 'https://www.mediawiki.org/wiki/API:SetNotificationTimestamp';