Index: trunk/extensions/CentralNotice/CentralNotice.php |
— | — | @@ -25,11 +25,10 @@ |
26 | 26 | |
27 | 27 | // Enable the notice-hosting infrastructure on this wiki... |
28 | 28 | // Leave at false for wikis that only use a sister site for the control. |
29 | | -// All remaining options apply only to the infrastructure wiki. |
30 | 29 | $wgNoticeInfrastructure = true; |
31 | 30 | |
32 | 31 | // The name of the database which hosts the centralized campaign data |
33 | | -$wgCentralDBname = ''; |
| 32 | +$wgCentralDBname = $wgDBname; |
34 | 33 | |
35 | 34 | // The script path on the wiki that hosts the CentralNotice infrastructure |
36 | 35 | // For example 'http://meta.wikimedia.org/w/index.php' |
— | — | @@ -40,24 +39,8 @@ |
41 | 40 | // for cached content |
42 | 41 | $wgCentralNoticeLoader = true; |
43 | 42 | |
44 | | -// If true, notice only displays if 'sitenotice=yes' is in the query string |
45 | | -$wgNoticeTestMode = false; |
46 | | - |
47 | | -// Array of '$lang.$project' for exceptions to the test mode rule |
48 | | -$wgNoticeEnabledSites = array(); |
49 | | - |
50 | | -// Client-side cache timeout for the loader JS stub. |
51 | | -// If 0, clients will (probably) rechceck it on every hit, |
52 | | -// which is good for testing. |
53 | | -$wgNoticeTimeout = 0; |
54 | | - |
55 | | -// Server-side cache timeout for the loader JS stub. |
56 | | -// Should be big if you won't include the counter info in the text, |
57 | | -// smallish if you will. :) |
58 | | -$wgNoticeServerTimeout = 0; |
59 | | - |
60 | 43 | // Source for live counter information |
61 | | -$wgNoticeCounterSource = "http://donate.wikimedia.org/counter.php"; |
| 44 | +$wgNoticeCounterSource = 'http://wikimediafoundation.org/wiki/Special:ContributionTotal?action=raw'; |
62 | 45 | |
63 | 46 | // Domain to set global cookies for. |
64 | 47 | // Example: '.wikipedia.org' |
Index: trunk/extensions/CentralNotice/SpecialBannerLoader.php |
— | — | @@ -28,14 +28,18 @@ |
29 | 29 | |
30 | 30 | if ( $wgRequest->getText( 'banner' ) ) { |
31 | 31 | $bannerName = $wgRequest->getText( 'banner' ); |
32 | | - $content = $this->getJsNotice( $bannerName ); |
33 | | - if ( preg_match( "/<centralnotice-template-\w+>\z/", $content ) ) { |
34 | | - echo "/* Failed cache lookup */"; |
35 | | - } elseif ( strlen( $content ) == 0 ) { |
36 | | - // Hack for IE/Mac 0-length keepalive problem, see RawPage.php |
37 | | - echo "/* Empty */"; |
38 | | - } else { |
39 | | - echo $content; |
| 32 | + try { |
| 33 | + $content = $this->getJsNotice( $bannerName ); |
| 34 | + if ( preg_match( "/<centralnotice-template-\w+>\z/", $content ) ) { |
| 35 | + echo "/* Failed cache lookup */"; |
| 36 | + } elseif ( strlen( $content ) == 0 ) { |
| 37 | + // Hack for IE/Mac 0-length keepalive problem, see RawPage.php |
| 38 | + echo "/* Empty */"; |
| 39 | + } else { |
| 40 | + echo $content; |
| 41 | + } |
| 42 | + } catch (SpecialBannerLoaderException $e) { |
| 43 | + echo "/* Banner could not be generated */"; |
40 | 44 | } |
41 | 45 | } else { |
42 | 46 | echo "/* No banner specified */"; |
— | — | @@ -106,7 +110,7 @@ |
107 | 111 | $field = $match[1]; |
108 | 112 | $params = array(); |
109 | 113 | if ( $field == 'amount' ) { |
110 | | - $params = array( $this->formatNum( $this->getDonationAmount() ) ); |
| 114 | + $params = array( $this->toMillions( $this->getDonationAmount() ) ); |
111 | 115 | } |
112 | 116 | $message = "centralnotice-{$this->bannerName}-$field"; |
113 | 117 | $source = $this->getMessage( $message, $params ); |
— | — | @@ -116,7 +120,7 @@ |
117 | 121 | /** |
118 | 122 | * Convert number of dollars to millions of dollars |
119 | 123 | */ |
120 | | - private function formatNum( $num ) { |
| 124 | + private function toMillions( $num ) { |
121 | 125 | $num = sprintf( "%.1f", $num / 1e6 ); |
122 | 126 | if ( substr( $num, - 2 ) == '.0' ) { |
123 | 127 | $num = substr( $num, 0, - 2 ); |
— | — | @@ -151,6 +155,20 @@ |
152 | 156 | |
153 | 157 | return $out; |
154 | 158 | } |
| 159 | + |
| 160 | + private function fetchUrl($url) { |
| 161 | + $ctx = stream_context_create('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 | + } |
155 | 173 | |
156 | 174 | /** |
157 | 175 | * Pull the current amount raised during a fundraiser |
— | — | @@ -161,16 +179,13 @@ |
162 | 180 | $count = intval( $wgMemc->get( wfMemcKey( 'centralnotice', 'counter' ) ) ); |
163 | 181 | if ( !$count ) { |
164 | 182 | // Pull from dynamic counter |
165 | | - wfSuppressWarnings(); |
166 | | - $count = intval( file_get_contents( $wgNoticeCounterSource ) ); |
167 | | - wfRestoreWarnings(); |
| 183 | + $count = intval( $this->fetchUrl( $wgNoticeCounterSource )); |
168 | 184 | if ( !$count ) { |
169 | 185 | // Pull long-cached amount |
170 | 186 | $count = intval( $wgMemc->get( |
171 | 187 | wfMemcKey( 'centralnotice', 'counter', 'fallback' ) ) ); |
172 | 188 | if ( !$count ) { |
173 | | - // Return hard-coded amount if all else fails |
174 | | - return 1100000; // Update as needed during fundraiser |
| 189 | + throw new DonationAmountUnknownException(); |
175 | 190 | } |
176 | 191 | } |
177 | 192 | // Expire in 60 seconds |
— | — | @@ -181,3 +196,25 @@ |
182 | 197 | return $count; |
183 | 198 | } |
184 | 199 | } |
| 200 | +/** |
| 201 | + * @defgroup Exception Exception |
| 202 | + */ |
| 203 | + |
| 204 | +/** |
| 205 | + * SpecialBannerLoaderException exception |
| 206 | + * |
| 207 | + * This exception is being thrown whenever |
| 208 | + * some fatal error occurs that may affect |
| 209 | + * how the banner is presented. |
| 210 | + * |
| 211 | + * @ingroup Exception |
| 212 | + */ |
| 213 | + |
| 214 | +class SpecialBannerLoaderException extends Exception { |
| 215 | +} |
| 216 | + |
| 217 | +class RemoteServerProblemException extends SpecialBannerLoaderException { |
| 218 | +} |
| 219 | + |
| 220 | +class DonationAmountUnknownException extends SpecialBannerLoaderException { |
| 221 | +} |