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
24 * API module to allow users to watch a page
28 class ApiWatch
extends ApiBase
{
29 private $mPageSet = null;
31 public function execute() {
32 $user = $this->getUser();
33 if ( !$user->isLoggedIn() ) {
34 $this->dieWithError( 'watchlistanontext', 'notloggedin' );
37 $this->checkUserRightsAny( 'editmywatchlist' );
39 $params = $this->extractRequestParams();
41 $continuationManager = new ApiContinuationManager( $this, [], [] );
42 $this->setContinuationManager( $continuationManager );
44 $pageSet = $this->getPageSet();
45 // by default we use pageset to extract the page to work on.
46 // title is still supported for backward compatibility
47 if ( !isset( $params['title'] ) ) {
49 $res = $pageSet->getInvalidTitlesAndRevisions( [
57 foreach ( $pageSet->getMissingTitles() as $title ) {
58 $r = $this->watchTitle( $title, $user, $params );
63 foreach ( $pageSet->getGoodTitles() as $title ) {
64 $r = $this->watchTitle( $title, $user, $params );
67 ApiResult
::setIndexedTagName( $res, 'w' );
69 // dont allow use of old title parameter with new pageset parameters.
70 $extraParams = array_keys( array_filter( $pageSet->extractRequestParams(), function ( $x ) {
71 return $x !== null && $x !== false;
77 'apierror-invalidparammix-cannotusewith',
78 $this->encodeParamName( 'title' ),
79 $pageSet->encodeParamName( $extraParams[0] )
85 $title = Title
::newFromText( $params['title'] );
86 if ( !$title ||
!$title->isWatchable() ) {
87 $this->dieWithError( [ 'invalidtitle', $params['title'] ] );
89 $res = $this->watchTitle( $title, $user, $params, true );
91 $this->getResult()->addValue( null, $this->getModuleName(), $res );
93 $this->setContinuationManager( null );
94 $continuationManager->setContinuationIntoResult( $this->getResult() );
97 private function watchTitle( Title
$title, User
$user, array $params,
98 $compatibilityMode = false
100 if ( !$title->isWatchable() ) {
101 return [ 'title' => $title->getPrefixedText(), 'watchable' => 0 ];
104 $res = [ 'title' => $title->getPrefixedText() ];
106 if ( $params['unwatch'] ) {
107 $status = UnwatchAction
::doUnwatch( $title, $user );
108 $res['unwatched'] = $status->isOK();
110 $status = WatchAction
::doWatch( $title, $user );
111 $res['watched'] = $status->isOK();
114 if ( !$status->isOK() ) {
115 if ( $compatibilityMode ) {
116 $this->dieStatus( $status );
118 $res['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
119 $res['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
120 if ( !$res['warnings'] ) {
121 unset( $res['warnings'] );
129 * Get a cached instance of an ApiPageSet object
132 private function getPageSet() {
133 if ( $this->mPageSet
=== null ) {
134 $this->mPageSet
= new ApiPageSet( $this );
137 return $this->mPageSet
;
140 public function mustBePosted() {
144 public function isWriteMode() {
148 public function needsToken() {
152 public function getAllowedParams( $flags = 0 ) {
155 ApiBase
::PARAM_TYPE
=> 'string',
156 ApiBase
::PARAM_DEPRECATED
=> true
160 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
164 $result +
= $this->getPageSet()->getFinalParams( $flags );
170 protected function getExamplesMessages() {
172 'action=watch&titles=Main_Page&token=123ABC'
173 => 'apihelp-watch-example-watch',
174 'action=watch&titles=Main_Page&unwatch=&token=123ABC'
175 => 'apihelp-watch-example-unwatch',
176 'action=watch&generator=allpages&gapnamespace=0&token=123ABC'
177 => 'apihelp-watch-example-generator',
181 public function getHelpUrls() {
182 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watch';