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