r114047 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114046‎ | r114047 | r114048 >
Date:02:23, 17 March 2012
Author:bsitu
Status:reverted (Comments)
Tags:gerritmigration 
Comment:
fix for bug33214 - catch all exceptions in api execute and provides necessary parameters required by the custom printer
Modified paths:
  • /trunk/phase3/includes/api/ApiFeedContributions.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiFeedContributions.php
@@ -43,56 +43,80 @@
4444 }
4545
4646 public function execute() {
47 - $params = $this->extractRequestParams();
4847
4948 global $wgFeed, $wgFeedClasses, $wgSitename, $wgLanguageCode;
5049
51 - if( !$wgFeed ) {
52 - $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
53 - }
 50+ try {
 51+ $params = $this->extractRequestParams();
 52+
 53+ if( !$wgFeed ) {
 54+ $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
 55+ }
 56+
 57+ if( !isset( $wgFeedClasses[ $params['feedformat'] ] ) ) {
 58+ $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
 59+ }
5460
55 - if( !isset( $wgFeedClasses[ $params['feedformat'] ] ) ) {
56 - $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
57 - }
 61+ global $wgMiserMode;
 62+ if ( $params['showsizediff'] && $wgMiserMode ) {
 63+ $this->dieUsage( 'Size difference is disabled in Miser Mode', 'sizediffdisabled' );
 64+ }
 65+
 66+ $msg = wfMsgForContent( 'Contributions' );
 67+ $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']';
 68+ $feedUrl = SpecialPage::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
 69+
 70+ $target = $params['user'] == 'newbies'
 71+ ? 'newbies'
 72+ : Title::makeTitleSafe( NS_USER, $params['user'] )->getText();
 73+
 74+ $feed = new $wgFeedClasses[$params['feedformat']] (
 75+ $feedTitle,
 76+ htmlspecialchars( $msg ),
 77+ $feedUrl
 78+ );
 79+
 80+ $pager = new ContribsPager( $this->getContext(), array(
 81+ 'target' => $target,
 82+ 'namespace' => $params['namespace'],
 83+ 'year' => $params['year'],
 84+ 'month' => $params['month'],
 85+ 'tagFilter' => $params['tagfilter'],
 86+ 'deletedOnly' => $params['deletedonly'],
 87+ 'topOnly' => $params['toponly'],
 88+ 'showSizeDiff' => $params['showsizediff'],
 89+ ) );
 90+
 91+ $feedItems = array();
 92+ if( $pager->getNumRows() > 0 ) {
 93+ foreach ( $pager->mResult as $row ) {
 94+ $feedItems[] = $this->feedItem( $row );
 95+ }
 96+ }
 97+
 98+ ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
 99+
 100+ } catch ( Exception $e ) {
 101+ // Error results should not be cached
 102+ $this->getMain()->setCacheMaxAge( 0 );
58103
59 - global $wgMiserMode;
60 - if ( $params['showsizediff'] && $wgMiserMode ) {
61 - $this->dieUsage( 'Size difference is disabled in Miser Mode', 'sizediffdisabled' );
62 - }
 104+ $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent( 'contributions' ) . ' [' . $wgLanguageCode . ']';
 105+ $feedUrl = SpecialPage::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
63106
64 - $msg = wfMsgForContent( 'Contributions' );
65 - $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']';
66 - $feedUrl = SpecialPage::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
 107+ $feedFormat = isset( $params['feedformat'] ) ? $params['feedformat'] : 'rss';
 108+ $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'contributions' ) ), $feedUrl );
67109
68 - $target = $params['user'] == 'newbies'
69 - ? 'newbies'
70 - : Title::makeTitleSafe( NS_USER, $params['user'] )->getText();
 110+ if ( $e instanceof UsageException ) {
 111+ $errorCode = $e->getCodeString();
 112+ } else {
 113+ // Something is seriously wrong
 114+ $errorCode = 'internal_api_error';
 115+ }
71116
72 - $feed = new $wgFeedClasses[$params['feedformat']] (
73 - $feedTitle,
74 - htmlspecialchars( $msg ),
75 - $feedUrl
76 - );
77 -
78 - $pager = new ContribsPager( $this->getContext(), array(
79 - 'target' => $target,
80 - 'namespace' => $params['namespace'],
81 - 'year' => $params['year'],
82 - 'month' => $params['month'],
83 - 'tagFilter' => $params['tagfilter'],
84 - 'deletedOnly' => $params['deletedonly'],
85 - 'topOnly' => $params['toponly'],
86 - 'showSizeDiff' => $params['showsizediff'],
87 - ) );
88 -
89 - $feedItems = array();
90 - if( $pager->getNumRows() > 0 ) {
91 - foreach ( $pager->mResult as $row ) {
92 - $feedItems[] = $this->feedItem( $row );
93 - }
 117+ $errorText = $e->getMessage();
 118+ $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
 119+ ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
94120 }
95 -
96 - ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
97121 }
98122
99123 protected function feedItem( $row ) {

Follow-up revisions

RevisionCommit summaryAuthorDate
r114335Revert r107309, r113601, r113704, r113742, r113792, r113838, r113859, r113893......catrope00:16, 21 March 2012

Comments

#Comment by Bawolff (talk | contribs)   02:42, 17 March 2012

To be honest, it kind of seems odd to return an RSS feed in case of an error, since errors can't really be expressed in that format. At the very least if an rss feed is returned in error conditions it should be a non-200 status code imho.

#Comment by Bsitu (talk | contribs)   00:36, 24 March 2012

Thank you for the comment, I agree with what you said, that means the custom printer needs to be altered to some other printers on error since the custom printer requires some additional parameters which exception does not provide, that doesn't seem to be an easy fix to me as the API can be executed in a mode with exception not being caught and there looks quite a lot of dependencies in ApiMain.php, However, there may be an easy fix to this which I don't see so I am open to any feedbacks, :)

#Comment by Hashar (talk | contribs)   12:48, 22 March 2012

Change was copied to Gerrit as https://gerrit.wikimedia.org/r/3328

Follow up there :)

Status & tagging log