Index: trunk/extensions/CentralNotice/BannerLoader.php |
— | — | @@ -4,9 +4,9 @@ |
5 | 5 | * Generates banner HTML files |
6 | 6 | */ |
7 | 7 | class BannerLoader extends UnlistedSpecialPage { |
8 | | - public $project = 'wikipedia'; // Project name |
| 8 | + public $siteName = 'Wikipedia'; // Site name |
9 | 9 | 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 |
11 | 11 | protected $maxAge = 0; // No client-side banner caching so we get all impressions |
12 | 12 | protected $contentType = 'text/html'; |
13 | 13 | |
— | — | @@ -24,8 +24,8 @@ |
25 | 25 | // Get user language from the query string |
26 | 26 | $this->language = $wgRequest->getText( 'language', 'en' ); |
27 | 27 | |
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' ); |
30 | 30 | |
31 | 31 | if ( $wgRequest->getText( 'banner' ) ) { |
32 | 32 | $bannerName = $wgRequest->getText( 'banner' ); |
— | — | @@ -64,79 +64,52 @@ |
65 | 65 | } |
66 | 66 | } |
67 | 67 | |
| 68 | + /** |
| 69 | + * Get the body of the banner with only {{int:...}} messages translated |
| 70 | + */ |
68 | 71 | function getNoticeTemplate() { |
69 | | - return $this->getMessage( "centralnotice-template-{$this->bannerName}" ); |
| 72 | + $out = $this->getMessage( "centralnotice-template-{$this->bannerName}" ); |
| 73 | + return $out; |
70 | 74 | } |
71 | 75 | |
72 | 76 | function getNoticeField( $matches ) { |
73 | 77 | $field = $matches[1]; |
74 | | - $params = array(); |
75 | | - if ( $field == 'amount' ) { |
76 | | - $params = array( $this->formatNum( $this->getDonationAmount() ) ); |
77 | | - } |
78 | 78 | $message = "centralnotice-{$this->bannerName}-$field"; |
79 | | - $source = $this->getMessage( $message, $params ); |
| 79 | + $source = $this->getMessage( $message ); |
80 | 80 | return $source; |
81 | 81 | } |
82 | | - |
| 82 | + |
| 83 | + /** |
| 84 | + * Convert number of dollars to millions of dollars |
| 85 | + */ |
83 | 86 | private function formatNum( $num ) { |
84 | 87 | $num = sprintf( "%.1f", $num / 1e6 ); |
85 | 88 | if ( substr( $num, - 2 ) == '.0' ) { |
86 | | - $num = substr( $num, 0, - 2 ); |
| 89 | + $num = substr( $num, 0, - 2 ); |
87 | 90 | } |
88 | 91 | $lang = Language::factory( $this->language ); |
89 | 92 | return $lang->formatNum( $num ); |
90 | 93 | } |
91 | | - |
92 | | - private function getMessage( $msg, $params = array() ) { |
| 94 | + |
| 95 | + private function getMessage( $msg ) { |
| 96 | + global $wgLang; |
| 97 | + |
93 | 98 | // A god-damned dirty hack! :D |
94 | | - $old = array(); |
95 | | - $old['wgSitename'] = $GLOBALS['wgSitename']; |
96 | | - $old['wgLang'] = $GLOBALS['wgLang']; |
| 99 | + $oldLang = $wgLang; |
97 | 100 | |
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' ) ); |
100 | 103 | |
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 | + |
113 | 111 | return $out; |
114 | 112 | } |
115 | 113 | |
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 | | - |
141 | 114 | /** |
142 | 115 | * Pull the current amount raised during a fundraiser |
143 | 116 | */ |
— | — | @@ -154,7 +127,7 @@ |
155 | 128 | $count = intval( $wgMemc->get( 'centralnotice:counter:fallback' ) ); |
156 | 129 | if ( !$count ) { |
157 | 130 | // Return hard-coded amount if all else fails |
158 | | - return 100; // Update as needed during fundraiser |
| 131 | + return 1100000; // Update as needed during fundraiser |
159 | 132 | } |
160 | 133 | } |
161 | 134 | $wgMemc->set( 'centralnotice:counter', $count, 60 ); // Expire in 60 seconds |
Index: trunk/extensions/CentralNotice/SpecialNoticeTemplate.php |
— | — | @@ -317,8 +317,8 @@ |
318 | 318 | $htmlOut .= Xml::element( 'h2', null, wfMsg( 'centralnotice-banner-heading', $currentTemplate ) ); |
319 | 319 | |
320 | 320 | // Show preview of banner |
321 | | - $render = new SpecialNoticeText(); |
322 | | - $render->project = 'wikipedia'; |
| 321 | + $render = new BannerLoader(); |
| 322 | + $render->siteName = 'Wikipedia'; |
323 | 323 | $render->language = $wgRequest->getVal( 'wpUserLanguage' ); |
324 | 324 | if ( $render->language != '' ) { |
325 | 325 | $htmlOut .= Xml::fieldset( wfMsg( 'centralnotice-preview' ) . " ($render->language)", |