more fixes for languages with no language files; allow UI customization for all langu...
[lhc/web/wiklou.git] / maintenance / InitialiseMessages.inc
1 <?php
2 /**
3 * Script to initialise the MediaWiki namespace
4 *
5 * This script is included from update.php and install.php. Do not run it
6 * by itself.
7 *
8 * @deprecated
9 * @package MediaWiki
10 * @subpackage Maintenance
11 */
12
13
14
15 function initialiseMessages( $overwrite = false, $messageArray = false ) {
16 global $wgContLang, $wgContLanguageCode;
17 global $wgContLangClass, $wgAllMessagesEn;
18 global $wgDisableLangConversion;
19 global $IP;
20
21 # overwrite language conversion option so that all variants
22 # of the messages are initialised
23 $wgDisableLangConversion = false;
24
25 if ( $messageArray ) {
26 $sortedArray = $messageArray;
27 } else {
28 $sortedArray = $wgAllMessagesEn;
29 }
30
31 ksort( $sortedArray );
32 $messages=array();
33
34 $variants = $wgContLang->getVariants();
35 if(!in_array($wgContLanguageCode, $variants))
36 $variants[]=$wgContLanguageCode;
37
38 foreach ($variants as $v) {
39 $langclass = 'Language'. str_replace( '-', '_', ucfirst( $v ) );
40 if( !class_exists($langclass) ) {
41 die ("class $langclass not defined. perhaps you need to include the file $langclass.php in $wgContLangClass.php?");
42 }
43 $lang = new $langclass;
44
45 if($v==$wgContLanguageCode)
46 $suffix='';
47 else
48 $suffix="/$v";
49 foreach ($sortedArray as $key => $msg) {
50 $messages[$key.$suffix] = $lang->getMessage($key);
51 }
52 }
53
54 initialiseMessagesReal( $overwrite, $messages );
55 }
56
57
58
59
60
61
62
63 /** */
64 function initialiseMessagesReal( $overwrite = false, $messageArray = false ) {
65 global $wgContLang, $wgScript, $wgServer, $wgAllMessagesEn;
66 global $wgOut, $wgArticle, $wgUser;
67 global $wgMessageCache, $wgMemc, $wgDBname, $wgUseMemCached;
68
69 # Initialise $wgOut and $wgUser for a command line script
70 $wgOut->disable();
71
72 $wgUser = new User;
73 $wgUser->setLoaded( true ); # Don't load from DB
74 $wgUser->setName( 'MediaWiki default' );
75
76 # Don't try to draw messages from the database we're initialising
77 $wgMessageCache->disable();
78 $wgMessageCache->disableTransform();
79
80 $fname = 'initialiseMessages';
81 $ns = NS_MEDIAWIKI;
82 # cur_user_text responsible for the modifications
83 # Don't change it unless you're prepared to update the DBs accordingly, otherwise the
84 # default messages won't be overwritte
85 $username = 'MediaWiki default';
86
87
88 print "Initialising \"MediaWiki\" namespace...\n";
89
90
91 $dbr =& wfGetDB( DB_SLAVE );
92 $dbw =& wfGetDB( DB_MASTER );
93 $cur = $dbr->tableName( 'cur' );
94
95 $timestamp = wfTimestampNow();
96 $invTimestamp = wfInvertTimestamp( $timestamp );
97
98 $sql = "SELECT cur_title,cur_is_new,cur_user_text FROM $cur WHERE cur_namespace=$ns AND cur_title IN(";
99
100 # Get keys from $wgAllMessagesEn, which is more complete than the local language
101 $first = true;
102 if ( $messageArray ) {
103 $sortedArray = $messageArray;
104 } else {
105 $sortedArray = $wgAllMessagesEn;
106 }
107
108 ksort( $sortedArray );
109
110 # SELECT all existing messages
111 # Can't afford to be locking all rows for update, this script can take quite a long time to complete
112 foreach ( $sortedArray as $key => $enMsg ) {
113 if ( $key == '' ) {
114 continue; // Skip odd members
115 }
116 if ( $first ) {
117 $first = false;
118 } else {
119 $sql .= ',';
120 }
121 $titleObj = Title::newFromText( $key );
122 $enctitle = $dbr->strencode($titleObj->getDBkey());
123 $sql .= "'$enctitle'";
124 }
125 $sql .= ')';
126 $res = $dbr->query( $sql );
127 $row = $dbr->fetchObject( $res );
128
129 # Read the results into an array
130 # Decide whether or not each one needs to be overwritten
131 $existingTitles = array();
132 while ( $row ) {
133 if ( $row->cur_user_text != $username ) {
134 $existingTitles[$row->cur_title] = 'keep';
135 } else {
136 $existingTitles[$row->cur_title] = 'chuck';
137 }
138
139 $row = $dbr->fetchObject( $res );
140 }
141
142 # Insert queries are done in one multi-row insert
143 # Here's the start of it:
144 $arr = array();
145 $talk = $wgContLang->getNsText( NS_TALK );
146 $mwtalk = $wgContLang->getNsText( NS_MEDIAWIKI_TALK );
147
148 # Process each message
149 foreach ( $sortedArray as $key => $enMsg ) {
150 if ( $key == '' ) {
151 continue; // Skip odd members
152 }
153 # Get message text
154 if ( $messageArray ) {
155 $message = $enMsg;
156 } else {
157 $message = wfMsgNoDBForContent( $key );
158 }
159 $titleObj = Title::newFromText( $key );
160 $title = $titleObj->getDBkey();
161
162 # Update messages which already exist
163 if ( array_key_exists( $title, $existingTitles ) ) {
164 if ( $existingTitles[$title] == 'chuck' || $overwrite) {
165 # print "$title\n";
166 $mwTitleObj = Title::makeTitle( NS_MEDIAWIKI, $title );
167 $article = new Article( $mwTitleObj );
168 $article->quickEdit( $message );
169 }
170 $doInsert = false;
171 } else {
172 array_push( $arr, array(
173 'cur_namespace' => $ns,
174 'cur_title' => $title,
175 'cur_text' => $message,
176 'cur_user' => 0,
177 'cur_user_text' => $username,
178 'cur_timestamp' => $dbw->timestamp( $timestamp ),
179 'cur_restrictions' => 'sysop',
180 'cur_is_new' => 1,
181 'inverse_timestamp' => $invTimestamp,
182 'cur_touched' => $dbw->timestamp( $timestamp ) ) );
183 }
184 }
185
186 $dbw->insert( 'cur', $arr, $fname );
187
188 # Clear the relevant memcached key
189 print 'Clearing message cache...';
190 $wgMessageCache->clear();
191 print "Done.\n";
192 }
193
194 function loadLanguageFile( $filename )
195 {
196 $contents = file_get_contents( $filename );
197 # Remove header line
198 $p = strpos( $contents, "\n" ) + 1;
199 $contents = substr( $contents, $p );
200 # Unserialize
201 return unserialize( $contents );
202 }
203
204 function doUpdates() {
205 global $wgDeferredUpdateList;
206 foreach ( $wgDeferredUpdateList as $up ) { $up->doUpdate(); }
207 }
208
209 ?>