MediaWiki  REL1_23
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, $wgFeedLimit, $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             'newOnly' => $params['newonly'],
00082             'showSizeDiff' => $params['showsizediff'],
00083         ) );
00084 
00085         if ( $pager->getLimit() > $wgFeedLimit ) {
00086             $pager->setLimit( $wgFeedLimit );
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(),
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         global $wgFeedClasses;
00163         $feedFormatNames = array_keys( $wgFeedClasses );
00164 
00165         return array(
00166             'feedformat' => array(
00167                 ApiBase::PARAM_DFLT => 'rss',
00168                 ApiBase::PARAM_TYPE => $feedFormatNames
00169             ),
00170             'user' => array(
00171                 ApiBase::PARAM_TYPE => 'user',
00172                 ApiBase::PARAM_REQUIRED => true,
00173             ),
00174             'namespace' => array(
00175                 ApiBase::PARAM_TYPE => 'namespace'
00176             ),
00177             'year' => array(
00178                 ApiBase::PARAM_TYPE => 'integer'
00179             ),
00180             'month' => array(
00181                 ApiBase::PARAM_TYPE => 'integer'
00182             ),
00183             'tagfilter' => array(
00184                 ApiBase::PARAM_ISMULTI => true,
00185                 ApiBase::PARAM_TYPE => array_values( ChangeTags::listDefinedTags() ),
00186                 ApiBase::PARAM_DFLT => '',
00187             ),
00188             'deletedonly' => false,
00189             'toponly' => false,
00190             'newonly' => false,
00191             'showsizediff' => false,
00192         );
00193     }
00194 
00195     public function getParamDescription() {
00196         return array(
00197             'feedformat' => 'The format of the feed',
00198             'user' => 'What users to get the contributions for',
00199             'namespace' => 'What namespace to filter the contributions by',
00200             'year' => 'From year (and earlier)',
00201             'month' => 'From month (and earlier)',
00202             'tagfilter' => 'Filter contributions that have these tags',
00203             'deletedonly' => 'Show only deleted contributions',
00204             'toponly' => 'Only show edits that are latest revisions',
00205             'newonly' => 'Only show edits that are page creations',
00206             'showsizediff' => 'Show the size difference between revisions. Disabled in Miser Mode',
00207         );
00208     }
00209 
00210     public function getDescription() {
00211         return 'Returns a user contributions feed.';
00212     }
00213 
00214     public function getPossibleErrors() {
00215         return array_merge( parent::getPossibleErrors(), array(
00216             array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
00217             array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
00218             array( 'code' => 'sizediffdisabled', 'info' => 'Size difference is disabled in Miser Mode' ),
00219         ) );
00220     }
00221 
00222     public function getExamples() {
00223         return array(
00224             'api.php?action=feedcontributions&user=Reedy',
00225         );
00226     }
00227 }