Merge "Add CollationFa"
[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 } else {
114 $status = WatchAction::doWatch( $title, $user );
115 $res['watched'] = $status->isOK();
116 }
117
118 if ( !$status->isOK() ) {
119 if ( $compatibilityMode ) {
120 $this->dieStatus( $status );
121 }
122 $res['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
123 $res['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
124 if ( !$res['warnings'] ) {
125 unset( $res['warnings'] );
126 }
127 }
128
129 return $res;
130 }
131
132 /**
133 * Get a cached instance of an ApiPageSet object
134 * @return ApiPageSet
135 */
136 private function getPageSet() {
137 if ( $this->mPageSet === null ) {
138 $this->mPageSet = new ApiPageSet( $this );
139 }
140
141 return $this->mPageSet;
142 }
143
144 public function mustBePosted() {
145 return true;
146 }
147
148 public function isWriteMode() {
149 return true;
150 }
151
152 public function needsToken() {
153 return 'watch';
154 }
155
156 public function getAllowedParams( $flags = 0 ) {
157 $result = [
158 'title' => [
159 ApiBase::PARAM_TYPE => 'string',
160 ApiBase::PARAM_DEPRECATED => true
161 ],
162 'unwatch' => false,
163 'continue' => [
164 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
165 ],
166 ];
167 if ( $flags ) {
168 $result += $this->getPageSet()->getFinalParams( $flags );
169 }
170
171 return $result;
172 }
173
174 protected function getExamplesMessages() {
175 return [
176 'action=watch&titles=Main_Page&token=123ABC'
177 => 'apihelp-watch-example-watch',
178 'action=watch&titles=Main_Page&unwatch=&token=123ABC'
179 => 'apihelp-watch-example-unwatch',
180 'action=watch&generator=allpages&gapnamespace=0&token=123ABC'
181 => 'apihelp-watch-example-generator',
182 ];
183 }
184
185 public function getHelpUrls() {
186 return 'https://www.mediawiki.org/wiki/API:Watch';
187 }
188 }