don't unescape preview text (made things like \&lt;pre\&gt; a plain \<pre\> in previe...
[lhc/web/wiklou.git] / maintenance / InitialiseMessages.inc
index 4f59afd..1252246 100755 (executable)
-<?
+<?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, $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 );
-       $navText = wfMsgNoDB( "allmessagestext" );
+       $mwMsg =& MagicWord::get( MAG_MSG );
+       $navText = str_replace( "$1", "allmessagestext", $mwMsg->getSynonym( 0 ) );
        $navText .= "
 
 <table border=1 width=100%><tr><td>
-  '''Name'''
+'''Name'''
 </td><td>
-  '''Default text'''
+'''Default text'''
 </td><td>
-  '''Current text'''
+'''Current text'''
 </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 = ksort( $wgAllMessagesEn );
-
+       if ( $messageArray ) {
+               $sortedArray = $wgAllMessagesEn;
+       } else {
+               $sortedArray = $wgAllMessagesEn;
+       }
+       
+       ksort( $sortedArray );
+       
+       # SELECT all existing messages
        foreach ( $sortedArray as $key => $enMsg ) {
+               if ( $key == "" ) {
+                       continue; // Skip odd members
+               }
                if ( $first ) {
                        $first = false;
                } else {
                        $sql .= ",";
                }
-
-               $message = wfMsgNoDB( $key );
                $titleObj = Title::newFromText( $key );
                $enctitle = wfStrencode($titleObj->getDBkey());
                $sql .= "'$enctitle'";
        }
        $sql .= ")";
-       print "Reading...";
        $res = wfQuery( $sql, DB_READ );
-       print ".";
        $row = wfFetchObject( $res );
-       print "done\n";
 
-       print "Setting up...";
-       $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 );
        }
-       print ".";
+
+       # 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      ";
        $first = true;
        $mwObj =& MagicWord::get( MAG_MSGNW );
        $msgnw = $mwObj->getSynonym( 0 );
-       print "Done\n";
-       
-       print "Processing...";
+       $talk = $wgLang->getNsText( NS_TALK );
+       $mwtalk = $wgLang->getNsText( NS_MEDIAWIKI_TALK );
        
+       # Process each message
        foreach ( $sortedArray as $key => $enMsg ) {
-               $message = wfMsgNoDB( $key );
+               if ( $key == "" ) {
+                       continue; // Skip odd members
+               }
+               # 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 ) {
-                       $sql = "DELETE FROM cur WHERE cur_namespace=$ns AND cur_title='$title'";
-                       wfQuery( $sql, 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 );
                $navText .= 
 "<tr><td>
-  [$wgServer$wgScript?title=MediaWiki:$title&action=edit $key]
+[$wgServer$wgScript?title=MediaWiki:$title&action=edit $key]<br>
+[[$mwtalk:$title|$talk]]
 </td><td>
-  $message
+$message
 </td><td>
-  $mw
+$mw
 </td></tr>";
        }
-       print "Done\n";
-       print "Writing...";
-       wfQuery( $sql, DB_WRITE, $fname );
-       print "Done\n";
 
-       $navText .= "</table>";
+       # 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 );
        $wgOut->disable();
        $wgUser = User::newFromName( 'MediaWiki default' );
-       print "Writing to " . $titleObj->getPrefixedDBkey() . "\n";
        if ( $titleObj->getArticleID() ) {
                $wgArticle->updateArticle( $navText, '', 0, 0 );
        } else {
                $wgArticle->insertNewArticle( $navText, '', 0, 0 );
        }
-       print "Finished\n";
+       
+       # 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 );
 }
 
+?>