598e95de24ccd279f153ef87b1ca3489e2faa64f
[lhc/web/wiklou.git] / includes / api / ApiFeedWatchlist.php
1 <?php
2
3 /*
4 * Created on Oct 13, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ("ApiBase.php");
29 }
30
31 /**
32 * @addtogroup API
33 */
34 class ApiFeedWatchlist extends ApiBase {
35
36 public function __construct($main, $action) {
37 parent :: __construct($main, $action);
38 }
39
40 public function getCustomPrinter() {
41 return new ApiFormatFeedWrapper($this->getMain());
42 }
43
44 public function execute() {
45 $params = $this->extractRequestParams();
46
47 // limit to the number of hours going from now back
48 $endTime = wfTimestamp(TS_MW, time() - intval($params['hours'] * 60 * 60));
49
50 // Prepare nested request
51 $fauxReq = new FauxRequest(array (
52 'action' => 'query',
53 'meta' => 'siteinfo',
54 'siprop' => 'general',
55 'list' => 'watchlist',
56 'wlprop' => 'user|comment|timestamp',
57 'wldir' => 'older', // reverse order - from newest to oldest
58 'wlend' => $endTime, // stop at this time
59 'wllimit' => 50
60 ));
61
62 // Execute
63 $module = new ApiMain($fauxReq);
64
65 global $wgFeedClasses, $wgSitename, $wgContLanguageCode;
66
67 try {
68 $module->execute();
69
70 // Get data array
71 $data = $module->getResultData();
72
73 $feedItems = array ();
74 foreach ($data['query']['watchlist'] as $info) {
75 $feedItems[] = $this->createFeedItem($info);
76 }
77
78 $feedTitle = $wgSitename . ' - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
79 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullUrl();
80
81 $feed = new $wgFeedClasses[$params['feedformat']] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl);
82
83 ApiFormatFeedWrapper :: setResult($this->getResult(), $feed, $feedItems);
84
85 } catch (Exception $e) {
86
87 // Error results should not be cached
88 $this->getMain()->setCacheMaxAge(0);
89
90 $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
91 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullUrl();
92
93 $feed = new $wgFeedClasses[$params['feedformat']] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl);
94
95
96 if ($e instanceof UsageException) {
97 $errorCode = $e->getCodeString();
98 } else {
99 // Something is seriously wrong
100 $errorCode = 'internal_api_error';
101 }
102
103 $errorText = $e->getMessage();
104 $feedItems[] = new FeedItem("Error ($errorCode)", $errorText, "", "", "");
105 ApiFormatFeedWrapper :: setResult($this->getResult(), $feed, $feedItems);
106 }
107 }
108
109 private function createFeedItem($info) {
110 $titleStr = $info['title'];
111 $title = Title :: newFromText($titleStr);
112 $titleUrl = $title->getFullUrl();
113 $comment = isset( $info['comment'] ) ? $info['comment'] : null;
114 $timestamp = $info['timestamp'];
115 $user = $info['user'];
116
117 $completeText = "$comment ($user)";
118
119 return new FeedItem($titleStr, $completeText, $titleUrl, $timestamp, $user);
120 }
121
122 protected function getAllowedParams() {
123 global $wgFeedClasses;
124 $feedFormatNames = array_keys($wgFeedClasses);
125 return array (
126 'feedformat' => array (
127 ApiBase :: PARAM_DFLT => 'rss',
128 ApiBase :: PARAM_TYPE => $feedFormatNames
129 ),
130 'hours' => array (
131 ApiBase :: PARAM_DFLT => 24,
132 ApiBase :: PARAM_TYPE => 'integer',
133 ApiBase :: PARAM_MIN => 1,
134 ApiBase :: PARAM_MAX => 72,
135 )
136 );
137 }
138
139 protected function getParamDescription() {
140 return array (
141 'feedformat' => 'The format of the feed',
142 'hours' => 'List pages modified within this many hours from now'
143 );
144 }
145
146 protected function getDescription() {
147 return 'This module returns a watchlist feed';
148 }
149
150 protected function getExamples() {
151 return array (
152 'api.php?action=feedwatchlist'
153 );
154 }
155
156 public function getVersion() {
157 return __CLASS__ . ': $Id$';
158 }
159 }
160 ?>