r96189 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96188‎ | r96189 | r96190 >
Date:14:38, 3 September 2011
Author:maxsem
Status:resolved
Tags:
Comment:
* Good bye, ugly globals!
* Made getCode() return valid code for prefabs, i.e. a:A1 instead of a&A1
Modified paths:
  • /trunk/extensions/wikihiero/wikihiero.body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/wikihiero/wikihiero.body.php
@@ -27,9 +27,6 @@
2828 die( 'Not an entry point' );
2929 }
3030
31 -global $wh_phonemes, $wh_prefabs, $wh_files, $wh_phonemes, $wh_text_conv;
32 -include( dirname( __FILE__ ) . '/wh_list.php' );
33 -
3431 // D E F I N E S
3532 define( "WH_TABLE_S", '<table class="mw-hiero-table">' );
3633 define( "WH_TABLE_E", '</table>' );
@@ -65,10 +62,27 @@
6663
6764 private $scale = 100;
6865
 66+ private static $phonemes, $prefabs, $files, $textConv;
 67+
6968 public function __construct( $scale = WH_SCALE_DEFAULT ) {
 69+ self::loadData();
7070 }
7171
7272 /**
 73+ * Loads
 74+ */
 75+ private static function loadData() {
 76+ if ( self::$phonemes ) {
 77+ return;
 78+ }
 79+ require_once( dirname( __FILE__ ) . '/wh_list.php' );
 80+ self::$phonemes = $wh_phonemes;
 81+ self::$prefabs = $wh_prefabs;
 82+ self::$files = $wh_files;
 83+ self::$textConv = $wh_text_conv;
 84+ }
 85+
 86+ /**
7387 * Render hieroglyph text
7488 *
7589 * @param $text string: text to convert
@@ -117,9 +131,6 @@
118132 * @return string: a string to add to the stream
119133 */
120134 private function renderGlyph( $glyph, $option = '' ) {
121 - global $wh_phonemes;
122 - global $wh_files;
123 -
124135 if ( $glyph == ".." ) { // Render void block
125136 $width = WH_HEIGHT;
126137 return "<table class=\"mw-hiero-table\" style=\"width: {$width}px;\"><tr><td>&#160;</td></tr></table>";
@@ -132,25 +143,25 @@
133144 elseif ( $glyph == '<' ) // Render open cartouche
134145 {
135146 $height = intval( WH_HEIGHT * $this->scale / 100 );
136 - $code = $wh_phonemes[$glyph];
 147+ $code = self::$phonemes[$glyph];
137148 return "<img src='" . htmlspecialchars( WH_IMG_DIR . WH_IMG_PRE . "{$code}." . self::IMG_EXT ) . "' height='{$height}' title='" . htmlspecialchars( $glyph ) . "' alt='" . htmlspecialchars( $glyph ) . "' />";
138149 }
139150 elseif ( $glyph == '>' ) // Render close cartouche
140151 {
141152 $height = intval( WH_HEIGHT * $this->scale / 100 );
142 - $code = $wh_phonemes[$glyph];
 153+ $code = self::$phonemes[$glyph];
143154 return "<img src='" . htmlspecialchars( WH_IMG_DIR . WH_IMG_PRE . "{$code}." . self::IMG_EXT ) . "' height='{$height}' title='" . htmlspecialchars( $glyph ) . "' alt='" . htmlspecialchars( $glyph ) . "' />";
144155 }
145156
146 - if ( array_key_exists( $glyph, $wh_phonemes ) )
 157+ if ( array_key_exists( $glyph, self::$phonemes ) )
147158 {
148 - $code = $wh_phonemes[$glyph];
149 - if ( array_key_exists( $code, $wh_files ) )
 159+ $code = self::$phonemes[$glyph];
 160+ if ( array_key_exists( $code, self::$files ) )
150161 return "<img style='margin:" . WH_IMG_MARGIN . "px;' $option src='" . htmlspecialchars( WH_IMG_DIR . WH_IMG_PRE . "{$code}." . self::IMG_EXT ) . "' title='" . htmlspecialchars( "{$code} [{$glyph}]" ) . "' alt='" . htmlspecialchars( $glyph ) . "' />";
151162 else
152163 return "<font title='" . htmlspecialchars( $code ) . "'>" . htmlspecialchars( $glyph ) . "</font>";
153164 }
154 - elseif ( array_key_exists( $glyph, $wh_files ) )
 165+ elseif ( array_key_exists( $glyph, self::$files ) )
155166 return "<img style='margin:" . WH_IMG_MARGIN . "px;' $option src='" . htmlspecialchars( WH_IMG_DIR . WH_IMG_PRE . "{$glyph}." . self::IMG_EXT ) . "' title='" . htmlspecialchars( $glyph ) . "' alt='" . htmlspecialchars( $glyph ) . "' />";
156167 else
157168 return htmlspecialchars( $glyph );
@@ -165,11 +176,8 @@
166177 * @return size
167178 */
168179 private function resizeGlyph( $item, $is_cartouche = false, $total = 0 ) {
169 - global $wh_phonemes;
170 - global $wh_files;
171 -
172 - if ( array_key_exists( $item, $wh_phonemes ) ) {
173 - $glyph = $wh_phonemes[$item];
 180+ if ( array_key_exists( $item, self::$phonemes ) ) {
 181+ $glyph = self::$phonemes[$item];
174182 } else {
175183 $glyph = $item;
176184 }
@@ -179,8 +187,8 @@
180188 $margin += 2 * intval( WH_CARTOUCHE_WIDTH * $this->scale / 100 );
181189 }
182190
183 - if ( array_key_exists( $glyph, $wh_files ) ) {
184 - $height = $margin + $wh_files[$glyph][1];
 191+ if ( array_key_exists( $glyph, self::$files ) ) {
 192+ $height = $margin + self::$files[$glyph][1];
185193 if ( $total ) {
186194 if ( $total > WH_HEIGHT ) {
187195 return ( intval( $height * WH_HEIGHT / $total ) - $margin ) * $this->scale / 100;
@@ -207,8 +215,6 @@
208216 * @return string: converted code
209217 */
210218 public function renderText( $hiero, $line = false ) {
211 - global $wh_text_conv;
212 -
213219 $html = "";
214220
215221 if ( $line )
@@ -216,9 +222,9 @@
217223
218224 for ( $char = 0; $char < strlen( $hiero ); $char++ )
219225 {
220 - if ( array_key_exists( $hiero[$char], $wh_text_conv ) )
 226+ if ( array_key_exists( $hiero[$char], self::$textConv ) )
221227 {
222 - $html .= $wh_text_conv[$hiero[$char]];
 228+ $html .= self::$textConv[$hiero[$char]];
223229 if ( $hiero[$char] == '!' )
224230 if ( $line )
225231 $html .= "<hr />\n";
@@ -239,10 +245,6 @@
240246 * @return string: converted code
241247 */
242248 public function renderHtml( $hiero, $scale = WH_SCALE_DEFAULT, $line = false ) {
243 - global $wh_prefabs;
244 - global $wh_files;
245 - global $wh_phonemes;
246 -
247249 if ( $scale != WH_SCALE_DEFAULT )
248250 $this->setScale( $scale );
249251
@@ -380,7 +382,7 @@
381383 }
382384
383385 // test is block is into tje prefabs list
384 - if ( in_array( $temp, $wh_prefabs ) ) {
 386+ if ( in_array( $temp, self::$prefabs ) ) {
385387 $option = "height='" . $this->resizeGlyph( $temp, $is_cartouche ) . "'";
386388
387389 $contentHtml .= WH_TD_S . self::renderGlyph( $temp, $option ) . WH_TD_E;
@@ -405,13 +407,13 @@
406408 $line_max = $height;
407409 }
408410 } else {
409 - if ( array_key_exists( $t, $wh_phonemes ) ) {
410 - $glyph = $wh_phonemes[$t];
 411+ if ( array_key_exists( $t, self::$phonemes ) ) {
 412+ $glyph = self::$phonemes[$t];
411413 } else {
412414 $glyph = $t;
413415 }
414 - if ( array_key_exists( $glyph, $wh_files ) ) {
415 - $height = 2 + $wh_files[$glyph][1];
 416+ if ( array_key_exists( $glyph, self::$files ) ) {
 417+ $height = 2 + self::$files[$glyph][1];
416418 }
417419 }
418420 } // end foreach
@@ -464,6 +466,7 @@
465467 * @return string: converted code
466468 */
467469 public static function getCode( $file ) {
468 - return substr( $file, strlen( WH_IMG_PRE ), -( 1 + strlen( self::IMG_EXT ) ) );
 470+ $s = substr( $file, strlen( WH_IMG_PRE ), -( 1 + strlen( self::IMG_EXT ) ) );
 471+ return str_replace( '&', ':', $s );
469472 }
470473 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r96243Revert the getCode() part of r96189: breaks prefabs table generationmaxsem18:15, 4 September 2011

Status & tagging log