overwriting of old default messages; ability to dump messages to a file
[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 ( $first ) {
52 $first = false;
53 } else {
54 $sql .= ",";
55 }
56 $titleObj = Title::newFromText( $key );
57 $enctitle = wfStrencode($titleObj->getDBkey());
58 $sql .= "'$enctitle'";
59 }
60 $sql .= ")";
61 $res = wfQuery( $sql, DB_READ );
62 $row = wfFetchObject( $res );
63
64 # Read the results into an array
65 # Decide whether or not each one needs to be overwritten
66 $existingTitles = array();
67 while ( $row ) {
68 if ( !$row->cur_is_new || $row->cur_user_text != $username ) {
69 $existingTitles[$row->cur_title] = "keep";
70 } else {
71 $existingTitles[$row->cur_title] = "chuck";
72 }
73
74 $row = wfFetchObject( $res );
75 }
76
77 # Insert queries are done in one multi-row insert
78 # Here's the start of it:
79 $sql = "INSERT INTO cur (cur_namespace, cur_title, cur_text,
80 cur_user_text, cur_timestamp, cur_restrictions,
81 cur_is_new, inverse_timestamp, cur_touched) VALUES ";
82 $first = true;
83 $mwObj =& MagicWord::get( MAG_MSGNW );
84 $msgnw = $mwObj->getSynonym( 0 );
85 $talk = $wgLang->getNsText( NS_TALK );
86 $mwtalk = $wgLang->getNsText( NS_MEDIAWIKI_TALK );
87
88 # Process each message
89 foreach ( $sortedArray as $key => $enMsg ) {
90 # Get message text
91 if ( $messageArray ) {
92 $message = $enMsg;
93 } else {
94 $message = wfMsgNoDB( $key );
95 }
96 $titleObj = Title::newFromText( $key );
97 $title = $titleObj->getDBkey();
98 $dbencMsg = wfStrencode( $message );
99
100 # Update messages which already exist
101 # Note: UPDATE is now used instead of DELETE/INSERT to avoid wiping cur_restrictions
102 if ( array_key_exists( $title, $existingTitles ) ) {
103 if ( $existingTitles[$title] == "chuck" || $overwrite) {
104 wfQuery( "UPDATE cur
105 SET
106 cur_text='$dbencMsg',
107 cur_user=0,
108 cur_user_text='$username',
109 cur_timestamp='$timestamp',
110 cur_touched='$timestamp',
111 inverse_timestamp='$invTimestamp'
112 WHERE cur_namespace=8 and cur_title='$title'", DB_WRITE
113 );
114 }
115 $doInsert = false;
116 } else {
117 # Queue for insertion
118 if ( $first ) {
119 $first = false;
120 } else {
121 $sql .= ",";
122 }
123 $sql .=
124 "($ns,
125 '$title',
126 '$dbencMsg',
127 '$username',
128 '$timestamp',
129 'sysop',
130 1,
131 '$invTimestamp',
132 '$timestamp')";
133 }
134
135 # Make table row for navigation page
136 $mw = str_replace( "$1", $key, $msgnw );
137
138 $message = wfEscapeWikiText( $message );
139 $navText .=
140 "<tr><td>
141 [$wgServer$wgScript?title=MediaWiki:$title&action=edit $key]<br>
142 [[$mwtalk:$title|$talk]]
143 </td><td>
144 $message
145 </td><td>
146 $mw
147 </td></tr>";
148 }
149
150 # Perform the insert query
151 if ( !$first ) {
152 wfQuery( $sql, DB_WRITE, $fname );
153 }
154
155 # Write the navigation page
156
157 $navText .= "</table>";
158 $title = wfMsgNoDB( "allmessages" );
159 $titleObj = Title::makeTitle( NS_MEDIAWIKI, $title );
160 $wgArticle = new Article( $titleObj );
161 $wgOut->disable();
162 $wgUser = User::newFromName( 'MediaWiki default' );
163 if ( $titleObj->getArticleID() ) {
164 $wgArticle->updateArticle( $navText, '', 0, 0 );
165 } else {
166 $wgArticle->insertNewArticle( $navText, '', 0, 0 );
167 }
168
169 # Clear the relevant memcached key
170 if( $wgUseMemCached ) {
171 print "Clearing message cache...";
172 $wgMemc->delete( "$wgDBname:messages" );
173 print "Done.\n";
174 }
175 }
176
177 function loadArrayFromFile( $filename )
178 {
179 $contents = file_get_contents( $filename );
180 return unserialize( $contents );
181 }
182
183 ?>