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
{
34 public function __construct( $main, $action ) {
35 parent
::__construct( $main, $action );
38 public function execute() {
39 $user = $this->getUser();
40 if ( !$user->isLoggedIn() ) {
41 $this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' );
44 $params = $this->extractRequestParams();
45 $title = Title
::newFromText( $params['title'] );
47 if ( !$title ||
$title->getNamespace() < 0 ) {
48 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
51 $res = array( 'title' => $title->getPrefixedText() );
53 if ( $params['unwatch'] ) {
54 $res['unwatched'] = '';
55 $res['message'] = $this->msg( 'removedwatchtext', $title->getPrefixedText() )->title( $title )->parseAsBlock();
56 $success = UnwatchAction
::doUnwatch( $title, $user );
59 $res['message'] = $this->msg( 'addedwatchtext', $title->getPrefixedText() )->title( $title )->parseAsBlock();
60 $success = WatchAction
::doWatch( $title, $user );
63 $this->dieUsageMsg( 'hookaborted' );
65 $this->getResult()->addValue( null, $this->getModuleName(), $res );
68 public function mustBePosted() {
72 public function isWriteMode() {
76 public function needsToken() {
80 public function getTokenSalt() {
84 public function getAllowedParams() {
87 ApiBase
::PARAM_TYPE
=> 'string',
88 ApiBase
::PARAM_REQUIRED
=> true
92 ApiBase
::PARAM_TYPE
=> 'string',
93 ApiBase
::PARAM_REQUIRED
=> true
98 public function getParamDescription() {
100 'title' => 'The page to (un)watch',
101 'unwatch' => 'If set the page will be unwatched rather than watched',
102 'token' => 'A token previously acquired via prop=info',
106 public function getResultProperties() {
110 'unwatched' => 'boolean',
111 'watched' => 'boolean',
112 'message' => 'string'
117 public function getDescription() {
118 return 'Add or remove a page from/to the current user\'s watchlist';
121 public function getPossibleErrors() {
122 return array_merge( parent
::getPossibleErrors(), array(
123 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
124 array( 'invalidtitle', 'title' ),
125 array( 'hookaborted' ),
129 public function getExamples() {
131 'api.php?action=watch&title=Main_Page' => 'Watch the page "Main Page"',
132 'api.php?action=watch&title=Main_Page&unwatch=' => 'Unwatch the page "Main Page"',
136 public function getHelpUrls() {
137 return 'https://www.mediawiki.org/wiki/API:Watch';
140 public function getVersion() {
141 return __CLASS__
. ': $Id$';