Merge "Add a class to interlanguage links"
[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 execute() {
35 $user = $this->getUser();
36 if ( !$user->isLoggedIn() ) {
37 $this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' );
38 }
39 if ( !$user->isAllowed( 'editmywatchlist' ) ) {
40 $this->dieUsage( 'You don\'t have permission to edit your watchlist', 'permissiondenied' );
41 }
42
43 $params = $this->extractRequestParams();
44 $title = Title::newFromText( $params['title'] );
45
46 if ( !$title || $title->isExternal() || !$title->canExist() ) {
47 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
48 }
49
50 $res = array( 'title' => $title->getPrefixedText() );
51
52 // Currently unnecessary, code to act as a safeguard against any change
53 // in current behavior of uselang.
54 // Copy from ApiParse
55 $oldLang = null;
56 if ( isset( $params['uselang'] ) &&
57 $params['uselang'] != $this->getContext()->getLanguage()->getCode()
58 ) {
59 $oldLang = $this->getContext()->getLanguage(); // Backup language
60 $this->getContext()->setLanguage( Language::factory( $params['uselang'] ) );
61 }
62
63 if ( $params['unwatch'] ) {
64 $res['unwatched'] = '';
65 $res['message'] = $this->msg( 'removedwatchtext', $title->getPrefixedText() )
66 ->title( $title )->parseAsBlock();
67 $status = UnwatchAction::doUnwatch( $title, $user );
68 } else {
69 $res['watched'] = '';
70 $res['message'] = $this->msg( 'addedwatchtext', $title->getPrefixedText() )
71 ->title( $title )->parseAsBlock();
72 $status = WatchAction::doWatch( $title, $user );
73 }
74
75 if ( !is_null( $oldLang ) ) {
76 $this->getContext()->setLanguage( $oldLang ); // Reset language to $oldLang
77 }
78
79 if ( !$status->isOK() ) {
80 $this->dieStatus( $status );
81 }
82 $this->getResult()->addValue( null, $this->getModuleName(), $res );
83 }
84
85 public function mustBePosted() {
86 return true;
87 }
88
89 public function isWriteMode() {
90 return true;
91 }
92
93 public function needsToken() {
94 return true;
95 }
96
97 public function getTokenSalt() {
98 return 'watch';
99 }
100
101 public function getAllowedParams() {
102 return array(
103 'title' => array(
104 ApiBase::PARAM_TYPE => 'string',
105 ApiBase::PARAM_REQUIRED => true
106 ),
107 'unwatch' => false,
108 'uselang' => null,
109 'token' => array(
110 ApiBase::PARAM_TYPE => 'string',
111 ApiBase::PARAM_REQUIRED => true
112 ),
113 );
114 }
115
116 public function getParamDescription() {
117 return array(
118 'title' => 'The page to (un)watch',
119 'unwatch' => 'If set the page will be unwatched rather than watched',
120 'uselang' => 'Language to show the message in',
121 'token' => 'A token previously acquired via prop=info',
122 );
123 }
124
125 public function getResultProperties() {
126 return array(
127 '' => array(
128 'title' => 'string',
129 'unwatched' => 'boolean',
130 'watched' => 'boolean',
131 'message' => 'string'
132 )
133 );
134 }
135
136 public function getDescription() {
137 return 'Add or remove a page from/to the current user\'s watchlist';
138 }
139
140 public function getPossibleErrors() {
141 return array_merge( parent::getPossibleErrors(), array(
142 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
143 array( 'invalidtitle', 'title' ),
144 array( 'hookaborted' ),
145 ) );
146 }
147
148 public function getExamples() {
149 return array(
150 'api.php?action=watch&title=Main_Page' => 'Watch the page "Main Page"',
151 'api.php?action=watch&title=Main_Page&unwatch=' => 'Unwatch the page "Main Page"',
152 );
153 }
154
155 public function getHelpUrls() {
156 return 'https://www.mediawiki.org/wiki/API:Watch';
157 }
158 }