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();
114 $status = WatchAction
::doWatch( $title, $user );
115 $res['watched'] = $status->isOK();
118 if ( !$status->isOK() ) {
119 if ( $compatibilityMode ) {
120 $this->dieStatus( $status );
122 $res['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
123 $res['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
124 if ( !$res['warnings'] ) {
125 unset( $res['warnings'] );
133 * Get a cached instance of an ApiPageSet object
136 private function getPageSet() {
137 if ( $this->mPageSet
=== null ) {
138 $this->mPageSet
= new ApiPageSet( $this );
141 return $this->mPageSet
;
144 public function mustBePosted() {
148 public function isWriteMode() {
152 public function needsToken() {
156 public function getAllowedParams( $flags = 0 ) {
159 ApiBase
::PARAM_TYPE
=> 'string',
160 ApiBase
::PARAM_DEPRECATED
=> true
164 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
168 $result +
= $this->getPageSet()->getFinalParams( $flags );
174 protected function getExamplesMessages() {
176 'action=watch&titles=Main_Page&token=123ABC'
177 => 'apihelp-watch-example-watch',
178 'action=watch&titles=Main_Page&unwatch=&token=123ABC'
179 => 'apihelp-watch-example-unwatch',
180 'action=watch&generator=allpages&gapnamespace=0&token=123ABC'
181 => 'apihelp-watch-example-generator',
185 public function getHelpUrls() {
186 return 'https://www.mediawiki.org/wiki/API:Watch';