Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiWatch.php
blobacb071a6a846992d5233f68f2acf87d565af5dc4
1 <?php
2 /**
3 * Copyright © 2008 Yuri Astrakhan "<Firstname><Lastname>@gmail.com",
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 namespace MediaWiki\Api;
25 use MediaWiki\MainConfigNames;
26 use MediaWiki\Page\PageIdentity;
27 use MediaWiki\Title\Title;
28 use MediaWiki\Title\TitleFormatter;
29 use MediaWiki\User\User;
30 use MediaWiki\Watchlist\WatchlistManager;
31 use Wikimedia\ParamValidator\ParamValidator;
32 use Wikimedia\ParamValidator\TypeDef\ExpiryDef;
34 /**
35 * API module to allow users to watch a page
37 * @ingroup API
39 class ApiWatch extends ApiBase {
40 /** @var ApiPageSet|null */
41 private $mPageSet = null;
43 /** @var bool Whether watchlist expiries are enabled. */
44 private $expiryEnabled;
46 /** @var string Relative maximum expiry. */
47 private $maxDuration;
49 protected WatchlistManager $watchlistManager;
50 private TitleFormatter $titleFormatter;
52 public function __construct(
53 ApiMain $mainModule,
54 string $moduleName,
55 WatchlistManager $watchlistManager,
56 TitleFormatter $titleFormatter
57 ) {
58 parent::__construct( $mainModule, $moduleName );
60 $this->watchlistManager = $watchlistManager;
61 $this->titleFormatter = $titleFormatter;
62 $this->expiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry );
63 $this->maxDuration = $this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
66 public function execute() {
67 $user = $this->getUser();
68 if ( !$user->isRegistered()
69 || ( $user->isTemp() && !$user->isAllowed( 'editmywatchlist' ) )
70 ) {
71 $this->dieWithError( 'watchlistanontext', 'notloggedin' );
74 $this->checkUserRightsAny( 'editmywatchlist' );
76 $params = $this->extractRequestParams();
78 $continuationManager = new ApiContinuationManager( $this, [], [] );
79 $this->setContinuationManager( $continuationManager );
81 $pageSet = $this->getPageSet();
82 // by default we use pageset to extract the page to work on.
83 // title is still supported for backward compatibility
84 if ( !isset( $params['title'] ) ) {
85 $pageSet->execute();
86 $res = $pageSet->getInvalidTitlesAndRevisions( [
87 'invalidTitles',
88 'special',
89 'missingIds',
90 'missingRevIds',
91 'interwikiTitles'
92 ] );
94 foreach ( $pageSet->getMissingPages() as $page ) {
95 $r = $this->watchTitle( $page, $user, $params );
96 $r['missing'] = true;
97 $res[] = $r;
100 foreach ( $pageSet->getGoodPages() as $page ) {
101 $r = $this->watchTitle( $page, $user, $params );
102 $res[] = $r;
104 ApiResult::setIndexedTagName( $res, 'w' );
105 } else {
106 // dont allow use of old title parameter with new pageset parameters.
107 $extraParams = array_keys( array_filter( $pageSet->extractRequestParams(), static function ( $x ) {
108 return $x !== null && $x !== false;
109 } ) );
111 if ( $extraParams ) {
112 $this->dieWithError(
114 'apierror-invalidparammix-cannotusewith',
115 $this->encodeParamName( 'title' ),
116 $pageSet->encodeParamName( $extraParams[0] )
118 'invalidparammix'
122 $title = Title::newFromText( $params['title'] );
123 if ( !$title || !$this->watchlistManager->isWatchable( $title ) ) {
124 $this->dieWithError( [ 'invalidtitle', $params['title'] ] );
126 $res = $this->watchTitle( $title, $user, $params, true );
128 $this->getResult()->addValue( null, $this->getModuleName(), $res );
130 $this->setContinuationManager( null );
131 $continuationManager->setContinuationIntoResult( $this->getResult() );
134 private function watchTitle( PageIdentity $page, User $user, array $params,
135 $compatibilityMode = false
137 $res = [ 'title' => $this->titleFormatter->getPrefixedText( $page ), 'ns' => $page->getNamespace() ];
139 if ( !$this->watchlistManager->isWatchable( $page ) ) {
140 $res['watchable'] = 0;
141 return $res;
144 if ( $params['unwatch'] ) {
145 $status = $this->watchlistManager->removeWatch( $user, $page );
146 $res['unwatched'] = $status->isOK();
147 } else {
148 $expiry = null;
150 // NOTE: If an expiry parameter isn't given, any existing expiries remain unchanged.
151 if ( $this->expiryEnabled && isset( $params['expiry'] ) ) {
152 $expiry = $params['expiry'];
153 $res['expiry'] = ApiResult::formatExpiry( $expiry );
156 $status = $this->watchlistManager->addWatch( $user, $page, $expiry );
157 $res['watched'] = $status->isOK();
160 if ( !$status->isOK() ) {
161 if ( $compatibilityMode ) {
162 $this->dieStatus( $status );
164 $res['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
165 $res['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
166 if ( !$res['warnings'] ) {
167 unset( $res['warnings'] );
171 return $res;
175 * Get a cached instance of an ApiPageSet object
176 * @return ApiPageSet
178 private function getPageSet() {
179 $this->mPageSet ??= new ApiPageSet( $this );
181 return $this->mPageSet;
184 public function mustBePosted() {
185 return true;
188 public function isWriteMode() {
189 return true;
192 public function needsToken() {
193 return 'watch';
196 public function getAllowedParams( $flags = 0 ) {
197 $result = [
198 'title' => [
199 ParamValidator::PARAM_TYPE => 'string',
200 ParamValidator::PARAM_DEPRECATED => true,
202 'expiry' => [
203 ParamValidator::PARAM_TYPE => 'expiry',
204 ExpiryDef::PARAM_MAX => $this->maxDuration,
205 ExpiryDef::PARAM_USE_MAX => true,
207 'unwatch' => false,
208 'continue' => [
209 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
213 // If expiry is not enabled, don't accept the parameter.
214 if ( !$this->expiryEnabled ) {
215 unset( $result['expiry'] );
218 if ( $flags ) {
219 $result += $this->getPageSet()->getFinalParams( $flags );
222 return $result;
225 protected function getExamplesMessages() {
226 $title = Title::newMainPage()->getPrefixedText();
227 $mp = rawurlencode( $title );
229 // Logically expiry example should go before unwatch examples.
230 $examples = [
231 "action=watch&titles={$mp}&token=123ABC"
232 => 'apihelp-watch-example-watch',
234 if ( $this->expiryEnabled ) {
235 $examples["action=watch&titles={$mp}|Foo|Bar&expiry=1%20month&token=123ABC"]
236 = 'apihelp-watch-example-watch-expiry';
239 return array_merge( $examples, [
240 "action=watch&titles={$mp}&unwatch=&token=123ABC"
241 => 'apihelp-watch-example-unwatch',
242 'action=watch&generator=allpages&gapnamespace=0&token=123ABC'
243 => 'apihelp-watch-example-generator',
244 ] );
247 public function getHelpUrls() {
248 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watch';
252 /** @deprecated class alias since 1.43 */
253 class_alias( ApiWatch::class, 'ApiWatch' );