r1695 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r1694‎ | r1695 | r1696 >
Date:13:10, 21 September 2003
Author:timstarling
Status:old
Tags:
Comment:
MediaWiki namespace
Modified paths:
  • /trunk/phase3/docs/memcached.doc (modified) (history)
  • /trunk/phase3/includes/Article.php (modified) (history)
  • /trunk/phase3/includes/DatabaseFunctions.php (modified) (history)
  • /trunk/phase3/includes/DefaultSettings.php (modified) (history)
  • /trunk/phase3/includes/GlobalFunctions.php (modified) (history)
  • /trunk/phase3/includes/MagicWord.php (modified) (history)
  • /trunk/phase3/includes/OutputPage.php (modified) (history)
  • /trunk/phase3/install.php (modified) (history)
  • /trunk/phase3/languages/Language.php (modified) (history)
  • /trunk/phase3/maintenance/tables.sql (modified) (history)
  • /trunk/phase3/update.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/tables.sql
@@ -114,7 +114,7 @@
115115 ipb_reason tinyblob NOT NULL default '',
116116 ipb_timestamp char(14) binary NOT NULL default '',
117117 ipb_auto tinyint(1) NOT NULL default '0',
118 - UNIQUE KEY ipb_id
 118+ UNIQUE KEY ipb_id (ipb_id)
119119 ) TYPE=MyISAM PACK_KEYS=1;
120120
121121 DROP TABLE IF EXISTS image;
Index: trunk/phase3/docs/memcached.doc
@@ -109,5 +109,11 @@
110110 set in: LinkCache::addLink()
111111 cleared by: LinkCache::clearBadLink()
112112 should be cleared on page deletion and rename
113 -
 113+MediaWiki namespace:
 114+ key: $wgDBname:MediaWiki:title:$title
 115+ ex: wikidb:MediaWiki:title:Blockedtext
 116+ stores: string
 117+ set in: wfMsg()
 118+ cleared by: Article::editUpdates()
 119+
114120 ... more to come ...
Index: trunk/phase3/install.php
@@ -21,6 +21,7 @@
2222 $DP = "./includes";
2323 include_once( "./LocalSettings.php" );
2424 include_once( "./AdminSettings.php" );
 25+include_once( "./maintenance/InitialiseMessages.php" );
2526
2627 if ( $wgUseTeX && ( ! is_executable( "./math/texvc" ) ) ) {
2728 print "To use math functions, you must first compile texvc by\n" .
@@ -272,26 +273,30 @@
273274 }
274275
275276 $wns = Namespace::getWikipedia();
276 - $ulp = addslashes( wfMsg( "uploadlogpage" ) );
277 - $dlp = addslashes( wfMsg( "dellogpage" ) );
 277+ $ulp = addslashes( wfMsgNoDB( "uploadlogpage" ) );
 278+ $dlp = addslashes( wfMsgNoDB( "dellogpage" ) );
278279
279280 $sql = "DELETE FROM cur";
280281 wfQuery( $sql, DB_WRITE, $fname );
281282
282283 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
283284 "cur_restrictions) VALUES ({$wns},'{$ulp}','" .
284 - wfStrencode( wfMsg( "uploadlogpagetext" ) ) . "','sysop')";
 285+ wfStrencode( wfMsgNoDB( "uploadlogpagetext" ) ) . "','sysop')";
285286 wfQuery( $sql, DB_WRITE, $fname );
286287
287288 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
288289 "cur_restrictions) VALUES ({$wns},'{$dlp}','" .
289 - wfStrencode( wfMsg( "dellogpagetext" ) ) . "','sysop')";
 290+ wfStrencode( wfMsgNoDB( "dellogpagetext" ) ) . "','sysop')";
290291 wfQuery( $sql, DB_WRITE, $fname );
291 -
 292+
 293+ $titleobj = Title::newFromText( wfMsgNoDB( "mainpage" ) );
 294+ $title = $titleobj->getDBkey();
292295 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text) " .
293 - "VALUES (0,'" . wfStrencode( wfMsg( "mainpage" ) ) . "','" .
294 - wfStrencode( wfMsg( "mainpagetext" ) ) . "')";
 296+ "VALUES (0,'$title','" .
 297+ wfStrencode( wfMsgNoDB( "mainpagetext" ) ) . "')";
295298 wfQuery( $sql, DB_WRITE, $fname );
 299+
 300+ initialiseMessages();
296301 }
297302
298303 ?>
Index: trunk/phase3/includes/MagicWord.php
@@ -15,7 +15,7 @@
1616
1717 class MagicWord {
1818 /*private*/ var $mId, $mSynonyms, $mCaseSensitive, $mRegex;
19 - /*private*/ var $mRegexStart, $mBaseRegex;
 19+ /*private*/ var $mRegexStart, $mBaseRegex, $mVariableRegex;
2020
2121 function MagicWord($id = 0, $syn = "", $cs = false)
2222 {
@@ -24,6 +24,7 @@
2525 $this->mCaseSensitive = $cs;
2626 $this->mRegex = "";
2727 $this->mRegexStart = "";
 28+ $this->mVariableRegex = "";
2829 }
2930
3031 /*static*/ function &get( $id )
@@ -53,6 +54,8 @@
5455 $case = $this->mCaseSensitive ? "" : "i";
5556 $this->mRegex = "/{$this->mBaseRegex}/{$case}";
5657 $this->mRegexStart = "/^{$this->mBaseRegex}/{$case}";
 58+ $this->mVariableRegex = str_replace( "\\$1", "([A-Za-z0-9]*)", $this->mRegex );
 59+ wfDebug( "{$this->mVariableRegex}\n" );
5760 }
5861
5962 function getRegex()
@@ -100,6 +103,23 @@
101104 {
102105 return preg_replace( $this->getRegex(), $replacement, $subject );
103106 }
 107+
 108+ function substituteCallback( $text, $callback ) {
 109+ $regex = $this->getVariableRegex();
 110+ return preg_replace_callback( $this->getVariableRegex(), $callback, $text );
 111+ }
 112+
 113+ function getVariableRegex()
 114+ {
 115+ if ( $this->mVariableRegex == "" ) {
 116+ $this->initRegex();
 117+ }
 118+ return $this->mVariableRegex;
 119+ }
 120+
 121+ function getSynonym( $i ) {
 122+ return $this->mSynonyms[$i];
 123+ }
104124 }
105125
106126 /*private*/ function pregRemoveAndRecord( $match )
Index: trunk/phase3/includes/Article.php
@@ -953,7 +953,7 @@
954954
955955 /* private */ function editUpdates( $text )
956956 {
957 - global $wgDeferredUpdateList;
 957+ global $wgDeferredUpdateList, $wgDBname, $wgMemc;
958958
959959 wfSeedRandom();
960960 if ( 0 == mt_rand( 0, 999 ) ) {
@@ -976,6 +976,11 @@
977977 $u = new UserTalkUpdate( 1, $this->mTitle->getNamespace(),
978978 $this->mTitle->getDBkey() );
979979 array_push( $wgDeferredUpdateList, $u );
 980+
 981+ if ( $this->getNamespace == NS_MEDIAWIKI ) {
 982+ $key = "$wgDBname:MediaWiki:title:" . $this->mTitle->getDBkey();
 983+ $wgMemc->delete( $key );
 984+ }
980985 }
981986 }
982987
@@ -1055,6 +1060,11 @@
10561061 } else {
10571062 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
10581063 }
 1064+
 1065+ # {{SUBST:xxx}} variables
 1066+ #
 1067+ $mw =& MagicWord::get( MAG_SUBST );
 1068+ $text = $mw->substituteCallback( $text, "replaceMsgVar" );
10591069
10601070 return $text;
10611071 }
Index: trunk/phase3/includes/GlobalFunctions.php
@@ -173,38 +173,83 @@
174174 }
175175
176176 $wgReplacementKeys = array( "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$9" );
177 -function wfMsg( $key )
178 -{
179 - global $wgLang, $wgReplacementKeys;
180 - $message = $wgLang->getMessage( $key );
 177+
 178+function wfMsg( $key ) {
 179+ $args = func_get_args();
 180+ if ( count( $args ) ) {
 181+ array_shift( $args );
 182+ }
 183+ return wfMsgReal( $key, $args, true );
 184+}
 185+
 186+function wfMsgNoDB( $key ) {
 187+ $args = func_get_args();
 188+ if ( count( $args ) ) {
 189+ array_shift( $args );
 190+ }
 191+ return wfMsgReal( $key, $args, false );
 192+}
 193+
 194+function wfMsgReal( $key, $args, $useDB ) {
 195+ global $wgLang, $wgReplacementKeys, $wgMemc, $wgDBname;
 196+ global $wgUseDatabaseMessages;
 197+
 198+ static $l1cache = array();
 199+ $fname = "wfMsg";
 200+ $message = false;
 201+ $l1hit = false;
181202
182 - if ( $message{0} == ":" ) {
183 - # Get message from the database
184 - $message = substr( $message, 1 );
185 - $title = Title::newFromText( $message );
186 - $dbKey = $title->getDBkey();
187 - $ns = $title->getNamespace();
188 - $sql = "SELECT cur_text FROM cur WHERE cur_namespace=$ns AND cur_title='$dbKey'";
189 - $res = wfQuery( $sql, DB_READ, $fname );
190 - if( ( $s = wfFetchObject( $res ) ) and ( $s->cur_text != "" ) ) {
191 - $message = $s->cur_text;
192 - # filter out a comment at the top if there is one
193 - $commentPos = strpos( $message, "__START__" );
194 - if ( $commentPos !== false ) {
195 - $message = substr( $message, $commentPos + strlen( "__START__" ) );
196 - wfDebug( "Comment filtered at pos $commentPos, \"$message\"\n" );
 203+ # Check for DB suppression
 204+ if ( !$wgUseDatabaseMessages || !$useDB ) {
 205+ $message = $wgLang->getMessage( $key );
 206+ }
 207+
 208+ # Try L1 cache
 209+ if ( $message === false && array_key_exists( $key, $l1cache ) ) {
 210+ $message = $l1cache[$key];
 211+ if ( $message === false ) {
 212+ $message = $wgLang->getMessage( $key );
 213+ }
 214+ $l1hit = true;
 215+ }
 216+
 217+ # Try memcached
 218+ if ( $message === false ) {
 219+ $titleObj = Title::newFromText( $key );
 220+ $title = $titleObj->getDBkey();
 221+ $mcKey = "$wgDBname:MediaWiki:title:$title";
 222+ $message = $wgMemc->get( $mcKey );
 223+ }
 224+
 225+ # Try database
 226+ if ( $message === false) {
 227+ if ( $useDB ) {
 228+ $sql = "SELECT cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI .
 229+ " AND cur_title='$title'";
 230+ $res = wfQuery( $sql, DB_READ, $fname );
 231+
 232+ if ( wfNumRows( $res ) ) {
 233+ # Got it from the database, now store in MemCached
 234+ $obj = wfFetchObject( $res );
 235+ $message = $obj->cur_text;
 236+ wfFreeResult( $res );
 237+ $wgMemc->set( $key, $message, time() + 1800 );
197238 }
198 - } else {
199 - # if the page doesn't exist, just make a link to where it should be
200 - $message = "[[$message]]";
201 - }
202 - wfFreeResult( $res );
 239+ }
203240 }
204 - if( func_num_args() > 1 ) {
205 - $reps = func_get_args();
206 - array_shift( $reps );
207 - $message = str_replace( $wgReplacementKeys, $reps, $message );
 241+
 242+ # Finally, try the array in $wgLang
 243+ if ( $message === false ) {
 244+ $message = $wgLang->getMessage( $key );
 245+ $l1cache[$key] = false;
 246+ } elseif ( !$l1hit && $wgUseDatabaseMessages) {
 247+ $l1cache[$key] = $message;
208248 }
 249+
 250+ # Replace arguments
 251+ if( count( $args ) ) {
 252+ $message = str_replace( $wgReplacementKeys, $args, $message );
 253+ }
209254
210255 if ( "" == $message ) {
211256 # Let's at least _try_ to be graceful about this.
@@ -590,4 +635,14 @@
591636 return array( $limit, $offset );
592637 }
593638
 639+# Used in OutputPage::replaceVariables and Article:pstPass2
 640+function replaceMsgVar( $matches ) {
 641+ return wfMsg( $matches[1] );
 642+}
 643+
 644+function replaceMsgVarNw( $matches ) {
 645+ $text = htmlspecialchars( wfMsg( $matches[1] ) );
 646+ return $text;
 647+}
 648+
594649 ?>
Index: trunk/phase3/includes/DefaultSettings.php
@@ -61,6 +61,10 @@
6262 $wgShowIPinHeader = true; # For non-logged in users
6363 $wgUseDynamicDates = true; # Allows the user to pick their preferred date format
6464
 65+# Translation using MediaWiki: namespace
 66+# Not recommended unless memcached is installed
 67+$wgUseDatabaseMessages = false;
 68+
6569 # Miscellaneous configuration settings
6670 #
6771 $wgReadOnlyFile = "{$wgUploadDirectory}/lock_yBgMBwiR";
@@ -70,6 +74,8 @@
7175 $wgSqlLogFile = "{$wgUploadDirectory}/sqllog_mFhyRe6";
7276 $wgLogQueries = false;
7377 $wgUseBetterLinksUpdate = true;
 78+
 79+# User rights settings
7480 $wgSysopUserBans = true; # Allow sysops to ban logged-in users
7581 $wgIPBlockExpiration = 86400; # IP blocks expire after this many seconds, 0=infinite
7682 $wgUserBlockExpiration = 0; # As above, but for logged-in users
Index: trunk/phase3/includes/OutputPage.php
@@ -428,28 +428,29 @@
429429 function databaseError( $fname )
430430 {
431431 global $wgUser, $wgCommandLineMode;
432 -
433 - $this->setPageTitle( wfMsg( "databaseerror" ) );
 432+
 433+ $this->setPageTitle( wfMsgNoDB( "databaseerror" ) );
434434 $this->setRobotpolicy( "noindex,nofollow" );
435435 $this->setArticleFlag( false );
436436
437437 if ( $wgCommandLineMode ) {
438 - $msg = wfMsg( "dberrortextcl" );
 438+ $msg = wfMsgNoDB( "dberrortextcl" );
439439 } else {
440 - $msg = wfMsg( "dberrortextcl" );
 440+ $msg = wfMsgNoDB( "dberrortextcl" );
441441 }
 442+
442443 $msg = str_replace( "$1", htmlspecialchars( wfLastDBquery() ), $msg );
443444 $msg = str_replace( "$2", htmlspecialchars( $fname ), $msg );
444445 $msg = str_replace( "$3", wfLastErrno(), $msg );
445446 $msg = str_replace( "$4", htmlspecialchars( wfLastError() ), $msg );
446 -
 447+
447448 if ( $wgCommandLineMode ) {
448 - print $msg;
 449+ print "$msg\n";
449450 exit();
450451 }
451452 $sk = $wgUser->getSkin();
452 - $shlink = $sk->makeKnownLink( wfMsg( "searchhelppage" ),
453 - wfMsg( "searchingwikipedia" ) );
 453+ $shlink = $sk->makeKnownLink( wfMsgNoDB( "searchhelppage" ),
 454+ wfMsgNoDB( "searchingwikipedia" ) );
454455 $msg = str_replace( "$5", $shlink, $msg );
455456
456457 $this->mBodytext = $msg;
@@ -1246,6 +1247,14 @@
12471248 $v = wfNumberOfArticles();
12481249 $text = $mw->replace( $v, $text );
12491250 }
 1251+
 1252+ # The callbacks are in GlobalFunctions.php
 1253+ $mw =& MagicWord::get( MAG_MSG );
 1254+ $text = $mw->substituteCallback( $text, "replaceMsgVar" );
 1255+
 1256+ $mw =& MagicWord::get( MAG_MSGNW );
 1257+ $text = $mw->substituteCallback( $text, "replaceMsgVarNw" );
 1258+
12501259 wfProfileOut();
12511260 return $text;
12521261 }
Index: trunk/phase3/includes/DatabaseFunctions.php
@@ -13,10 +13,10 @@
1414 {
1515 global $wgDBserver, $wgDBuser, $wgDBpassword;
1616 global $wgDBname, $wgDBconnection, $wgEmergencyContact;
 17+
 18+ $noconn = str_replace( "$1", $wgDBserver, wfMsgNoDB( "noconnect" ) );
 19+ $nodb = str_replace( "$1", $wgDBname, wfMsgNoDB( "nodb" ) );
1720
18 - $noconn = str_replace( "$1", $wgDBserver, wfMsg( "noconnect" ) );
19 - $nodb = str_replace( "$1", $wgDBname, wfMsg( "nodb" ) );
20 -
2121 $helpme = "\n<p>If this error persists after reloading and clearing " .
2222 "your browser cache, please notify the <a href=\"mailto:" .
2323 $wgEmergencyContact . "\">Wikipedia developers</a>.</p>";
@@ -55,7 +55,7 @@
5656 global $wgTitle, $wgUseFileCache, $title, $wgOutputEncoding;
5757
5858 header( "Content-type: text/html; charset=$wgOutputEncoding" );
59 - if($msg == "") $msg = wfMsg( "noconnect" );
 59+ if($msg == "") $msg = wfMsgNoDB( "noconnect" );
6060 $text = $msg;
6161
6262 if($wgUseFileCache) {
@@ -65,14 +65,14 @@
6666 if($title) {
6767 $t = Title::newFromURL( $title );
6868 } else {
69 - $t = Title::newFromText( wfMsg("mainpage") );
 69+ $t = Title::newFromText( wfMsgNoDB( "mainpage" ) );
7070 }
7171 }
7272
7373 $cache = new CacheManager( $t );
7474 if( $cache->isFileCached() ) {
7575 $msg = "<p style='color: red'><b>$msg<br>\n" .
76 - wfMsg( "cachederror" ) . "</b></p>\n";
 76+ wfMsgNoDB( "cachederror" ) . "</b></p>\n";
7777
7878 $tag = "<div id='article'>";
7979 $text = str_replace(
@@ -98,7 +98,6 @@
9999 global $wgLastDatabaseQuery, $wgOut;
100100 ## wfProfileIn( "wfQuery" );
101101 $wgLastDatabaseQuery = $sql;
102 -
103102 $conn = wfGetDB();
104103 $ret = mysql_query( $sql, $conn );
105104
Index: trunk/phase3/languages/Language.php
@@ -14,6 +14,8 @@
1515 define("NS_WP_TALK", 5);
1616 define("NS_IMAGE", 6);
1717 define("NS_IMAGE_TALK", 7);
 18+define("NS_MEDIAWIKI", 8);
 19+define("NS_MEDIAWIKI_TALK", 9);
1820
1921 # Magic words
2022 define("MAG_REDIRECT", 0);
@@ -27,6 +29,9 @@
2830 define("MAG_CURRENTTIME", 8);
2931 define("MAG_NUMBEROFARTICLES", 9);
3032 define("MAG_CURRENTMONTHNAMEGEN", 10);
 33+define("MAG_MSG", 11);
 34+define("MAG_SUBST", 12);
 35+define("MAG_MSGNW", 13);
3136
3237 #--------------------------------------------------------------------------
3338 # Language-specific text
@@ -48,7 +53,9 @@
4954 4 => "Wikipedia",
5055 5 => "Wikipedia_talk",
5156 6 => "Image",
52 - 7 => "Image_talk"
 57+ 7 => "Image_talk",
 58+ 8 => "MediaWiki",
 59+ 9 => "MediaWiki_talk"
5360 );
5461
5562 /* private */ $wgDefaultUserOptionsEn = array(
@@ -285,6 +292,9 @@
286293 MAG_CURRENTTIME => array( 1, "{{CURRENTTIME}}" ),
287294 MAG_NUMBEROFARTICLES => array( 1, "{{NUMBEROFARTICLES}}" ),
288295 MAG_CURRENTMONTHNAMEGEN => array( 1, "{{CURRENTMONTHNAMEGEN}}"),
 296+ MAG_MSG => array( 1, "{{MSG:$1}}" ),
 297+ MAG_SUBST => array( 1, "{{SUBST:$1}}" ),
 298+ MAG_MSGNW => array( 1, "{{MSGNW:$1}}" )
289299 );
290300
291301 # All special pages have to be listed here: a description of ""
@@ -1223,6 +1233,10 @@
12241234 amusement.",
12251235 "exportcuronly" => "Include only the current revision, not the full history",
12261236
 1237+# Namespace 8 related
 1238+
 1239+"allmessages" => "All_messages",
 1240+"allmessagestext" => "This is a list of all messages available in the MediaWiki: namespace"
12271241 );
12281242
12291243 #--------------------------------------------------------------------------
@@ -1426,6 +1440,12 @@
14271441 return $wgAllMessagesEn[$key];
14281442 }
14291443
 1444+ function getAllMessages()
 1445+ {
 1446+ global $wgAllMessagesEn;
 1447+ return $wgAllMessagesEn;
 1448+ }
 1449+
14301450 function iconv( $in, $out, $string ) {
14311451 # For most languages, this is a wrapper for iconv
14321452 return iconv( $in, $out, $string );
Index: trunk/phase3/update.php
@@ -25,13 +25,15 @@
2626
2727 include_once( "Version.php" );
2828 include_once( "{$IP}/Setup.php" );
 29+include_once( "./maintenance/InitialiseMessages.php" );
 30+
2931 $wgTitle = Title::newFromText( "Update script" );
3032 $wgCommandLineMode = true;
3133 $wgAlterSpecs = array();
3234
3335 do_revision_updates();
34 -
3536 alter_ipblocks();
 37+initialiseMessages();
3638
3739 #
3840 # Run ALTER TABLE queries.

Status & tagging log