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