Recentchanges optimization: avoid a lot of senseless parsing of link text coming...
[lhc/web/wiklou.git] / maintenance / InitialiseMessages.inc
index 5eeb0d2..d625d7d 100755 (executable)
-<?
+<?php
+/**
+ * Script to initialise the MediaWiki namespace
+ *
+ * $Id$
+ *
+ * This script is included from update.php and install.php. Do not run it 
+ * by itself.
+ *
+ * @deprecated
+ * @package MediaWiki
+ * @subpackage Maintenance
+ */
 
-# This script is included from update.php and install.php. Do not run it 
-# by itself.
 
-function initialiseMessages( $overwrite = false) {
-       global $wgLang, $wgScript, $wgServer, $wgAllMessagesEn;
+
+function initialiseMessages( $overwrite = false, $messageArray = false ) {
+       global $wgContLang, $wgContLanguageCode;
+       global $wgContLangClass, $wgAllMessagesEn;
+       global $IP;
+       
+       $langclass = 'Language'. str_replace( '-', '_', ucfirst( $wgContLanguageCode ) );
+       require_once("$IP/languages/$langclass.php");
+
+       $variants = $wgContLang->getVariants();
+       if(!in_array($wgContLanguageCode, $variants))
+               $variants[]=$wgContLanguageCode;
+
+       if ( $messageArray ) {
+               $sortedArray = $messageArray;
+       } else {
+               $sortedArray = $wgAllMessagesEn;
+       }
+       
+       ksort( $sortedArray );
+       
+       $messages=array();
+       foreach ($variants as $v) {
+               $langclass = 'Language'. str_replace( '-', '_', ucfirst( $v ) );
+               $lang = new $langclass;
+               if(!is_object($lang)) {
+                       die ("class $langclass not defined. perhaps you need to include the file $langclass.php in $wgContLangClass.php?");
+               }
+               if($v==$wgContLanguageCode)
+                       $suffix='';
+               else
+                       $suffix="/$v";
+               foreach ($sortedArray as $key => $msg) {
+                       $messages[$key.$suffix] = $lang->getMessage($key);
+               }
+       }       
+
+       initialiseMessagesReal( $overwrite, $messages );
+}
+
+
+
+
+
+
+
+/** */
+function initialiseMessagesReal( $overwrite = false, $messageArray = false ) {
+       global $wgContLang, $wgScript, $wgServer, $wgAllMessagesEn;
        global $wgOut, $wgArticle, $wgUser;
+       global $wgMessageCache, $wgMemc, $wgDBname, $wgUseMemCached;
+
+       # Initialise $wgOut and $wgUser for a command line script
+       $wgOut->disable();
 
-       $fname = "initialiseMessages";
+       $wgUser = new User;
+       $wgUser->setLoaded( true ); # Don't load from DB
+       $wgUser->setName( 'MediaWiki default' );
+       
+       # Don't try to draw messages from the database we're initialising
+       $wgMessageCache->disable();
+       $wgMessageCache->disableTransform();
+
+       $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" );
-       $navText .= "
-
-<table border=1 width=100%><tr><td>
-  '''Name'''
-</td><td>
-  '''Default text'''
-</td><td>
-  '''Current text'''
-</td></tr>";
        
        print "Initialising \"MediaWiki\" namespace...\n";
-       $sql = "SELECT cur_title FROM cur WHERE cur_namespace=$ns AND cur_title IN(";
 
+       
+       $dbr =& wfGetDB( DB_SLAVE );
+       $dbw =& wfGetDB( DB_MASTER );
+       $cur = $dbr->tableName( 'cur' );
+
+       $timestamp = wfTimestampNow();
+       $invTimestamp = wfInvertTimestamp( $timestamp );
+
+       $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 = $messageArray;
+       } else {
+               $sortedArray = $wgAllMessagesEn;
+       }
+       
        ksort( $sortedArray );
 
+       # SELECT all existing messages
+       # Can't afford to be locking all rows for update, this script can take quite a long time to complete
        foreach ( $sortedArray as $key => $enMsg ) {
+               if ( $key == '' ) {
+                       continue; // Skip odd members
+               }
                if ( $first ) {
                        $first = false;
                } else {
-                       $sql .= ",";
+                       $sql .= ',';
                }
-
-               $message = wfMsgNoDB( $key );
                $titleObj = Title::newFromText( $key );
-               $enctitle = wfStrencode($titleObj->getDBkey());
+               $enctitle = $dbr->strencode($titleObj->getDBkey());
                $sql .= "'$enctitle'";
        }
-       $sql .= ")";
-       print "Reading...";
-       $res = wfQuery( $sql, DB_READ );
-       print ".";
-       $row = wfFetchObject( $res );
-       print "done\n";
-
-       print "Setting up...";
-       $exists = array();
+       $sql .= ')';
+       $res = $dbr->query( $sql );
+       $row = $dbr->fetchObject( $res );
+
+       # 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;
-               $row = wfFetchObject( $res );
+               if ( $row->cur_user_text != $username ) {
+                       $existingTitles[$row->cur_title] = 'keep';
+               } else {
+                       $existingTitles[$row->cur_title] = 'chuck';
+               }
+
+               $row = $dbr->fetchObject( $res );
        }
-       print ".";
-       $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...";
+
+       # Insert queries are done in one multi-row insert
+       # Here's the start of it:
+       $arr = array();
+       $talk = $wgContLang->getNsText( NS_TALK );
+       $mwtalk = $wgContLang->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 = wfMsgNoDBForContent( $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;
-                       }
-               }
-               if ( $doInsert ) {
-                       if ( $first ) {
-                               $first = false;
-                       } else {
-                               $sql .= ",";
+
+               # Update messages which already exist
+               if ( array_key_exists( $title, $existingTitles ) ) {
+                       if ( $existingTitles[$title] == 'chuck' || $overwrite) {
+                               # print "$title\n";
+                               $mwTitleObj = Title::makeTitle( NS_MEDIAWIKI, $title );
+                               $article = new Article( $mwTitleObj );
+                               $article->quickEdit( $message );
                        }
-                       $sql .=
-                               "($ns,
-                               '$title',
-                               '$dbencMsg',
-                               'MediaWiki default',
-                               '$timestamp',
-                               'sysop',
-                               1,
-                               '$invTimestamp',
-                               '$timestamp')";
+                       $doInsert = false;
+               } else {
+                       array_push( $arr, array(
+                               'cur_namespace' => $ns,
+                               'cur_title' => $title,
+                               'cur_text' => $message,
+                               'cur_user' => 0,
+                               'cur_user_text' => $username,
+                               'cur_timestamp' => $dbw->timestamp( $timestamp ),
+                               'cur_restrictions' => 'sysop',
+                               'cur_is_new' => 1,
+                               'inverse_timestamp' => $invTimestamp,
+                               'cur_touched' => $dbw->timestamp( $timestamp ) ) );
                }
-               $mw = str_replace( "$1", $key, $msgnw );
-
-               $message = wfEscapeWikiText( $message );
-               $navText .= 
-"<tr><td>
-  [$wgServer$wgScript?title=MediaWiki:$title&action=edit $key]
-</td><td>
-  $message
-</td><td>
-  $mw
-</td></tr>";
        }
-       print "Done\n";
-       print "Writing...";
-       wfQuery( $sql, DB_WRITE, $fname );
-       print "Done\n";
 
-       $navText .= "</table>";
+       $dbw->insert( $cur, $arr, $fname );
 
-       $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
+       print 'Clearing message cache...';
+       $wgMessageCache->clear();
+       print "Done.\n";
+}
+
+function loadLanguageFile( $filename )
+{
+       $contents = file_get_contents( $filename );
+       # Remove header line
+       $p = strpos( $contents, "\n" ) + 1;
+       $contents = substr( $contents, $p );
+       # Unserialize
+       return unserialize( $contents );
+}
+
+function doUpdates() {
+       global $wgDeferredUpdateList;
+       foreach ( $wgDeferredUpdateList as $up ) { $up->doUpdate(); }
 }
 
+?>