3 * Script to initialise the MediaWiki namespace
5 * This script is included from update.php and install.php. Do not run it
10 * @subpackage Maintenance
14 function initialiseMessages( $overwrite = false, $messageArray = false, $outputCallback = false ) {
15 global $wgContLang, $wgContLanguageCode;
16 global $wgContLangClass;
17 global $wgDisableLangConversion;
18 global $wgForceUIMsgAsContentMsg;
19 global $wgLanguageNames;
22 # overwrite language conversion option so that all variants
23 # of the messages are initialised
24 $wgDisableLangConversion = false;
26 if ( $messageArray ) {
27 $sortedArray = $messageArray;
29 $sortedArray = Language::getMessagesFor( 'en' );
32 ksort( $sortedArray );
35 $variants = $wgContLang->getVariants();
36 if(!in_array($wgContLanguageCode, $variants))
37 $variants[]=$wgContLanguageCode;
39 foreach ($variants as $v) {
40 $lang = Language::factory( $v );
42 if($v==$wgContLanguageCode)
46 foreach ($sortedArray as $key => $msg) {
47 $messages[$key.$suffix] = $lang->getMessage($key);
51 require_once('languages/Names.php');
54 initialize all messages in $wgForceUIMsgAsContentMsg for all
55 languages in Names.php
57 if( is_array( $wgForceUIMsgAsContentMsg ) ) {
58 foreach( $wgForceUIMsgAsContentMsg as $uikey ) {
59 foreach( $wgLanguageNames as $code => $name) {
60 if( $code == $wgContLanguageCode )
62 $msg = $wgContLang->getMessage( $uikey );
64 $messages[$uikey. '/' . $code] = $msg;
68 initialiseMessagesReal( $overwrite, $messages, $outputCallback );
72 function initialiseMessagesReal( $overwrite = false, $messageArray = false, $outputCallback = false ) {
73 global $wgContLang, $wgScript, $wgServer, $wgLanguageCode;
74 global $wgOut, $wgArticle, $wgUser;
75 global $wgMessageCache, $wgMemc, $wgUseMemCached;
77 # Initialise $wgOut and $wgUser for a command line script
80 $wgUser = User::newFromName( 'MediaWiki default' );
81 if ( $wgUser->isAnon() ) {
82 $wgUser->addToDatabase();
85 # Don't try to draw messages from the database we're initialising
86 $wgMessageCache->disable();
87 $wgMessageCache->disableTransform();
89 $fname = 'initialiseMessages';
91 # username responsible for the modifications
92 # Don't change it unless you're prepared to update the DBs accordingly, otherwise the
93 # default messages won't be overwritten
94 $username = 'MediaWiki default';
96 if ( !$outputCallback ) {
97 # Print is not a function, and there doesn't appear to be any built-in
98 # workalikes, so let's just make our own anonymous function to do the
100 $outputCallback = create_function( '$s', 'print $s;' );
103 $outputCallback( "Initialising \"MediaWiki\" namespace for language code $wgLanguageCode...\n" );
105 # Check that the serialized data files are OK
106 if ( Language::isLocalisationOutOfDate( $wgLanguageCode ) ) {
107 $outputCallback( "Warning: serialized data file may be out of date.\n" );
110 $dbr =& wfGetDB( DB_SLAVE );
111 $dbw =& wfGetDB( DB_MASTER );
112 $page = $dbr->tableName( 'page' );
113 $revision = $dbr->tableName( 'revision' );
115 $timestamp = wfTimestampNow();
118 if ( $messageArray ) {
119 $sortedArray = $messageArray;
121 $sortedArray = $wgContLang->getAllMessages();
124 ksort( $sortedArray );
126 # SELECT all existing messages
127 # Can't afford to be locking all rows for update, this script can take quite a long time to complete
129 $nitems = count($sortedArray);
130 $maxitems = $dbr->maxListLen();
133 $chunks = array_chunk($sortedArray, $maxitems);
135 $chunks = array($sortedArray);
137 foreach ($chunks as $chunk) {
139 $sql = "SELECT page_title,page_is_new,rev_user_text FROM $page, $revision WHERE
140 page_namespace=$ns AND rev_id=page_latest AND page_title IN(";
142 foreach ( $chunk as $key => $enMsg ) {
144 continue; // Skip odd members
151 $titleObj = Title::newFromText( $wgContLang->ucfirst( $key ) );
152 $enctitle = $dbr->strencode($titleObj->getDBkey());
153 $sql .= "'$enctitle'";
157 $res = $dbr->query( $sql );
158 while ($row = $dbr->fetchObject($res))
162 # Read the results into an array
163 # Decide whether or not each one needs to be overwritten
164 $existingTitles = array();
165 foreach ($rows as $row) {
166 if ( $row->rev_user_text != $username && $row->rev_user_text != 'Template namespace initialisation script' ) {
167 $existingTitles[$row->page_title] = 'keep';
169 $existingTitles[$row->page_title] = 'chuck';
173 # Insert queries are done in one multi-row insert
174 # Here's the start of it:
176 $talk = $wgContLang->getNsText( NS_TALK );
177 $mwtalk = $wgContLang->getNsText( NS_MEDIAWIKI_TALK );
183 # Merge these into a single transaction for speed
186 # Process each message
187 foreach ( $sortedArray as $key => $message ) {
189 continue; // Skip odd members
192 if ( !$messageArray ) {
193 $message = wfMsgNoDBForContent( $key );
195 if ( is_null( $message ) ) {
196 # This happens sometimes with out of date serialized data files
197 $outputCallback( "Warning: Skipping null message $key\n" );
201 $titleObj = Title::newFromText( $wgContLang->ucfirst( $key ), NS_MEDIAWIKI );
202 $title = $titleObj->getDBkey();
204 # Update messages which already exist
205 if ( array_key_exists( $title, $existingTitles ) ) {
206 if ( $existingTitles[$title] == 'chuck' || $overwrite) {
207 # Don't bother writing a new revision if we're the same
208 # as the current text!
209 $revision = Revision::newFromTitle( $titleObj );
210 if( is_null( $revision ) || $revision->getText() != $message ) {
211 $article = new Article( $titleObj );
212 $article->quickEdit( $message );
221 $article = new Article( $titleObj );
222 $newid = $article->insertOn( $dbw );
223 # FIXME: set restrictions
224 $revision = new Revision( array(
228 'user_text' => $username,
231 $revid = $revision->insertOn( $dbw );
232 $article->updateRevisionOn( $dbw, $revision );
238 # Clear the relevant memcached key
239 $wgMessageCache->clear();
240 $outputCallback( "Done. Updated: $numUpdated, inserted: $numInserted, kept: $numKept.\n" );
244 function loadLanguageFile( $filename ) {
245 $contents = file_get_contents( $filename );
247 $p = strpos( $contents, "\n" ) + 1;
248 $contents = substr( $contents, $p );
250 return unserialize( $contents );
254 function doUpdates() {
255 global $wgDeferredUpdateList;
256 foreach ( $wgDeferredUpdateList as $up ) { $up->doUpdate(); }