Index: branches/wmf/1.17wmf1/extensions/CentralNotice/SpecialBannerLoader.php |
— | — | @@ -1,183 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -/** |
5 | | - * Generates banner HTML files |
6 | | - */ |
7 | | -class SpecialBannerLoader extends UnlistedSpecialPage { |
8 | | - public $siteName = 'Wikipedia'; // Site name |
9 | | - public $language = 'en'; // User language |
10 | | - protected $sharedMaxAge = 600; // Cache for 10 minutes on the server side |
11 | | - protected $maxAge = 0; // No client-side banner caching so we get all impressions |
12 | | - |
13 | | - function __construct() { |
14 | | - // Register special page |
15 | | - parent::__construct( "BannerLoader" ); |
16 | | - } |
17 | | - |
18 | | - function execute( $par ) { |
19 | | - global $wgOut, $wgRequest; |
20 | | - |
21 | | - $wgOut->disable(); |
22 | | - $this->sendHeaders(); |
23 | | - |
24 | | - // Get user language from the query string |
25 | | - $this->language = $wgRequest->getText( 'userlang', 'en' ); |
26 | | - |
27 | | - // Get site name from the query string |
28 | | - $this->siteName = $wgRequest->getText( 'sitename', 'Wikipedia' ); |
29 | | - |
30 | | - if ( $wgRequest->getText( 'banner' ) ) { |
31 | | - $bannerName = $wgRequest->getText( 'banner' ); |
32 | | - $content = $this->getJsNotice( $bannerName ); |
33 | | - if ( preg_match( "/<centralnotice-template-\w{1,}>\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; |
40 | | - } |
41 | | - } else { |
42 | | - echo "/* No banner specified */"; |
43 | | - } |
44 | | - } |
45 | | - |
46 | | - /** |
47 | | - * Generate the HTTP response headers for the banner file |
48 | | - */ |
49 | | - function sendHeaders() { |
50 | | - global $wgJsMimeType; |
51 | | - header( "Content-type: $wgJsMimeType; charset=utf-8" ); |
52 | | - header( "Cache-Control: public, s-maxage=$this->sharedMaxAge, max-age=$this->maxAge" ); |
53 | | - } |
54 | | - |
55 | | - /** |
56 | | - * Generate the JS for the requested banner |
57 | | - * @return a string of Javascript containing a call to insertBanner() |
58 | | - * with JSON containing the banner content as the parameter |
59 | | - */ |
60 | | - function getJsNotice( $bannerName ) { |
61 | | - // Make sure the banner exists |
62 | | - if ( SpecialNoticeTemplate::templateExists( $bannerName ) ) { |
63 | | - $this->bannerName = $bannerName; |
64 | | - $bannerHtml = ''; |
65 | | - $bannerHtml .= preg_replace_callback( |
66 | | - '/{{{(.*?)}}}/', |
67 | | - array( $this, 'getNoticeField' ), |
68 | | - $this->getNoticeTemplate() |
69 | | - ); |
70 | | - $bannerArray = array( 'banner' => $bannerHtml ); |
71 | | - $bannerJs = 'insertBanner('.FormatJson::encode( $bannerArray ).');'; |
72 | | - return $bannerJs; |
73 | | - } |
74 | | - } |
75 | | - |
76 | | - /** |
77 | | - * Generate the HTML for the requested banner |
78 | | - */ |
79 | | - function getHtmlNotice( $bannerName ) { |
80 | | - // Make sure the banner exists |
81 | | - if ( SpecialNoticeTemplate::templateExists( $bannerName ) ) { |
82 | | - $this->bannerName = $bannerName; |
83 | | - $bannerHtml = ''; |
84 | | - $bannerHtml .= preg_replace_callback( |
85 | | - '/{{{(.*?)}}}/', |
86 | | - array( $this, 'getNoticeField' ), |
87 | | - $this->getNoticeTemplate() |
88 | | - ); |
89 | | - return $bannerHtml; |
90 | | - } |
91 | | - } |
92 | | - |
93 | | - /** |
94 | | - * Get the body of the banner with only {{int:...}} messages translated |
95 | | - */ |
96 | | - function getNoticeTemplate() { |
97 | | - $out = $this->getMessage( "centralnotice-template-{$this->bannerName}" ); |
98 | | - return $out; |
99 | | - } |
100 | | - |
101 | | - /** |
102 | | - * Extract a message name and send to getMessage() for translation |
103 | | - * @param $match A message array with 2 members: raw match, short name of message |
104 | | - * @return translated messsage string |
105 | | - */ |
106 | | - function getNoticeField( $match ) { |
107 | | - $field = $match[1]; |
108 | | - $params = array(); |
109 | | - if ( $field == 'amount' ) { |
110 | | - $params = array( $this->formatNum( $this->getDonationAmount() ) ); |
111 | | - } |
112 | | - $message = "centralnotice-{$this->bannerName}-$field"; |
113 | | - $source = $this->getMessage( $message, $params ); |
114 | | - return $source; |
115 | | - } |
116 | | - |
117 | | - /** |
118 | | - * Convert number of dollars to millions of dollars |
119 | | - */ |
120 | | - private function formatNum( $num ) { |
121 | | - $num = sprintf( "%.1f", $num / 1e6 ); |
122 | | - if ( substr( $num, - 2 ) == '.0' ) { |
123 | | - $num = substr( $num, 0, - 2 ); |
124 | | - } |
125 | | - $lang = Language::factory( $this->language ); |
126 | | - return $lang->formatNum( $num ); |
127 | | - } |
128 | | - |
129 | | - /** |
130 | | - * Retrieve a translated message |
131 | | - * @param $msg The full name of the message |
132 | | - * @return translated messsage string |
133 | | - */ |
134 | | - private function getMessage( $msg, $params = array() ) { |
135 | | - global $wgLang, $wgSitename; |
136 | | - |
137 | | - // A god-damned dirty hack! :D |
138 | | - $oldLang = $wgLang; |
139 | | - $oldSitename = $wgSitename; |
140 | | - |
141 | | - $wgSitename = $this->siteName; // hack for {{SITENAME}} |
142 | | - $wgLang = Language::factory( $this->language ); // hack for {{int:...}} |
143 | | - |
144 | | - $options = array( 'language' => $this->language, 'parsemag' ); |
145 | | - array_unshift( $params, $options ); |
146 | | - array_unshift( $params, $msg ); |
147 | | - $out = call_user_func_array( 'wfMsgExt', $params ); |
148 | | - |
149 | | - // Restore global variables |
150 | | - $wgLang = $oldLang; |
151 | | - $wgSitename = $oldSitename; |
152 | | - |
153 | | - return $out; |
154 | | - } |
155 | | - |
156 | | - /** |
157 | | - * Pull the current amount raised during a fundraiser |
158 | | - */ |
159 | | - private function getDonationAmount() { |
160 | | - global $wgNoticeCounterSource, $wgMemc; |
161 | | - // Pull short-cached amount |
162 | | - $count = intval( $wgMemc->get( wfMemcKey( 'centralnotice', 'counter' ) ) ); |
163 | | - if ( !$count ) { |
164 | | - // Pull from dynamic counter |
165 | | - wfSuppressWarnings(); |
166 | | - $count = intval( file_get_contents( $wgNoticeCounterSource ) ); |
167 | | - wfRestoreWarnings(); |
168 | | - if ( !$count ) { |
169 | | - // Pull long-cached amount |
170 | | - $count = intval( $wgMemc->get( |
171 | | - wfMemcKey( 'centralnotice', 'counter', 'fallback' ) ) ); |
172 | | - if ( !$count ) { |
173 | | - // Return hard-coded amount if all else fails |
174 | | - return 1100000; // Update as needed during fundraiser |
175 | | - } |
176 | | - } |
177 | | - // Expire in 60 seconds |
178 | | - $wgMemc->set( wfMemcKey( 'centralnotice', 'counter' ), $count, 60 ); |
179 | | - // No expiration |
180 | | - $wgMemc->set( wfMemcKey( 'centralnotice', 'counter', 'fallback' ), $count ); |
181 | | - } |
182 | | - return $count; |
183 | | - } |
184 | | -} |
Index: branches/wmf/1.17wmf1/extensions/CentralNotice/SpecialHideBanners.php |
— | — | @@ -1,40 +0,0 @@ |
2 | | -<?php |
3 | | -if ( !defined( 'MEDIAWIKI' ) ) { |
4 | | - echo "CentralNotice extension\n"; |
5 | | - exit( 1 ); |
6 | | -} |
7 | | - |
8 | | -/** |
9 | | - * Unlisted Special Page which sets a cookie for hiding banners across all languages of a project. |
10 | | - */ |
11 | | -class SpecialHideBanners extends UnlistedSpecialPage { |
12 | | - function __construct() { |
13 | | - parent::__construct( 'HideBanners' ); |
14 | | - } |
15 | | - |
16 | | - function execute( $par ) { |
17 | | - global $wgOut; |
18 | | - |
19 | | - $this->setHideCookie(); |
20 | | - |
21 | | - $wgOut->disable(); |
22 | | - wfResetOutputBuffers(); |
23 | | - |
24 | | - header( 'Content-Type: image/png' ); |
25 | | - header( 'Cache-Control: no-cache' ); |
26 | | - |
27 | | - readfile( dirname( __FILE__ ) . '/1x1.png' ); |
28 | | - } |
29 | | - |
30 | | - function setHideCookie() { |
31 | | - global $wgNoticeCookieDomain, $wgCookieSecure; |
32 | | - $exp = time() + 86400 * 14; // Cookie expires after 2 weeks |
33 | | - if ( is_callable( 'CentralAuthUser', 'getCookieDomain' ) ) { |
34 | | - $cookieDomain = CentralAuthUser::getCookieDomain(); |
35 | | - } else { |
36 | | - $cookieDomain = $wgNoticeCookieDomain; |
37 | | - } |
38 | | - // Hide banners for this domain |
39 | | - setcookie( 'hidesnmessage', '1', $exp, '/', $cookieDomain, $wgCookieSecure ); |
40 | | - } |
41 | | -} |