r83744 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r83743‎ | r83744 | r83745 >
Date:02:26, 12 March 2011
Author:saper
Status:ok (Comments)
Tags:todo 
Comment:
Followup to r83715: use internal HTTP client, handle exception when previewing

* Replace own HTTP client with the generic HTTP::get (as per code review)
* SpecialBannerLoaderException needs to be handled when previewing
banners. This exception needs to handled in the user interface,
currently a newly introduced 'centralnotice-nopreview' message
is displayed in case of banner generation failure.

Further work:
* Unfortunately, UI code is full of duplication and should
probably be refactored.
* getDonationAmount() and friends should factored out
of the SpecialBannerLoader
Modified paths:
  • /trunk/extensions/CentralNotice/CentralNotice.i18n.php (modified) (history)
  • /trunk/extensions/CentralNotice/SpecialBannerLoader.php (modified) (history)
  • /trunk/extensions/CentralNotice/SpecialCentralNotice.php (modified) (history)
  • /trunk/extensions/CentralNotice/SpecialNoticeTemplate.php (modified) (history)
  • /trunk/extensions/CentralNotice/TemplatePager.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CentralNotice/TemplatePager.php
@@ -57,12 +57,17 @@
5858 $render = new SpecialBannerLoader();
5959 $render->siteName = 'Wikipedia';
6060 $render->language = $this->mRequest->getVal( 'wpUserLanguage' );
 61+ try {
 62+ $preview = $render->getHtmlNotice( $row->tmp_name );
 63+ } catch ( SpecialBannerLoaderException $e ) {
 64+ $preview = wfMsg( 'centralnotice-nopreview' );
 65+ }
6166 $htmlOut .= Xml::tags( 'td', array( 'valign' => 'top' ),
6267 $this->getSkin()->makeLinkObj( $this->viewPage,
6368 htmlspecialchars( $row->tmp_name ),
6469 'template=' . urlencode( $row->tmp_name ) ) .
6570 Xml::fieldset( wfMsg( 'centralnotice-preview' ),
66 - $render->getHtmlNotice( $row->tmp_name ),
 71+ $preview,
6772 array( 'class' => 'cn-bannerpreview')
6873 )
6974 );
Index: trunk/extensions/CentralNotice/SpecialCentralNotice.php
@@ -995,16 +995,23 @@
996996 );
997997
998998 $viewPage = $this->getTitleFor( 'NoticeTemplate', 'view' );
 999+
 1000+ /* XXX this code is duplicated in the CentralNoticePager::formatRow */
9991001 $render = new SpecialBannerLoader();
10001002 $render->siteName = 'Wikipedia';
10011003 global $wgRequest;
10021004 $render->language = $wgRequest->getVal( 'wpUserLanguage' );
 1005+ try {
 1006+ $preview = $render->getHtmlNotice( $row->tmp_name );
 1007+ } catch ( SpecialBannerLoaderException $e ) {
 1008+ $preview = wfMsg( 'centralnotice-nopreview' );
 1009+ }
10031010 $htmlOut .= Xml::tags( 'td', array( 'valign' => 'top' ),
10041011 $sk->makeLinkObj( $viewPage,
10051012 htmlspecialchars( $row->tmp_name ),
10061013 'template=' . urlencode( $row->tmp_name ) ) .
10071014 Xml::fieldset( wfMsg( 'centralnotice-preview' ),
1008 - $render->getHtmlNotice( $row->tmp_name ),
 1015+ $preview,
10091016 array( 'class' => 'cn-bannerpreview')
10101017 )
10111018 );
@@ -1807,12 +1814,17 @@
18081815 $render = new SpecialBannerLoader();
18091816 $render->siteName = 'Wikipedia';
18101817 $render->language = $this->mRequest->getVal( 'wpUserLanguage' );
 1818+ try {
 1819+ $preview = $render->getHtmlNotice( $row->tmp_name );
 1820+ } catch ( SpecialBannerLoaderException $e ) {
 1821+ $preview = wfMsg( 'centralnotice-nopreview' );
 1822+ }
18111823 $htmlOut .= Xml::tags( 'td', array( 'valign' => 'top' ),
18121824 $this->getSkin()->makeLinkObj( $this->viewPage,
18131825 htmlspecialchars( $row->tmp_name ),
18141826 'template=' . urlencode( $row->tmp_name ) ) .
18151827 Xml::fieldset( wfMsg( 'centralnotice-preview' ),
1816 - $render->getHtmlNotice( $row->tmp_name ),
 1828+ $preview,
18171829 array( 'class' => 'cn-bannerpreview')
18181830 )
18191831 );
Index: trunk/extensions/CentralNotice/SpecialBannerLoader.php
@@ -59,6 +59,7 @@
6060 * Generate the JS for the requested banner
6161 * @return a string of Javascript containing a call to insertBanner()
6262 * with JSON containing the banner content as the parameter
 63+ * @throws SpecialBannerLoaderException
6364 */
6465 function getJsNotice( $bannerName ) {
6566 // Make sure the banner exists
@@ -78,6 +79,7 @@
7980
8081 /**
8182 * Generate the HTML for the requested banner
 83+ * @throws SpecialBannerLoaderException
8284 */
8385 function getHtmlNotice( $bannerName ) {
8486 // Make sure the banner exists
@@ -105,6 +107,7 @@
106108 * Extract a message name and send to getMessage() for translation
107109 * @param $match A message array with 2 members: raw match, short name of message
108110 * @return translated messsage string
 111+ * @throws SpecialBannerLoaderException
109112 */
110113 function getNoticeField( $match ) {
111114 $field = $match[1];
@@ -156,22 +159,9 @@
157160 return $out;
158161 }
159162
160 - private function fetchUrl($url) {
161 - $ctx = stream_context_create(array('http' => array(
162 - 'method' => "GET",
163 - 'header' => "User-Agent: CentralNotice/1.0 (+http://www.mediawiki.org/wiki/Extension:CentralNotice)\r\n")
164 - ));
165 - wfSuppressWarnings();
166 - $content = file_get_contents( $url, false, $ctx);
167 - wfRestoreWarnings();
168 - if( !$content ) {
169 - throw new RemoteServerProblemException();
170 - }
171 - return $content;
172 - }
173 -
174163 /**
175164 * Pull the current amount raised during a fundraiser
 165+ * @throws SpecialBannerLoaderException
176166 */
177167 private function getDonationAmount() {
178168 global $wgNoticeCounterSource, $wgMemc;
@@ -179,7 +169,11 @@
180170 $count = intval( $wgMemc->get( wfMemcKey( 'centralnotice', 'counter' ) ) );
181171 if ( !$count ) {
182172 // Pull from dynamic counter
183 - $count = intval( $this->fetchUrl( $wgNoticeCounterSource ));
 173+ $counter_value = Http::get( $wgNoticeCounterSource );
 174+ if( !$counter_value ) {
 175+ throw new RemoteServerProblemException();
 176+ }
 177+ $count = intval( $counter_value );
184178 if ( !$count ) {
185179 // Pull long-cached amount
186180 $count = intval( $wgMemc->get(
Index: trunk/extensions/CentralNotice/CentralNotice.i18n.php
@@ -25,6 +25,7 @@
2626 'centralnotice-modify' => 'Submit',
2727 'centralnotice-save-banner' => 'Save banner',
2828 'centralnotice-preview' => 'Preview',
 29+ 'centralnotice-nopreview' => '(Preview not available)',
2930 'centralnotice-add-new' => 'Add a new campaign',
3031 'centralnotice-remove' => 'Remove',
3132 'centralnotice-translate-heading' => 'Translation for $1',
@@ -167,6 +168,7 @@
168169 'centralnotice-modify' => '{{Identical|Submit}}',
169170 'centralnotice-save-banner' => 'Label for the submit button which saves a CentralNotice banner.',
170171 'centralnotice-preview' => '{{Identical|Preview}}',
 172+ 'centralnotice-nopreview' => '{{Identical|Nopreview}}',
171173 'centralnotice-remove' => '{{Identical|Remove}}',
172174 'centralnotice-translate-heading' => 'Fieldset label. $1 is a name of a template.',
173175 'centralnotice-add' => '{{Identical|Add}}',
Index: trunk/extensions/CentralNotice/SpecialNoticeTemplate.php
@@ -338,14 +338,19 @@
339339 $render = new SpecialBannerLoader();
340340 $render->siteName = 'Wikipedia';
341341 $render->language = $wpUserLang;
 342+ try {
 343+ $preview = $render->getHtmlNotice( $wgRequest->getText( 'template' ) );
 344+ } catch ( SpecialBannerLoaderException $e ) {
 345+ $preview = wfMsg( 'centralnotice-nopreview' );
 346+ }
342347 if ( $render->language != '' ) {
343348 $htmlOut .= Xml::fieldset(
344349 wfMsg( 'centralnotice-preview' ) . " ($render->language)",
345 - $render->getHtmlNotice( $wgRequest->getText( 'template' ) )
 350+ $preview
346351 );
347352 } else {
348353 $htmlOut .= Xml::fieldset( wfMsg( 'centralnotice-preview' ),
349 - $render->getHtmlNotice( $wgRequest->getText( 'template' ) )
 354+ $preview
350355 );
351356 }
352357
@@ -628,12 +633,17 @@
629634 $render = new SpecialBannerLoader();
630635 $render->siteName = 'Wikipedia';
631636 $render->language = $lang;
 637+ try {
 638+ $preview = $render->getHtmlNotice( $template );
 639+ } catch ( SpecialBannerLoaderException $e ) {
 640+ $preview = wfMsg( 'centralnotice-nopreview' );
 641+ }
632642 $htmlOut .= Xml::tags( 'td', array( 'valign' => 'top' ),
633643 $sk->makeLinkObj( $viewPage,
634644 $lang,
635645 'template=' . urlencode( $template ) . "&wpUserLanguage=$lang" ) .
636646 Xml::fieldset( wfMsg( 'centralnotice-preview' ),
637 - $render->getHtmlNotice( $template ),
 647+ $preview,
638648 array( 'class' => 'cn-bannerpreview')
639649 )
640650 );

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r83715Improve fetching of the donation amount, remove unecessary globals....saper20:29, 11 March 2011

Comments

#Comment by Brion VIBBER (talk | contribs)   18:04, 27 June 2011

This adds a lot of duplication -- the same try/catch block and dual calls to getHtmlNotice & wfMsg are done over and over. It looks like that try/catch block might belong *in* getHtmlNotice...?

#Comment by Brion VIBBER (talk | contribs)   18:08, 27 June 2011

Marking this as 'todo' for now; there is code duplication that's unneeded but it looks harmless. kaldari will follow up on it later with additional cleanup.

Status & tagging log