MediaWiki
master
|
00001 <?php 00030 class ApiFeedContributions extends ApiBase { 00031 00032 public function __construct( $main, $action ) { 00033 parent::__construct( $main, $action ); 00034 } 00035 00041 public function getCustomPrinter() { 00042 return new ApiFormatFeedWrapper( $this->getMain() ); 00043 } 00044 00045 public function execute() { 00046 $params = $this->extractRequestParams(); 00047 00048 global $wgFeed, $wgFeedClasses, $wgSitename, $wgLanguageCode; 00049 00050 if( !$wgFeed ) { 00051 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' ); 00052 } 00053 00054 if( !isset( $wgFeedClasses[ $params['feedformat'] ] ) ) { 00055 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' ); 00056 } 00057 00058 global $wgMiserMode; 00059 if ( $params['showsizediff'] && $wgMiserMode ) { 00060 $this->dieUsage( 'Size difference is disabled in Miser Mode', 'sizediffdisabled' ); 00061 } 00062 00063 $msg = wfMessage( 'Contributions' )->inContentLanguage()->text(); 00064 $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']'; 00065 $feedUrl = SpecialPage::getTitleFor( 'Contributions', $params['user'] )->getFullURL(); 00066 00067 $target = $params['user'] == 'newbies' 00068 ? 'newbies' 00069 : Title::makeTitleSafe( NS_USER, $params['user'] )->getText(); 00070 00071 $feed = new $wgFeedClasses[$params['feedformat']] ( 00072 $feedTitle, 00073 htmlspecialchars( $msg ), 00074 $feedUrl 00075 ); 00076 00077 $pager = new ContribsPager( $this->getContext(), array( 00078 'target' => $target, 00079 'namespace' => $params['namespace'], 00080 'year' => $params['year'], 00081 'month' => $params['month'], 00082 'tagFilter' => $params['tagfilter'], 00083 'deletedOnly' => $params['deletedonly'], 00084 'topOnly' => $params['toponly'], 00085 'showSizeDiff' => $params['showsizediff'], 00086 ) ); 00087 00088 $feedItems = array(); 00089 if( $pager->getNumRows() > 0 ) { 00090 foreach ( $pager->mResult as $row ) { 00091 $feedItems[] = $this->feedItem( $row ); 00092 } 00093 } 00094 00095 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems ); 00096 } 00097 00098 protected function feedItem( $row ) { 00099 $title = Title::makeTitle( intval( $row->page_namespace ), $row->page_title ); 00100 if( $title ) { 00101 $date = $row->rev_timestamp; 00102 $comments = $title->getTalkPage()->getFullURL(); 00103 $revision = Revision::newFromRow( $row ); 00104 00105 return new FeedItem( 00106 $title->getPrefixedText(), 00107 $this->feedItemDesc( $revision ), 00108 $title->getFullURL(), 00109 $date, 00110 $this->feedItemAuthor( $revision ), 00111 $comments 00112 ); 00113 } else { 00114 return null; 00115 } 00116 } 00117 00122 protected function feedItemAuthor( $revision ) { 00123 return $revision->getUserText(); 00124 } 00125 00130 protected function feedItemDesc( $revision ) { 00131 if( $revision ) { 00132 $msg = wfMessage( 'colon-separator' )->inContentLanguage()->text(); 00133 $content = $revision->getContent(); 00134 00135 if ( $content instanceof TextContent ) { 00136 // only textual content has a "source view". 00137 $html = nl2br( htmlspecialchars( $content->getNativeData() ) ); 00138 } else { 00139 //XXX: we could get an HTML representation of the content via getParserOutput, but that may 00140 // contain JS magic and generally may not be suitable for inclusion in a feed. 00141 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method. 00142 //Compare also FeedUtils::formatDiffRow. 00143 $html = ''; 00144 } 00145 00146 return '<p>' . htmlspecialchars( $revision->getUserText() ) . $msg . 00147 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) . 00148 "</p>\n<hr />\n<div>" . $html . "</div>"; 00149 } 00150 return ''; 00151 } 00152 00153 public function getAllowedParams() { 00154 global $wgFeedClasses; 00155 $feedFormatNames = array_keys( $wgFeedClasses ); 00156 return array ( 00157 'feedformat' => array( 00158 ApiBase::PARAM_DFLT => 'rss', 00159 ApiBase::PARAM_TYPE => $feedFormatNames 00160 ), 00161 'user' => array( 00162 ApiBase::PARAM_TYPE => 'user', 00163 ApiBase::PARAM_REQUIRED => true, 00164 ), 00165 'namespace' => array( 00166 ApiBase::PARAM_TYPE => 'namespace' 00167 ), 00168 'year' => array( 00169 ApiBase::PARAM_TYPE => 'integer' 00170 ), 00171 'month' => array( 00172 ApiBase::PARAM_TYPE => 'integer' 00173 ), 00174 'tagfilter' => array( 00175 ApiBase::PARAM_ISMULTI => true, 00176 ApiBase::PARAM_TYPE => array_values( ChangeTags::listDefinedTags() ), 00177 ApiBase::PARAM_DFLT => '', 00178 ), 00179 'deletedonly' => false, 00180 'toponly' => false, 00181 'showsizediff' => false, 00182 ); 00183 } 00184 00185 public function getParamDescription() { 00186 return array( 00187 'feedformat' => 'The format of the feed', 00188 'user' => 'What users to get the contributions for', 00189 'namespace' => 'What namespace to filter the contributions by', 00190 'year' => 'From year (and earlier)', 00191 'month' => 'From month (and earlier)', 00192 'tagfilter' => 'Filter contributions that have these tags', 00193 'deletedonly' => 'Show only deleted contributions', 00194 'toponly' => 'Only show edits that are latest revisions', 00195 'showsizediff' => 'Show the size difference between revisions. Disabled in Miser Mode', 00196 ); 00197 } 00198 00199 public function getDescription() { 00200 return 'Returns a user contributions feed'; 00201 } 00202 00203 public function getPossibleErrors() { 00204 return array_merge( parent::getPossibleErrors(), array( 00205 array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ), 00206 array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ), 00207 array( 'code' => 'sizediffdisabled', 'info' => 'Size difference is disabled in Miser Mode' ), 00208 ) ); 00209 } 00210 00211 public function getExamples() { 00212 return array( 00213 'api.php?action=feedcontributions&user=Reedy', 00214 ); 00215 } 00216 00217 public function getVersion() { 00218 return __CLASS__ . ': $Id$'; 00219 } 00220 }