r72304 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72303‎ | r72304 | r72305 >
Date:19:06, 3 September 2010
Author:kaldari
Status:ok (Comments)
Tags:
Comment:
replacing sitename and amount hacks with better method, changing banner preview to use BannerLoader
Modified paths:
  • /trunk/extensions/CentralNotice/BannerLoader.php (modified) (history)
  • /trunk/extensions/CentralNotice/SpecialNoticeTemplate.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CentralNotice/BannerLoader.php
@@ -4,9 +4,9 @@
55 * Generates banner HTML files
66 */
77 class BannerLoader extends UnlistedSpecialPage {
8 - public $project = 'wikipedia'; // Project name
 8+ public $siteName = 'Wikipedia'; // Site name
99 public $language = 'en'; // User language
10 - protected $sharedMaxAge = 22; // Cache for ? hours on the server side
 10+ protected $sharedMaxAge = 22; // Cache for 2 hours on the server side
1111 protected $maxAge = 0; // No client-side banner caching so we get all impressions
1212 protected $contentType = 'text/html';
1313
@@ -24,8 +24,8 @@
2525 // Get user language from the query string
2626 $this->language = $wgRequest->getText( 'language', 'en' );
2727
28 - // Get project name from the query string
29 - $this->project = $wgRequest->getText( 'project', 'wikipedia' );
 28+ // Get site name from the query string
 29+ $this->siteName = $wgRequest->getText( 'site', 'Wikipedia' );
3030
3131 if ( $wgRequest->getText( 'banner' ) ) {
3232 $bannerName = $wgRequest->getText( 'banner' );
@@ -64,79 +64,52 @@
6565 }
6666 }
6767
 68+ /**
 69+ * Get the body of the banner with only {{int:...}} messages translated
 70+ */
6871 function getNoticeTemplate() {
69 - return $this->getMessage( "centralnotice-template-{$this->bannerName}" );
 72+ $out = $this->getMessage( "centralnotice-template-{$this->bannerName}" );
 73+ return $out;
7074 }
7175
7276 function getNoticeField( $matches ) {
7377 $field = $matches[1];
74 - $params = array();
75 - if ( $field == 'amount' ) {
76 - $params = array( $this->formatNum( $this->getDonationAmount() ) );
77 - }
7878 $message = "centralnotice-{$this->bannerName}-$field";
79 - $source = $this->getMessage( $message, $params );
 79+ $source = $this->getMessage( $message );
8080 return $source;
8181 }
82 -
 82+
 83+ /**
 84+ * Convert number of dollars to millions of dollars
 85+ */
8386 private function formatNum( $num ) {
8487 $num = sprintf( "%.1f", $num / 1e6 );
8588 if ( substr( $num, - 2 ) == '.0' ) {
86 - $num = substr( $num, 0, - 2 );
 89+ $num = substr( $num, 0, - 2 );
8790 }
8891 $lang = Language::factory( $this->language );
8992 return $lang->formatNum( $num );
9093 }
91 -
92 - private function getMessage( $msg, $params = array() ) {
 94+
 95+ private function getMessage( $msg ) {
 96+ global $wgLang;
 97+
9398 // A god-damned dirty hack! :D
94 - $old = array();
95 - $old['wgSitename'] = $GLOBALS['wgSitename'];
96 - $old['wgLang'] = $GLOBALS['wgLang'];
 99+ $oldLang = $wgLang;
97100
98 - $GLOBALS['wgSitename'] = $this->projectName();
99 - $GLOBALS['wgLang'] = Language::factory( $this->language ); // hack for {{int:...}}
 101+ $wgLang = Language::factory( $this->language ); // hack for {{int:...}}
 102+ $out = wfMsgExt( $msg, array( 'language' => $this->language, 'parsemag' ) );
100103
101 - $options = array(
102 - 'language' => $this->language,
103 - 'parsemag',
104 - );
105 - array_unshift( $params, $options );
106 - array_unshift( $params, $msg );
107 - $out = call_user_func_array( 'wfMsgExt', $params );
108 -
109 - // Restore globals
110 - $GLOBALS['wgSitename'] = $old['wgSitename'];
111 - $GLOBALS['wgLang'] = $old['wgLang'];
112 -
 104+ // Restore global
 105+ $wgLang = $oldLang;
 106+
 107+ // Replace variables in banner with values
 108+ $out = str_ireplace( '$amount', $this->formatNum( $this->getDonationAmount() ), $out );
 109+ $out = str_ireplace( '$sitename', $this->siteName, $out );
 110+
113111 return $out;
114112 }
115113
116 - private function projectName() {
117 - global $wgConf;
118 -
119 - $wgConf->loadFullData();
120 -
121 - // Special cases for commons and meta who have no lang
122 - if ( $this->project == 'commons' )
123 - return "Commons";
124 - else if ( $this->project == 'meta' )
125 - return "Wikimedia";
126 -
127 - // Guess dbname since we don't have it atm
128 - $dbname = $this->language .
129 - ( ( $this->project == 'wikipedia' ) ? "wiki" : $this->project );
130 - $name = $wgConf->get( 'wgSitename', $dbname, $this->project,
131 - array( 'lang' => $this->language, 'site' => $this->project ) );
132 -
133 - if ( $name ) {
134 - return $name;
135 - } else {
136 - global $wgLang;
137 - return $wgLang->ucfirst( $this->project );
138 - }
139 - }
140 -
141114 /**
142115 * Pull the current amount raised during a fundraiser
143116 */
@@ -154,7 +127,7 @@
155128 $count = intval( $wgMemc->get( 'centralnotice:counter:fallback' ) );
156129 if ( !$count ) {
157130 // Return hard-coded amount if all else fails
158 - return 100; // Update as needed during fundraiser
 131+ return 1100000; // Update as needed during fundraiser
159132 }
160133 }
161134 $wgMemc->set( 'centralnotice:counter', $count, 60 ); // Expire in 60 seconds
Index: trunk/extensions/CentralNotice/SpecialNoticeTemplate.php
@@ -317,8 +317,8 @@
318318 $htmlOut .= Xml::element( 'h2', null, wfMsg( 'centralnotice-banner-heading', $currentTemplate ) );
319319
320320 // Show preview of banner
321 - $render = new SpecialNoticeText();
322 - $render->project = 'wikipedia';
 321+ $render = new BannerLoader();
 322+ $render->siteName = 'Wikipedia';
323323 $render->language = $wgRequest->getVal( 'wpUserLanguage' );
324324 if ( $render->language != '' ) {
325325 $htmlOut .= Xml::fieldset( wfMsg( 'centralnotice-preview' ) . " ($render->language)",

Comments

#Comment by Kaldari (talk | contribs)   19:10, 3 September 2010
+ protected $sharedMaxAge = 22; // Cache for 2 hours on the server side

Obviously this is not currently accurate. I've set it lower than 2 hours for now for testing purposes.

#Comment by Catrope (talk | contribs)   17:48, 8 September 2010
-		return $this->getMessage( "centralnotice-template-{$this->bannerName}" );
+		$out = $this->getMessage( "centralnotice-template-{$this->bannerName}" );
+		return $out;

Why would you do this?

#Comment by Kaldari (talk | contribs)   17:57, 8 September 2010

This is purely to make debugging easier. I'm often var_dumping $out to see what effect various changes have on the banner output. Otherwise it is completely unnecessary, and I'll probably change it back once phase 2 is finished.

Status & tagging log