3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
23 namespace MediaWiki\Api
;
26 use MediaWiki\Feed\FeedItem
;
27 use MediaWiki\MainConfigNames
;
28 use MediaWiki\Parser\ParserFactory
;
29 use MediaWiki\Request\FauxRequest
;
30 use MediaWiki\SpecialPage\SpecialPage
;
31 use MediaWiki\Title\Title
;
32 use Wikimedia\ParamValidator\ParamValidator
;
33 use Wikimedia\ParamValidator\TypeDef\IntegerDef
;
36 * This action allows users to get their watchlist items in RSS/Atom formats.
37 * When executed, it performs a nested call to the API to get the needed data,
38 * and formats it in a proper format.
42 class ApiFeedWatchlist
extends ApiBase
{
44 /** @var ApiBase|null */
45 private $watchlistModule = null;
47 private $linkToSections = false;
49 private ParserFactory
$parserFactory;
51 public function __construct(
54 ParserFactory
$parserFactory
56 parent
::__construct( $main, $action );
57 $this->parserFactory
= $parserFactory;
61 * This module uses a custom feed wrapper printer.
63 * @return ApiFormatFeedWrapper
65 public function getCustomPrinter() {
66 return new ApiFormatFeedWrapper( $this->getMain() );
70 * Make a nested call to the API to request watchlist items in the last $hours.
71 * Wrap the result as an RSS/Atom feed.
73 public function execute() {
74 $config = $this->getConfig();
75 $feedClasses = $config->get( MainConfigNames
::FeedClasses
);
79 $params = $this->extractRequestParams();
81 if ( !$config->get( MainConfigNames
::Feed
) ) {
82 $this->dieWithError( 'feed-unavailable' );
85 if ( !isset( $feedClasses[$params['feedformat']] ) ) {
86 $this->dieWithError( 'feed-invalid' );
89 // limit to the number of hours going from now back
90 $endTime = wfTimestamp( TS_MW
, time() - (int)$params['hours'] * 60 * 60 );
92 // Prepare parameters for nested request
96 'siprop' => 'general',
97 'list' => 'watchlist',
98 'wlprop' => 'title|user|comment|timestamp|ids|loginfo',
99 'wldir' => 'older', // reverse order - from newest to oldest
100 'wlend' => $endTime, // stop at this time
101 'wllimit' => min( 50, $this->getConfig()->get( MainConfigNames
::FeedLimit
) )
104 if ( $params['wlowner'] !== null ) {
105 $fauxReqArr['wlowner'] = $params['wlowner'];
107 if ( $params['wltoken'] !== null ) {
108 $fauxReqArr['wltoken'] = $params['wltoken'];
110 if ( $params['wlexcludeuser'] !== null ) {
111 $fauxReqArr['wlexcludeuser'] = $params['wlexcludeuser'];
113 if ( $params['wlshow'] !== null ) {
114 $fauxReqArr['wlshow'] = ParamValidator
::implodeMultiValue( $params['wlshow'] );
116 if ( $params['wltype'] !== null ) {
117 $fauxReqArr['wltype'] = ParamValidator
::implodeMultiValue( $params['wltype'] );
120 // Support linking directly to sections when possible
121 // (possible only if section name is present in comment)
122 if ( $params['linktosections'] ) {
123 $this->linkToSections
= true;
126 // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
127 if ( $params['allrev'] ) {
128 $fauxReqArr['wlallrev'] = '';
131 $fauxReq = new FauxRequest( $fauxReqArr );
133 $module = new ApiMain( $fauxReq );
136 $data = $module->getResult()->getResultData( [ 'query', 'watchlist' ] );
137 foreach ( (array)$data as $key => $info ) {
138 if ( ApiResult
::isMetadataKey( $key ) ) {
141 $feedItem = $this->createFeedItem( $info );
143 $feedItems[] = $feedItem;
147 $msg = $this->msg( 'watchlist' )->inContentLanguage()->text();
149 $feedTitle = $this->getConfig()->get( MainConfigNames
::Sitename
) . ' - ' . $msg .
150 ' [' . $this->getConfig()->get( MainConfigNames
::LanguageCode
) . ']';
151 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullURL();
153 $feed = new $feedClasses[$params['feedformat']] (
155 htmlspecialchars( $msg ),
159 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
160 } catch ( Exception
$e ) {
161 // Error results should not be cached
162 $this->getMain()->setCacheMaxAge( 0 );
164 // @todo FIXME: Localise brackets
165 $feedTitle = $this->getConfig()->get( MainConfigNames
::Sitename
) . ' - Error - ' .
166 $this->msg( 'watchlist' )->inContentLanguage()->text() .
167 ' [' . $this->getConfig()->get( MainConfigNames
::LanguageCode
) . ']';
168 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullURL();
170 $feedFormat = $params['feedformat'] ??
'rss';
171 $msg = $this->msg( 'watchlist' )->inContentLanguage()->escaped();
172 $feed = new $feedClasses[$feedFormat] ( $feedTitle, $msg, $feedUrl );
174 if ( $e instanceof ApiUsageException
) {
175 foreach ( $e->getStatusValue()->getMessages() as $msg ) {
176 // @phan-suppress-next-line PhanUndeclaredMethod
177 $msg = ApiMessage
::create( $msg )
178 ->inLanguage( $this->getLanguage() );
179 $errorTitle = $this->msg( 'api-feed-error-title', $msg->getApiCode() );
180 $errorText = $msg->text();
181 $feedItems[] = new FeedItem( $errorTitle, $errorText, '', '', '' );
184 // Something is seriously wrong
185 $errorCode = 'internal_api_error';
186 $errorTitle = $this->msg( 'api-feed-error-title', $errorCode );
187 $errorText = $e->getMessage();
188 $feedItems[] = new FeedItem( $errorTitle, $errorText, '', '', '' );
191 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
197 * @return FeedItem|null
199 private function createFeedItem( $info ) {
200 if ( !isset( $info['title'] ) ) {
201 // Probably a revdeled log entry, skip it.
205 $titleStr = $info['title'];
206 $title = Title
::newFromText( $titleStr );
208 if ( !$title ||
$title->isExternal() ) {
209 // Probably a formerly-valid title that's now conflicting with an
210 // interwiki prefix or the like.
211 if ( isset( $info['pageid'] ) ) {
212 $title = Title
::newFromID( $info['pageid'] );
213 $curidParam = [ 'curid' => $info['pageid'] ];
215 if ( !$title ||
$title->isExternal() ) {
219 if ( isset( $info['revid'] ) ) {
220 if ( $info['revid'] === 0 && isset( $info['logid'] ) ) {
221 $logTitle = Title
::makeTitle( NS_SPECIAL
, 'Log' );
222 $titleUrl = $logTitle->getFullURL( [ 'logid' => $info['logid'] ] );
224 $titleUrl = $title->getFullURL( [ 'diff' => $info['revid'] ] );
227 $titleUrl = $title->getFullURL( $curidParam );
229 $comment = $info['comment'] ??
null;
231 // Create an anchor to section.
232 // The anchor won't work for sections that have dupes on page
233 // as there's no way to strip that info from ApiWatchlist (apparently?).
234 // RegExp in the line below is equal to MediaWiki\CommentFormatter\CommentParser::doSectionLinks().
235 if ( $this->linkToSections
&& $comment !== null &&
236 preg_match( '!(.*)/\*\s*(.*?)\s*\*/(.*)!', $comment, $matches )
238 $titleUrl .= $this->parserFactory
->getMainInstance()->guessSectionNameFromWikiText( $matches[ 2 ] );
241 $timestamp = $info['timestamp'];
243 if ( isset( $info['user'] ) ) {
244 $user = $info['user'];
245 $completeText = "$comment ($user)";
248 $completeText = (string)$comment;
251 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
254 private function getWatchlistModule() {
255 $this->watchlistModule ??
= $this->getMain()->getModuleManager()->getModule( 'query' )
256 ->getModuleManager()->getModule( 'watchlist' );
258 return $this->watchlistModule
;
261 public function getAllowedParams( $flags = 0 ) {
262 $feedFormatNames = array_keys( $this->getConfig()->get( MainConfigNames
::FeedClasses
) );
265 ParamValidator
::PARAM_DEFAULT
=> 'rss',
266 ParamValidator
::PARAM_TYPE
=> $feedFormatNames
269 ParamValidator
::PARAM_DEFAULT
=> 24,
270 ParamValidator
::PARAM_TYPE
=> 'integer',
271 IntegerDef
::PARAM_MIN
=> 1,
272 IntegerDef
::PARAM_MAX
=> 72,
274 'linktosections' => false,
278 'allrev' => 'allrev',
279 'owner' => 'wlowner',
280 'token' => 'wltoken',
283 'excludeuser' => 'wlexcludeuser',
285 // @phan-suppress-next-line PhanParamTooMany
286 $wlparams = $this->getWatchlistModule()->getAllowedParams( $flags );
287 foreach ( $copyParams as $from => $to ) {
288 $p = $wlparams[$from];
289 if ( !is_array( $p ) ) {
290 $p = [ ParamValidator
::PARAM_DEFAULT
=> $p ];
292 if ( !isset( $p[ApiBase
::PARAM_HELP_MSG
] ) ) {
293 $p[ApiBase
::PARAM_HELP_MSG
] = "apihelp-query+watchlist-param-$from";
295 if ( isset( $p[ParamValidator
::PARAM_TYPE
] ) && is_array( $p[ParamValidator
::PARAM_TYPE
] ) &&
296 isset( $p[ApiBase
::PARAM_HELP_MSG_PER_VALUE
] )
298 foreach ( $p[ParamValidator
::PARAM_TYPE
] as $v ) {
299 if ( !isset( $p[ApiBase
::PARAM_HELP_MSG_PER_VALUE
][$v] ) ) {
300 $p[ApiBase
::PARAM_HELP_MSG_PER_VALUE
][$v] = "apihelp-query+watchlist-paramvalue-$from-$v";
310 protected function getExamplesMessages() {
312 'action=feedwatchlist'
313 => 'apihelp-feedwatchlist-example-default',
314 'action=feedwatchlist&allrev=&hours=6'
315 => 'apihelp-feedwatchlist-example-all6hrs',
319 public function getHelpUrls() {
320 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watchlist_feed';
324 /** @deprecated class alias since 1.43 */
325 class_alias( ApiFeedWatchlist
::class, 'ApiFeedWatchlist' );