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