Merge "Warn if stateful ParserOutput transforms are used"
[lhc/web/wiklou.git] / includes / api / ApiWatch.php
1 <?php
2 /**
3 * Copyright © 2008 Yuri Astrakhan "<Firstname><Lastname>@gmail.com",
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * API module to allow users to watch a page
25 *
26 * @ingroup API
27 */
28 class ApiWatch extends ApiBase {
29 private $mPageSet = null;
30
31 public function execute() {
32 $user = $this->getUser();
33 if ( !$user->isLoggedIn() ) {
34 $this->dieWithError( 'watchlistanontext', 'notloggedin' );
35 }
36
37 $this->checkUserRightsAny( 'editmywatchlist' );
38
39 $params = $this->extractRequestParams();
40
41 $continuationManager = new ApiContinuationManager( $this, [], [] );
42 $this->setContinuationManager( $continuationManager );
43
44 $pageSet = $this->getPageSet();
45 // by default we use pageset to extract the page to work on.
46 // title is still supported for backward compatibility
47 if ( !isset( $params['title'] ) ) {
48 $pageSet->execute();
49 $res = $pageSet->getInvalidTitlesAndRevisions( [
50 'invalidTitles',
51 'special',
52 'missingIds',
53 'missingRevIds',
54 'interwikiTitles'
55 ] );
56
57 foreach ( $pageSet->getMissingTitles() as $title ) {
58 $r = $this->watchTitle( $title, $user, $params );
59 $r['missing'] = true;
60 $res[] = $r;
61 }
62
63 foreach ( $pageSet->getGoodTitles() as $title ) {
64 $r = $this->watchTitle( $title, $user, $params );
65 $res[] = $r;
66 }
67 ApiResult::setIndexedTagName( $res, 'w' );
68 } else {
69 // dont allow use of old title parameter with new pageset parameters.
70 $extraParams = array_keys( array_filter( $pageSet->extractRequestParams(), function ( $x ) {
71 return $x !== null && $x !== false;
72 } ) );
73
74 if ( $extraParams ) {
75 $this->dieWithError(
76 [
77 'apierror-invalidparammix-cannotusewith',
78 $this->encodeParamName( 'title' ),
79 $pageSet->encodeParamName( $extraParams[0] )
80 ],
81 'invalidparammix'
82 );
83 }
84
85 $title = Title::newFromText( $params['title'] );
86 if ( !$title || !$title->isWatchable() ) {
87 $this->dieWithError( [ 'invalidtitle', $params['title'] ] );
88 }
89 $res = $this->watchTitle( $title, $user, $params, true );
90 }
91 $this->getResult()->addValue( null, $this->getModuleName(), $res );
92
93 $this->setContinuationManager( null );
94 $continuationManager->setContinuationIntoResult( $this->getResult() );
95 }
96
97 private function watchTitle( Title $title, User $user, array $params,
98 $compatibilityMode = false
99 ) {
100 if ( !$title->isWatchable() ) {
101 return [ 'title' => $title->getPrefixedText(), 'watchable' => 0 ];
102 }
103
104 $res = [ 'title' => $title->getPrefixedText() ];
105
106 if ( $params['unwatch'] ) {
107 $status = UnwatchAction::doUnwatch( $title, $user );
108 $res['unwatched'] = $status->isOK();
109 } else {
110 $status = WatchAction::doWatch( $title, $user );
111 $res['watched'] = $status->isOK();
112 }
113
114 if ( !$status->isOK() ) {
115 if ( $compatibilityMode ) {
116 $this->dieStatus( $status );
117 }
118 $res['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
119 $res['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
120 if ( !$res['warnings'] ) {
121 unset( $res['warnings'] );
122 }
123 }
124
125 return $res;
126 }
127
128 /**
129 * Get a cached instance of an ApiPageSet object
130 * @return ApiPageSet
131 */
132 private function getPageSet() {
133 if ( $this->mPageSet === null ) {
134 $this->mPageSet = new ApiPageSet( $this );
135 }
136
137 return $this->mPageSet;
138 }
139
140 public function mustBePosted() {
141 return true;
142 }
143
144 public function isWriteMode() {
145 return true;
146 }
147
148 public function needsToken() {
149 return 'watch';
150 }
151
152 public function getAllowedParams( $flags = 0 ) {
153 $result = [
154 'title' => [
155 ApiBase::PARAM_TYPE => 'string',
156 ApiBase::PARAM_DEPRECATED => true
157 ],
158 'unwatch' => false,
159 'continue' => [
160 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
161 ],
162 ];
163 if ( $flags ) {
164 $result += $this->getPageSet()->getFinalParams( $flags );
165 }
166
167 return $result;
168 }
169
170 protected function getExamplesMessages() {
171 return [
172 'action=watch&titles=Main_Page&token=123ABC'
173 => 'apihelp-watch-example-watch',
174 'action=watch&titles=Main_Page&unwatch=&token=123ABC'
175 => 'apihelp-watch-example-unwatch',
176 'action=watch&generator=allpages&gapnamespace=0&token=123ABC'
177 => 'apihelp-watch-example-generator',
178 ];
179 }
180
181 public function getHelpUrls() {
182 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watch';
183 }
184 }