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 ) {
15 global $wgContLang, $wgContLanguageCode;
16 global $wgContLangClass, $wgAllMessagesEn;
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 = $wgAllMessagesEn;
32 ksort( $sortedArray );
35 $variants = $wgContLang->getVariants();
36 if(!in_array($wgContLanguageCode, $variants))
37 $variants[]=$wgContLanguageCode;
39 foreach ($variants as $v) {
40 $langclass = 'Language'. str_replace( '-', '_', ucfirst( $v ) );
41 if( !class_exists($langclass) ) {
42 die ("class $langclass not defined. perhaps you need to include the file $langclass.php in $wgContLangClass.php?");
44 $lang = new $langclass;
46 if($v==$wgContLanguageCode)
50 foreach ($sortedArray as $key => $msg) {
51 $messages[$key.$suffix] = $lang->getMessage($key);
55 require_once('languages/Names.php');
58 initialize all messages in $wgForceUIMsgAsContentMsg for all
59 languages in Names.php
61 if( is_array( $wgForceUIMsgAsContentMsg ) ) {
62 foreach( $wgForceUIMsgAsContentMsg as $uikey ) {
63 foreach( $wgLanguageNames as $code => $name) {
64 if( $code == $wgContLanguageCode )
66 $msg = $wgContLang->getMessage( $uikey );
68 $messages[$uikey. '/' . $code] = $msg;
72 initialiseMessagesReal( $overwrite, $messages );
76 function initialiseMessagesReal( $overwrite = false, $messageArray = false ) {
77 global $wgContLang, $wgScript, $wgServer, $wgAllMessagesEn;
78 global $wgOut, $wgArticle, $wgUser;
79 global $wgMessageCache, $wgMemc, $wgDBname, $wgUseMemCached;
81 # Initialise $wgOut and $wgUser for a command line script
85 $wgUser->setLoaded( true ); # Don't load from DB
86 $wgUser->setName( 'MediaWiki default' );
88 # Don't try to draw messages from the database we're initialising
89 $wgMessageCache->disable();
90 $wgMessageCache->disableTransform();
92 $fname = 'initialiseMessages';
94 # cur_user_text responsible for the modifications
95 # Don't change it unless you're prepared to update the DBs accordingly, otherwise the
96 # default messages won't be overwritte
97 $username = 'MediaWiki default';
100 print "Initialising \"MediaWiki\" namespace...\n";
103 $dbr =& wfGetDB( DB_SLAVE );
104 $dbw =& wfGetDB( DB_MASTER );
105 $page = $dbr->tableName( 'page' );
106 $revision = $dbr->tableName( 'revision' );
108 $timestamp = wfTimestampNow();
110 #$sql = "SELECT cur_title,cur_is_new,cur_user_text FROM $cur WHERE cur_namespace=$ns AND cur_title IN(";
111 $sql = "SELECT page_title,page_is_new,rev_user_text FROM $page, $revision WHERE
112 page_namespace=$ns AND rev_page=page_id AND page_title IN(";
114 # Get keys from $wgAllMessagesEn, which is more complete than the local language
116 if ( $messageArray ) {
117 $sortedArray = $messageArray;
119 $sortedArray = $wgAllMessagesEn;
122 ksort( $sortedArray );
124 # SELECT all existing messages
125 # Can't afford to be locking all rows for update, this script can take quite a long time to complete
126 foreach ( $sortedArray as $key => $enMsg ) {
128 continue; // Skip odd members
135 $titleObj = Title::newFromText( $wgContLang->ucfirst( $key ) );
136 $enctitle = $dbr->strencode($titleObj->getDBkey());
137 $sql .= "'$enctitle'";
140 $res = $dbr->query( $sql );
141 $row = $dbr->fetchObject( $res );
143 # Read the results into an array
144 # Decide whether or not each one needs to be overwritten
145 $existingTitles = array();
147 if ( $row->rev_user_text != $username ) {
148 $existingTitles[$row->page_title] = 'keep';
150 $existingTitles[$row->page_title] = 'chuck';
153 $row = $dbr->fetchObject( $res );
156 # Insert queries are done in one multi-row insert
157 # Here's the start of it:
159 $talk = $wgContLang->getNsText( NS_TALK );
160 $mwtalk = $wgContLang->getNsText( NS_MEDIAWIKI_TALK );
162 # Process each message
163 foreach ( $sortedArray as $key => $enMsg ) {
165 continue; // Skip odd members
168 if ( $messageArray ) {
171 $message = wfMsgNoDBForContent( $key );
173 $titleObj = Title::newFromText( $wgContLang->ucfirst( $key ) );
174 $title = $titleObj->getDBkey();
176 # Update messages which already exist
177 if ( array_key_exists( $title, $existingTitles ) ) {
178 if ( $existingTitles[$title] == 'chuck' || $overwrite) {
180 $mwTitleObj = Title::makeTitle( NS_MEDIAWIKI, $title );
181 $article = new Article( $mwTitleObj );
182 $article->quickEdit( $message );
185 extract( $dbw->tableNames( 'text', 'page', 'revision' ) );
186 $sql = "INSERT INTO $text (old_text, old_flags) VALUES ('" .
187 wfStrencode( $message ) .
189 $dbw->query( $sql, $fname );
190 $text_id = $dbw->insertID();
192 $sql = "INSERT INTO $page (page_namespace, page_title, page_restrictions, page_counter, page_is_redirect,
193 page_is_new, page_random, page_touched, page_latest) VALUES (
194 {$ns}, '{$title}', 'sysop', 0, 0, 1, 0.5, '{$timestamp}', {$text_id} )";
195 $dbw->query( $sql, $fname );
196 $page_id = $dbw->insertID();
198 $sql = "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text,
199 rev_timestamp, rev_minor_edit)
200 VALUES ({$text_id}, {$page_id}, '', 0, '{$username}', '{$timestamp}', 0)";
201 $dbw->query( $sql, $fname );
206 # Clear the relevant memcached key
207 print 'Clearing message cache...';
208 $wgMessageCache->clear();
213 function loadLanguageFile( $filename ) {
214 $contents = file_get_contents( $filename );
216 $p = strpos( $contents, "\n" ) + 1;
217 $contents = substr( $contents, $p );
219 return unserialize( $contents );
223 function doUpdates() {
224 global $wgDeferredUpdateList;
225 foreach ( $wgDeferredUpdateList as $up ) { $up->doUpdate(); }