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;
22 $langclass = 'Language'. str_replace( '-', '_', ucfirst( $wgContLanguageCode ) );
23 require_once("$IP/languages/$langclass.php");
25 $variants = $wgContLang->getVariants();
26 if(!in_array($wgContLanguageCode, $variants))
27 $variants[]=$wgContLanguageCode;
29 if ( $messageArray ) {
30 $sortedArray = $messageArray;
32 $sortedArray = $wgAllMessagesEn;
35 ksort( $sortedArray );
38 foreach ($variants as $v) {
39 $langclass = 'Language'. str_replace( '-', '_', ucfirst( $v ) );
40 $lang = new $langclass;
41 if(!is_object($lang)) {
42 die ("class $langclass not defined. perhaps you need to include the file $langclass.php in $wgContLangClass.php?");
44 if($v==$wgContLanguageCode)
48 foreach ($sortedArray as $key => $msg) {
49 $messages[$key.$suffix] = $lang->getMessage($key);
53 initialiseMessagesReal( $overwrite, $messages );
63 function initialiseMessagesReal( $overwrite = false, $messageArray = false ) {
64 global $wgContLang, $wgScript, $wgServer, $wgAllMessagesEn;
65 global $wgOut, $wgArticle, $wgUser;
66 global $wgMessageCache, $wgMemc, $wgDBname, $wgUseMemCached;
68 # Initialise $wgOut and $wgUser for a command line script
72 $wgUser->setLoaded( true ); # Don't load from DB
73 $wgUser->setName( 'MediaWiki default' );
75 # Don't try to draw messages from the database we're initialising
76 $wgMessageCache->disable();
77 $wgMessageCache->disableTransform();
79 $fname = 'initialiseMessages';
81 # cur_user_text responsible for the modifications
82 # Don't change it unless you're prepared to update the DBs accordingly, otherwise the
83 # default messages won't be overwritte
84 $username = 'MediaWiki default';
87 print "Initialising \"MediaWiki\" namespace...\n";
90 $dbr =& wfGetDB( DB_SLAVE );
91 $dbw =& wfGetDB( DB_MASTER );
92 $cur = $dbr->tableName( 'cur' );
94 $timestamp = wfTimestampNow();
95 $invTimestamp = wfInvertTimestamp( $timestamp );
97 $sql = "SELECT cur_title,cur_is_new,cur_user_text FROM $cur WHERE cur_namespace=$ns AND cur_title IN(";
99 # Get keys from $wgAllMessagesEn, which is more complete than the local language
101 if ( $messageArray ) {
102 $sortedArray = $messageArray;
104 $sortedArray = $wgAllMessagesEn;
107 ksort( $sortedArray );
109 # SELECT all existing messages
110 # Can't afford to be locking all rows for update, this script can take quite a long time to complete
111 foreach ( $sortedArray as $key => $enMsg ) {
113 continue; // Skip odd members
120 $titleObj = Title::newFromText( $key );
121 $enctitle = $dbr->strencode($titleObj->getDBkey());
122 $sql .= "'$enctitle'";
125 $res = $dbr->query( $sql );
126 $row = $dbr->fetchObject( $res );
128 # Read the results into an array
129 # Decide whether or not each one needs to be overwritten
130 $existingTitles = array();
132 if ( $row->cur_user_text != $username ) {
133 $existingTitles[$row->cur_title] = 'keep';
135 $existingTitles[$row->cur_title] = 'chuck';
138 $row = $dbr->fetchObject( $res );
141 # Insert queries are done in one multi-row insert
142 # Here's the start of it:
144 $talk = $wgContLang->getNsText( NS_TALK );
145 $mwtalk = $wgContLang->getNsText( NS_MEDIAWIKI_TALK );
147 # Process each message
148 foreach ( $sortedArray as $key => $enMsg ) {
150 continue; // Skip odd members
153 if ( $messageArray ) {
156 $message = wfMsgNoDBForContent( $key );
158 $titleObj = Title::newFromText( $key );
159 $title = $titleObj->getDBkey();
161 # Update messages which already exist
162 if ( array_key_exists( $title, $existingTitles ) ) {
163 if ( $existingTitles[$title] == 'chuck' || $overwrite) {
165 $mwTitleObj = Title::makeTitle( NS_MEDIAWIKI, $title );
166 $article = new Article( $mwTitleObj );
167 $article->quickEdit( $message );
171 array_push( $arr, array(
172 'cur_namespace' => $ns,
173 'cur_title' => $title,
174 'cur_text' => $message,
176 'cur_user_text' => $username,
177 'cur_timestamp' => $dbw->timestamp( $timestamp ),
178 'cur_restrictions' => 'sysop',
180 'inverse_timestamp' => $invTimestamp,
181 'cur_touched' => $dbw->timestamp( $timestamp ) ) );
185 $dbw->insert( $cur, $arr, $fname );
187 # Clear the relevant memcached key
188 print 'Clearing message cache...';
189 $wgMessageCache->clear();
193 function loadLanguageFile( $filename )
195 $contents = file_get_contents( $filename );
197 $p = strpos( $contents, "\n" ) + 1;
198 $contents = substr( $contents, $p );
200 return unserialize( $contents );
203 function doUpdates() {
204 global $wgDeferredUpdateList;
205 foreach ( $wgDeferredUpdateList as $up ) { $up->doUpdate(); }