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