00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if (!defined('MEDIAWIKI')) {
00027
00028 require_once ("ApiBase.php");
00029 }
00030
00038 class ApiFeedWatchlist extends ApiBase {
00039
00040 public function __construct($main, $action) {
00041 parent :: __construct($main, $action);
00042 }
00043
00047 public function getCustomPrinter() {
00048 return new ApiFormatFeedWrapper($this->getMain());
00049 }
00050
00055 public function execute() {
00056
00057 global $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgContLanguageCode;
00058
00059 try {
00060 $params = $this->extractRequestParams();
00061
00062
00063 $endTime = wfTimestamp(TS_MW, time() - intval($params['hours'] * 60 * 60));
00064
00065 $dbr = wfGetDB( DB_SLAVE );
00066
00067 $fauxReqArr = array (
00068 'action' => 'query',
00069 'meta' => 'siteinfo',
00070 'siprop' => 'general',
00071 'list' => 'watchlist',
00072 'wlprop' => 'title|user|comment|timestamp',
00073 'wldir' => 'older',
00074 'wlend' => $dbr->timestamp($endTime),
00075 'wllimit' => (50 > $wgFeedLimit) ? $wgFeedLimit : 50
00076 );
00077
00078
00079 if ( ! is_null ( $params['allrev'] ) ) $fauxReqArr['wlallrev'] = '';
00080
00081
00082 $fauxReq = new FauxRequest ( $fauxReqArr );
00083
00084
00085 $module = new ApiMain($fauxReq);
00086 $module->execute();
00087
00088
00089 $data = $module->getResultData();
00090
00091 $feedItems = array ();
00092 foreach ((array)$data['query']['watchlist'] as $info) {
00093 $feedItems[] = $this->createFeedItem($info);
00094 }
00095
00096 $feedTitle = $wgSitename . ' - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
00097 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullUrl();
00098
00099 $feed = new $wgFeedClasses[$params['feedformat']] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl);
00100
00101 ApiFormatFeedWrapper :: setResult($this->getResult(), $feed, $feedItems);
00102
00103 } catch (Exception $e) {
00104
00105
00106 $this->getMain()->setCacheMaxAge(0);
00107
00108 $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
00109 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullUrl();
00110
00111 $feedFormat = isset($params['feedformat']) ? $params['feedformat'] : 'rss';
00112 $feed = new $wgFeedClasses[$feedFormat] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl);
00113
00114
00115 if ($e instanceof UsageException) {
00116 $errorCode = $e->getCodeString();
00117 } else {
00118
00119 $errorCode = 'internal_api_error';
00120 }
00121
00122 $errorText = $e->getMessage();
00123 $feedItems[] = new FeedItem("Error ($errorCode)", $errorText, "", "", "");
00124 ApiFormatFeedWrapper :: setResult($this->getResult(), $feed, $feedItems);
00125 }
00126 }
00127
00128 private function createFeedItem($info) {
00129 $titleStr = $info['title'];
00130 $title = Title :: newFromText($titleStr);
00131 $titleUrl = $title->getFullUrl();
00132 $comment = isset( $info['comment'] ) ? $info['comment'] : null;
00133 $timestamp = $info['timestamp'];
00134 $user = $info['user'];
00135
00136 $completeText = "$comment ($user)";
00137
00138 return new FeedItem($titleStr, $completeText, $titleUrl, $timestamp, $user);
00139 }
00140
00141 public function getAllowedParams() {
00142 global $wgFeedClasses;
00143 $feedFormatNames = array_keys($wgFeedClasses);
00144 return array (
00145 'feedformat' => array (
00146 ApiBase :: PARAM_DFLT => 'rss',
00147 ApiBase :: PARAM_TYPE => $feedFormatNames
00148 ),
00149 'hours' => array (
00150 ApiBase :: PARAM_DFLT => 24,
00151 ApiBase :: PARAM_TYPE => 'integer',
00152 ApiBase :: PARAM_MIN => 1,
00153 ApiBase :: PARAM_MAX => 72,
00154 ),
00155 'allrev' => null
00156 );
00157 }
00158
00159 public function getParamDescription() {
00160 return array (
00161 'feedformat' => 'The format of the feed',
00162 'hours' => 'List pages modified within this many hours from now',
00163 'allrev' => 'Include multiple revisions of the same page within given timeframe.'
00164 );
00165 }
00166
00167 public function getDescription() {
00168 return 'This module returns a watchlist feed';
00169 }
00170
00171 protected function getExamples() {
00172 return array (
00173 'api.php?action=feedwatchlist'
00174 );
00175 }
00176
00177 public function getVersion() {
00178 return __CLASS__ . ': $Id: ApiFeedWatchlist.php 46848 2009-02-05 15:31:06Z catrope $';
00179 }
00180 }