Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
58.04% |
65 / 112 |
|
50.00% |
5 / 10 |
CRAP | |
0.00% |
0 / 1 |
ApiWatch | |
58.04% |
65 / 112 |
|
50.00% |
5 / 10 |
96.51 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
57.78% |
26 / 45 |
|
0.00% |
0 / 1 |
20.11 | |||
watchTitle | |
52.38% |
11 / 21 |
|
0.00% |
0 / 1 |
14.91 | |||
getPageSet | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
mustBePosted | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isWriteMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
needsToken | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAllowedParams | |
90.00% |
18 / 20 |
|
0.00% |
0 / 1 |
3.01 | |||
getExamplesMessages | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
6 | |||
getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
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 | use MediaWiki\MainConfigNames; |
24 | use MediaWiki\Page\PageIdentity; |
25 | use MediaWiki\Title\Title; |
26 | use MediaWiki\Title\TitleFormatter; |
27 | use MediaWiki\User\User; |
28 | use MediaWiki\Watchlist\WatchlistManager; |
29 | use Wikimedia\ParamValidator\ParamValidator; |
30 | use Wikimedia\ParamValidator\TypeDef\ExpiryDef; |
31 | |
32 | /** |
33 | * API module to allow users to watch a page |
34 | * |
35 | * @ingroup API |
36 | */ |
37 | class ApiWatch extends ApiBase { |
38 | private $mPageSet = null; |
39 | |
40 | /** @var bool Whether watchlist expiries are enabled. */ |
41 | private $expiryEnabled; |
42 | |
43 | /** @var string Relative maximum expiry. */ |
44 | private $maxDuration; |
45 | |
46 | protected WatchlistManager $watchlistManager; |
47 | private TitleFormatter $titleFormatter; |
48 | |
49 | public function __construct( |
50 | ApiMain $mainModule, |
51 | $moduleName, |
52 | WatchlistManager $watchlistManager, |
53 | TitleFormatter $titleFormatter |
54 | ) { |
55 | parent::__construct( $mainModule, $moduleName ); |
56 | |
57 | $this->watchlistManager = $watchlistManager; |
58 | $this->titleFormatter = $titleFormatter; |
59 | $this->expiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry ); |
60 | $this->maxDuration = $this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration ); |
61 | } |
62 | |
63 | public function execute() { |
64 | $user = $this->getUser(); |
65 | if ( !$user->isRegistered() |
66 | || ( $user->isTemp() && !$user->isAllowed( 'editmywatchlist' ) ) |
67 | ) { |
68 | $this->dieWithError( 'watchlistanontext', 'notloggedin' ); |
69 | } |
70 | |
71 | $this->checkUserRightsAny( 'editmywatchlist' ); |
72 | |
73 | $params = $this->extractRequestParams(); |
74 | |
75 | $continuationManager = new ApiContinuationManager( $this, [], [] ); |
76 | $this->setContinuationManager( $continuationManager ); |
77 | |
78 | $pageSet = $this->getPageSet(); |
79 | // by default we use pageset to extract the page to work on. |
80 | // title is still supported for backward compatibility |
81 | if ( !isset( $params['title'] ) ) { |
82 | $pageSet->execute(); |
83 | $res = $pageSet->getInvalidTitlesAndRevisions( [ |
84 | 'invalidTitles', |
85 | 'special', |
86 | 'missingIds', |
87 | 'missingRevIds', |
88 | 'interwikiTitles' |
89 | ] ); |
90 | |
91 | foreach ( $pageSet->getMissingPages() as $page ) { |
92 | $r = $this->watchTitle( $page, $user, $params ); |
93 | $r['missing'] = true; |
94 | $res[] = $r; |
95 | } |
96 | |
97 | foreach ( $pageSet->getGoodPages() as $page ) { |
98 | $r = $this->watchTitle( $page, $user, $params ); |
99 | $res[] = $r; |
100 | } |
101 | ApiResult::setIndexedTagName( $res, 'w' ); |
102 | } else { |
103 | // dont allow use of old title parameter with new pageset parameters. |
104 | $extraParams = array_keys( array_filter( $pageSet->extractRequestParams(), static function ( $x ) { |
105 | return $x !== null && $x !== false; |
106 | } ) ); |
107 | |
108 | if ( $extraParams ) { |
109 | $this->dieWithError( |
110 | [ |
111 | 'apierror-invalidparammix-cannotusewith', |
112 | $this->encodeParamName( 'title' ), |
113 | $pageSet->encodeParamName( $extraParams[0] ) |
114 | ], |
115 | 'invalidparammix' |
116 | ); |
117 | } |
118 | |
119 | $title = Title::newFromText( $params['title'] ); |
120 | if ( !$title || !$this->watchlistManager->isWatchable( $title ) ) { |
121 | $this->dieWithError( [ 'invalidtitle', $params['title'] ] ); |
122 | } |
123 | $res = $this->watchTitle( $title, $user, $params, true ); |
124 | } |
125 | $this->getResult()->addValue( null, $this->getModuleName(), $res ); |
126 | |
127 | $this->setContinuationManager( null ); |
128 | $continuationManager->setContinuationIntoResult( $this->getResult() ); |
129 | } |
130 | |
131 | private function watchTitle( PageIdentity $page, User $user, array $params, |
132 | $compatibilityMode = false |
133 | ) { |
134 | $res = [ 'title' => $this->titleFormatter->getPrefixedText( $page ), 'ns' => $page->getNamespace() ]; |
135 | |
136 | if ( !$this->watchlistManager->isWatchable( $page ) ) { |
137 | $res['watchable'] = 0; |
138 | return $res; |
139 | } |
140 | |
141 | if ( $params['unwatch'] ) { |
142 | $status = $this->watchlistManager->removeWatch( $user, $page ); |
143 | $res['unwatched'] = $status->isOK(); |
144 | } else { |
145 | $expiry = null; |
146 | |
147 | // NOTE: If an expiry parameter isn't given, any existing expiries remain unchanged. |
148 | if ( $this->expiryEnabled && isset( $params['expiry'] ) ) { |
149 | $expiry = $params['expiry']; |
150 | $res['expiry'] = ApiResult::formatExpiry( $expiry ); |
151 | } |
152 | |
153 | $status = $this->watchlistManager->addWatch( $user, $page, $expiry ); |
154 | $res['watched'] = $status->isOK(); |
155 | } |
156 | |
157 | if ( !$status->isOK() ) { |
158 | if ( $compatibilityMode ) { |
159 | $this->dieStatus( $status ); |
160 | } |
161 | $res['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' ); |
162 | $res['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' ); |
163 | if ( !$res['warnings'] ) { |
164 | unset( $res['warnings'] ); |
165 | } |
166 | } |
167 | |
168 | return $res; |
169 | } |
170 | |
171 | /** |
172 | * Get a cached instance of an ApiPageSet object |
173 | * @return ApiPageSet |
174 | */ |
175 | private function getPageSet() { |
176 | $this->mPageSet ??= new ApiPageSet( $this ); |
177 | |
178 | return $this->mPageSet; |
179 | } |
180 | |
181 | public function mustBePosted() { |
182 | return true; |
183 | } |
184 | |
185 | public function isWriteMode() { |
186 | return true; |
187 | } |
188 | |
189 | public function needsToken() { |
190 | return 'watch'; |
191 | } |
192 | |
193 | public function getAllowedParams( $flags = 0 ) { |
194 | $result = [ |
195 | 'title' => [ |
196 | ParamValidator::PARAM_TYPE => 'string', |
197 | ParamValidator::PARAM_DEPRECATED => true, |
198 | ], |
199 | 'expiry' => [ |
200 | ParamValidator::PARAM_TYPE => 'expiry', |
201 | ExpiryDef::PARAM_MAX => $this->maxDuration, |
202 | ExpiryDef::PARAM_USE_MAX => true, |
203 | ], |
204 | 'unwatch' => false, |
205 | 'continue' => [ |
206 | ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
207 | ], |
208 | ]; |
209 | |
210 | // If expiry is not enabled, don't accept the parameter. |
211 | if ( !$this->expiryEnabled ) { |
212 | unset( $result['expiry'] ); |
213 | } |
214 | |
215 | if ( $flags ) { |
216 | $result += $this->getPageSet()->getFinalParams( $flags ); |
217 | } |
218 | |
219 | return $result; |
220 | } |
221 | |
222 | protected function getExamplesMessages() { |
223 | $title = Title::newMainPage()->getPrefixedText(); |
224 | $mp = rawurlencode( $title ); |
225 | |
226 | // Logically expiry example should go before unwatch examples. |
227 | $examples = [ |
228 | "action=watch&titles={$mp}&token=123ABC" |
229 | => 'apihelp-watch-example-watch', |
230 | ]; |
231 | if ( $this->expiryEnabled ) { |
232 | $examples["action=watch&titles={$mp}|Foo|Bar&expiry=1%20month&token=123ABC"] |
233 | = 'apihelp-watch-example-watch-expiry'; |
234 | } |
235 | |
236 | return array_merge( $examples, [ |
237 | "action=watch&titles={$mp}&unwatch=&token=123ABC" |
238 | => 'apihelp-watch-example-unwatch', |
239 | 'action=watch&generator=allpages&gapnamespace=0&token=123ABC' |
240 | => 'apihelp-watch-example-generator', |
241 | ] ); |
242 | } |
243 | |
244 | public function getHelpUrls() { |
245 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watch'; |
246 | } |
247 | } |