5 * Created on Jan 4, 2008
7 * Copyright © 2008 Yuri Astrakhan "<Firstname><Lastname>@gmail.com",
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * API module to allow users to watch a page
32 class ApiWatch
extends ApiBase
{
33 private $mPageSet = null;
35 public function execute() {
36 $user = $this->getUser();
37 if ( !$user->isLoggedIn() ) {
38 $this->dieWithError( 'watchlistanontext', 'notloggedin' );
41 $this->checkUserRightsAny( 'editmywatchlist' );
43 $params = $this->extractRequestParams();
45 $continuationManager = new ApiContinuationManager( $this, [], [] );
46 $this->setContinuationManager( $continuationManager );
48 $pageSet = $this->getPageSet();
49 // by default we use pageset to extract the page to work on.
50 // title is still supported for backward compatibility
51 if ( !isset( $params['title'] ) ) {
53 $res = $pageSet->getInvalidTitlesAndRevisions( [
61 foreach ( $pageSet->getMissingTitles() as $title ) {
62 $r = $this->watchTitle( $title, $user, $params );
67 foreach ( $pageSet->getGoodTitles() as $title ) {
68 $r = $this->watchTitle( $title, $user, $params );
71 ApiResult
::setIndexedTagName( $res, 'w' );
73 // dont allow use of old title parameter with new pageset parameters.
74 $extraParams = array_keys( array_filter( $pageSet->extractRequestParams(), function ( $x ) {
75 return $x !== null && $x !== false;
81 'apierror-invalidparammix-cannotusewith',
82 $this->encodeParamName( 'title' ),
83 $pageSet->encodeParamName( $extraParams[0] )
89 $title = Title
::newFromText( $params['title'] );
90 if ( !$title ||
!$title->isWatchable() ) {
91 $this->dieWithError( [ 'invalidtitle', $params['title'] ] );
93 $res = $this->watchTitle( $title, $user, $params, true );
95 $this->getResult()->addValue( null, $this->getModuleName(), $res );
97 $this->setContinuationManager( null );
98 $continuationManager->setContinuationIntoResult( $this->getResult() );
101 private function watchTitle( Title
$title, User
$user, array $params,
102 $compatibilityMode = false
104 if ( !$title->isWatchable() ) {
105 return [ 'title' => $title->getPrefixedText(), 'watchable' => 0 ];
108 $res = [ 'title' => $title->getPrefixedText() ];
110 if ( $params['unwatch'] ) {
111 $status = UnwatchAction
::doUnwatch( $title, $user );
112 $res['unwatched'] = $status->isOK();
113 if ( $status->isOK() ) {
114 $msgKey = $title->isTalkPage() ?
'removedwatchtext-talk' : 'removedwatchtext';
115 $res['message'] = $this->msg( $msgKey, $title->getPrefixedText() )
116 ->title( $title )->parseAsBlock();
119 $status = WatchAction
::doWatch( $title, $user );
120 $res['watched'] = $status->isOK();
121 if ( $status->isOK() ) {
122 $msgKey = $title->isTalkPage() ?
'addedwatchtext-talk' : 'addedwatchtext';
123 $res['message'] = $this->msg( $msgKey, $title->getPrefixedText() )
124 ->title( $title )->parseAsBlock();
128 if ( !$status->isOK() ) {
129 if ( $compatibilityMode ) {
130 $this->dieStatus( $status );
132 $res['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
133 $res['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
134 if ( !$res['warnings'] ) {
135 unset( $res['warnings'] );
143 * Get a cached instance of an ApiPageSet object
146 private function getPageSet() {
147 if ( $this->mPageSet
=== null ) {
148 $this->mPageSet
= new ApiPageSet( $this );
151 return $this->mPageSet
;
154 public function mustBePosted() {
158 public function isWriteMode() {
162 public function needsToken() {
166 public function getAllowedParams( $flags = 0 ) {
169 ApiBase
::PARAM_TYPE
=> 'string',
170 ApiBase
::PARAM_DEPRECATED
=> true
174 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
178 $result +
= $this->getPageSet()->getFinalParams( $flags );
184 protected function getExamplesMessages() {
186 'action=watch&titles=Main_Page&token=123ABC'
187 => 'apihelp-watch-example-watch',
188 'action=watch&titles=Main_Page&unwatch=&token=123ABC'
189 => 'apihelp-watch-example-unwatch',
190 'action=watch&generator=allpages&gapnamespace=0&token=123ABC'
191 => 'apihelp-watch-example-generator',
195 public function getHelpUrls() {
196 return 'https://www.mediawiki.org/wiki/API:Watch';