5 * Created on Oct 13, 2006
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
28 * This action allows users to get their watchlist items in RSS/Atom formats.
29 * When executed, it performs a nested call to the API to get the needed data,
30 * and formats it in a proper format.
34 class ApiFeedWatchlist
extends ApiBase
{
36 private $watchlistModule = null;
37 private $linkToSections = false;
40 * This module uses a custom feed wrapper printer.
42 * @return ApiFormatFeedWrapper
44 public function getCustomPrinter() {
45 return new ApiFormatFeedWrapper( $this->getMain() );
49 * Make a nested call to the API to request watchlist items in the last $hours.
50 * Wrap the result as an RSS/Atom feed.
52 public function execute() {
53 $config = $this->getConfig();
54 $feedClasses = $config->get( 'FeedClasses' );
56 $params = $this->extractRequestParams();
58 if ( !$config->get( 'Feed' ) ) {
59 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
62 if ( !isset( $feedClasses[$params['feedformat']] ) ) {
63 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
66 // limit to the number of hours going from now back
67 $endTime = wfTimestamp( TS_MW
, time() - intval( $params['hours'] * 60 * 60 ) );
69 // Prepare parameters for nested request
73 'siprop' => 'general',
74 'list' => 'watchlist',
75 'wlprop' => 'title|user|comment|timestamp|ids',
76 'wldir' => 'older', // reverse order - from newest to oldest
77 'wlend' => $endTime, // stop at this time
78 'wllimit' => min( 50, $this->getConfig()->get( 'FeedLimit' ) )
81 if ( $params['wlowner'] !== null ) {
82 $fauxReqArr['wlowner'] = $params['wlowner'];
84 if ( $params['wltoken'] !== null ) {
85 $fauxReqArr['wltoken'] = $params['wltoken'];
87 if ( $params['wlexcludeuser'] !== null ) {
88 $fauxReqArr['wlexcludeuser'] = $params['wlexcludeuser'];
90 if ( $params['wlshow'] !== null ) {
91 $fauxReqArr['wlshow'] = $params['wlshow'];
93 if ( $params['wltype'] !== null ) {
94 $fauxReqArr['wltype'] = $params['wltype'];
97 // Support linking directly to sections when possible
98 // (possible only if section name is present in comment)
99 if ( $params['linktosections'] ) {
100 $this->linkToSections
= true;
103 // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
104 if ( $params['allrev'] ) {
105 $fauxReqArr['wlallrev'] = '';
108 // Create the request
109 $fauxReq = new FauxRequest( $fauxReqArr );
112 $module = new ApiMain( $fauxReq );
115 $data = $module->getResult()->getResultData( array( 'query', 'watchlist' ) );
116 $feedItems = array();
117 foreach ( (array)$data as $key => $info ) {
118 if ( ApiResult
::isMetadataKey( $key ) ) {
121 $feedItem = $this->createFeedItem( $info );
123 $feedItems[] = $feedItem;
127 $msg = wfMessage( 'watchlist' )->inContentLanguage()->text();
129 $feedTitle = $this->getConfig()->get( 'Sitename' ) . ' - ' . $msg . ' [' . $this->getConfig()->get( 'LanguageCode' ) . ']';
130 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullURL();
132 $feed = new $feedClasses[$params['feedformat']] (
134 htmlspecialchars( $msg ),
138 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
139 } catch ( Exception
$e ) {
140 // Error results should not be cached
141 $this->getMain()->setCacheMaxAge( 0 );
143 // @todo FIXME: Localise brackets
144 $feedTitle = $this->getConfig()->get( 'Sitename' ) . ' - Error - ' .
145 wfMessage( 'watchlist' )->inContentLanguage()->text() .
146 ' [' . $this->getConfig()->get( 'LanguageCode' ) . ']';
147 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullURL();
149 $feedFormat = isset( $params['feedformat'] ) ?
$params['feedformat'] : 'rss';
150 $msg = wfMessage( 'watchlist' )->inContentLanguage()->escaped();
151 $feed = new $feedClasses[$feedFormat] ( $feedTitle, $msg, $feedUrl );
153 if ( $e instanceof UsageException
) {
154 $errorCode = $e->getCodeString();
156 // Something is seriously wrong
157 $errorCode = 'internal_api_error';
160 $errorText = $e->getMessage();
161 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
162 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
170 private function createFeedItem( $info ) {
171 $titleStr = $info['title'];
172 $title = Title
::newFromText( $titleStr );
173 $curidParam = array();
174 if ( !$title ||
$title->isExternal() ) {
175 // Probably a formerly-valid title that's now conflicting with an
176 // interwiki prefix or the like.
177 if ( isset( $info['pageid'] ) ) {
178 $title = Title
::newFromId( $info['pageid'] );
179 $curidParam = array( 'curid' => $info['pageid'] );
181 if ( !$title ||
$title->isExternal() ) {
185 if ( isset( $info['revid'] ) ) {
186 $titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) );
188 $titleUrl = $title->getFullURL( $curidParam );
190 $comment = isset( $info['comment'] ) ?
$info['comment'] : null;
192 // Create an anchor to section.
193 // The anchor won't work for sections that have dupes on page
194 // as there's no way to strip that info from ApiWatchlist (apparently?).
195 // RegExp in the line below is equal to Linker::formatAutocomments().
196 if ( $this->linkToSections
&& $comment !== null &&
197 preg_match( '!(.*)/\*\s*(.*?)\s*\*/(.*)!', $comment, $matches )
201 $sectionTitle = $wgParser->stripSectionName( $matches[2] );
202 $sectionTitle = Sanitizer
::normalizeSectionNameWhitespace( $sectionTitle );
203 $titleUrl .= Title
::newFromText( '#' . $sectionTitle )->getFragmentForURL();
206 $timestamp = $info['timestamp'];
207 $user = $info['user'];
209 $completeText = "$comment ($user)";
211 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
214 private function getWatchlistModule() {
215 if ( $this->watchlistModule
=== null ) {
216 $this->watchlistModule
= $this->getMain()->getModuleManager()->getModule( 'query' )
217 ->getModuleManager()->getModule( 'watchlist' );
220 return $this->watchlistModule
;
223 public function getAllowedParams( $flags = 0 ) {
224 $feedFormatNames = array_keys( $this->getConfig()->get( 'FeedClasses' ) );
226 'feedformat' => array(
227 ApiBase
::PARAM_DFLT
=> 'rss',
228 ApiBase
::PARAM_TYPE
=> $feedFormatNames
231 ApiBase
::PARAM_DFLT
=> 24,
232 ApiBase
::PARAM_TYPE
=> 'integer',
233 ApiBase
::PARAM_MIN
=> 1,
234 ApiBase
::PARAM_MAX
=> 72,
236 'linktosections' => false,
240 'allrev' => 'allrev',
241 'owner' => 'wlowner',
242 'token' => 'wltoken',
245 'excludeuser' => 'wlexcludeuser',
248 $wlparams = $this->getWatchlistModule()->getAllowedParams( $flags );
249 foreach ( $copyParams as $from => $to ) {
250 $p = $wlparams[$from];
251 if ( !is_array( $p ) ) {
252 $p = array( ApiBase
::PARAM_DFLT
=> $p );
254 if ( !isset( $p[ApiBase
::PARAM_HELP_MSG
] ) ) {
255 $p[ApiBase
::PARAM_HELP_MSG
] = "apihelp-query+watchlist-param-$from";
260 foreach ( $copyParams as $from => $to ) {
268 protected function getExamplesMessages() {
270 'action=feedwatchlist'
271 => 'apihelp-feedwatchlist-example-default',
272 'action=feedwatchlist&allrev=&hours=6'
273 => 'apihelp-feedwatchlist-example-all6hrs',
277 public function getHelpUrls() {
278 return 'https://www.mediawiki.org/wiki/API:Watchlist_feed';