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