Index: trunk/extensions/CentralNotice/SpecialNoticeText.php |
— | — | @@ -1,300 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -/** |
5 | | - * Generates content for static Javascript files |
6 | | - */ |
7 | | -class SpecialNoticeText extends NoticePage { |
8 | | - var $project = 'wikipedia'; |
9 | | - var $language = 'en'; |
10 | | - var $centralNoticeDB; |
11 | | - |
12 | | - function __construct() { |
13 | | - parent::__construct( "NoticeText" ); |
14 | | - |
15 | | - $this->centralNoticeDB = new CentralNoticeDB(); |
16 | | - } |
17 | | - |
18 | | - /** |
19 | | - * Clients can cache this as long as they like -- if it changes, |
20 | | - * we'll be bumping things at the loader level, bringing a new URL. |
21 | | - * |
22 | | - * Let's say a week. |
23 | | - */ |
24 | | - protected function maxAge() { |
25 | | - return 86400 * 7; |
26 | | - } |
27 | | - |
28 | | - /** |
29 | | - * Given a project key, generate the body for a static Javascript file |
30 | | - */ |
31 | | - function getOutput( $par ) { |
32 | | - |
33 | | - // Break $par into separate parameters and assign to $this->project and $this->language |
34 | | - $this->setLanguage( $par ); |
35 | | - |
36 | | - // Quick short circuit to be able to show preferred notices |
37 | | - $templates = array(); |
38 | | - |
39 | | - if ( $this->language == 'en' && $this->project != null ) { |
40 | | - // See if we have any preferred notices for all of en |
41 | | - $notices = $this->centralNoticeDB->getNotices( '', 'en', '', '', 1 ); |
42 | | - |
43 | | - if ( $notices ) { |
44 | | - // Pull out values |
45 | | - foreach ( $notices as $notice => $val ) { |
46 | | - // Either match against ALL project or a specific project |
47 | | - if ( $val['project'] == '' || $val['project'] == $this->project ) { |
48 | | - $templates = $this->centralNoticeDB->selectTemplatesAssigned( $notice ); |
49 | | - break; |
50 | | - } |
51 | | - } |
52 | | - } |
53 | | - } |
54 | | - |
55 | | - if ( !$templates && $this->project == 'wikipedia' ) { |
56 | | - $notices = $this->centralNoticeDB->getNotices( 'wikipedia', $this->language, '', '', 1 ); |
57 | | - if ( $notices && is_array( $notices ) ) { |
58 | | - foreach ( $notices as $notice => $val ) { |
59 | | - $templates = $this->centralNoticeDB->selectTemplatesAssigned( $notice ); |
60 | | - break; |
61 | | - } |
62 | | - } |
63 | | - } |
64 | | - |
65 | | - // Didn't find any preferred matches so do an old style lookup |
66 | | - if ( !$templates ) { |
67 | | - $templates = CentralNotice::selectNoticeTemplates( $this->project, $this->language ); |
68 | | - } |
69 | | - |
70 | | - // Slice the columns of the $templates array into separate arrays. |
71 | | - // This is required due to how pickTemplate() currently works. |
72 | | - $templateNames = array(); |
73 | | - $templateWeights = array(); |
74 | | - $templateDisplayAnons = array(); |
75 | | - $templateDisplayAccounts = array(); |
76 | | - foreach ( $templates as $template ) { |
77 | | - $templateNames[] = $template['name']; |
78 | | - $templateWeights[] = $template['weight']; |
79 | | - $templateDisplayAnons[] = $template['display_anon']; |
80 | | - $templateDisplayAccounts[] = $template['display_account']; |
81 | | - } |
82 | | - |
83 | | - $templateTexts = array_map( |
84 | | - array( $this, 'getHtmlNotice' ), |
85 | | - $templateNames ); |
86 | | - |
87 | | - if ( preg_grep( "/<centralnotice-template-\w{1,}>\z/", $templateTexts ) ) { |
88 | | - return false; // Bailing out if we have a failed cache lookup |
89 | | - } |
90 | | - |
91 | | - return |
92 | | - $this->getScriptFunctions() . |
93 | | - $this->getToggleScripts() . |
94 | | - 'wgNotice=pickTemplate(' . |
95 | | - Xml::encodeJsVar( $templateTexts ) . |
96 | | - "," . |
97 | | - Xml::encodeJsVar( $templateWeights ) . |
98 | | - "," . |
99 | | - Xml::encodeJsVar( $templateDisplayAnons ) . |
100 | | - "," . |
101 | | - Xml::encodeJsVar( $templateDisplayAccounts ) . |
102 | | - ");\n" . |
103 | | - "if (wgNotice != '')\n" . |
104 | | - "wgNotice='<div id=\"centralNotice\" class=\"' + " . |
105 | | - "(wgNoticeToggleState ? 'expanded' : 'collapsed') + " . |
106 | | - "'\">' + wgNotice+'</div>';\n"; |
107 | | - } |
108 | | - |
109 | | - function getHtmlNotice( $templateName ) { |
110 | | - $this->templateName = $templateName; |
111 | | - return preg_replace_callback( |
112 | | - '/{{{(.*?)}}}/', |
113 | | - array( $this, 'getNoticeField' ), |
114 | | - $this->getNoticeTemplate() ); |
115 | | - } |
116 | | - |
117 | | - function getToggleScripts() { |
118 | | - $showStyle = <<<END |
119 | | -<style type="text/css"> |
120 | | -#centralNotice .siteNoticeSmall {display:none;} |
121 | | -#centralNotice .siteNoticeSmallAnon {display:none;} |
122 | | -#centralNotice .siteNoticeSmallUser {display:none;} |
123 | | -#centralNotice.collapsed .siteNoticeBig {display:none;} |
124 | | -#centralNotice.collapsed .siteNoticeSmall {display:block;} |
125 | | -#centralNotice.collapsed .siteNoticeSmallUser {display:block;} |
126 | | -#centralNotice.collapsed .siteNoticeSmallAnon {display:block;} |
127 | | -#centralNotice.anonnotice .siteNoticeSmallUser {display:none !important;} |
128 | | -#centralNotice.usernotice .siteNoticeSmallAnon {display:none !important;} |
129 | | -</style> |
130 | | -END; |
131 | | - $encShowStyle = Xml::encodeJsVar( $showStyle ); |
132 | | - |
133 | | - $script = " |
134 | | -var wgNoticeToggleState = (document.cookie.indexOf('hidesnmessage=1')==-1); |
135 | | -document.writeln($encShowStyle);\n\n"; |
136 | | - return $script; |
137 | | - } |
138 | | - |
139 | | - function getScriptFunctions() { |
140 | | - $script = " |
141 | | -function toggleNotice() { |
142 | | - var notice = document.getElementById('centralNotice'); |
143 | | - if (!wgNoticeToggleState) { |
144 | | - notice.className = notice.className.replace('collapsed', 'expanded'); |
145 | | - toggleNoticeCookie('0'); |
146 | | - } else { |
147 | | - notice.className = notice.className.replace('expanded', 'collapsed'); |
148 | | - toggleNoticeCookie('1'); |
149 | | - } |
150 | | - wgNoticeToggleState = !wgNoticeToggleState; |
151 | | -} |
152 | | -function toggleNoticeStyle(elems, display) { |
153 | | - if(elems) |
154 | | - for(var i=0;i<elems.length;i++) |
155 | | - elems[i].style.display = display; |
156 | | -} |
157 | | -function toggleNoticeCookie(state) { |
158 | | - var e = new Date(); |
159 | | - e.setTime( e.getTime() + (7*24*60*60*1000) ); // one week |
160 | | - var work='hidesnmessage='+state+'; expires=' + e.toGMTString() + '; path=/'; |
161 | | - document.cookie = work; |
162 | | -} |
163 | | -function pickTemplate(templates, weights, displayAnons, displayAccounts) { |
164 | | - var weightedTemplates = new Array(); |
165 | | - var currentTemplate = 0; |
166 | | - var totalWeight = 0; |
167 | | - |
168 | | - if (templates.length == 0) |
169 | | - return ''; |
170 | | - |
171 | | - while (currentTemplate < templates.length) { |
172 | | - if ((wgUserName && displayAccounts[currentTemplate]) || (!wgUserName && displayAnons[currentTemplate])) { |
173 | | - totalWeight += weights[currentTemplate]; |
174 | | - for (var i=0; i<weights[currentTemplate]; i++) { |
175 | | - weightedTemplates[weightedTemplates.length] = templates[currentTemplate]; |
176 | | - } |
177 | | - } |
178 | | - currentTemplate++; |
179 | | - } |
180 | | - |
181 | | - if (totalWeight == 0) |
182 | | - return ''; |
183 | | - |
184 | | - var randomnumber=Math.floor(Math.random()*totalWeight); |
185 | | - return weightedTemplates[randomnumber]; |
186 | | -}\n\n"; |
187 | | - return $script; |
188 | | - } |
189 | | - |
190 | | - private function formatNum( $num ) { |
191 | | - $num = sprintf( "%.1f", $num / 1e6 ); |
192 | | - if ( substr( $num, - 2 ) == '.0' ) { |
193 | | - $num = substr( $num, 0, - 2 ); |
194 | | - } |
195 | | - $lang = Language::factory( $this->language ); |
196 | | - return $lang->formatNum( $num ); |
197 | | - } |
198 | | - |
199 | | - private function setLanguage( $par ) { |
200 | | - // Strip extra ? bits if they've gotten in. Sigh. |
201 | | - $bits = explode( '?', $par, 2 ); |
202 | | - $par = $bits[0]; |
203 | | - |
204 | | - // Special:NoticeText/project/language |
205 | | - $bits = explode( '/', $par ); |
206 | | - if ( count( $bits ) >= 2 ) { |
207 | | - $this->project = $bits[0]; |
208 | | - $this->language = $bits[1]; |
209 | | - } |
210 | | - } |
211 | | - |
212 | | - function getNoticeTemplate() { |
213 | | - return $this->getMessage( "centralnotice-template-{$this->templateName}" ); |
214 | | - } |
215 | | - |
216 | | - function getNoticeField( $matches ) { |
217 | | - $field = $matches[1]; |
218 | | - $params = array(); |
219 | | - if ( $field == 'amount' ) { |
220 | | - $params = array( $this->formatNum( $this->getDonationAmount() ) ); |
221 | | - } |
222 | | - $message = "centralnotice-{$this->templateName}-$field"; |
223 | | - $source = $this->getMessage( $message, $params ); |
224 | | - return $source; |
225 | | - } |
226 | | - |
227 | | - private function getMessage( $msg, $params = array() ) { |
228 | | - // A god-damned dirty hack! :D |
229 | | - $old = array(); |
230 | | - $old['wgSitename'] = $GLOBALS['wgSitename']; |
231 | | - $old['wgLang'] = $GLOBALS['wgLang']; |
232 | | - |
233 | | - $GLOBALS['wgSitename'] = $this->projectName(); |
234 | | - $GLOBALS['wgLang'] = Language::factory( $this->language ); // hack for {{int:...}} |
235 | | - |
236 | | - $options = array( |
237 | | - 'language' => $this->language, |
238 | | - 'parsemag', |
239 | | - ); |
240 | | - array_unshift( $params, $options ); |
241 | | - array_unshift( $params, $msg ); |
242 | | - $out = call_user_func_array( 'wfMsgExt', $params ); |
243 | | - |
244 | | - // Restore globals |
245 | | - $GLOBALS['wgSitename'] = $old['wgSitename']; |
246 | | - $GLOBALS['wgLang'] = $old['wgLang']; |
247 | | - |
248 | | - return $out; |
249 | | - } |
250 | | - |
251 | | - private function projectName() { |
252 | | - global $wgConf; |
253 | | - |
254 | | - $wgConf->loadFullData(); |
255 | | - |
256 | | - // Special cases for commons and meta who have no lang |
257 | | - if ( $this->project == 'commons' ) |
258 | | - return "Commons"; |
259 | | - else if ( $this->project == 'meta' ) |
260 | | - return "Wikimedia"; |
261 | | - |
262 | | - // Guess dbname since we don't have it atm |
263 | | - $dbname = $this->language . |
264 | | - ( ( $this->project == 'wikipedia' ) ? "wiki" : $this->project ); |
265 | | - $name = $wgConf->get( 'wgSitename', $dbname, $this->project, |
266 | | - array( 'lang' => $this->language, 'site' => $this->project ) ); |
267 | | - |
268 | | - if ( $name ) { |
269 | | - return $name; |
270 | | - } else { |
271 | | - global $wgLang; |
272 | | - return $wgLang->ucfirst( $this->project ); |
273 | | - } |
274 | | - } |
275 | | - |
276 | | - private function getDonationAmount() { |
277 | | - global $wgNoticeCounterSource, $wgMemc; |
278 | | - $count = intval( $wgMemc->get( 'centralnotice:counter' ) ); |
279 | | - if ( !$count ) { |
280 | | - $count = intval( @file_get_contents( $wgNoticeCounterSource ) ); |
281 | | - if ( !$count ) { |
282 | | - // nooooo |
283 | | - return $this->getFallbackDonationAmount(); |
284 | | - } |
285 | | - |
286 | | - $wgMemc->set( 'centralnotice:counter', $count, 60 ); |
287 | | - $wgMemc->set( 'centralnotice:counter:fallback', $count ); // no expiry |
288 | | - } |
289 | | - |
290 | | - return $count; |
291 | | - } |
292 | | - |
293 | | - private function getFallbackDonationAmount() { |
294 | | - global $wgMemc; |
295 | | - $count = intval( $wgMemc->get( 'centralnotice:counter:fallback' ) ); |
296 | | - if ( !$count ) { |
297 | | - return 16672; // number last i saw... dirty hack ;) |
298 | | - } |
299 | | - return $count; |
300 | | - } |
301 | | -} |
Index: trunk/extensions/CentralNotice/newCentralNotice.js |
— | — | @@ -4,12 +4,6 @@ |
5 | 5 | * Mostly stubbed functionallity for central notice improvements |
6 | 6 | * May or may not be used, definitely will be changed. |
7 | 7 | * More of a sketch of what we think needs to be done. |
8 | | - * |
9 | | - * QUESTIONS: |
10 | | - * 1. How do I determin if a user is logged in or not? |
11 | | - * A: See function pickTemplate() in SpecialNoticeText.php |
12 | | - * 2. How do I determin a users location? |
13 | | - * A: The country value given by http://geoiplookup.wikimedia.org/ |
14 | 8 | */ |
15 | 9 | ( function( $ ) { |
16 | 10 | $.centralNotice = { |
Index: trunk/extensions/CentralNotice/CentralNotice.php |
— | — | @@ -101,11 +101,6 @@ |
102 | 102 | $wgSpecialPages['NoticeTemplate'] = 'SpecialNoticeTemplate'; |
103 | 103 | $wgAutoloadClasses['SpecialNoticeTemplate'] = $dir . 'SpecialNoticeTemplate.php'; |
104 | 104 | |
105 | | - // remove these as soon as banner loader is complete |
106 | | - $wgSpecialPages['NoticeText'] = 'SpecialNoticeText'; |
107 | | - $wgAutoloadClasses['SpecialNoticeText'] = $dir . 'SpecialNoticeText.php'; |
108 | | - $wgAutoloadClasses['NoticePage'] = $dir . 'NoticePage.php'; |
109 | | - |
110 | 105 | $wgAutoloadClasses['CentralNoticeDB'] = $dir . 'CentralNotice.db.php'; |
111 | 106 | $wgAutoloadClasses['TemplatePager'] = $dir . 'TemplatePager.php'; |
112 | 107 | } |
Index: trunk/extensions/CentralNotice/TemplatePager.php |
— | — | @@ -55,8 +55,8 @@ |
56 | 56 | |
57 | 57 | // Link and Preview |
58 | 58 | $viewPage = SpecialPage::getTitleFor( 'NoticeTemplate', 'view' ); |
59 | | - $render = new SpecialNoticeText(); |
60 | | - $render->project = 'wikipedia'; |
| 59 | + $render = new SpecialBannerLoader(); |
| 60 | + $render->siteName = 'Wikipedia'; |
61 | 61 | $render->language = $this->mRequest->getVal( 'wpUserLanguage' ); |
62 | 62 | $htmlOut .= Xml::tags( 'td', array( 'valign' => 'top' ), |
63 | 63 | $this->getSkin()->makeLinkObj( $this->viewPage, |
Index: trunk/extensions/CentralNotice/SpecialNoticeTemplate.php |
— | — | @@ -565,8 +565,8 @@ |
566 | 566 | foreach ( $langs as $lang ) { |
567 | 567 | // Link and Preview all available translations |
568 | 568 | $viewPage = $this->getTitle( 'view' ); |
569 | | - $render = new SpecialNoticeText(); |
570 | | - $render->project = 'wikipedia'; |
| 569 | + $render = new SpecialBannerLoader(); |
| 570 | + $render->siteName = 'Wikipedia'; |
571 | 571 | $render->language = $lang; |
572 | 572 | $htmlOut .= Xml::tags( 'td', array( 'valign' => 'top' ), |
573 | 573 | $sk->makeLinkObj( $viewPage, |