overwriting of old default messages; ability to dump messages to a file
authorTim Starling <tstarling@users.mediawiki.org>
Mon, 8 Mar 2004 09:30:24 +0000 (09:30 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Mon, 8 Mar 2004 09:30:24 +0000 (09:30 +0000)
maintenance/InitialiseMessages.inc
maintenance/rebuildMessages.php

index 24917f0..6159425 100755 (executable)
@@ -1,16 +1,23 @@
 <?php
+# Script to initialise the MediaWiki namespace
 
 # This script is included from update.php and install.php. Do not run it 
 # by itself.
 
-function initialiseMessages( $overwrite = false) {
+function initialiseMessages( $overwrite = false, $messageArray = false ) {
        global $wgLang, $wgScript, $wgServer, $wgAllMessagesEn;
        global $wgOut, $wgArticle, $wgUser;
-       global $wgMessageCache, $wgMemc, $wgDBname, $wgDatabaseMessages;
+       global $wgMessageCache, $wgMemc, $wgDBname, $wgUseMemCached;
+
+       # Don't try to draw messages from the database we're initialising
        $wgMessageCache->disable();
 
        $fname = "initialiseMessages";
        $ns = NS_MEDIAWIKI;
+       # cur_user_text responsible for the modifications
+       # Don't change it unless you're prepared to update the DBs accordingly, otherwise the 
+       # default messages won't be overwritte
+       $username = "MediaWiki default";
 
        $timestamp = wfTimestampNow();
        $invTimestamp = wfInvertTimestamp( $timestamp );
@@ -27,20 +34,25 @@ function initialiseMessages( $overwrite = false) {
 </td></tr>";
        
        print "Initialising \"MediaWiki\" namespace...\n";
-       $sql = "SELECT cur_title FROM cur WHERE cur_namespace=$ns AND cur_title IN(";
+       $sql = "SELECT cur_title,cur_is_new,cur_user_text FROM cur WHERE cur_namespace=$ns AND cur_title IN(";
 
+       # Get keys from $wgAllMessagesEn, which is more complete than the local language
        $first = true;
-       $sortedArray = $wgAllMessagesEn;
+       if ( $messageArray ) {
+               $sortedArray = $wgAllMessagesEn;
+       } else {
+               $sortedArray = $wgAllMessagesEn;
+       }
+       
        ksort( $sortedArray );
        
+       # SELECT all existing messages
        foreach ( $sortedArray as $key => $enMsg ) {
                if ( $first ) {
                        $first = false;
                } else {
                        $sql .= ",";
                }
-
-               $message = wfMsgNoDB( $key );
                $titleObj = Title::newFromText( $key );
                $enctitle = wfStrencode($titleObj->getDBkey());
                $sql .= "'$enctitle'";
@@ -49,11 +61,21 @@ function initialiseMessages( $overwrite = false) {
        $res = wfQuery( $sql, DB_READ );
        $row = wfFetchObject( $res );
 
-       $exists = array();
+       # Read the results into an array
+       # Decide whether or not each one needs to be overwritten
+       $existingTitles = array();
        while ( $row ) {
-               $exists[$row->cur_title] = 1;
+               if ( !$row->cur_is_new || $row->cur_user_text != $username ) {
+                       $existingTitles[$row->cur_title] = "keep";
+               } else {
+                       $existingTitles[$row->cur_title] = "chuck";
+               }
+
                $row = wfFetchObject( $res );
        }
+
+       # Insert queries are done in one multi-row insert
+       # Here's the start of it:
        $sql = "INSERT INTO cur (cur_namespace, cur_title, cur_text,
                cur_user_text, cur_timestamp, cur_restrictions,
                cur_is_new, inverse_timestamp, cur_touched) VALUES      ";
@@ -63,37 +85,54 @@ function initialiseMessages( $overwrite = false) {
        $talk = $wgLang->getNsText( NS_TALK );
        $mwtalk = $wgLang->getNsText( NS_MEDIAWIKI_TALK );
        
+       # Process each message
        foreach ( $sortedArray as $key => $enMsg ) {
-               $message = wfMsgNoDB( $key );
+               # Get message text
+               if ( $messageArray ) {
+                       $message = $enMsg;
+               } else {
+                       $message = wfMsgNoDB( $key );
+               }
                $titleObj = Title::newFromText( $key );
                $title = $titleObj->getDBkey();
                $dbencMsg = wfStrencode( $message );
 
-               $doInsert = true;
-               if ( $overwrite ) {
-                       wfQuery( "DELETE FROM cur WHERE cur_namespace=$ns AND cur_title='$title'", DB_WRITE, $fname );
-               } else {        
-                       if (array_key_exists($title, $exists)) {
-                               $doInsert = false;
+               # Update messages which already exist
+               # Note: UPDATE is now used instead of DELETE/INSERT to avoid wiping cur_restrictions
+               if ( array_key_exists( $title, $existingTitles ) ) {
+                       if ( $existingTitles[$title] == "chuck" || $overwrite) {
+                               wfQuery( "UPDATE cur 
+                                 SET
+                                   cur_text='$dbencMsg',
+                                   cur_user=0,
+                                   cur_user_text='$username',
+                                   cur_timestamp='$timestamp',
+                                       cur_touched='$timestamp',
+                                   inverse_timestamp='$invTimestamp'
+                                 WHERE cur_namespace=8 and cur_title='$title'", DB_WRITE
+                               );
                        }
-               }
-               if ( $doInsert ) {
+                       $doInsert = false;
+               } else {
+                       # Queue for insertion
                        if ( $first ) {
                                $first = false;
                        } else {
                                $sql .= ",";
                        }
                        $sql .=
-                               "($ns,
-                               '$title',
-                               '$dbencMsg',
-                               'MediaWiki default',
-                               '$timestamp',
-                               'sysop',
-                               1,
-                               '$invTimestamp',
-                               '$timestamp')";
+                         "($ns,
+                         '$title',
+                         '$dbencMsg',
+                         '$username',
+                         '$timestamp',
+                         'sysop',
+                         1,
+                         '$invTimestamp',
+                         '$timestamp')";
                }
+               
+               # Make table row for navigation page
                $mw = str_replace( "$1", $key, $msgnw );
 
                $message = wfEscapeWikiText( $message );
@@ -108,13 +147,14 @@ function initialiseMessages( $overwrite = false) {
 </td></tr>";
        }
 
+       # Perform the insert query
        if ( !$first ) {
                wfQuery( $sql, DB_WRITE, $fname );
        }
 
+       # Write the navigation page
 
        $navText .= "</table>";
-
        $title = wfMsgNoDB( "allmessages" );
        $titleObj = Title::makeTitle( NS_MEDIAWIKI, $title );
        $wgArticle = new Article( $titleObj );
@@ -126,12 +166,18 @@ function initialiseMessages( $overwrite = false) {
                $wgArticle->insertNewArticle( $navText, '', 0, 0 );
        }
        
-       if( $wgDatabaseMessages ) {
+       # Clear the relevant memcached key
+       if( $wgUseMemCached ) {
                print "Clearing message cache...";
                $wgMemc->delete( "$wgDBname:messages" );
                print "Done.\n";
        }
+}
 
+function loadArrayFromFile( $filename )
+{
+       $contents = file_get_contents( $filename );
+       return unserialize( $contents );
 }
 
-?>
\ No newline at end of file
+?>
index 657aea3..58bcdc6 100755 (executable)
@@ -39,6 +39,12 @@ $wgTitle = Title::newFromText( "Rebuild messages script" );
 $wgCommandLineMode = true;
 set_time_limit(0);
 
+if ( count( $argv ) >= 3 ) {
+       $messages = loadArrayFromFile( $argv[3] );
+} else {
+       $messages = false;
+}
+
 if ( $response == 0 ) {
        $row = wfGetArray( "cur", array("count(*) as c"), array("cur_namespace" => NS_MEDIAWIKI) );
        print "Current namespace size: {$row->c}\n";
@@ -62,10 +68,10 @@ if ( $response == 0 ) {
 
 switch ( $response ) {
        case 1:
-               initialiseMessages( false );
+               initialiseMessages( false, $messages );
                break;
        case 2:
-               initialiseMessages( true );
+               initialiseMessages( true, $messages );
                break;
 }