41c207bd9a36316e28e93751fed268221160d44d
[lhc/web/wiklou.git] / includes / api / ApiSetNotificationTimestamp.php
1 <?php
2
3 /**
4 * API for MediaWiki 1.14+
5 *
6 * Copyright © 2012 Wikimedia Foundation and contributors
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 */
25
26 use MediaWiki\MediaWikiServices;
27
28 /**
29 * API interface for setting the wl_notificationtimestamp field
30 * @ingroup API
31 */
32 class ApiSetNotificationTimestamp extends ApiBase {
33
34 private $mPageSet = null;
35
36 public function execute() {
37 $user = $this->getUser();
38
39 if ( $user->isAnon() ) {
40 $this->dieWithError( 'watchlistanontext', 'notloggedin' );
41 }
42 $this->checkUserRightsAny( 'editmywatchlist' );
43
44 $params = $this->extractRequestParams();
45 $this->requireMaxOneParameter( $params, 'timestamp', 'torevid', 'newerthanrevid' );
46
47 $continuationManager = new ApiContinuationManager( $this, [], [] );
48 $this->setContinuationManager( $continuationManager );
49
50 $pageSet = $this->getPageSet();
51 if ( $params['entirewatchlist'] && $pageSet->getDataSource() !== null ) {
52 $this->dieWithError(
53 [
54 'apierror-invalidparammix-cannotusewith',
55 $this->encodeParamName( 'entirewatchlist' ),
56 $pageSet->encodeParamName( $pageSet->getDataSource() )
57 ],
58 'multisource'
59 );
60 }
61
62 $dbw = wfGetDB( DB_MASTER, 'api' );
63
64 $timestamp = null;
65 if ( isset( $params['timestamp'] ) ) {
66 $timestamp = $dbw->timestamp( $params['timestamp'] );
67 }
68
69 if ( !$params['entirewatchlist'] ) {
70 $pageSet->execute();
71 }
72
73 if ( isset( $params['torevid'] ) ) {
74 if ( $params['entirewatchlist'] || $pageSet->getGoodTitleCount() > 1 ) {
75 $this->dieWithError( [ 'apierror-multpages', $this->encodeParamName( 'torevid' ) ] );
76 }
77 $titles = $pageSet->getGoodTitles();
78 $title = reset( $titles );
79 if ( $title ) {
80 $timestamp = MediaWikiServices::getInstance()->getRevisionStore()
81 ->getTimestampFromId( $title, $params['torevid'], IDBAccessObject::READ_LATEST );
82 if ( $timestamp ) {
83 $timestamp = $dbw->timestamp( $timestamp );
84 } else {
85 $timestamp = null;
86 }
87 }
88 } elseif ( isset( $params['newerthanrevid'] ) ) {
89 if ( $params['entirewatchlist'] || $pageSet->getGoodTitleCount() > 1 ) {
90 $this->dieWithError( [ 'apierror-multpages', $this->encodeParamName( 'newerthanrevid' ) ] );
91 }
92 $titles = $pageSet->getGoodTitles();
93 $title = reset( $titles );
94 if ( $title ) {
95 $revid = $title->getNextRevisionID( $params['newerthanrevid'], Title::GAID_FOR_UPDATE );
96 if ( $revid ) {
97 $timestamp = $dbw->timestamp(
98 MediaWikiServices::getInstance()->getRevisionStore()->getTimestampFromId( $title, $revid )
99 );
100 } else {
101 $timestamp = null;
102 }
103 }
104 }
105
106 $watchedItemStore = MediaWikiServices::getInstance()->getWatchedItemStore();
107 $apiResult = $this->getResult();
108 $result = [];
109 if ( $params['entirewatchlist'] ) {
110 // Entire watchlist mode: Just update the thing and return a success indicator
111 if ( is_null( $timestamp ) ) {
112 $watchedItemStore->resetAllNotificationTimestampsForUser( $user );
113 } else {
114 $watchedItemStore->setNotificationTimestampsForUser(
115 $user,
116 $timestamp
117 );
118 }
119
120 $result['notificationtimestamp'] = is_null( $timestamp )
121 ? ''
122 : wfTimestamp( TS_ISO_8601, $timestamp );
123 } else {
124 // First, log the invalid titles
125 foreach ( $pageSet->getInvalidTitlesAndReasons() as $r ) {
126 $r['invalid'] = true;
127 $result[] = $r;
128 }
129 foreach ( $pageSet->getMissingPageIDs() as $p ) {
130 $page = [];
131 $page['pageid'] = $p;
132 $page['missing'] = true;
133 $page['notwatched'] = true;
134 $result[] = $page;
135 }
136 foreach ( $pageSet->getMissingRevisionIDs() as $r ) {
137 $rev = [];
138 $rev['revid'] = $r;
139 $rev['missing'] = true;
140 $rev['notwatched'] = true;
141 $result[] = $rev;
142 }
143
144 if ( $pageSet->getTitles() ) {
145 // Now process the valid titles
146 $watchedItemStore->setNotificationTimestampsForUser(
147 $user,
148 $timestamp,
149 $pageSet->getTitles()
150 );
151
152 // Query the results of our update
153 $timestamps = $watchedItemStore->getNotificationTimestampsBatch(
154 $user,
155 $pageSet->getTitles()
156 );
157
158 // Now, put the valid titles into the result
159 /** @var Title $title */
160 foreach ( $pageSet->getTitles() as $title ) {
161 $ns = $title->getNamespace();
162 $dbkey = $title->getDBkey();
163 $r = [
164 'ns' => intval( $ns ),
165 'title' => $title->getPrefixedText(),
166 ];
167 if ( !$title->exists() ) {
168 $r['missing'] = true;
169 if ( $title->isKnown() ) {
170 $r['known'] = true;
171 }
172 }
173 if ( isset( $timestamps[$ns] ) && array_key_exists( $dbkey, $timestamps[$ns] ) ) {
174 $r['notificationtimestamp'] = '';
175 if ( $timestamps[$ns][$dbkey] !== null ) {
176 $r['notificationtimestamp'] = wfTimestamp( TS_ISO_8601, $timestamps[$ns][$dbkey] );
177 }
178 } else {
179 $r['notwatched'] = true;
180 }
181 $result[] = $r;
182 }
183 }
184
185 ApiResult::setIndexedTagName( $result, 'page' );
186 }
187 $apiResult->addValue( null, $this->getModuleName(), $result );
188
189 $this->setContinuationManager( null );
190 $continuationManager->setContinuationIntoResult( $apiResult );
191 }
192
193 /**
194 * Get a cached instance of an ApiPageSet object
195 * @return ApiPageSet
196 */
197 private function getPageSet() {
198 if ( $this->mPageSet === null ) {
199 $this->mPageSet = new ApiPageSet( $this );
200 }
201
202 return $this->mPageSet;
203 }
204
205 public function mustBePosted() {
206 return true;
207 }
208
209 public function isWriteMode() {
210 return true;
211 }
212
213 public function needsToken() {
214 return 'csrf';
215 }
216
217 public function getAllowedParams( $flags = 0 ) {
218 $result = [
219 'entirewatchlist' => [
220 ApiBase::PARAM_TYPE => 'boolean'
221 ],
222 'timestamp' => [
223 ApiBase::PARAM_TYPE => 'timestamp'
224 ],
225 'torevid' => [
226 ApiBase::PARAM_TYPE => 'integer'
227 ],
228 'newerthanrevid' => [
229 ApiBase::PARAM_TYPE => 'integer'
230 ],
231 'continue' => [
232 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
233 ],
234 ];
235 if ( $flags ) {
236 $result += $this->getPageSet()->getFinalParams( $flags );
237 }
238
239 return $result;
240 }
241
242 protected function getExamplesMessages() {
243 return [
244 'action=setnotificationtimestamp&entirewatchlist=&token=123ABC'
245 => 'apihelp-setnotificationtimestamp-example-all',
246 'action=setnotificationtimestamp&titles=Main_page&token=123ABC'
247 => 'apihelp-setnotificationtimestamp-example-page',
248 'action=setnotificationtimestamp&titles=Main_page&' .
249 'timestamp=2012-01-01T00:00:00Z&token=123ABC'
250 => 'apihelp-setnotificationtimestamp-example-pagetimestamp',
251 'action=setnotificationtimestamp&generator=allpages&gapnamespace=2&token=123ABC'
252 => 'apihelp-setnotificationtimestamp-example-allpages',
253 ];
254 }
255
256 public function getHelpUrls() {
257 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetNotificationTimestamp';
258 }
259 }