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 wfDie( "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 # Get keys from $wgAllMessagesEn, which is more complete than the local language
113 if ( $messageArray ) {
114 $sortedArray = $messageArray;
116 $sortedArray = $wgAllMessagesEn;
119 ksort( $sortedArray );
121 # SELECT all existing messages
122 # Can't afford to be locking all rows for update, this script can take quite a long time to complete
124 $nitems = count($sortedArray);
125 $maxitems = $dbr->maxListLen();
128 $chunks = array_chunk($sortedArray, $maxitems);
130 $chunks = array($sortedArray);
132 foreach ($chunks as $chunk) {
134 $sql = "SELECT page_title,page_is_new,rev_user_text FROM $page, $revision WHERE
135 page_namespace=$ns AND rev_page=page_id AND page_title IN(";
137 foreach ( $chunk as $key => $enMsg ) {
139 continue; // Skip odd members
146 $titleObj = Title::newFromText( $wgContLang->ucfirst( $key ) );
147 $enctitle = $dbr->strencode($titleObj->getDBkey());
148 $sql .= "'$enctitle'";
152 $res = $dbr->query( $sql );
153 while ($row = $dbr->fetchObject($res))
157 # Read the results into an array
158 # Decide whether or not each one needs to be overwritten
159 $existingTitles = array();
160 foreach ($rows as $row) {
161 if ( $row->rev_user_text != $username && $row->rev_user_text != 'Template namespace initialisation script' ) {
162 $existingTitles[$row->page_title] = 'keep';
164 $existingTitles[$row->page_title] = 'chuck';
168 # Insert queries are done in one multi-row insert
169 # Here's the start of it:
171 $talk = $wgContLang->getNsText( NS_TALK );
172 $mwtalk = $wgContLang->getNsText( NS_MEDIAWIKI_TALK );
174 # Merge these into a single transaction for speed
177 # Process each message
178 foreach ( $sortedArray as $key => $enMsg ) {
180 continue; // Skip odd members
183 if ( $messageArray ) {
186 $message = wfMsgNoDBForContent( $key );
188 $titleObj = Title::newFromText( $wgContLang->ucfirst( $key ), NS_MEDIAWIKI );
189 $title = $titleObj->getDBkey();
191 # Update messages which already exist
192 if ( array_key_exists( $title, $existingTitles ) ) {
193 if ( $existingTitles[$title] == 'chuck' || $overwrite) {
194 # Don't bother writing a new revision if we're the same
195 # as the current text!
196 $revision = Revision::newFromTitle( $titleObj );
197 if( is_null( $revision ) || $revision->getText() != $message ) {
198 $article = new Article( $titleObj );
199 $article->quickEdit( $message );
203 $article = new Article( $titleObj );
204 $newid = $article->insertOn( $dbw );
205 # FIXME: set restrictions
206 $revision = new Revision( array(
210 'user_text' => $username,
213 $revid = $revision->insertOn( $dbw );
214 $article->updateRevisionOn( $dbw, $revision );
219 # Clear the relevant memcached key
220 print 'Clearing message cache...';
221 $wgMessageCache->clear();
226 function loadLanguageFile( $filename ) {
227 $contents = file_get_contents( $filename );
229 $p = strpos( $contents, "\n" ) + 1;
230 $contents = substr( $contents, $p );
232 return unserialize( $contents );
236 function doUpdates() {
237 global $wgDeferredUpdateList;
238 foreach ( $wgDeferredUpdateList as $up ) { $up->doUpdate(); }