]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiFeedWatchlist.php
MediaWiki 1.17.4
[autoinstalls/mediawiki.git] / includes / api / ApiFeedWatchlist.php
1 <?php
2 /**
3  * API for MediaWiki 1.8+
4  *
5  * Created on Oct 13, 2006
6  *
7  * Copyright © 2006 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 if ( !defined( 'MEDIAWIKI' ) ) {
28         // Eclipse helper - will be ignored in production
29         require_once( "ApiBase.php" );
30 }
31
32 /**
33  * This action allows users to get their watchlist items in RSS/Atom formats.
34  * When executed, it performs a nested call to the API to get the needed data,
35  * and formats it in a proper format.
36  *
37  * @ingroup API
38  */
39 class ApiFeedWatchlist extends ApiBase {
40
41         public function __construct( $main, $action ) {
42                 parent::__construct( $main, $action );
43         }
44
45         /**
46          * This module uses a custom feed wrapper printer.
47          */
48         public function getCustomPrinter() {
49                 return new ApiFormatFeedWrapper( $this->getMain() );
50         }
51
52         private $linkToDiffs = false;
53
54         /**
55          * Make a nested call to the API to request watchlist items in the last $hours.
56          * Wrap the result as an RSS/Atom feed.
57          */
58         public function execute() {
59                 global $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
60
61                 try {
62                         $params = $this->extractRequestParams();
63
64                         // limit to the number of hours going from now back
65                         $endTime = wfTimestamp( TS_MW, time() - intval( $params['hours'] * 60 * 60 ) );
66
67                         // Prepare parameters for nested request
68                         $fauxReqArr = array(
69                                 'action' => 'query',
70                                 'meta' => 'siteinfo',
71                                 'siprop' => 'general',
72                                 'list' => 'watchlist',
73                                 'wlprop' => 'title|user|comment|timestamp',
74                                 'wldir' => 'older', // reverse order - from newest to oldest
75                                 'wlend' => $endTime, // stop at this time
76                                 'wllimit' => ( 50 > $wgFeedLimit ) ? $wgFeedLimit : 50
77                         );
78
79                         if ( !is_null( $params['wlowner'] ) ) {
80                                 $fauxReqArr['wlowner'] = $params['wlowner'];
81                         }
82                         if ( !is_null( $params['wltoken'] ) ) {
83                                 $fauxReqArr['wltoken'] = $params['wltoken'];
84                         }
85
86                         // Support linking to diffs instead of article
87                         if ( $params['linktodiffs'] ) {
88                                 $this->linkToDiffs = true;
89                                 $fauxReqArr['wlprop'] .= '|ids';
90                         }
91
92                         // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
93                         if ( !is_null( $params['allrev'] ) ) {
94                                 $fauxReqArr['wlallrev'] = '';
95                         }
96
97                         // Create the request
98                         $fauxReq = new FauxRequest( $fauxReqArr );
99
100                         // Execute
101                         $module = new ApiMain( $fauxReq );
102                         $module->execute();
103
104                         // Get data array
105                         $data = $module->getResultData();
106
107                         $feedItems = array();
108                         foreach ( (array)$data['query']['watchlist'] as $info ) {
109                                 $feedItems[] = $this->createFeedItem( $info );
110                         }
111
112                         $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgLanguageCode . ']';
113                         $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
114
115                         $feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
116
117                         ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
118
119                 } catch ( Exception $e ) {
120
121                         // Error results should not be cached
122                         $this->getMain()->setCacheMaxAge( 0 );
123
124                         $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgLanguageCode . ']';
125                         $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
126
127                         $feedFormat = isset( $params['feedformat'] ) ? $params['feedformat'] : 'rss';
128                         $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
129
130                         if ( $e instanceof UsageException ) {
131                                 $errorCode = $e->getCodeString();
132                         } else {
133                                 // Something is seriously wrong
134                                 $errorCode = 'internal_api_error';
135                         }
136
137                         $errorText = $e->getMessage();
138                         $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
139                         ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
140                 }
141         }
142
143         private function createFeedItem( $info ) {
144                 $titleStr = $info['title'];
145                 $title = Title::newFromText( $titleStr );
146                 if ( $this->linkToDiffs && isset( $info['revid'] ) ) {
147                         $titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) );
148                 } else {
149                         $titleUrl = $title->getFullURL();
150                 }
151                 $comment = isset( $info['comment'] ) ? $info['comment'] : null;
152                 $timestamp = $info['timestamp'];
153                 $user = $info['user'];
154
155                 $completeText = "$comment ($user)";
156
157                 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
158         }
159
160         public function getAllowedParams() {
161                 global $wgFeedClasses;
162                 $feedFormatNames = array_keys( $wgFeedClasses );
163                 return array (
164                         'feedformat' => array(
165                                 ApiBase::PARAM_DFLT => 'rss',
166                                 ApiBase::PARAM_TYPE => $feedFormatNames
167                         ),
168                         'hours' => array(
169                                 ApiBase::PARAM_DFLT => 24,
170                                 ApiBase::PARAM_TYPE => 'integer',
171                                 ApiBase::PARAM_MIN => 1,
172                                 ApiBase::PARAM_MAX => 72,
173                         ),
174                         'allrev' => null,
175                         'wlowner' => array(
176                                 ApiBase::PARAM_TYPE => 'user'
177                         ),
178                         'wltoken' => array(
179                                 ApiBase::PARAM_TYPE => 'string'
180                         ),
181                         'linktodiffs' => false,
182                 );
183         }
184
185         public function getParamDescription() {
186                 return array(
187                         'feedformat' => 'The format of the feed',
188                         'hours'      => 'List pages modified within this many hours from now',
189                         'allrev'     => 'Include multiple revisions of the same page within given timeframe',
190                         'wlowner'    => "The user whose watchlist you want (must be accompanied by {$this->getModulePrefix()}token if it's not you)",
191                         'wltoken'    => 'Security token that requested user set in their preferences',
192                         'linktodiffs'=> 'Link to change differences instead of article pages'
193                 );
194         }
195
196         public function getDescription() {
197                 return 'This module returns a watchlist feed';
198         }
199
200         protected function getExamples() {
201                 return array(
202                         'api.php?action=feedwatchlist',
203                         'api.php?action=feedwatchlist&allrev=allrev&linktodiffs=&hours=6'
204                 );
205         }
206
207         public function getVersion() {
208                 return __CLASS__ . ': $Id$';
209         }
210 }