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