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, $wgDBname, $wgUseMemCached;
77 # Initialise $wgOut and $wgUser for a command line script
81 $wgUser->setLoaded( true ); # Don't load from DB
82 $wgUser->setName( 'MediaWiki default' );
84 # Don't try to draw messages from the database we're initialising
85 $wgMessageCache->disable();
86 $wgMessageCache->disableTransform();
88 $fname = 'initialiseMessages';
90 # username responsible for the modifications
91 # Don't change it unless you're prepared to update the DBs accordingly, otherwise the
92 # default messages won't be overwritten
93 $username = 'MediaWiki default';
95 if ( !$outputCallback ) {
96 # Print is not a function, and there doesn't appear to be any built-in
97 # workalikes, so let's just make our own anonymous function to do the
99 $outputCallback = create_function( '$s', 'print $s;' );
102 $outputCallback( "Initialising \"MediaWiki\" namespace for language code $wgLanguageCode...\n" );
104 # Check that the serialized data files are OK
105 if ( Language::isLocalisationOutOfDate( $wgLanguageCode ) ) {
106 $outputCallback( "Warning: serialized data file may be out of date.\n" );
109 $dbr =& wfGetDB( DB_SLAVE );
110 $dbw =& wfGetDB( DB_MASTER );
111 $page = $dbr->tableName( 'page' );
112 $revision = $dbr->tableName( 'revision' );
114 $timestamp = wfTimestampNow();
117 if ( $messageArray ) {
118 $sortedArray = $messageArray;
120 $sortedArray = $wgContLang->getAllMessages();
123 ksort( $sortedArray );
125 # SELECT all existing messages
126 # Can't afford to be locking all rows for update, this script can take quite a long time to complete
128 $nitems = count($sortedArray);
129 $maxitems = $dbr->maxListLen();
132 $chunks = array_chunk($sortedArray, $maxitems);
134 $chunks = array($sortedArray);
136 foreach ($chunks as $chunk) {
138 $sql = "SELECT page_title,page_is_new,rev_user_text FROM $page, $revision WHERE
139 page_namespace=$ns AND rev_id=page_latest AND page_title IN(";
141 foreach ( $chunk as $key => $enMsg ) {
143 continue; // Skip odd members
150 $titleObj = Title::newFromText( $wgContLang->ucfirst( $key ) );
151 $enctitle = $dbr->strencode($titleObj->getDBkey());
152 $sql .= "'$enctitle'";
156 $res = $dbr->query( $sql );
157 while ($row = $dbr->fetchObject($res))
161 # Read the results into an array
162 # Decide whether or not each one needs to be overwritten
163 $existingTitles = array();
164 foreach ($rows as $row) {
165 if ( $row->rev_user_text != $username && $row->rev_user_text != 'Template namespace initialisation script' ) {
166 $existingTitles[$row->page_title] = 'keep';
168 $existingTitles[$row->page_title] = 'chuck';
172 # Insert queries are done in one multi-row insert
173 # Here's the start of it:
175 $talk = $wgContLang->getNsText( NS_TALK );
176 $mwtalk = $wgContLang->getNsText( NS_MEDIAWIKI_TALK );
182 # Merge these into a single transaction for speed
185 # Process each message
186 foreach ( $sortedArray as $key => $message ) {
188 continue; // Skip odd members
191 if ( !$messageArray ) {
192 $message = wfMsgNoDBForContent( $key );
194 if ( is_null( $message ) ) {
195 # This happens sometimes with out of date serialized data files
196 $outputCallback( "Warning: Skipping null message $key\n" );
200 $titleObj = Title::newFromText( $wgContLang->ucfirst( $key ), NS_MEDIAWIKI );
201 $title = $titleObj->getDBkey();
203 # Update messages which already exist
204 if ( array_key_exists( $title, $existingTitles ) ) {
205 if ( $existingTitles[$title] == 'chuck' || $overwrite) {
206 # Don't bother writing a new revision if we're the same
207 # as the current text!
208 $revision = Revision::newFromTitle( $titleObj );
209 if( is_null( $revision ) || $revision->getText() != $message ) {
210 $article = new Article( $titleObj );
211 $article->quickEdit( $message );
220 $article = new Article( $titleObj );
221 $newid = $article->insertOn( $dbw );
222 # FIXME: set restrictions
223 $revision = new Revision( array(
227 'user_text' => $username,
230 $revid = $revision->insertOn( $dbw );
231 $article->updateRevisionOn( $dbw, $revision );
237 # Clear the relevant memcached key
238 $wgMessageCache->clear();
239 $outputCallback( "Done. Updated: $numUpdated, inserted: $numInserted, kept: $numKept.\n" );
243 function loadLanguageFile( $filename ) {
244 $contents = file_get_contents( $filename );
246 $p = strpos( $contents, "\n" ) + 1;
247 $contents = substr( $contents, $p );
249 return unserialize( $contents );
253 function doUpdates() {
254 global $wgDeferredUpdateList;
255 foreach ( $wgDeferredUpdateList as $up ) { $up->doUpdate(); }