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
24 * This action allows users to get their watchlist items in RSS/Atom formats.
25 * When executed, it performs a nested call to the API to get the needed data,
26 * and formats it in a proper format.
30 class ApiFeedWatchlist
extends ApiBase
{
32 private $watchlistModule = null;
33 private $linkToSections = false;
36 * This module uses a custom feed wrapper printer.
38 * @return ApiFormatFeedWrapper
40 public function getCustomPrinter() {
41 return new ApiFormatFeedWrapper( $this->getMain() );
45 * Make a nested call to the API to request watchlist items in the last $hours.
46 * Wrap the result as an RSS/Atom feed.
48 public function execute() {
49 $config = $this->getConfig();
50 $feedClasses = $config->get( 'FeedClasses' );
53 $params = $this->extractRequestParams();
55 if ( !$config->get( 'Feed' ) ) {
56 $this->dieWithError( 'feed-unavailable' );
59 if ( !isset( $feedClasses[$params['feedformat']] ) ) {
60 $this->dieWithError( 'feed-invalid' );
63 // limit to the number of hours going from now back
64 $endTime = wfTimestamp( TS_MW
, time() - (int)$params['hours'] * 60 * 60 );
66 // Prepare parameters for nested request
70 'siprop' => 'general',
71 'list' => 'watchlist',
72 'wlprop' => 'title|user|comment|timestamp|ids|loginfo',
73 'wldir' => 'older', // reverse order - from newest to oldest
74 'wlend' => $endTime, // stop at this time
75 'wllimit' => min( 50, $this->getConfig()->get( 'FeedLimit' ) )
78 if ( $params['wlowner'] !== null ) {
79 $fauxReqArr['wlowner'] = $params['wlowner'];
81 if ( $params['wltoken'] !== null ) {
82 $fauxReqArr['wltoken'] = $params['wltoken'];
84 if ( $params['wlexcludeuser'] !== null ) {
85 $fauxReqArr['wlexcludeuser'] = $params['wlexcludeuser'];
87 if ( $params['wlshow'] !== null ) {
88 $fauxReqArr['wlshow'] = $params['wlshow'];
90 if ( $params['wltype'] !== null ) {
91 $fauxReqArr['wltype'] = $params['wltype'];
94 // Support linking directly to sections when possible
95 // (possible only if section name is present in comment)
96 if ( $params['linktosections'] ) {
97 $this->linkToSections
= true;
100 // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
101 if ( $params['allrev'] ) {
102 $fauxReqArr['wlallrev'] = '';
105 $fauxReq = new FauxRequest( $fauxReqArr );
107 $module = new ApiMain( $fauxReq );
110 $data = $module->getResult()->getResultData( [ 'query', 'watchlist' ] );
112 foreach ( (array)$data as $key => $info ) {
113 if ( ApiResult
::isMetadataKey( $key ) ) {
116 $feedItem = $this->createFeedItem( $info );
118 $feedItems[] = $feedItem;
122 $msg = wfMessage( 'watchlist' )->inContentLanguage()->text();
124 $feedTitle = $this->getConfig()->get( 'Sitename' ) . ' - ' . $msg .
125 ' [' . $this->getConfig()->get( 'LanguageCode' ) . ']';
126 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullURL();
128 $feed = new $feedClasses[$params['feedformat']] (
130 htmlspecialchars( $msg ),
134 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
135 } catch ( Exception
$e ) {
136 // Error results should not be cached
137 $this->getMain()->setCacheMaxAge( 0 );
139 // @todo FIXME: Localise brackets
140 $feedTitle = $this->getConfig()->get( 'Sitename' ) . ' - Error - ' .
141 wfMessage( 'watchlist' )->inContentLanguage()->text() .
142 ' [' . $this->getConfig()->get( 'LanguageCode' ) . ']';
143 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullURL();
145 $feedFormat = $params['feedformat'] ??
'rss';
146 $msg = wfMessage( 'watchlist' )->inContentLanguage()->escaped();
147 $feed = new $feedClasses[$feedFormat] ( $feedTitle, $msg, $feedUrl );
149 if ( $e instanceof ApiUsageException
) {
150 foreach ( $e->getStatusValue()->getErrors() as $error ) {
151 $msg = ApiMessage
::create( $error )
152 ->inLanguage( $this->getLanguage() );
153 $errorTitle = $this->msg( 'api-feed-error-title', $msg->getApiCode() );
154 $errorText = $msg->text();
155 $feedItems[] = new FeedItem( $errorTitle, $errorText, '', '', '' );
158 // Something is seriously wrong
159 $errorCode = 'internal_api_error';
160 $errorTitle = $this->msg( 'api-feed-error-title', $errorCode );
161 $errorText = $e->getMessage();
162 $feedItems[] = new FeedItem( $errorTitle, $errorText, '', '', '' );
165 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
173 private function createFeedItem( $info ) {
174 if ( !isset( $info['title'] ) ) {
175 // Probably a revdeled log entry, skip it.
179 $titleStr = $info['title'];
180 $title = Title
::newFromText( $titleStr );
182 if ( !$title ||
$title->isExternal() ) {
183 // Probably a formerly-valid title that's now conflicting with an
184 // interwiki prefix or the like.
185 if ( isset( $info['pageid'] ) ) {
186 $title = Title
::newFromID( $info['pageid'] );
187 $curidParam = [ 'curid' => $info['pageid'] ];
189 if ( !$title ||
$title->isExternal() ) {
193 if ( isset( $info['revid'] ) ) {
194 if ( $info['revid'] === 0 && isset( $info['logid'] ) ) {
195 $logTitle = Title
::makeTitle( NS_SPECIAL
, 'Log' );
196 $titleUrl = $logTitle->getFullURL( [ 'logid' => $info['logid'] ] );
198 $titleUrl = $title->getFullURL( [ 'diff' => $info['revid'] ] );
201 $titleUrl = $title->getFullURL( $curidParam );
203 $comment = $info['comment'] ??
null;
205 // Create an anchor to section.
206 // The anchor won't work for sections that have dupes on page
207 // as there's no way to strip that info from ApiWatchlist (apparently?).
208 // RegExp in the line below is equal to Linker::formatAutocomments().
209 if ( $this->linkToSections
&& $comment !== null &&
210 preg_match( '!(.*)/\*\s*(.*?)\s*\*/(.*)!', $comment, $matches )
213 $titleUrl .= $wgParser->guessSectionNameFromWikiText( $matches[ 2 ] );
216 $timestamp = $info['timestamp'];
218 if ( isset( $info['user'] ) ) {
219 $user = $info['user'];
220 $completeText = "$comment ($user)";
223 $completeText = (string)$comment;
226 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
229 private function getWatchlistModule() {
230 if ( $this->watchlistModule
=== null ) {
231 $this->watchlistModule
= $this->getMain()->getModuleManager()->getModule( 'query' )
232 ->getModuleManager()->getModule( 'watchlist' );
235 return $this->watchlistModule
;
238 public function getAllowedParams( $flags = 0 ) {
239 $feedFormatNames = array_keys( $this->getConfig()->get( 'FeedClasses' ) );
242 ApiBase
::PARAM_DFLT
=> 'rss',
243 ApiBase
::PARAM_TYPE
=> $feedFormatNames
246 ApiBase
::PARAM_DFLT
=> 24,
247 ApiBase
::PARAM_TYPE
=> 'integer',
248 ApiBase
::PARAM_MIN
=> 1,
249 ApiBase
::PARAM_MAX
=> 72,
251 'linktosections' => false,
255 'allrev' => 'allrev',
256 'owner' => 'wlowner',
257 'token' => 'wltoken',
260 'excludeuser' => 'wlexcludeuser',
263 $wlparams = $this->getWatchlistModule()->getAllowedParams( $flags );
264 foreach ( $copyParams as $from => $to ) {
265 $p = $wlparams[$from];
266 if ( !is_array( $p ) ) {
267 $p = [ ApiBase
::PARAM_DFLT
=> $p ];
269 if ( !isset( $p[ApiBase
::PARAM_HELP_MSG
] ) ) {
270 $p[ApiBase
::PARAM_HELP_MSG
] = "apihelp-query+watchlist-param-$from";
272 if ( isset( $p[ApiBase
::PARAM_TYPE
] ) && is_array( $p[ApiBase
::PARAM_TYPE
] ) &&
273 isset( $p[ApiBase
::PARAM_HELP_MSG_PER_VALUE
] )
275 foreach ( $p[ApiBase
::PARAM_TYPE
] as $v ) {
276 if ( !isset( $p[ApiBase
::PARAM_HELP_MSG_PER_VALUE
][$v] ) ) {
277 $p[ApiBase
::PARAM_HELP_MSG_PER_VALUE
][$v] = "apihelp-query+watchlist-paramvalue-$from-$v";
284 foreach ( $copyParams as $from => $to ) {
292 protected function getExamplesMessages() {
294 'action=feedwatchlist'
295 => 'apihelp-feedwatchlist-example-default',
296 'action=feedwatchlist&allrev=&hours=6'
297 => 'apihelp-feedwatchlist-example-all6hrs',
301 public function getHelpUrls() {
302 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watchlist_feed';