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