API: watchlist now reports a proper feed item when the user is not logged in. Also...
[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 $feedformat = null;
46 extract($this->extractRequestParams());
47
48 // limit to 1 day going from now back
49 $endTime = wfTimestamp(TS_MW, time() - intval(1 * 24 * 60 * 60));
50
51 // Prepare nested request
52 $params = new FauxRequest(array (
53 'action' => 'query',
54 'meta' => 'siteinfo',
55 'siprop' => 'general',
56 'list' => 'watchlist',
57 'wlprop' => 'user|comment|timestamp',
58 'wldir' => 'older',
59 'wlend' => $endTime,
60 'wllimit' => 50
61 ));
62
63 // Execute
64 $module = new ApiMain($params);
65
66 try {
67 $module->execute();
68
69 // Get data array
70 $data = $module->getResultData();
71
72 $feedItems = array ();
73 foreach ($data['query']['watchlist'] as $info) {
74 $feedItems[] = $this->createFeedItem($info);
75 }
76
77 global $wgFeedClasses, $wgSitename, $wgContLanguageCode;
78 $feedTitle = $wgSitename . ' - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
79 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullUrl();
80
81 $feed = new $wgFeedClasses[$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 global $wgFeedClasses, $wgSitename, $wgContLanguageCode;
91 $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
92 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullUrl();
93
94 $feed = new $wgFeedClasses[$feedformat] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl);
95
96
97 if ($e instanceof UsageException) {
98 $errorCode = $e->getCodeString();
99 } else {
100 // Something is seriously wrong
101 $errorCode = 'internal_api_error';
102 }
103
104 $errorText = $e->getMessage();
105 $feedItems[] = new FeedItem("Error ($errorCode)", $errorText, "", "", "");
106 ApiFormatFeedWrapper :: setResult($this->getResult(), $feed, $feedItems);
107 }
108 }
109
110 private function createFeedItem($info) {
111 $titleStr = $info['title'];
112 $title = Title :: newFromText($titleStr);
113 $titleUrl = $title->getFullUrl();
114 $comment = isset( $info['comment'] ) ? $info['comment'] : null;
115 $timestamp = $info['timestamp'];
116 $user = $info['user'];
117
118 $completeText = "$comment ($user)";
119
120 return new FeedItem($titleStr, $completeText, $titleUrl, $timestamp, $user);
121 }
122
123 protected function getAllowedParams() {
124 global $wgFeedClasses;
125 $feedFormatNames = array_keys($wgFeedClasses);
126 return array (
127 'feedformat' => array (
128 ApiBase :: PARAM_DFLT => 'rss',
129 ApiBase :: PARAM_TYPE => $feedFormatNames
130 )
131 );
132 }
133
134 protected function getParamDescription() {
135 return array (
136 'feedformat' => 'The format of the feed'
137 );
138 }
139
140 protected function getDescription() {
141 return 'This module returns a watchlist feed';
142 }
143
144 protected function getExamples() {
145 return array (
146 'api.php?action=feedwatchlist'
147 );
148 }
149
150 public function getVersion() {
151 return __CLASS__ . ': $Id$';
152 }
153 }
154 ?>