3f3dff2522e4882fc7bb1c1aaf0499d0cc06d506
[lhc/web/wiklou.git] / maintenance / InitialiseMessages.inc
1 <?php
2 # Script to initialise the MediaWiki namespace
3
4 # This script is included from update.php and install.php. Do not run it
5 # by itself.
6
7 function initialiseMessages( $overwrite = false, $messageArray = false ) {
8 global $wgLang, $wgScript, $wgServer, $wgAllMessagesEn;
9 global $wgOut, $wgArticle, $wgUser;
10 global $wgMessageCache, $wgMemc, $wgDBname, $wgUseMemCached;
11
12 # Initialise $wgOut and $wgUser for a command line script
13 $wgOut->disable();
14
15 $wgUser = new User;
16 $wgUser->setLoaded( true ); # Don't load from DB
17 $wgUser->setName( 'MediaWiki default' );
18
19 # Don't try to draw messages from the database we're initialising
20 $wgMessageCache->disable();
21
22 $fname = 'initialiseMessages';
23 $ns = NS_MEDIAWIKI;
24 # cur_user_text responsible for the modifications
25 # Don't change it unless you're prepared to update the DBs accordingly, otherwise the
26 # default messages won't be overwritte
27 $username = 'MediaWiki default';
28
29 $timestamp = wfTimestampNow();
30 $invTimestamp = wfInvertTimestamp( $timestamp );
31 $navText = '{{int:allmessagestext}}';
32 $navText .= "
33
34 <table border=1 width=100%><tr><td>
35 '''Name'''
36 </td><td>
37 '''Default text'''
38 </td><td>
39 '''Current text'''
40 </td></tr>";
41
42 print "Initialising \"MediaWiki\" namespace...\n";
43
44
45 $dbr =& wfGetDB( DB_SLAVE );
46 $dbw =& wfGetDB( DB_MASTER );
47 $cur = $dbr->tableName( 'cur' );
48
49 $sql = "SELECT cur_title,cur_is_new,cur_user_text FROM $cur WHERE cur_namespace=$ns AND cur_title IN(";
50
51 # Get keys from $wgAllMessagesEn, which is more complete than the local language
52 $first = true;
53 if ( $messageArray ) {
54 $sortedArray = $messageArray;
55 } else {
56 $sortedArray = $wgAllMessagesEn;
57 }
58
59 ksort( $sortedArray );
60
61 # SELECT all existing messages
62 # Can't afford to be locking all rows for update, this script can take quite a long time to complete
63 foreach ( $sortedArray as $key => $enMsg ) {
64 if ( $key == '' ) {
65 continue; // Skip odd members
66 }
67 if ( $first ) {
68 $first = false;
69 } else {
70 $sql .= ',';
71 }
72 $titleObj = Title::newFromText( $key );
73 $enctitle = $dbr->strencode($titleObj->getDBkey());
74 $sql .= "'$enctitle'";
75 }
76 $sql .= ')';
77 $res = $dbr->query( $sql );
78 $row = $dbr->fetchObject( $res );
79
80 # Read the results into an array
81 # Decide whether or not each one needs to be overwritten
82 $existingTitles = array();
83 while ( $row ) {
84 if ( $row->cur_user_text != $username ) {
85 $existingTitles[$row->cur_title] = 'keep';
86 } else {
87 $existingTitles[$row->cur_title] = 'chuck';
88 }
89
90 $row = $dbr->fetchObject( $res );
91 }
92
93 # Insert queries are done in one multi-row insert
94 # Here's the start of it:
95 $sql = "INSERT INTO $cur (cur_namespace, cur_title, cur_text,
96 cur_user_text, cur_timestamp, cur_restrictions,
97 cur_is_new, inverse_timestamp, cur_touched) VALUES ";
98 $first = true;
99 $talk = $wgLang->getNsText( NS_TALK );
100 $mwtalk = $wgLang->getNsText( NS_MEDIAWIKI_TALK );
101
102 # Process each message
103 foreach ( $sortedArray as $key => $enMsg ) {
104 if ( $key == '' ) {
105 continue; // Skip odd members
106 }
107 # Get message text
108 if ( $messageArray ) {
109 $message = $enMsg;
110 } else {
111 $message = wfMsgNoDB( $key );
112 }
113 $titleObj = Title::newFromText( $key );
114 $title = $titleObj->getDBkey();
115 $dbencMsg = $dbw->strencode( $message );
116
117 # Update messages which already exist
118 if ( array_key_exists( $title, $existingTitles ) ) {
119 if ( $existingTitles[$title] == 'chuck' || $overwrite) {
120 # print "$title\n";
121 $mwTitleObj = Title::makeTitle( NS_MEDIAWIKI, $title );
122 $article = new Article( $mwTitleObj );
123 $article->quickEdit( $message );
124 }
125 $doInsert = false;
126 } else {
127 # Queue for insertion
128 if ( $first ) {
129 $first = false;
130 } else {
131 $sql .= ',';
132 }
133 $sql .=
134 "($ns,
135 '$title',
136 '$dbencMsg',
137 '$username',
138 '$timestamp',
139 'sysop',
140 1,
141 '$invTimestamp',
142 '$timestamp')";
143 }
144
145 # Make table row for navigation page
146 $message = wfEscapeWikiText( $message );
147 $navText .=
148 "<tr><td>
149 [$wgServer$wgScript?title=MediaWiki:$title&action=edit $key]<br>
150 [[$mwtalk:$title|$talk]]
151 </td><td>
152 $message
153 </td><td>
154 {{int:$title}}
155 </td></tr>";
156 }
157
158 # Perform the insert query
159 if ( !$first ) {
160 $dbw->query( $sql, $fname );
161 }
162
163 # Write the navigation page
164
165 $navText .= '</table>';
166 $title = wfMsgNoDB( 'allmessages' );
167 $titleObj = Title::makeTitle( NS_WIKIPEDIA, $title );
168 $wgArticle = new Article( $titleObj );
169 $wgOut->disable();
170 $wgUser = User::newFromName( 'MediaWiki default' );
171 if ( $titleObj->getArticleID() ) {
172 $wgArticle->updateArticle( $navText, '', 0, 0 );
173 } else {
174 $wgArticle->insertNewArticle( $navText, '', 0, 0 );
175 }
176
177 # Clear the relevant memcached key
178 if( $wgUseMemCached ) {
179 print 'Clearing message cache...';
180 $wgMemc->delete( $wgDBname.':messages' );
181 print "Done.\n";
182 }
183 }
184
185 function loadLanguageFile( $filename )
186 {
187 $contents = file_get_contents( $filename );
188 # Remove header line
189 $p = strpos( $contents, "\n" ) + 1;
190 $contents = substr( $contents, $p );
191 # Unserialize
192 return unserialize( $contents );
193 }
194
195 function doUpdates() {
196 global $wgDeferredUpdateList;
197 foreach ( $wgDeferredUpdateList as $up ) { $up->doUpdate(); }
198 }
199
200 ?>