PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/api/ApiFeedWatchlist.php

https://gitlab.com/qiusct/mediawiki-i
PHP | 280 lines | 181 code | 40 blank | 59 comment | 18 complexity | 0bead26243f27d248cbe0dc205b33ce9 MD5 | raw file
Possible License(s): Apache-2.0, MIT, GPL-2.0
  1. <?php
  2. /**
  3. *
  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. * This action allows users to get their watchlist items in RSS/Atom formats.
  28. * When executed, it performs a nested call to the API to get the needed data,
  29. * and formats it in a proper format.
  30. *
  31. * @ingroup API
  32. */
  33. class ApiFeedWatchlist extends ApiBase {
  34. private $watchlistModule = null;
  35. private $linkToSections = false;
  36. /**
  37. * This module uses a custom feed wrapper printer.
  38. *
  39. * @return ApiFormatFeedWrapper
  40. */
  41. public function getCustomPrinter() {
  42. return new ApiFormatFeedWrapper( $this->getMain() );
  43. }
  44. /**
  45. * Make a nested call to the API to request watchlist items in the last $hours.
  46. * Wrap the result as an RSS/Atom feed.
  47. */
  48. public function execute() {
  49. global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
  50. try {
  51. $params = $this->extractRequestParams();
  52. if ( !$wgFeed ) {
  53. $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
  54. }
  55. if ( !isset( $wgFeedClasses[$params['feedformat']] ) ) {
  56. $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
  57. }
  58. // limit to the number of hours going from now back
  59. $endTime = wfTimestamp( TS_MW, time() - intval( $params['hours'] * 60 * 60 ) );
  60. // Prepare parameters for nested request
  61. $fauxReqArr = array(
  62. 'action' => 'query',
  63. 'meta' => 'siteinfo',
  64. 'siprop' => 'general',
  65. 'list' => 'watchlist',
  66. 'wlprop' => 'title|user|comment|timestamp|ids',
  67. 'wldir' => 'older', // reverse order - from newest to oldest
  68. 'wlend' => $endTime, // stop at this time
  69. 'wllimit' => min( 50, $wgFeedLimit )
  70. );
  71. if ( $params['wlowner'] !== null ) {
  72. $fauxReqArr['wlowner'] = $params['wlowner'];
  73. }
  74. if ( $params['wltoken'] !== null ) {
  75. $fauxReqArr['wltoken'] = $params['wltoken'];
  76. }
  77. if ( $params['wlexcludeuser'] !== null ) {
  78. $fauxReqArr['wlexcludeuser'] = $params['wlexcludeuser'];
  79. }
  80. if ( $params['wlshow'] !== null ) {
  81. $fauxReqArr['wlshow'] = $params['wlshow'];
  82. }
  83. if ( $params['wltype'] !== null ) {
  84. $fauxReqArr['wltype'] = $params['wltype'];
  85. }
  86. // Support linking directly to sections when possible
  87. // (possible only if section name is present in comment)
  88. if ( $params['linktosections'] ) {
  89. $this->linkToSections = true;
  90. }
  91. // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
  92. if ( $params['allrev'] ) {
  93. $fauxReqArr['wlallrev'] = '';
  94. }
  95. // Create the request
  96. $fauxReq = new FauxRequest( $fauxReqArr );
  97. // Execute
  98. $module = new ApiMain( $fauxReq );
  99. $module->execute();
  100. // Get data array
  101. $data = $module->getResultData();
  102. $feedItems = array();
  103. foreach ( (array)$data['query']['watchlist'] as $info ) {
  104. $feedItems[] = $this->createFeedItem( $info );
  105. }
  106. $msg = wfMessage( 'watchlist' )->inContentLanguage()->text();
  107. $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']';
  108. $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
  109. $feed = new $wgFeedClasses[$params['feedformat']] (
  110. $feedTitle,
  111. htmlspecialchars( $msg ),
  112. $feedUrl
  113. );
  114. ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
  115. } catch ( Exception $e ) {
  116. // Error results should not be cached
  117. $this->getMain()->setCacheMaxAge( 0 );
  118. // @todo FIXME: Localise brackets
  119. $feedTitle = $wgSitename . ' - Error - ' .
  120. wfMessage( 'watchlist' )->inContentLanguage()->text() .
  121. ' [' . $wgLanguageCode . ']';
  122. $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
  123. $feedFormat = isset( $params['feedformat'] ) ? $params['feedformat'] : 'rss';
  124. $msg = wfMessage( 'watchlist' )->inContentLanguage()->escaped();
  125. $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, $msg, $feedUrl );
  126. if ( $e instanceof UsageException ) {
  127. $errorCode = $e->getCodeString();
  128. } else {
  129. // Something is seriously wrong
  130. $errorCode = 'internal_api_error';
  131. }
  132. $errorText = $e->getMessage();
  133. $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
  134. ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
  135. }
  136. }
  137. /**
  138. * @param array $info
  139. * @return FeedItem
  140. */
  141. private function createFeedItem( $info ) {
  142. $titleStr = $info['title'];
  143. $title = Title::newFromText( $titleStr );
  144. if ( isset( $info['revid'] ) ) {
  145. $titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) );
  146. } else {
  147. $titleUrl = $title->getFullURL();
  148. }
  149. $comment = isset( $info['comment'] ) ? $info['comment'] : null;
  150. // Create an anchor to section.
  151. // The anchor won't work for sections that have dupes on page
  152. // as there's no way to strip that info from ApiWatchlist (apparently?).
  153. // RegExp in the line below is equal to Linker::formatAutocomments().
  154. if ( $this->linkToSections && $comment !== null &&
  155. preg_match( '!(.*)/\*\s*(.*?)\s*\*/(.*)!', $comment, $matches )
  156. ) {
  157. global $wgParser;
  158. $sectionTitle = $wgParser->stripSectionName( $matches[2] );
  159. $sectionTitle = Sanitizer::normalizeSectionNameWhitespace( $sectionTitle );
  160. $titleUrl .= Title::newFromText( '#' . $sectionTitle )->getFragmentForURL();
  161. }
  162. $timestamp = $info['timestamp'];
  163. $user = $info['user'];
  164. $completeText = "$comment ($user)";
  165. return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
  166. }
  167. private function getWatchlistModule() {
  168. if ( $this->watchlistModule === null ) {
  169. $this->watchlistModule = $this->getMain()->getModuleManager()->getModule( 'query' )
  170. ->getModuleManager()->getModule( 'watchlist' );
  171. }
  172. return $this->watchlistModule;
  173. }
  174. public function getAllowedParams( $flags = 0 ) {
  175. global $wgFeedClasses;
  176. $feedFormatNames = array_keys( $wgFeedClasses );
  177. $ret = array(
  178. 'feedformat' => array(
  179. ApiBase::PARAM_DFLT => 'rss',
  180. ApiBase::PARAM_TYPE => $feedFormatNames
  181. ),
  182. 'hours' => array(
  183. ApiBase::PARAM_DFLT => 24,
  184. ApiBase::PARAM_TYPE => 'integer',
  185. ApiBase::PARAM_MIN => 1,
  186. ApiBase::PARAM_MAX => 72,
  187. ),
  188. 'linktosections' => false,
  189. );
  190. if ( $flags ) {
  191. $wlparams = $this->getWatchlistModule()->getAllowedParams( $flags );
  192. $ret['allrev'] = $wlparams['allrev'];
  193. $ret['wlowner'] = $wlparams['owner'];
  194. $ret['wltoken'] = $wlparams['token'];
  195. $ret['wlshow'] = $wlparams['show'];
  196. $ret['wltype'] = $wlparams['type'];
  197. $ret['wlexcludeuser'] = $wlparams['excludeuser'];
  198. } else {
  199. $ret['allrev'] = null;
  200. $ret['wlowner'] = null;
  201. $ret['wltoken'] = null;
  202. $ret['wlshow'] = null;
  203. $ret['wltype'] = null;
  204. $ret['wlexcludeuser'] = null;
  205. }
  206. return $ret;
  207. }
  208. public function getParamDescription() {
  209. $wldescr = $this->getWatchlistModule()->getParamDescription();
  210. return array(
  211. 'feedformat' => 'The format of the feed',
  212. 'hours' => 'List pages modified within this many hours from now',
  213. 'linktosections' => 'Link directly to changed sections if possible',
  214. 'allrev' => $wldescr['allrev'],
  215. 'wlowner' => $wldescr['owner'],
  216. 'wltoken' => $wldescr['token'],
  217. 'wlshow' => $wldescr['show'],
  218. 'wltype' => $wldescr['type'],
  219. 'wlexcludeuser' => $wldescr['excludeuser'],
  220. );
  221. }
  222. public function getDescription() {
  223. return 'Returns a watchlist feed.';
  224. }
  225. public function getPossibleErrors() {
  226. return array_merge( parent::getPossibleErrors(), array(
  227. array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
  228. array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
  229. ) );
  230. }
  231. public function getExamples() {
  232. return array(
  233. 'api.php?action=feedwatchlist',
  234. 'api.php?action=feedwatchlist&allrev=&hours=6'
  235. );
  236. }
  237. public function getHelpUrls() {
  238. return 'https://www.mediawiki.org/wiki/API:Watchlist_feed';
  239. }
  240. }