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
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
;
35 * API module to allow users to watch a page
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. */
49 protected WatchlistManager
$watchlistManager;
50 private TitleFormatter
$titleFormatter;
52 public function __construct(
55 WatchlistManager
$watchlistManager,
56 TitleFormatter
$titleFormatter
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' ) )
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'] ) ) {
86 $res = $pageSet->getInvalidTitlesAndRevisions( [
94 foreach ( $pageSet->getMissingPages() as $page ) {
95 $r = $this->watchTitle( $page, $user, $params );
100 foreach ( $pageSet->getGoodPages() as $page ) {
101 $r = $this->watchTitle( $page, $user, $params );
104 ApiResult
::setIndexedTagName( $res, 'w' );
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;
111 if ( $extraParams ) {
114 'apierror-invalidparammix-cannotusewith',
115 $this->encodeParamName( 'title' ),
116 $pageSet->encodeParamName( $extraParams[0] )
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;
144 if ( $params['unwatch'] ) {
145 $status = $this->watchlistManager
->removeWatch( $user, $page );
146 $res['unwatched'] = $status->isOK();
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'] );
175 * Get a cached instance of an ApiPageSet object
178 private function getPageSet() {
179 $this->mPageSet ??
= new ApiPageSet( $this );
181 return $this->mPageSet
;
184 public function mustBePosted() {
188 public function isWriteMode() {
192 public function needsToken() {
196 public function getAllowedParams( $flags = 0 ) {
199 ParamValidator
::PARAM_TYPE
=> 'string',
200 ParamValidator
::PARAM_DEPRECATED
=> true,
203 ParamValidator
::PARAM_TYPE
=> 'expiry',
204 ExpiryDef
::PARAM_MAX
=> $this->maxDuration
,
205 ExpiryDef
::PARAM_USE_MAX
=> true,
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'] );
219 $result +
= $this->getPageSet()->getFinalParams( $flags );
225 protected function getExamplesMessages() {
226 $title = Title
::newMainPage()->getPrefixedText();
227 $mp = rawurlencode( $title );
229 // Logically expiry example should go before unwatch 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',
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' );