12522466c1aaec5d12ebdefe31c1e5cc5c4d6dcf
[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 # Don't try to draw messages from the database we're initialising
13 $wgMessageCache->disable();
14
15 $fname = "initialiseMessages";
16 $ns = NS_MEDIAWIKI;
17 # cur_user_text responsible for the modifications
18 # Don't change it unless you're prepared to update the DBs accordingly, otherwise the
19 # default messages won't be overwritte
20 $username = "MediaWiki default";
21
22 $timestamp = wfTimestampNow();
23 $invTimestamp = wfInvertTimestamp( $timestamp );
24 $mwMsg =& MagicWord::get( MAG_MSG );
25 $navText = str_replace( "$1", "allmessagestext", $mwMsg->getSynonym( 0 ) );
26 $navText .= "
27
28 <table border=1 width=100%><tr><td>
29 '''Name'''
30 </td><td>
31 '''Default text'''
32 </td><td>
33 '''Current text'''
34 </td></tr>";
35
36 print "Initialising \"MediaWiki\" namespace...\n";
37 $sql = "SELECT cur_title,cur_is_new,cur_user_text FROM cur WHERE cur_namespace=$ns AND cur_title IN(";
38
39 # Get keys from $wgAllMessagesEn, which is more complete than the local language
40 $first = true;
41 if ( $messageArray ) {
42 $sortedArray = $wgAllMessagesEn;
43 } else {
44 $sortedArray = $wgAllMessagesEn;
45 }
46
47 ksort( $sortedArray );
48
49 # SELECT all existing messages
50 foreach ( $sortedArray as $key => $enMsg ) {
51 if ( $key == "" ) {
52 continue; // Skip odd members
53 }
54 if ( $first ) {
55 $first = false;
56 } else {
57 $sql .= ",";
58 }
59 $titleObj = Title::newFromText( $key );
60 $enctitle = wfStrencode($titleObj->getDBkey());
61 $sql .= "'$enctitle'";
62 }
63 $sql .= ")";
64 $res = wfQuery( $sql, DB_READ );
65 $row = wfFetchObject( $res );
66
67 # Read the results into an array
68 # Decide whether or not each one needs to be overwritten
69 $existingTitles = array();
70 while ( $row ) {
71 if ( !$row->cur_is_new || $row->cur_user_text != $username ) {
72 $existingTitles[$row->cur_title] = "keep";
73 } else {
74 $existingTitles[$row->cur_title] = "chuck";
75 }
76
77 $row = wfFetchObject( $res );
78 }
79
80 # Insert queries are done in one multi-row insert
81 # Here's the start of it:
82 $sql = "INSERT INTO cur (cur_namespace, cur_title, cur_text,
83 cur_user_text, cur_timestamp, cur_restrictions,
84 cur_is_new, inverse_timestamp, cur_touched) VALUES ";
85 $first = true;
86 $mwObj =& MagicWord::get( MAG_MSGNW );
87 $msgnw = $mwObj->getSynonym( 0 );
88 $talk = $wgLang->getNsText( NS_TALK );
89 $mwtalk = $wgLang->getNsText( NS_MEDIAWIKI_TALK );
90
91 # Process each message
92 foreach ( $sortedArray as $key => $enMsg ) {
93 if ( $key == "" ) {
94 continue; // Skip odd members
95 }
96 # Get message text
97 if ( $messageArray ) {
98 $message = $enMsg;
99 } else {
100 $message = wfMsgNoDB( $key );
101 }
102 $titleObj = Title::newFromText( $key );
103 $title = $titleObj->getDBkey();
104 $dbencMsg = wfStrencode( $message );
105
106 # Update messages which already exist
107 # Note: UPDATE is now used instead of DELETE/INSERT to avoid wiping cur_restrictions
108 if ( array_key_exists( $title, $existingTitles ) ) {
109 if ( $existingTitles[$title] == "chuck" || $overwrite) {
110 wfQuery( "UPDATE cur
111 SET
112 cur_text='$dbencMsg',
113 cur_user=0,
114 cur_user_text='$username',
115 cur_timestamp='$timestamp',
116 cur_touched='$timestamp',
117 inverse_timestamp='$invTimestamp'
118 WHERE cur_namespace=8 and cur_title='$title'", DB_WRITE
119 );
120 }
121 $doInsert = false;
122 } else {
123 # Queue for insertion
124 if ( $first ) {
125 $first = false;
126 } else {
127 $sql .= ",";
128 }
129 $sql .=
130 "($ns,
131 '$title',
132 '$dbencMsg',
133 '$username',
134 '$timestamp',
135 'sysop',
136 1,
137 '$invTimestamp',
138 '$timestamp')";
139 }
140
141 # Make table row for navigation page
142 $mw = str_replace( "$1", $key, $msgnw );
143
144 $message = wfEscapeWikiText( $message );
145 $navText .=
146 "<tr><td>
147 [$wgServer$wgScript?title=MediaWiki:$title&action=edit $key]<br>
148 [[$mwtalk:$title|$talk]]
149 </td><td>
150 $message
151 </td><td>
152 $mw
153 </td></tr>";
154 }
155
156 # Perform the insert query
157 if ( !$first ) {
158 wfQuery( $sql, DB_WRITE, $fname );
159 }
160
161 # Write the navigation page
162
163 $navText .= "</table>";
164 $title = wfMsgNoDB( "allmessages" );
165 $titleObj = Title::makeTitle( NS_MEDIAWIKI, $title );
166 $wgArticle = new Article( $titleObj );
167 $wgOut->disable();
168 $wgUser = User::newFromName( 'MediaWiki default' );
169 if ( $titleObj->getArticleID() ) {
170 $wgArticle->updateArticle( $navText, '', 0, 0 );
171 } else {
172 $wgArticle->insertNewArticle( $navText, '', 0, 0 );
173 }
174
175 # Clear the relevant memcached key
176 if( $wgUseMemCached ) {
177 print "Clearing message cache...";
178 $wgMemc->delete( "$wgDBname:messages" );
179 print "Done.\n";
180 }
181 }
182
183 function loadArrayFromFile( $filename )
184 {
185 $contents = file_get_contents( $filename );
186 return unserialize( $contents );
187 }
188
189 ?>