Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / api / ApiSetNotificationTimestamp.php
blob5769ff6d3917335ca007988a70b35549203883ac
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
27 use MediaWiki\MediaWikiServices;
29 /**
30 * API interface for setting the wl_notificationtimestamp field
31 * @ingroup API
33 class ApiSetNotificationTimestamp extends ApiBase {
35 private $mPageSet;
37 public function execute() {
38 $user = $this->getUser();
40 if ( $user->isAnon() ) {
41 $this->dieWithError( 'watchlistanontext', 'notloggedin' );
43 $this->checkUserRightsAny( 'editmywatchlist' );
45 $params = $this->extractRequestParams();
46 $this->requireMaxOneParameter( $params, 'timestamp', 'torevid', 'newerthanrevid' );
48 $continuationManager = new ApiContinuationManager( $this, [], [] );
49 $this->setContinuationManager( $continuationManager );
51 $pageSet = $this->getPageSet();
52 if ( $params['entirewatchlist'] && $pageSet->getDataSource() !== null ) {
53 $this->dieWithError(
55 'apierror-invalidparammix-cannotusewith',
56 $this->encodeParamName( 'entirewatchlist' ),
57 $pageSet->encodeParamName( $pageSet->getDataSource() )
59 'multisource'
63 $dbw = wfGetDB( DB_MASTER, 'api' );
65 $timestamp = null;
66 if ( isset( $params['timestamp'] ) ) {
67 $timestamp = $dbw->timestamp( $params['timestamp'] );
70 if ( !$params['entirewatchlist'] ) {
71 $pageSet->execute();
74 if ( isset( $params['torevid'] ) ) {
75 if ( $params['entirewatchlist'] || $pageSet->getGoodTitleCount() > 1 ) {
76 $this->dieWithError( [ 'apierror-multpages', $this->encodeParamName( 'torevid' ) ] );
78 $title = reset( $pageSet->getGoodTitles() );
79 if ( $title ) {
80 $timestamp = Revision::getTimestampFromId(
81 $title, $params['torevid'], Revision::READ_LATEST );
82 if ( $timestamp ) {
83 $timestamp = $dbw->timestamp( $timestamp );
84 } else {
85 $timestamp = null;
88 } elseif ( isset( $params['newerthanrevid'] ) ) {
89 if ( $params['entirewatchlist'] || $pageSet->getGoodTitleCount() > 1 ) {
90 $this->dieWithError( [ 'apierror-multpages', $this->encodeParamName( 'newerthanrevid' ) ] );
92 $title = reset( $pageSet->getGoodTitles() );
93 if ( $title ) {
94 $revid = $title->getNextRevisionID(
95 $params['newerthanrevid'], Title::GAID_FOR_UPDATE );
96 if ( $revid ) {
97 $timestamp = $dbw->timestamp( Revision::getTimestampFromId( $title, $revid ) );
98 } else {
99 $timestamp = null;
104 $watchedItemStore = MediaWikiServices::getInstance()->getWatchedItemStore();
105 $apiResult = $this->getResult();
106 $result = [];
107 if ( $params['entirewatchlist'] ) {
108 // Entire watchlist mode: Just update the thing and return a success indicator
109 $watchedItemStore->setNotificationTimestampsForUser(
110 $user,
111 $timestamp
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;
123 foreach ( $pageSet->getMissingPageIDs() as $p ) {
124 $page = [];
125 $page['pageid'] = $p;
126 $page['missing'] = true;
127 $page['notwatched'] = true;
128 $result[] = $page;
130 foreach ( $pageSet->getMissingRevisionIDs() as $r ) {
131 $rev = [];
132 $rev['revid'] = $r;
133 $rev['missing'] = true;
134 $rev['notwatched'] = true;
135 $result[] = $rev;
138 if ( $pageSet->getTitles() ) {
139 // Now process the valid titles
140 $watchedItemStore->setNotificationTimestampsForUser(
141 $user,
142 $timestamp,
143 $pageSet->getTitles()
146 // Query the results of our update
147 $timestamps = $watchedItemStore->getNotificationTimestampsBatch(
148 $user,
149 $pageSet->getTitles()
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' => intval( $ns ),
159 'title' => $title->getPrefixedText(),
161 if ( !$title->exists() ) {
162 $r['missing'] = true;
163 if ( $title->isKnown() ) {
164 $r['known'] = true;
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] );
172 } else {
173 $r['notwatched'] = true;
175 $result[] = $r;
179 ApiResult::setIndexedTagName( $result, 'page' );
181 $apiResult->addValue( null, $this->getModuleName(), $result );
183 $this->setContinuationManager( null );
184 $continuationManager->setContinuationIntoResult( $apiResult );
188 * Get a cached instance of an ApiPageSet object
189 * @return ApiPageSet
191 private function getPageSet() {
192 if ( !isset( $this->mPageSet ) ) {
193 $this->mPageSet = new ApiPageSet( $this );
196 return $this->mPageSet;
199 public function mustBePosted() {
200 return true;
203 public function isWriteMode() {
204 return true;
207 public function needsToken() {
208 return 'csrf';
211 public function getAllowedParams( $flags = 0 ) {
212 $result = [
213 'entirewatchlist' => [
214 ApiBase::PARAM_TYPE => 'boolean'
216 'timestamp' => [
217 ApiBase::PARAM_TYPE => 'timestamp'
219 'torevid' => [
220 ApiBase::PARAM_TYPE => 'integer'
222 'newerthanrevid' => [
223 ApiBase::PARAM_TYPE => 'integer'
225 'continue' => [
226 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
229 if ( $flags ) {
230 $result += $this->getPageSet()->getFinalParams( $flags );
233 return $result;
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',
250 public function getHelpUrls() {
251 return 'https://www.mediawiki.org/wiki/API:SetNotificationTimestamp';