ApiFeedWatchlist.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /*
  3. * Created on Oct 13, 2006
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 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. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ("ApiBase.php");
  27. }
  28. /**
  29. * This action allows users to get their watchlist items in RSS/Atom formats.
  30. * When executed, it performs a nested call to the API to get the needed data,
  31. * and formats it in a proper format.
  32. *
  33. * @ingroup API
  34. */
  35. class ApiFeedWatchlist extends ApiBase {
  36. public function __construct($main, $action) {
  37. parent :: __construct($main, $action);
  38. }
  39. /**
  40. * This module uses a custom feed wrapper printer.
  41. */
  42. public function getCustomPrinter() {
  43. return new ApiFormatFeedWrapper($this->getMain());
  44. }
  45. /**
  46. * Make a nested call to the API to request watchlist items in the last $hours.
  47. * Wrap the result as an RSS/Atom feed.
  48. */
  49. public function execute() {
  50. global $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgContLanguageCode;
  51. try {
  52. $params = $this->extractRequestParams();
  53. // limit to the number of hours going from now back
  54. $endTime = wfTimestamp(TS_MW, time() - intval($params['hours'] * 60 * 60));
  55. $dbr = wfGetDB( DB_SLAVE );
  56. // Prepare parameters for nested request
  57. $fauxReqArr = array (
  58. 'action' => 'query',
  59. 'meta' => 'siteinfo',
  60. 'siprop' => 'general',
  61. 'list' => 'watchlist',
  62. 'wlprop' => 'title|user|comment|timestamp',
  63. 'wldir' => 'older', // reverse order - from newest to oldest
  64. 'wlend' => $dbr->timestamp($endTime), // stop at this time
  65. 'wllimit' => (50 > $wgFeedLimit) ? $wgFeedLimit : 50
  66. );
  67. // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
  68. if ( ! is_null ( $params['allrev'] ) ) $fauxReqArr['wlallrev'] = '';
  69. // Create the request
  70. $fauxReq = new FauxRequest ( $fauxReqArr );
  71. // Execute
  72. $module = new ApiMain($fauxReq);
  73. $module->execute();
  74. // Get data array
  75. $data = $module->getResultData();
  76. $feedItems = array ();
  77. foreach ((array)$data['query']['watchlist'] as $info) {
  78. $feedItems[] = $this->createFeedItem($info);
  79. }
  80. $feedTitle = $wgSitename . ' - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
  81. $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullUrl();
  82. $feed = new $wgFeedClasses[$params['feedformat']] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl);
  83. ApiFormatFeedWrapper :: setResult($this->getResult(), $feed, $feedItems);
  84. } catch (Exception $e) {
  85. // Error results should not be cached
  86. $this->getMain()->setCacheMaxAge(0);
  87. $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
  88. $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullUrl();
  89. $feedFormat = isset($params['feedformat']) ? $params['feedformat'] : 'rss';
  90. $feed = new $wgFeedClasses[$feedFormat] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl);
  91. if ($e instanceof UsageException) {
  92. $errorCode = $e->getCodeString();
  93. } else {
  94. // Something is seriously wrong
  95. $errorCode = 'internal_api_error';
  96. }
  97. $errorText = $e->getMessage();
  98. $feedItems[] = new FeedItem("Error ($errorCode)", $errorText, "", "", "");
  99. ApiFormatFeedWrapper :: setResult($this->getResult(), $feed, $feedItems);
  100. }
  101. }
  102. private function createFeedItem($info) {
  103. $titleStr = $info['title'];
  104. $title = Title :: newFromText($titleStr);
  105. $titleUrl = $title->getFullUrl();
  106. $comment = isset( $info['comment'] ) ? $info['comment'] : null;
  107. $timestamp = $info['timestamp'];
  108. $user = $info['user'];
  109. $completeText = "$comment ($user)";
  110. return new FeedItem($titleStr, $completeText, $titleUrl, $timestamp, $user);
  111. }
  112. public function getAllowedParams() {
  113. global $wgFeedClasses;
  114. $feedFormatNames = array_keys($wgFeedClasses);
  115. return array (
  116. 'feedformat' => array (
  117. ApiBase :: PARAM_DFLT => 'rss',
  118. ApiBase :: PARAM_TYPE => $feedFormatNames
  119. ),
  120. 'hours' => array (
  121. ApiBase :: PARAM_DFLT => 24,
  122. ApiBase :: PARAM_TYPE => 'integer',
  123. ApiBase :: PARAM_MIN => 1,
  124. ApiBase :: PARAM_MAX => 72,
  125. ),
  126. 'allrev' => null
  127. );
  128. }
  129. public function getParamDescription() {
  130. return array (
  131. 'feedformat' => 'The format of the feed',
  132. 'hours' => 'List pages modified within this many hours from now',
  133. 'allrev' => 'Include multiple revisions of the same page within given timeframe.'
  134. );
  135. }
  136. public function getDescription() {
  137. return 'This module returns a watchlist feed';
  138. }
  139. protected function getExamples() {
  140. return array (
  141. 'api.php?action=feedwatchlist'
  142. );
  143. }
  144. public function getVersion() {
  145. return __CLASS__ . ': $Id: ApiFeedWatchlist.php 46848 2009-02-05 15:31:06Z catrope $';
  146. }
  147. }