r53993 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r53992‎ | r53993 | r53994 >
Date:00:30, 30 July 2009
Author:conrad
Status:deferred
Tags:
Comment:
Add the "answer" parameter to override, some edge-case handling
Modified paths:
  • /trunk/extensions/Transliterator/Transliterator.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Transliterator/Transliterator.php
@@ -16,7 +16,8 @@
1717 * initial creation.
1818 * @version 1.0.1
1919 * better i18n support, adjustable limits, minor formal adjustment.
20 - *
 20+ * @version 1.1.0
 21+ * addition of answer parameter
2122 */
2223
2324 /**
@@ -48,10 +49,10 @@
4950
5051 $wgExtensionCredits['parserhook'][] = array(
5152 'name' => 'Transliterator',
52 - 'version' => '1.0.1',
 53+ 'version' => '1.1.0',
5354 'descriptionmsg' => 'transliterator-desc',
5455 'author' => 'Conrad Irwin',
55 - 'url' => 'http://en.wiktionary.org/wiki/User:Conrad.Irwin/Transliterator.php',
 56+ 'url' => 'http://www.mediawiki.org/wiki/Extension:Transliterator',
5657 'path' => __FILE__,
5758 );
5859
@@ -173,12 +174,19 @@
174175 $decompose = false;
175176
176177 // Split lines and remove comments and space
177 - $lines = split( "\n", html_entity_decode( preg_replace( '/^(\s*#.*)?\n| */m', '', "$input" ), ENT_NOQUOTES, "UTF-8" ) );
 178+ $lines = split( "\n", html_entity_decode( preg_replace( '/^\s*(#.*)?(\n|$)| */m', '', $input ), ENT_NOQUOTES, "UTF-8" ) );
178179
 180+ // If the last line was a comment then there will be an empty line at the end
 181+ if ( $lines[count( $lines ) - 1] == "" ) {
 182+ array_pop( $lines );
 183+ }
 184+
179185 if ( $lines[0] == "<decompose>" ) {
180186 $map['__decompose__'] = true;
181187 array_shift( $lines );
182188 $decompose = true;
 189+ } else if ( $lines[0] == "<$mappage>" ) {
 190+ return false;
183191 }
184192
185193 if ( count( $lines ) > $wgTransliteratorRuleCount )
@@ -231,11 +239,12 @@
232240 */
233241 function transliterate( $word, $map )
234242 {
235 - $word = "^" . str_replace(" ", "$ ^", $word) . "$";
 243+ $word = "^" . str_replace( " ", "$ ^", $word ) . "$";
236244 if ( isset( $map["__decompose__"] ) ) {
237245 $letters = $this->codepoints( $word );
238 - }else
 246+ } else {
239247 $letters = $this->letters( $word );
 248+ }
240249
241250 $output = ""; // The output
242251 $last_match = 0; // The position of the last character matched, or the first character of the current run
@@ -244,7 +253,7 @@
245254 $count = count($letters); // The total number of characters in the string
246255 $current = ""; // The substring that we are currently trying to find the longest match for.
247256
248 - while ($i < $count) {
 257+ while ( $i < $count ) {
249258
250259 $next = $current.$letters[$i];
251260
@@ -294,17 +303,28 @@
295304 }
296305
297306 /**
298 - * {{#transliterate:<mapname>|<word>[|<format>[|<onerror>]]}}
 307+ * {{#transliterate:<mapname>|<word>[|<format>[|<answer>[|<onerror>]]]}}
299308 *
300 - * It is envisaged that most usage is in the form {{#transliterate:<mapname>|<word>}}
301 - * However, when in use in multi-purpose templates, it would be very ugly to have
302 - * {{#if}}s around all calls to {{#transliterate}} to check whether the map
303 - * exists. The further two arguments can thus give very flexible output with
304 - * minimal hassle.
 309+ * Direct usage will generally be of the form {{#transilterate:<mapname>|<word>}} while
 310+ * generic templates may find the latter three parameters invaluable for easy use.
 311+ *
 312+ * $mapname is the name of the transliteration map to find.
 313+ * $word is the string to transliterate (if the map was found)
 314+ * $format is a string containing $1 to be replaced by the transliteration if the map exists
 315+ * $answer allows for a user-specified transliteration to override the automatic one
 316+ * $other is an error messsage to display if $answer is blank and an invalid map is specified
305317 */
306 - function render( &$parser, $mapname = '', $word = '', $format = '$1', $other = '' ) {
 318+ function render( &$parser, $mapname = '', $word = '', $format = '$1', $answer = '', $other = '' ) {
307319
308 - $prefix = wfMsg('transliterator-prefix');
 320+ if ( trim( $format ) == '') { // Handle the case when people use {{#transliterate:<>|<>||<>}}
 321+ $format = '$1';
 322+ }
 323+
 324+ if ( trim( $answer ) != '') {
 325+ return str_replace('$1', $answer, $format);
 326+ }
 327+
 328+ $prefix = wfMsg( 'transliterator-prefix' );
309329 $mappage = $prefix.$mapname;
310330
311331 $map = $this->getMap( $prefix, $mapname );

Status & tagging log