r72610 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72609‎ | r72610 | r72611 >
Date:21:23, 8 September 2010
Author:kaldari
Status:ok
Tags:
Comment:
switching from BannerLoader to SpecialBannerLoader per comment at r72177
Modified paths:
  • /trunk/extensions/CentralNotice/BannerLoader.php (deleted) (history)
  • /trunk/extensions/CentralNotice/CentralNotice.php (modified) (history)
  • /trunk/extensions/CentralNotice/SpecialBannerLoader.php (added) (history)
  • /trunk/extensions/CentralNotice/SpecialNoticeTemplate.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CentralNotice/BannerLoader.php
@@ -1,169 +0,0 @@
2 -<?php
3 -
4 -/**
5 - * Generates banner HTML files
6 - */
7 -class BannerLoader extends UnlistedSpecialPage {
8 - public $siteName = 'Wikipedia'; // Site name
9 - public $language = 'en'; // User language
10 - protected $sharedMaxAge = 22; // Cache for 2 hours on the server side
11 - protected $maxAge = 0; // No client-side banner caching so we get all impressions
12 - protected $contentType = 'text/html';
13 -
14 - function __construct() {
15 - // Register special page
16 - parent::__construct( "BannerLoader" );
17 - }
18 -
19 - function execute( $par ) {
20 - global $wgOut, $wgRequest;
21 -
22 - $wgOut->disable();
23 - $this->sendHeaders();
24 -
25 - // Get user language from the query string
26 - $this->language = $wgRequest->getText( 'userlang', 'en' );
27 -
28 - // Get site name from the query string
29 - $this->siteName = $wgRequest->getText( 'sitename', 'Wikipedia' );
30 -
31 - // If we're not pulling the banner into another page, we'll need to add some extra HTML
32 - $standAlone = $wgRequest->getBool( 'standalone' );
33 -
34 - if ( $wgRequest->getText( 'banner' ) ) {
35 - $bannerName = $wgRequest->getText( 'banner' );
36 - $content = $this->getHtmlNotice( $bannerName, $standAlone );
37 - if ( strlen( $content ) == 0 ) {
38 - // Hack for IE/Mac 0-length keepalive problem, see RawPage.php
39 - echo "/* Empty */";
40 - } else {
41 - echo $content;
42 - }
43 - } else {
44 - echo "/* No banner specified */";
45 - }
46 - }
47 -
48 - /**
49 - * Generate the HTTP response headers for the banner file
50 - */
51 - function sendHeaders() {
52 - header( "Content-type: $this->contentType; charset=utf-8" );
53 - header( "Cache-Control: public, s-maxage=$this->sharedMaxAge, max-age=$this->maxAge" );
54 - }
55 -
56 - /**
57 - * Generate the HTML for the requested banner
58 - */
59 - function getHtmlNotice( $bannerName, $standAlone = false ) {
60 - // Make sure the banner exists
61 - if ( SpecialNoticeTemplate::templateExists( $bannerName ) ) {
62 - $this->bannerName = $bannerName;
63 - $bannerHtml = '';
64 - if ( $standAlone ) {
65 - $bannerHtml .= <<<EOT
66 -<html>
67 -<head>
68 - <script type="text/javascript" src="http://bits.wikimedia.org/skins-1.5/common/jquery.min.js"></script>
69 -</head>
70 -<body>
71 -EOT;
72 - }
73 - $bannerHtml .= preg_replace_callback(
74 - '/{{{(.*?)}}}/',
75 - array( $this, 'getNoticeField' ),
76 - $this->getNoticeTemplate()
77 - );
78 - if ( $standAlone ) {
79 - $bannerHtml .= <<<EOT
80 -</body>
81 -</html>
82 -EOT;
83 - }
84 - return $bannerHtml;
85 - }
86 - }
87 -
88 - /**
89 - * Get the body of the banner with only {{int:...}} messages translated
90 - */
91 - function getNoticeTemplate() {
92 - $out = $this->getMessage( "centralnotice-template-{$this->bannerName}" );
93 - return $out;
94 - }
95 -
96 - /**
97 - * Extract a message name and send to getMessage() for translation
98 - * @param $match A message array with 2 members: raw match, short name of message
99 - * @return translated messsage string
100 - */
101 - function getNoticeField( $match ) {
102 - $field = $match[1];
103 - $message = "centralnotice-{$this->bannerName}-$field";
104 - $source = $this->getMessage( $message );
105 - return $source;
106 - }
107 -
108 - /**
109 - * Convert number of dollars to millions of dollars
110 - */
111 - private function formatNum( $num ) {
112 - $num = sprintf( "%.1f", $num / 1e6 );
113 - if ( substr( $num, - 2 ) == '.0' ) {
114 - $num = substr( $num, 0, - 2 );
115 - }
116 - $lang = Language::factory( $this->language );
117 - return $lang->formatNum( $num );
118 - }
119 -
120 - /**
121 - * Retrieve a translated message
122 - * @param $msg The full name of the message
123 - * @return translated messsage string
124 - */
125 - private function getMessage( $msg ) {
126 - global $wgLang;
127 -
128 - // A god-damned dirty hack! :D
129 - $oldLang = $wgLang;
130 -
131 - $wgLang = Language::factory( $this->language ); // hack for {{int:...}}
132 - $out = wfMsgExt( $msg, array( 'language' => $this->language, 'parsemag' ) );
133 -
134 - // Restore global
135 - $wgLang = $oldLang;
136 -
137 - // Replace variables in banner with values
138 - $out = str_ireplace( '$amount', $this->formatNum( $this->getDonationAmount() ), $out );
139 - $out = str_ireplace( '$sitename', $this->siteName, $out );
140 -
141 - return $out;
142 - }
143 -
144 - /**
145 - * Pull the current amount raised during a fundraiser
146 - */
147 - private function getDonationAmount() {
148 - global $wgNoticeCounterSource, $wgMemc;
149 - // Pull short-cached amount
150 - $count = intval( $wgMemc->get( 'centralnotice:counter' ) );
151 - if ( !$count ) {
152 - // Pull from dynamic counter
153 - wfSuppressWarnings();
154 - $count = intval( file_get_contents( $wgNoticeCounterSource ) );
155 - wfRestoreWarnings();
156 - if ( !$count ) {
157 - // Pull long-cached amount
158 - $count = intval( $wgMemc->get( 'centralnotice:counter:fallback' ) );
159 - if ( !$count ) {
160 - // Return hard-coded amount if all else fails
161 - return 1100000; // Update as needed during fundraiser
162 - }
163 - }
164 - $wgMemc->set( 'centralnotice:counter', $count, 60 ); // Expire in 60 seconds
165 - $wgMemc->set( 'centralnotice:counter:fallback', $count ); // No expiration
166 - }
167 - return $count;
168 - }
169 -
170 -}
Index: trunk/extensions/CentralNotice/CentralNotice.php
@@ -139,8 +139,8 @@
140140 $wgSpecialPageGroups['CentralNotice'] = 'wiki'; // Wiki data and tools"
141141 $wgAutoloadClasses['CentralNotice'] = $dir . 'SpecialCentralNotice.php';
142142
143 - $wgSpecialPages['BannerLoader'] = 'BannerLoader';
144 - $wgAutoloadClasses['BannerLoader'] = $dir . 'BannerLoader.php';
 143+ $wgSpecialPages['BannerLoader'] = 'SpecialBannerLoader';
 144+ $wgAutoloadClasses['SpecialBannerLoader'] = $dir . 'SpecialBannerLoader.php';
145145
146146 $wgSpecialPages['NoticeText'] = 'SpecialNoticeText';
147147 $wgAutoloadClasses['SpecialNoticeText'] = $dir . 'SpecialNoticeText.php';
Index: trunk/extensions/CentralNotice/SpecialBannerLoader.php
@@ -0,0 +1,169 @@
 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 = 22; // Cache for 2 hours on the server side
 11+ protected $maxAge = 0; // No client-side banner caching so we get all impressions
 12+ protected $contentType = 'text/html';
 13+
 14+ function __construct() {
 15+ // Register special page
 16+ parent::__construct( "BannerLoader" );
 17+ }
 18+
 19+ function execute( $par ) {
 20+ global $wgOut, $wgRequest;
 21+
 22+ $wgOut->disable();
 23+ $this->sendHeaders();
 24+
 25+ // Get user language from the query string
 26+ $this->language = $wgRequest->getText( 'userlang', 'en' );
 27+
 28+ // Get site name from the query string
 29+ $this->siteName = $wgRequest->getText( 'sitename', 'Wikipedia' );
 30+
 31+ // If we're not pulling the banner into another page, we'll need to add some extra HTML
 32+ $standAlone = $wgRequest->getBool( 'standalone' );
 33+
 34+ if ( $wgRequest->getText( 'banner' ) ) {
 35+ $bannerName = $wgRequest->getText( 'banner' );
 36+ $content = $this->getHtmlNotice( $bannerName, $standAlone );
 37+ if ( strlen( $content ) == 0 ) {
 38+ // Hack for IE/Mac 0-length keepalive problem, see RawPage.php
 39+ echo "/* Empty */";
 40+ } else {
 41+ echo $content;
 42+ }
 43+ } else {
 44+ echo "/* No banner specified */";
 45+ }
 46+ }
 47+
 48+ /**
 49+ * Generate the HTTP response headers for the banner file
 50+ */
 51+ function sendHeaders() {
 52+ header( "Content-type: $this->contentType; charset=utf-8" );
 53+ header( "Cache-Control: public, s-maxage=$this->sharedMaxAge, max-age=$this->maxAge" );
 54+ }
 55+
 56+ /**
 57+ * Generate the HTML for the requested banner
 58+ */
 59+ function getHtmlNotice( $bannerName, $standAlone = false ) {
 60+ // Make sure the banner exists
 61+ if ( SpecialNoticeTemplate::templateExists( $bannerName ) ) {
 62+ $this->bannerName = $bannerName;
 63+ $bannerHtml = '';
 64+ if ( $standAlone ) {
 65+ $bannerHtml .= <<<EOT
 66+<html>
 67+<head>
 68+ <script type="text/javascript" src="http://bits.wikimedia.org/skins-1.5/common/jquery.min.js"></script>
 69+</head>
 70+<body>
 71+EOT;
 72+ }
 73+ $bannerHtml .= preg_replace_callback(
 74+ '/{{{(.*?)}}}/',
 75+ array( $this, 'getNoticeField' ),
 76+ $this->getNoticeTemplate()
 77+ );
 78+ if ( $standAlone ) {
 79+ $bannerHtml .= <<<EOT
 80+</body>
 81+</html>
 82+EOT;
 83+ }
 84+ return $bannerHtml;
 85+ }
 86+ }
 87+
 88+ /**
 89+ * Get the body of the banner with only {{int:...}} messages translated
 90+ */
 91+ function getNoticeTemplate() {
 92+ $out = $this->getMessage( "centralnotice-template-{$this->bannerName}" );
 93+ return $out;
 94+ }
 95+
 96+ /**
 97+ * Extract a message name and send to getMessage() for translation
 98+ * @param $match A message array with 2 members: raw match, short name of message
 99+ * @return translated messsage string
 100+ */
 101+ function getNoticeField( $match ) {
 102+ $field = $match[1];
 103+ $message = "centralnotice-{$this->bannerName}-$field";
 104+ $source = $this->getMessage( $message );
 105+ return $source;
 106+ }
 107+
 108+ /**
 109+ * Convert number of dollars to millions of dollars
 110+ */
 111+ private function formatNum( $num ) {
 112+ $num = sprintf( "%.1f", $num / 1e6 );
 113+ if ( substr( $num, - 2 ) == '.0' ) {
 114+ $num = substr( $num, 0, - 2 );
 115+ }
 116+ $lang = Language::factory( $this->language );
 117+ return $lang->formatNum( $num );
 118+ }
 119+
 120+ /**
 121+ * Retrieve a translated message
 122+ * @param $msg The full name of the message
 123+ * @return translated messsage string
 124+ */
 125+ private function getMessage( $msg ) {
 126+ global $wgLang;
 127+
 128+ // A god-damned dirty hack! :D
 129+ $oldLang = $wgLang;
 130+
 131+ $wgLang = Language::factory( $this->language ); // hack for {{int:...}}
 132+ $out = wfMsgExt( $msg, array( 'language' => $this->language, 'parsemag' ) );
 133+
 134+ // Restore global
 135+ $wgLang = $oldLang;
 136+
 137+ // Replace variables in banner with values
 138+ $out = str_ireplace( '$amount', $this->formatNum( $this->getDonationAmount() ), $out );
 139+ $out = str_ireplace( '$sitename', $this->siteName, $out );
 140+
 141+ return $out;
 142+ }
 143+
 144+ /**
 145+ * Pull the current amount raised during a fundraiser
 146+ */
 147+ private function getDonationAmount() {
 148+ global $wgNoticeCounterSource, $wgMemc;
 149+ // Pull short-cached amount
 150+ $count = intval( $wgMemc->get( 'centralnotice:counter' ) );
 151+ if ( !$count ) {
 152+ // Pull from dynamic counter
 153+ wfSuppressWarnings();
 154+ $count = intval( file_get_contents( $wgNoticeCounterSource ) );
 155+ wfRestoreWarnings();
 156+ if ( !$count ) {
 157+ // Pull long-cached amount
 158+ $count = intval( $wgMemc->get( 'centralnotice:counter:fallback' ) );
 159+ if ( !$count ) {
 160+ // Return hard-coded amount if all else fails
 161+ return 1100000; // Update as needed during fundraiser
 162+ }
 163+ }
 164+ $wgMemc->set( 'centralnotice:counter', $count, 60 ); // Expire in 60 seconds
 165+ $wgMemc->set( 'centralnotice:counter:fallback', $count ); // No expiration
 166+ }
 167+ return $count;
 168+ }
 169+
 170+}
Property changes on: trunk/extensions/CentralNotice/SpecialBannerLoader.php
___________________________________________________________________
Added: svn:eol-style
1171 + native
Index: trunk/extensions/CentralNotice/SpecialNoticeTemplate.php
@@ -317,7 +317,7 @@
318318 $htmlOut .= Xml::element( 'h2', null, wfMsg( 'centralnotice-banner-heading', $currentTemplate ) );
319319
320320 // Show preview of banner
321 - $render = new BannerLoader();
 321+ $render = new SpecialBannerLoader();
322322 $render->siteName = 'Wikipedia';
323323 $render->language = $wpUserLang;
324324 if ( $render->language != '' ) {

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r72177initial version of BannerLoaderkaldari23:18, 1 September 2010

Status & tagging log