3 * Script to initialise the MediaWiki namespace
7 * This script is included from update.php and install.php. Do not run it
12 * @subpackage Maintenance
17 function initialiseMessages( $overwrite = false, $messageArray = false ) {
18 global $wgContLang, $wgContLanguageCode;
19 global $wgContLangClass, $wgAllMessagesEn;
21 $langclass = 'Language'. str_replace( '-', '_', ucfirst( $wgContLanguageCode ) );
22 require_once("languages/$langclass.php");
24 $variants = $wgContLang->getVariants();
25 if(!in_array($wgContLanguageCode, $variants))
26 $variants[]=$wgContLanguageCode;
28 if ( $messageArray ) {
29 $sortedArray = $messageArray;
31 $sortedArray = $wgAllMessagesEn;
34 ksort( $sortedArray );
37 foreach ($variants as $v) {
38 $langclass = 'Language'. str_replace( '-', '_', ucfirst( $v ) );
39 $lang = new $langclass;
40 if(!is_object($lang)) {
41 die ("class $langclass not defined. perhaps you need to include the file $langclass.php in $wgContLangClass.php?");
43 foreach ($sortedArray as $key => $msg) {
44 $messages[$key."/$v"] = $lang->getMessage($key);
48 initialiseMessagesReal( $overwrite, $messages );
58 function initialiseMessagesReal( $overwrite = false, $messageArray = false ) {
59 global $wgContLang, $wgScript, $wgServer, $wgAllMessagesEn;
60 global $wgOut, $wgArticle, $wgUser;
61 global $wgMessageCache, $wgMemc, $wgDBname, $wgUseMemCached;
63 # Initialise $wgOut and $wgUser for a command line script
67 $wgUser->setLoaded( true ); # Don't load from DB
68 $wgUser->setName( 'MediaWiki default' );
70 # Don't try to draw messages from the database we're initialising
71 $wgMessageCache->disable();
73 $fname = 'initialiseMessages';
75 # cur_user_text responsible for the modifications
76 # Don't change it unless you're prepared to update the DBs accordingly, otherwise the
77 # default messages won't be overwritte
78 $username = 'MediaWiki default';
81 print "Initialising \"MediaWiki\" namespace...\n";
84 $dbr =& wfGetDB( DB_SLAVE );
85 $dbw =& wfGetDB( DB_MASTER );
86 $cur = $dbr->tableName( 'cur' );
88 $timestamp = wfTimestampNow();
89 $invTimestamp = wfInvertTimestamp( $timestamp );
91 $sql = "SELECT cur_title,cur_is_new,cur_user_text FROM $cur WHERE cur_namespace=$ns AND cur_title IN(";
93 # Get keys from $wgAllMessagesEn, which is more complete than the local language
95 if ( $messageArray ) {
96 $sortedArray = $messageArray;
98 $sortedArray = $wgAllMessagesEn;
101 ksort( $sortedArray );
103 # SELECT all existing messages
104 # Can't afford to be locking all rows for update, this script can take quite a long time to complete
105 foreach ( $sortedArray as $key => $enMsg ) {
107 continue; // Skip odd members
114 $titleObj = Title::newFromText( $key );
115 $enctitle = $dbr->strencode($titleObj->getDBkey());
116 $sql .= "'$enctitle'";
119 $res = $dbr->query( $sql );
120 $row = $dbr->fetchObject( $res );
122 # Read the results into an array
123 # Decide whether or not each one needs to be overwritten
124 $existingTitles = array();
126 if ( $row->cur_user_text != $username ) {
127 $existingTitles[$row->cur_title] = 'keep';
129 $existingTitles[$row->cur_title] = 'chuck';
132 $row = $dbr->fetchObject( $res );
135 # Insert queries are done in one multi-row insert
136 # Here's the start of it:
138 $talk = $wgContLang->getNsText( NS_TALK );
139 $mwtalk = $wgContLang->getNsText( NS_MEDIAWIKI_TALK );
141 # Process each message
142 foreach ( $sortedArray as $key => $enMsg ) {
144 continue; // Skip odd members
147 if ( $messageArray ) {
150 $message = wfMsgNoDBForContent( $key );
152 $titleObj = Title::newFromText( $key );
153 $title = $titleObj->getDBkey();
155 # Update messages which already exist
156 if ( array_key_exists( $title, $existingTitles ) ) {
157 if ( $existingTitles[$title] == 'chuck' || $overwrite) {
159 $mwTitleObj = Title::makeTitle( NS_MEDIAWIKI, $title );
160 $article = new Article( $mwTitleObj );
161 $article->quickEdit( $message );
165 array_push( $arr, array(
166 'cur_namespace' => $ns,
167 'cur_title' => $title,
168 'cur_text' => $message,
170 'cur_user_text' => $username,
171 'cur_timestamp' => $dbw->timestamp( $timestamp ),
172 'cur_restrictions' => 'sysop',
174 'inverse_timestamp' => $invTimestamp,
175 'cur_touched' => $dbw->timestamp( $timestamp ) ) );
179 $dbw->insertArray( $cur, $arr, $fname );
181 # Clear the relevant memcached key
182 print 'Clearing message cache...';
183 $wgMessageCache->clear();
187 function loadLanguageFile( $filename )
189 $contents = file_get_contents( $filename );
191 $p = strpos( $contents, "\n" ) + 1;
192 $contents = substr( $contents, $p );
194 return unserialize( $contents );
197 function doUpdates() {
198 global $wgDeferredUpdateList;
199 foreach ( $wgDeferredUpdateList as $up ) { $up->doUpdate(); }