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->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' );
41 if ( !$user->isAllowed( 'editmywatchlist' ) ) {
42 $this->dieUsage( 'You don\'t have permission to edit your watchlist', 'permissiondenied' );
45 $params = $this->extractRequestParams();
47 $this->getResult()->beginContinuation( $params['continue'], array(), array() );
49 $pageSet = $this->getPageSet();
50 // by default we use pageset to extract the page to work on.
51 // title is still supported for backward compatibility
52 if ( !isset( $params['title'] ) ) {
54 $res = $pageSet->getInvalidTitlesAndRevisions( array(
62 foreach ( $pageSet->getMissingTitles() as $title ) {
63 $r = $this->watchTitle( $title, $user, $params );
68 foreach ( $pageSet->getGoodTitles() as $title ) {
69 $r = $this->watchTitle( $title, $user, $params );
72 $this->getResult()->setIndexedTagName( $res, 'w' );
74 // dont allow use of old title parameter with new pageset parameters.
75 $extraParams = array_keys( array_filter( $pageSet->extractRequestParams(), function ( $x ) {
76 return $x !== null && $x !== false;
80 $p = $this->getModulePrefix();
82 "The parameter {$p}title can not be used with " . implode( ", ", $extraParams ),
87 $this->logFeatureUsage( 'action=watch&title' );
88 $title = Title
::newFromText( $params['title'] );
89 if ( !$title ||
!$title->isWatchable() ) {
90 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
92 $res = $this->watchTitle( $title, $user, $params, true );
94 $this->getResult()->addValue( null, $this->getModuleName(), $res );
95 $this->getResult()->endContinuation();
98 private function watchTitle( Title
$title, User
$user, array $params,
99 $compatibilityMode = false
101 if ( !$title->isWatchable() ) {
102 return array( 'title' => $title->getPrefixedText(), 'watchable' => 0 );
105 $res = array( 'title' => $title->getPrefixedText() );
107 if ( $params['unwatch'] ) {
108 $status = UnwatchAction
::doUnwatch( $title, $user );
109 if ( $status->isOK() ) {
110 $res['unwatched'] = '';
111 $res['message'] = $this->msg( 'removedwatchtext', $title->getPrefixedText() )
112 ->title( $title )->parseAsBlock();
115 $status = WatchAction
::doWatch( $title, $user );
116 if ( $status->isOK() ) {
117 $res['watched'] = '';
118 $res['message'] = $this->msg( 'addedwatchtext', $title->getPrefixedText() )
119 ->title( $title )->parseAsBlock();
123 if ( !$status->isOK() ) {
124 if ( $compatibilityMode ) {
125 $this->dieStatus( $status );
127 $res['error'] = $this->getErrorFromStatus( $status );
134 * Get a cached instance of an ApiPageSet object
137 private function getPageSet() {
138 if ( $this->mPageSet
=== null ) {
139 $this->mPageSet
= new ApiPageSet( $this );
142 return $this->mPageSet
;
145 public function mustBePosted() {
149 public function isWriteMode() {
153 public function needsToken() {
157 public function getAllowedParams( $flags = 0 ) {
160 ApiBase
::PARAM_TYPE
=> 'string',
161 ApiBase
::PARAM_DEPRECATED
=> true
165 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
169 $result +
= $this->getPageSet()->getFinalParams( $flags );
175 public function getExamplesMessages() {
177 'action=watch&titles=Main_Page&token=123ABC'
178 => 'apihelp-watch-example-watch',
179 'action=watch&titles=Main_Page&unwatch=&token=123ABC'
180 => 'apihelp-watch-example-unwatch',
181 'action=watch&generator=allpages&gapnamespace=0&token=123ABC'
182 => 'apihelp-watch-example-generator',
186 public function getHelpUrls() {
187 return 'https://www.mediawiki.org/wiki/API:Watch';