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