Merge "Revert "AJAXify watchlist editor""
[lhc/web/wiklou.git] / includes / api / ApiWatch.php
1 <?php
2 /**
3 *
4 *
5 * Created on Jan 4, 2008
6 *
7 * Copyright © 2008 Yuri Astrakhan "<Firstname><Lastname>@gmail.com",
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * API module to allow users to watch a page
29 *
30 * @ingroup API
31 */
32 class ApiWatch extends ApiBase {
33
34 public function __construct( $main, $action ) {
35 parent::__construct( $main, $action );
36 }
37
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' );
42 }
43
44 $params = $this->extractRequestParams();
45 $title = Title::newFromText( $params['title'] );
46
47 if ( !$title || $title->getNamespace() < 0 ) {
48 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
49 }
50
51 $res = array( 'title' => $title->getPrefixedText() );
52
53 if ( $params['unwatch'] ) {
54 $res['unwatched'] = '';
55 $res['message'] = $this->msg( 'removedwatchtext', $title->getPrefixedText() )->title( $title )->parseAsBlock();
56 $success = UnwatchAction::doUnwatch( $title, $user );
57 } else {
58 $res['watched'] = '';
59 $res['message'] = $this->msg( 'addedwatchtext', $title->getPrefixedText() )->title( $title )->parseAsBlock();
60 $success = WatchAction::doWatch( $title, $user );
61 }
62 if ( !$success ) {
63 $this->dieUsageMsg( 'hookaborted' );
64 }
65 $this->getResult()->addValue( null, $this->getModuleName(), $res );
66 }
67
68 public function mustBePosted() {
69 return true;
70 }
71
72 public function isWriteMode() {
73 return true;
74 }
75
76 public function needsToken() {
77 return true;
78 }
79
80 public function getTokenSalt() {
81 return 'watch';
82 }
83
84 public function getAllowedParams() {
85 return array(
86 'title' => array(
87 ApiBase::PARAM_TYPE => 'string',
88 ApiBase::PARAM_REQUIRED => true
89 ),
90 'unwatch' => false,
91 'token' => array(
92 ApiBase::PARAM_TYPE => 'string',
93 ApiBase::PARAM_REQUIRED => true
94 ),
95 );
96 }
97
98 public function getParamDescription() {
99 return array(
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',
103 );
104 }
105
106 public function getResultProperties() {
107 return array(
108 '' => array(
109 'title' => 'string',
110 'unwatched' => 'boolean',
111 'watched' => 'boolean',
112 'message' => 'string'
113 )
114 );
115 }
116
117 public function getDescription() {
118 return 'Add or remove a page from/to the current user\'s watchlist';
119 }
120
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' ),
126 ) );
127 }
128
129 public function getExamples() {
130 return array(
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"',
133 );
134 }
135
136 public function getHelpUrls() {
137 return 'https://www.mediawiki.org/wiki/API:Watch';
138 }
139
140 public function getVersion() {
141 return __CLASS__ . ': $Id$';
142 }
143 }