Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / api / ApiFeedWatchlist.php
blobddcd6ac4b801f496525d416cf128cd07e29ae911
1 <?php
2 /**
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
24 * @file
27 /**
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.
32 * @ingroup API
34 class ApiFeedWatchlist extends ApiBase {
36 private $watchlistModule = null;
37 private $linkToDiffs = false;
38 private $linkToSections = false;
40 /**
41 * This module uses a custom feed wrapper printer.
43 * @return ApiFormatFeedWrapper
45 public function getCustomPrinter() {
46 return new ApiFormatFeedWrapper( $this->getMain() );
49 /**
50 * Make a nested call to the API to request watchlist items in the last $hours.
51 * Wrap the result as an RSS/Atom feed.
53 public function execute() {
54 global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
56 try {
57 $params = $this->extractRequestParams();
59 if ( !$wgFeed ) {
60 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
63 if ( !isset( $wgFeedClasses[$params['feedformat']] ) ) {
64 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
67 // limit to the number of hours going from now back
68 $endTime = wfTimestamp( TS_MW, time() - intval( $params['hours'] * 60 * 60 ) );
70 // Prepare parameters for nested request
71 $fauxReqArr = array(
72 'action' => 'query',
73 'meta' => 'siteinfo',
74 'siprop' => 'general',
75 'list' => 'watchlist',
76 'wlprop' => 'title|user|comment|timestamp',
77 'wldir' => 'older', // reverse order - from newest to oldest
78 'wlend' => $endTime, // stop at this time
79 'wllimit' => ( 50 > $wgFeedLimit ) ? $wgFeedLimit : 50
82 if ( $params['wlowner'] !== null ) {
83 $fauxReqArr['wlowner'] = $params['wlowner'];
85 if ( $params['wltoken'] !== null ) {
86 $fauxReqArr['wltoken'] = $params['wltoken'];
88 if ( $params['wlexcludeuser'] !== null ) {
89 $fauxReqArr['wlexcludeuser'] = $params['wlexcludeuser'];
91 if ( $params['wlshow'] !== null ) {
92 $fauxReqArr['wlshow'] = $params['wlshow'];
95 // Support linking to diffs instead of article
96 if ( $params['linktodiffs'] ) {
97 $this->linkToDiffs = true;
98 $fauxReqArr['wlprop'] .= '|ids';
101 // Support linking directly to sections when possible
102 // (possible only if section name is present in comment)
103 if ( $params['linktosections'] ) {
104 $this->linkToSections = true;
107 // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
108 if ( $params['allrev'] ) {
109 $fauxReqArr['wlallrev'] = '';
112 // Create the request
113 $fauxReq = new FauxRequest( $fauxReqArr );
115 // Execute
116 $module = new ApiMain( $fauxReq );
117 $module->execute();
119 // Get data array
120 $data = $module->getResultData();
122 $feedItems = array();
123 foreach ( (array)$data['query']['watchlist'] as $info ) {
124 $feedItems[] = $this->createFeedItem( $info );
127 $msg = wfMessage( 'watchlist' )->inContentLanguage()->text();
129 $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']';
130 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
132 $feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( $msg ), $feedUrl );
134 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
136 } catch ( Exception $e ) {
138 // Error results should not be cached
139 $this->getMain()->setCacheMaxAge( 0 );
141 $feedTitle = $wgSitename . ' - Error - ' . wfMessage( 'watchlist' )->inContentLanguage()->text() . ' [' . $wgLanguageCode . ']';
142 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
144 $feedFormat = isset( $params['feedformat'] ) ? $params['feedformat'] : 'rss';
145 $msg = wfMessage( 'watchlist' )->inContentLanguage()->escaped();
146 $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, $msg, $feedUrl );
148 if ( $e instanceof UsageException ) {
149 $errorCode = $e->getCodeString();
150 } else {
151 // Something is seriously wrong
152 $errorCode = 'internal_api_error';
155 $errorText = $e->getMessage();
156 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
157 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
162 * @param $info array
163 * @return FeedItem
165 private function createFeedItem( $info ) {
166 $titleStr = $info['title'];
167 $title = Title::newFromText( $titleStr );
168 if ( $this->linkToDiffs && isset( $info['revid'] ) ) {
169 $titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) );
170 } else {
171 $titleUrl = $title->getFullURL();
173 $comment = isset( $info['comment'] ) ? $info['comment'] : null;
175 // Create an anchor to section.
176 // The anchor won't work for sections that have dupes on page
177 // as there's no way to strip that info from ApiWatchlist (apparently?).
178 // RegExp in the line below is equal to Linker::formatAutocomments().
179 if ( $this->linkToSections && $comment !== null && preg_match( '!(.*)/\*\s*(.*?)\s*\*/(.*)!', $comment, $matches ) ) {
180 global $wgParser;
181 $sectionTitle = $wgParser->stripSectionName( $matches[2] );
182 $sectionTitle = Sanitizer::normalizeSectionNameWhitespace( $sectionTitle );
183 $titleUrl .= Title::newFromText( '#' . $sectionTitle )->getFragmentForURL();
186 $timestamp = $info['timestamp'];
187 $user = $info['user'];
189 $completeText = "$comment ($user)";
191 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
194 private function getWatchlistModule() {
195 if ( $this->watchlistModule === null ) {
196 $this->watchlistModule = $this->getMain()->getModuleManager()->getModule( 'query' )
197 ->getModuleManager()->getModule( 'watchlist' );
199 return $this->watchlistModule;
202 public function getAllowedParams( $flags = 0 ) {
203 global $wgFeedClasses;
204 $feedFormatNames = array_keys( $wgFeedClasses );
205 $ret = array(
206 'feedformat' => array(
207 ApiBase::PARAM_DFLT => 'rss',
208 ApiBase::PARAM_TYPE => $feedFormatNames
210 'hours' => array(
211 ApiBase::PARAM_DFLT => 24,
212 ApiBase::PARAM_TYPE => 'integer',
213 ApiBase::PARAM_MIN => 1,
214 ApiBase::PARAM_MAX => 72,
216 'linktodiffs' => false,
217 'linktosections' => false,
219 if ( $flags ) {
220 $wlparams = $this->getWatchlistModule()->getAllowedParams( $flags );
221 $ret['allrev'] = $wlparams['allrev'];
222 $ret['wlowner'] = $wlparams['owner'];
223 $ret['wltoken'] = $wlparams['token'];
224 $ret['wlshow'] = $wlparams['show'];
225 $ret['wlexcludeuser'] = $wlparams['excludeuser'];
226 } else {
227 $ret['allrev'] = null;
228 $ret['wlowner'] = null;
229 $ret['wltoken'] = null;
230 $ret['wlshow'] = null;
231 $ret['wlexcludeuser'] = null;
233 return $ret;
236 public function getParamDescription() {
237 $wldescr = $this->getWatchlistModule()->getParamDescription();
238 return array(
239 'feedformat' => 'The format of the feed',
240 'hours' => 'List pages modified within this many hours from now',
241 'linktodiffs' => 'Link to change differences instead of article pages',
242 'linktosections' => 'Link directly to changed sections if possible',
243 'allrev' => $wldescr['allrev'],
244 'wlowner' => $wldescr['owner'],
245 'wltoken' => $wldescr['token'],
246 'wlshow' => $wldescr['show'],
247 'wlexcludeuser' => $wldescr['excludeuser'],
251 public function getDescription() {
252 return 'Returns a watchlist feed';
255 public function getPossibleErrors() {
256 return array_merge( parent::getPossibleErrors(), array(
257 array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
258 array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
259 ) );
262 public function getExamples() {
263 return array(
264 'api.php?action=feedwatchlist',
265 'api.php?action=feedwatchlist&allrev=&linktodiffs=&hours=6'
269 public function getHelpUrls() {
270 return 'https://www.mediawiki.org/wiki/API:Watchlist_feed';