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