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