Merge "Remove parameter 'options' from hook 'SkinEditSectionLinks'"
[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 // 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 );
83 if ( $timestamp ) {
84 $timestamp = $dbw->timestamp( $timestamp );
85 } else {
86 $timestamp = null;
87 }
88 }
89 } elseif ( isset( $params['newerthanrevid'] ) ) {
90 if ( $params['entirewatchlist'] || $pageSet->getGoodTitleCount() > 1 ) {
91 $this->dieWithError( [ 'apierror-multpages', $this->encodeParamName( 'newerthanrevid' ) ] );
92 }
93 $titles = $pageSet->getGoodTitles();
94 $title = reset( $titles );
95 if ( $title ) {
96 $revid = $title->getNextRevisionID( $params['newerthanrevid'], Title::GAID_FOR_UPDATE );
97 if ( $revid ) {
98 $timestamp = $dbw->timestamp(
99 MediaWikiServices::getInstance()->getRevisionStore()->getTimestampFromId( $title, $revid )
100 );
101 } else {
102 $timestamp = null;
103 }
104 }
105 }
106
107 $watchedItemStore = MediaWikiServices::getInstance()->getWatchedItemStore();
108 $apiResult = $this->getResult();
109 $result = [];
110 if ( $params['entirewatchlist'] ) {
111 // Entire watchlist mode: Just update the thing and return a success indicator
112 $watchedItemStore->resetAllNotificationTimestampsForUser( $user, $timestamp );
113
114 $result['notificationtimestamp'] = is_null( $timestamp )
115 ? ''
116 : wfTimestamp( TS_ISO_8601, $timestamp );
117 } else {
118 // First, log the invalid titles
119 foreach ( $pageSet->getInvalidTitlesAndReasons() as $r ) {
120 $r['invalid'] = true;
121 $result[] = $r;
122 }
123 foreach ( $pageSet->getMissingPageIDs() as $p ) {
124 $page = [];
125 $page['pageid'] = $p;
126 $page['missing'] = true;
127 $page['notwatched'] = true;
128 $result[] = $page;
129 }
130 foreach ( $pageSet->getMissingRevisionIDs() as $r ) {
131 $rev = [];
132 $rev['revid'] = $r;
133 $rev['missing'] = true;
134 $rev['notwatched'] = true;
135 $result[] = $rev;
136 }
137
138 if ( $pageSet->getTitles() ) {
139 // Now process the valid titles
140 $watchedItemStore->setNotificationTimestampsForUser(
141 $user,
142 $timestamp,
143 $pageSet->getTitles()
144 );
145
146 // Query the results of our update
147 $timestamps = $watchedItemStore->getNotificationTimestampsBatch(
148 $user,
149 $pageSet->getTitles()
150 );
151
152 // Now, put the valid titles into the result
153 /** @var Title $title */
154 foreach ( $pageSet->getTitles() as $title ) {
155 $ns = $title->getNamespace();
156 $dbkey = $title->getDBkey();
157 $r = [
158 'ns' => (int)$ns,
159 'title' => $title->getPrefixedText(),
160 ];
161 if ( !$title->exists() ) {
162 $r['missing'] = true;
163 if ( $title->isKnown() ) {
164 $r['known'] = true;
165 }
166 }
167 if ( isset( $timestamps[$ns] ) && array_key_exists( $dbkey, $timestamps[$ns] ) ) {
168 $r['notificationtimestamp'] = '';
169 if ( $timestamps[$ns][$dbkey] !== null ) {
170 $r['notificationtimestamp'] = wfTimestamp( TS_ISO_8601, $timestamps[$ns][$dbkey] );
171 }
172 } else {
173 $r['notwatched'] = true;
174 }
175 $result[] = $r;
176 }
177 }
178
179 ApiResult::setIndexedTagName( $result, 'page' );
180 }
181 $apiResult->addValue( null, $this->getModuleName(), $result );
182
183 $this->setContinuationManager( null );
184 $continuationManager->setContinuationIntoResult( $apiResult );
185 }
186
187 /**
188 * Get a cached instance of an ApiPageSet object
189 * @return ApiPageSet
190 */
191 private function getPageSet() {
192 if ( $this->mPageSet === null ) {
193 $this->mPageSet = new ApiPageSet( $this );
194 }
195
196 return $this->mPageSet;
197 }
198
199 public function mustBePosted() {
200 return true;
201 }
202
203 public function isWriteMode() {
204 return true;
205 }
206
207 public function needsToken() {
208 return 'csrf';
209 }
210
211 public function getAllowedParams( $flags = 0 ) {
212 $result = [
213 'entirewatchlist' => [
214 ApiBase::PARAM_TYPE => 'boolean'
215 ],
216 'timestamp' => [
217 ApiBase::PARAM_TYPE => 'timestamp'
218 ],
219 'torevid' => [
220 ApiBase::PARAM_TYPE => 'integer'
221 ],
222 'newerthanrevid' => [
223 ApiBase::PARAM_TYPE => 'integer'
224 ],
225 'continue' => [
226 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
227 ],
228 ];
229 if ( $flags ) {
230 $result += $this->getPageSet()->getFinalParams( $flags );
231 }
232
233 return $result;
234 }
235
236 protected function getExamplesMessages() {
237 return [
238 'action=setnotificationtimestamp&entirewatchlist=&token=123ABC'
239 => 'apihelp-setnotificationtimestamp-example-all',
240 'action=setnotificationtimestamp&titles=Main_page&token=123ABC'
241 => 'apihelp-setnotificationtimestamp-example-page',
242 'action=setnotificationtimestamp&titles=Main_page&' .
243 'timestamp=2012-01-01T00:00:00Z&token=123ABC'
244 => 'apihelp-setnotificationtimestamp-example-pagetimestamp',
245 'action=setnotificationtimestamp&generator=allpages&gapnamespace=2&token=123ABC'
246 => 'apihelp-setnotificationtimestamp-example-allpages',
247 ];
248 }
249
250 public function getHelpUrls() {
251 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetNotificationTimestamp';
252 }
253 }