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