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();
46 $pageSet = $this->getPageSet();
47 // by default we use pageset to extract the page to work on.
48 // title is still supported for backward compatibility
49 if ( !isset( $params['title'] ) ) {
50 if ( $params['entirewatchlist'] && $pageSet->getDataSource() !== null ) {
52 "Cannot use 'entirewatchlist' at the same time as '{$pageSet->getDataSource()}'",
57 if ( $params['entirewatchlist'] ) {
58 if ( !$params['unwatch'] ) {
60 "'entirewatchlist' option can only be used with 'unwatch' parameter.",
64 // We're going to do this in the database as a bulk operation
65 // instead of one at a time, so it doesn't time out on largeish lists.
66 $dbw = wfGetDB( DB_MASTER
);
67 $dbw->delete( 'watchlist', array( 'wl_user' => $user->getId() ), __METHOD__
);
68 $user->invalidateCache();
73 $res = $pageSet->getInvalidTitlesAndRevisions( array(
81 foreach ( $pageSet->getMissingTitles() as $title ) {
82 $r = $this->watchTitle( $title, $user, $params );
87 foreach ( $pageSet->getGoodTitles() as $title ) {
88 $r = $this->watchTitle( $title, $user, $params );
91 $this->getResult()->setIndexedTagName( $res, 'w' );
94 // dont allow use of old title parameter with new pageset parameters.
95 $extraParams = array_keys( array_filter( $pageSet->extractRequestParams(), function ( $x ) {
96 return $x !== null && $x !== false;
100 $p = $this->getModulePrefix();
102 "The parameter {$p}title can not be used with " . implode( ", ", $extraParams ),
107 $title = Title
::newFromText( $params['title'] );
108 if ( !$title ||
!$title->isWatchable() ) {
109 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
111 $res = $this->watchTitle( $title, $user, $params, true );
113 $this->getResult()->addValue( null, $this->getModuleName(), $res );
116 private function watchTitle( Title
$title, User
$user, array $params,
117 $compatibilityMode = false
119 if ( !$title->isWatchable() ) {
120 return array( 'title' => $title->getPrefixedText(), 'watchable' => 0 );
123 $res = array( 'title' => $title->getPrefixedText() );
125 // Currently unnecessary, code to act as a safeguard against any change
126 // in current behavior of uselang.
127 // Copy from ApiParse
129 if ( isset( $params['uselang'] ) &&
130 $params['uselang'] != $this->getContext()->getLanguage()->getCode()
132 $oldLang = $this->getContext()->getLanguage(); // Backup language
133 $this->getContext()->setLanguage( Language
::factory( $params['uselang'] ) );
136 if ( $params['unwatch'] ) {
137 $status = UnwatchAction
::doUnwatch( $title, $user );
138 if ( $status->isOK() ) {
139 $res['unwatched'] = '';
140 $res['message'] = $this->msg( 'removedwatchtext', $title->getPrefixedText() )
141 ->title( $title )->parseAsBlock();
144 $status = WatchAction
::doWatch( $title, $user );
145 if ( $status->isOK() ) {
146 $res['watched'] = '';
147 $res['message'] = $this->msg( 'addedwatchtext', $title->getPrefixedText() )
148 ->title( $title )->parseAsBlock();
152 if ( !is_null( $oldLang ) ) {
153 $this->getContext()->setLanguage( $oldLang ); // Reset language to $oldLang
156 if ( !$status->isOK() ) {
157 if ( $compatibilityMode ) {
158 $this->dieStatus( $status );
160 $res['error'] = $this->getErrorFromStatus( $status );
167 * Get a cached instance of an ApiPageSet object
170 private function getPageSet() {
171 if ( $this->mPageSet
=== null ) {
172 $this->mPageSet
= new ApiPageSet( $this );
175 return $this->mPageSet
;
178 public function mustBePosted() {
182 public function isWriteMode() {
186 public function needsToken() {
190 public function getTokenSalt() {
194 public function getAllowedParams( $flags = 0 ) {
197 ApiBase
::PARAM_TYPE
=> 'string',
198 ApiBase
::PARAM_DEPRECATED
=> true
201 'entirewatchlist' => false,
204 ApiBase
::PARAM_TYPE
=> 'string',
205 ApiBase
::PARAM_REQUIRED
=> true
209 $result +
= $this->getPageSet()->getFinalParams( $flags );
215 public function getParamDescription() {
216 $psModule = $this->getPageSet();
218 return $psModule->getParamDescription() +
array(
219 'title' => 'The page to (un)watch. use titles instead',
220 'unwatch' => 'If set the page will be unwatched rather than watched',
221 'entirewatchlist' => 'Work on all watched pages without returning a list; '
222 . 'can only be used together with \'unwatch\' option.',
223 'uselang' => 'Language to show the message in',
224 'token' => 'A token previously acquired via prop=info',
228 public function getResultProperties() {
232 'unwatched' => 'boolean',
233 'watched' => 'boolean',
234 'message' => 'string'
239 public function getDescription() {
240 return 'Add or remove pages from/to the current user\'s watchlist.';
243 public function getPossibleErrors() {
244 return array_merge( parent
::getPossibleErrors(), array(
245 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
246 array( 'invalidtitle', 'title' ),
247 array( 'hookaborted' ),
251 public function getExamples() {
253 'api.php?action=watch&titles=Main_Page' => 'Watch the page "Main Page"',
254 'api.php?action=watch&titles=Main_Page&unwatch=' => 'Unwatch the page "Main Page"',
258 public function getHelpUrls() {
259 return 'https://www.mediawiki.org/wiki/API:Watch';