r1614 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r1613‎ | r1614 | r1615 >
Date:09:46, 31 August 2003
Author:timstarling
Status:old
Tags:
Comment:
Internationalisation of "magic words" such as #redirect
Modified paths:
  • /trunk/phase3/includes/Article.php (modified) (history)
  • /trunk/phase3/includes/MagicWord.php (added) (history)
  • /trunk/phase3/includes/OutputPage.php (modified) (history)
  • /trunk/phase3/includes/Setup.php (modified) (history)
  • /trunk/phase3/languages/Language.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/MagicWord.php
@@ -0,0 +1,111 @@
 2+<?
 3+
 4+# This class encapsulates "magic words" such as #redirect, __NOTOC__, etc.
 5+# Usage:
 6+# if (MagicWord::get( MAG_REDIRECT )->match( $text ) )
 7+#
 8+# Possible future improvements:
 9+# * Simultaneous searching for a number of magic words
 10+# * $wgMagicWords in shared memory
 11+#
 12+# Please avoid reading the data out of one of these objects and then writing
 13+# special case code. If possible, add another match()-like function here.
 14+
 15+/*private*/ $wgMagicFound = false;
 16+
 17+class MagicWord {
 18+ /*private*/ var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
 19+ /*private*/ var $mRegexStart, $mBaseRegex;
 20+
 21+ function MagicWord($id = 0, $syn = "", $cs = false)
 22+ {
 23+ $this->mId = $id;
 24+ $this->mSynonyms = (array)$syn;
 25+ $this->mCaseSensitive = $cs;
 26+ $this->mRegex = "";
 27+ $this->mRegexStart = "";
 28+ }
 29+
 30+ /*static*/ function &get( $id )
 31+ {
 32+ global $wgMagicWords;
 33+
 34+ if (!array_key_exists( $id, $wgMagicWords ) ) {
 35+ $mw = new MagicWord();
 36+ $mw->load( $id );
 37+ $wgMagicWords[$id] = $mw;
 38+ }
 39+ return $wgMagicWords[$id];
 40+ }
 41+
 42+ function load( $id )
 43+ {
 44+ global $wgLang;
 45+
 46+ $this->mId = $id;
 47+ $wgLang->getMagic( $this );
 48+ }
 49+
 50+ /* private */ function initRegex()
 51+ {
 52+ $escSyn = array_map( "preg_quote", $this->mSynonyms );
 53+ $this->mBaseRegex = implode( "|", $escSyn );
 54+ $case = $this->mCaseSensitive ? "" : "i";
 55+ $this->mRegex = "/{$this->mBaseRegex}/{$case}";
 56+ $this->mRegexStart = "/^{$this->mBaseRegex}/{$case}";
 57+ }
 58+
 59+ function getRegex()
 60+ {
 61+ if ($this->mRegex == "" ) {
 62+ $this->initRegex();
 63+ }
 64+ return $this->mRegex;
 65+ }
 66+
 67+ function getRegexStart()
 68+ {
 69+ if ($this->mRegex == "" ) {
 70+ $this->initRegex();
 71+ }
 72+ return $this->mRegexStart;
 73+ }
 74+
 75+ function getBaseRegex()
 76+ {
 77+ if ($this->mRegex == "") {
 78+ $this->initRegex();
 79+ }
 80+ return $this->mBaseRegex;
 81+ }
 82+
 83+ function match( $text ) {
 84+ return preg_match( $this->getRegex(), $text );
 85+ }
 86+
 87+ function matchStart( $text )
 88+ {
 89+ return preg_match( $this->getRegexStart(), $text );
 90+ }
 91+
 92+ function matchAndRemove( &$text )
 93+ {
 94+ global $wgMagicFound;
 95+ $wgMagicFound = false;
 96+ $text = preg_replace_callback( $this->getRegex(), "pregRemoveAndRecord", $text );
 97+ return $wgMagicFound;
 98+ }
 99+
 100+ function replace( $replacement, $subject )
 101+ {
 102+ return preg_replace( $this->getRegex(), $replacement, $subject );
 103+ }
 104+}
 105+
 106+/*private*/ function pregRemoveAndRecord( $match )
 107+{
 108+ global $wgMagicFound;
 109+ $wgMagicFound = true;
 110+ return "";
 111+}
 112+
Property changes on: trunk/phase3/includes/MagicWord.php
___________________________________________________________________
Added: svn:eol-style
1113 + native
Added: svn:keywords
2114 + Author Date Id Revision
Index: trunk/phase3/includes/Article.php
@@ -102,7 +102,7 @@
103103
104104 function loadContent( $noredir = false )
105105 {
106 - global $wgOut, $wgTitle;
 106+ global $wgOut, $wgTitle, $wgMwRedir;
107107 global $oldid, $redirect; # From query
108108
109109 if ( $this->mContentLoaded ) return;
@@ -130,9 +130,8 @@
131131
132132 # If we got a redirect, follow it (unless we've been told
133133 # not to by either the function parameter or the query
134 -
135134 if ( ( "no" != $redirect ) && ( false == $noredir ) &&
136 - ( preg_match( "/^#redirect/i", $s->cur_text ) ) ) {
 135+ ( $wgMwRedir->matchStart( $s->cur_text ) ) ) {
137136 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
138137 $s->cur_text, $m ) ) {
139138 $rt = Title::newFromText( $m[1] );
@@ -204,10 +203,10 @@
205204
206205 function isCountable( $text )
207206 {
208 - global $wgTitle, $wgUseCommaCount;
209 -
 207+ global $wgTitle, $wgUseCommaCount, $wgMwRedir;
 208+
210209 if ( 0 != $wgTitle->getNamespace() ) { return 0; }
211 - if ( preg_match( "/^#redirect/i", $text ) ) { return 0; }
 210+ if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
212211 $token = ($wgUseCommaCount ? "," : "[[" );
213212 if ( false === strstr( $text, $token ) ) { return 0; }
214213 return 1;
@@ -331,13 +330,13 @@
332331
333332 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
334333 {
335 - global $wgOut, $wgUser, $wgTitle, $wgLinkCache;
 334+ global $wgOut, $wgUser, $wgTitle, $wgLinkCache, $wgMwRedir;
336335 $fname = "Article::insertNewArticle";
337336
338337 $ns = $wgTitle->getNamespace();
339338 $ttl = $wgTitle->getDBkey();
340339 $text = $this->preSaveTransform( $text );
341 - if ( preg_match( "/^#redirect/i", $text ) ) { $redir = 1; }
 340+ if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
342341 else { $redir = 0; }
343342
344343 $now = wfTimestampNow();
@@ -381,7 +380,7 @@
382381 function updateArticle( $text, $summary, $minor, $watchthis, $section="" )
383382 {
384383 global $wgOut, $wgUser, $wgTitle, $wgLinkCache;
385 - global $wgDBtransactions;
 384+ global $wgDBtransactions, $wgMwRedir;
386385 $fname = "Article::updateArticle";
387386
388387 $this->loadLastEdit();
@@ -403,7 +402,7 @@
404403 }
405404 if ( $this->mMinorEdit ) { $me1 = 1; } else { $me1 = 0; }
406405 if ( $minor ) { $me2 = 1; } else { $me2 = 0; }
407 - if ( preg_match( "/^(#redirect[^\\n]+)/i", $text, $m ) ) {
 406+ if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
408407 $redir = 1;
409408 $text = $m[1] . "\n"; # Remove all content but redirect
410409 }
@@ -498,6 +497,7 @@
499498 function showArticle( $text, $subtitle )
500499 {
501500 global $wgOut, $wgTitle, $wgUser, $wgLinkCache, $wgUseBetterLinksUpdate;
 501+ global $wgMwRedir;
502502
503503 $wgLinkCache = new LinkCache();
504504
@@ -511,7 +511,7 @@
512512 $wgOut->addWikiText( $text );
513513
514514 $this->editUpdates( $text );
515 - if( preg_match( "/^#redirect/i", $text ) )
 515+ if( $wgMwRedir->matchStart( $text ) )
516516 $r = "redirect=no";
517517 else
518518 $r = "";
Index: trunk/phase3/includes/Setup.php
@@ -16,13 +16,12 @@
1717 include_once( "$IP/LinkCache.php" );
1818 include_once( "$IP/Title.php" );
1919 include_once( "$IP/Article.php" );
 20+include_once( "$IP/MagicWord.php" );
2021 include_once( "$IP/MemCachedClient.inc.php" );
2122
22 -wfDebug( "\n\n" );
23 -
2423 global $wgUser, $wgLang, $wgOut, $wgTitle;
2524 global $wgArticle, $wgDeferredUpdateList, $wgLinkCache;
26 -global $wgMemc;
 25+global $wgMemc, $wgMagicWords, $wgMwRedir;
2726
2827 class MemCachedClientforWiki extends MemCachedClient {
2928 function _debug( $text ) {
@@ -39,6 +38,8 @@
4039 include_once( "$IP/Language.php" );
4140
4241 $wgOut = new OutputPage();
 42+wfDebug( "\n\n" );
 43+
4344 $wgLangClass = "Language" . ucfirst( $wgLanguageCode );
4445 if( ! class_exists( $wgLangClass ) ) {
4546 include_once( "$IP/LanguageUtf8.php" );
@@ -49,5 +50,6 @@
5051 $wgUser = User::loadFromSession();
5152 $wgDeferredUpdateList = array();
5253 $wgLinkCache = new LinkCache();
53 -
 54+$wgMagicWords = array();
 55+$wgMwRedir =& MagicWord::get( MAG_REDIRECT );
5456 ?>
Index: trunk/phase3/includes/OutputPage.php
@@ -1109,23 +1109,37 @@
11101110 /* As with sigs, use server's local time --
11111111 ensure this is appropriate for your audience! */
11121112 $v = date( "m" );
1113 - $text = str_replace( "{{CURRENTMONTH}}", $v, $text );
 1113+ $mw =& MagicWord::get( MAG_CURRENTMONTH );
 1114+ $text = $mw->replace( $v, $text );
 1115+
11141116 $v = $wgLang->getMonthName( date( "n" ) );
1115 - $text = str_replace( "{{CURRENTMONTHNAME}}", $v, $text );
 1117+ $mw =& MagicWord::get( MAG_CURRENTMONTHNAME );
 1118+ $text = $mw->replace( $v, $text );
 1119+
11161120 $v = $wgLang->getMonthNameGen( date( "n" ) );
1117 - $text = str_replace( "{{CURRENTMONTHNAMEGEN}}", $v, $text );
 1121+ $mw =& MagicWord::get( MAG_CURRENTMONTHNAMEGEN );
 1122+ $text = $mw->replace( $v, $text );
 1123+
11181124 $v = date( "j" );
1119 - $text = str_replace( "{{CURRENTDAY}}", $v, $text );
 1125+ $mw = MagicWord::get( MAG_CURRENTDAY );
 1126+ $text = $mw->replace( $v, $text );
 1127+
11201128 $v = $wgLang->getWeekdayName( date( "w" )+1 );
1121 - $text = str_replace( "{{CURRENTDAYNAME}}", $v, $text );
 1129+ $mw =& MagicWord::get( MAG_CURRENTDAYNAME );
 1130+ $text = $mw->replace( $v, $text );
 1131+
11221132 $v = date( "Y" );
1123 - $text = str_replace( "{{CURRENTYEAR}}", $v, $text );
 1133+ $mw =& MagicWord::get( MAG_CURRENTYEAR );
 1134+ $text = $mw->replace( $v, $text );
 1135+
11241136 $v = $wgLang->time( wfTimestampNow(), false );
1125 - $text = str_replace( "{{CURRENTTIME}}", $v, $text );
 1137+ $mw =& MagicWord::get( MAG_CURRENTTIME );
 1138+ $text = $mw->replace( $v, $text );
11261139
1127 - if ( false !== strstr( $text, "{{NUMBEROFARTICLES}}" ) ) {
 1140+ $mw =& MagicWord::get( MAG_NUMBEROFARTICLES );
 1141+ if ( $mw->match( $text ) ) {
11281142 $v = wfNumberOfArticles();
1129 - $text = str_replace( "{{NUMBEROFARTICLES}}", $v, $text );
 1143+ $text = $mw->replace( $v, $text );
11301144 }
11311145 wfProfileOut();
11321146 return $text;
@@ -1262,10 +1276,8 @@
12631277 }
12641278 # if the string __NOTOC__ (not case-sensitive) occurs in the HTML, do not
12651279 # add TOC
1266 - if(preg_match("/__NOTOC__/i",$text)) {
1267 - $text=preg_replace("/__NOTOC__/i","",$text);
1268 - $st=0;
1269 - }
 1280+ $mw =& MagicWord::get( MAG_NOTOC );
 1281+ $st = ! $mw->matchAndRemove( $text );
12701282
12711283 # never add the TOC to the Main Page. This is an entry page that should not
12721284 # be more than 1-2 screens large anyway
Index: trunk/phase3/languages/Language.php
@@ -1,5 +1,37 @@
22 <?
33
 4+#--------------------------------------------------------------------------
 5+# Constants
 6+#--------------------------------------------------------------------------
 7+
 8+# Namespaces
 9+define("NS_SPECIAL", -1);
 10+define("NS_MAIN", 0);
 11+define("NS_TALK", 1);
 12+define("NS_USER", 2);
 13+define("NS_USER_TALK", 3);
 14+define("NS_WP", 4);
 15+define("NS_WP_TALK", 5);
 16+define("NS_IMAGE", 6);
 17+define("NS_IMAGE_TALK", 7);
 18+
 19+# Magic words
 20+define("MAG_REDIRECT", 0);
 21+define("MAG_NOTOC", 1);
 22+define("MAG_START", 2);
 23+define("MAG_CURRENTMONTH", 3);
 24+define("MAG_CURRENTMONTHNAME", 4);
 25+define("MAG_CURRENTDAY", 5);
 26+define("MAG_CURRENTDAYNAME", 6);
 27+define("MAG_CURRENTYEAR", 7);
 28+define("MAG_CURRENTTIME", 8);
 29+define("MAG_NUMBEROFARTICLES", 9);
 30+define("MAG_CURRENTMONTHNAMEGEN", 10);
 31+
 32+#--------------------------------------------------------------------------
 33+# Language-specific text
 34+#--------------------------------------------------------------------------
 35+
436 # NOTE: To turn off "Current Events" in the sidebar,
537 # set "currentevents" => "-"
638
@@ -240,6 +272,21 @@
241273 "Sep", "Oct", "Nov", "Dec"
242274 );
243275
 276+/* private */ $wgMagicWordsEn = array(
 277+# ID CASE SYNONYMS
 278+ MAG_REDIRECT => array( 0, "#redirect" ),
 279+ MAG_NOTOC => array( 0, "__NOTOC__" ),
 280+ MAG_START => array( 0, "__START__" ),
 281+ MAG_CURRENTMONTH => array( 1, "{{CURRENTMONTH}}" ),
 282+ MAG_CURRENTMONTHNAME => array( 1, "{{CURRENTMONTHNAME}}" ),
 283+ MAG_CURRENTDAY => array( 1, "{{CURRENTDAY}}" ),
 284+ MAG_CURRENTDAYNAME => array( 1, "{{CURRENTDAYNAME}}" ),
 285+ MAG_CURRENTYEAR => array( 1, "{{CURRENTYEAR}}" ),
 286+ MAG_CURRENTTIME => array( 1, "{{CURRENTTIME}}" ),
 287+ MAG_NUMBEROFARTICLES => array( 1, "{{NUMBEROFARTICLES}}" ),
 288+ MAG_CURRENTMONTHNAMEGEN => array( 1, "{{CURRENTMONTHNAMEGEN}}"),
 289+);
 290+
244291 # All special pages have to be listed here: a description of ""
245292 # will make them not show up on the "Special Pages" page, which
246293 # is the right thing for some of them (such as the "targeted" ones).
@@ -1144,6 +1191,10 @@
11451192
11461193 );
11471194
 1195+#--------------------------------------------------------------------------
 1196+# Internationalisation code
 1197+#--------------------------------------------------------------------------
 1198+
11481199 class Language {
11491200
11501201 function getDefaultUserOptions () {
@@ -1494,6 +1545,21 @@
14951546
14961547 # For right-to-left language support
14971548 function isRTL() { return false; }
 1549+
 1550+ function getMagicWords()
 1551+ {
 1552+ global $wgMagicWordsEn;
 1553+ return $wgMagicWordsEn;
 1554+ }
 1555+
 1556+ # Fill a MagicWord object with data from here
 1557+ function getMagic( &$mw )
 1558+ {
 1559+ $raw = $this->getMagicWords(); # don't worry, it's reference counted not deep copy
 1560+ $rawEntry = $raw[$mw->mId];
 1561+ $mw->mCaseSensitive = $rawEntry[0];
 1562+ $mw->mSynonyms = array_slice( $rawEntry, 1 );
 1563+ }
14981564 }
14991565
15001566 global $IP;

Status & tagging log