Merge "Replace Linker::link() with LinkRenderer in all revisiondelete pages"
[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 private $mPageSet = null;
34
35 public function execute() {
36 $user = $this->getUser();
37 if ( !$user->isLoggedIn() ) {
38 $this->dieWithError( 'watchlistanontext', 'notloggedin' );
39 }
40
41 $this->checkUserRightsAny( 'editmywatchlist' );
42
43 $params = $this->extractRequestParams();
44
45 $continuationManager = new ApiContinuationManager( $this, [], [] );
46 $this->setContinuationManager( $continuationManager );
47
48 $pageSet = $this->getPageSet();
49 // by default we use pageset to extract the page to work on.
50 // title is still supported for backward compatibility
51 if ( !isset( $params['title'] ) ) {
52 $pageSet->execute();
53 $res = $pageSet->getInvalidTitlesAndRevisions( [
54 'invalidTitles',
55 'special',
56 'missingIds',
57 'missingRevIds',
58 'interwikiTitles'
59 ] );
60
61 foreach ( $pageSet->getMissingTitles() as $title ) {
62 $r = $this->watchTitle( $title, $user, $params );
63 $r['missing'] = 1;
64 $res[] = $r;
65 }
66
67 foreach ( $pageSet->getGoodTitles() as $title ) {
68 $r = $this->watchTitle( $title, $user, $params );
69 $res[] = $r;
70 }
71 ApiResult::setIndexedTagName( $res, 'w' );
72 } else {
73 // dont allow use of old title parameter with new pageset parameters.
74 $extraParams = array_keys( array_filter( $pageSet->extractRequestParams(), function ( $x ) {
75 return $x !== null && $x !== false;
76 } ) );
77
78 if ( $extraParams ) {
79 $this->dieWithError(
80 [
81 'apierror-invalidparammix-cannotusewith',
82 $this->encodeParamName( 'title' ),
83 $pageSet->encodeParamName( $extraParams[0] )
84 ],
85 'invalidparammix'
86 );
87 }
88
89 $title = Title::newFromText( $params['title'] );
90 if ( !$title || !$title->isWatchable() ) {
91 $this->dieWithError( [ 'invalidtitle', $params['title'] ] );
92 }
93 $res = $this->watchTitle( $title, $user, $params, true );
94 }
95 $this->getResult()->addValue( null, $this->getModuleName(), $res );
96
97 $this->setContinuationManager( null );
98 $continuationManager->setContinuationIntoResult( $this->getResult() );
99 }
100
101 private function watchTitle( Title $title, User $user, array $params,
102 $compatibilityMode = false
103 ) {
104 if ( !$title->isWatchable() ) {
105 return [ 'title' => $title->getPrefixedText(), 'watchable' => 0 ];
106 }
107
108 $res = [ 'title' => $title->getPrefixedText() ];
109
110 if ( $params['unwatch'] ) {
111 $status = UnwatchAction::doUnwatch( $title, $user );
112 $res['unwatched'] = $status->isOK();
113 if ( $status->isOK() ) {
114 $msgKey = $title->isTalkPage() ? 'removedwatchtext-talk' : 'removedwatchtext';
115 $res['message'] = $this->msg( $msgKey, $title->getPrefixedText() )
116 ->title( $title )->parseAsBlock();
117 }
118 } else {
119 $status = WatchAction::doWatch( $title, $user );
120 $res['watched'] = $status->isOK();
121 if ( $status->isOK() ) {
122 $msgKey = $title->isTalkPage() ? 'addedwatchtext-talk' : 'addedwatchtext';
123 $res['message'] = $this->msg( $msgKey, $title->getPrefixedText() )
124 ->title( $title )->parseAsBlock();
125 }
126 }
127
128 if ( !$status->isOK() ) {
129 if ( $compatibilityMode ) {
130 $this->dieStatus( $status );
131 }
132 $res['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
133 $res['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
134 if ( !$res['warnings'] ) {
135 unset( $res['warnings'] );
136 }
137 }
138
139 return $res;
140 }
141
142 /**
143 * Get a cached instance of an ApiPageSet object
144 * @return ApiPageSet
145 */
146 private function getPageSet() {
147 if ( $this->mPageSet === null ) {
148 $this->mPageSet = new ApiPageSet( $this );
149 }
150
151 return $this->mPageSet;
152 }
153
154 public function mustBePosted() {
155 return true;
156 }
157
158 public function isWriteMode() {
159 return true;
160 }
161
162 public function needsToken() {
163 return 'watch';
164 }
165
166 public function getAllowedParams( $flags = 0 ) {
167 $result = [
168 'title' => [
169 ApiBase::PARAM_TYPE => 'string',
170 ApiBase::PARAM_DEPRECATED => true
171 ],
172 'unwatch' => false,
173 'continue' => [
174 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
175 ],
176 ];
177 if ( $flags ) {
178 $result += $this->getPageSet()->getFinalParams( $flags );
179 }
180
181 return $result;
182 }
183
184 protected function getExamplesMessages() {
185 return [
186 'action=watch&titles=Main_Page&token=123ABC'
187 => 'apihelp-watch-example-watch',
188 'action=watch&titles=Main_Page&unwatch=&token=123ABC'
189 => 'apihelp-watch-example-unwatch',
190 'action=watch&generator=allpages&gapnamespace=0&token=123ABC'
191 => 'apihelp-watch-example-generator',
192 ];
193 }
194
195 public function getHelpUrls() {
196 return 'https://www.mediawiki.org/wiki/API:Watch';
197 }
198 }