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