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