DELAYED can be trouble, when edit rate exceeds insert rate
[mediawiki.git] / maintenance / InitialiseMessages.inc
blob699ec66d5e64ce46b8958ea660eaf27fa6dcc657
1 <?php
2 # Script to initialise the MediaWiki namespace
4 # This script is included from update.php and install.php. Do not run it 
5 # by itself.
7 function initialiseMessages( $overwrite = false, $messageArray = false ) {
8         global $wgLang, $wgScript, $wgServer, $wgAllMessagesEn;
9         global $wgOut, $wgArticle, $wgUser;
10         global $wgMessageCache, $wgMemc, $wgDBname, $wgUseMemCached;
12         # Initialise $wgOut and $wgUser for a command line script
13         $wgOut->disable();
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();
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';
29         $timestamp = wfTimestampNow();
30         $invTimestamp = wfInvertTimestamp( $timestamp );
31         $navText = '{{int:allmessagestext}}';
32         $navText .= "
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(";
45         # Get keys from $wgAllMessagesEn, which is more complete than the local language
46         $first = true;
47         if ( $messageArray ) {
48                 $sortedArray = $messageArray;
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 );
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                 }
83                 $row = wfFetchObject( $res );
84         }
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 );
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         }
151         # Perform the insert query
152         if ( !$first ) {
153                 wfQuery( $sql, DB_WRITE, $fname );
154         }
156         # Write the navigation page
158         $navText .= '</table>';
159         $title = wfMsgNoDB( 'allmessages' );
160         $titleObj = Title::makeTitle( NS_WIKIPEDIA, $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         }
178 function loadLanguageFile( $filename )
180         $contents = file_get_contents( $filename );
181         # Remove header line
182         $p = strpos( $contents, "\n" ) + 1;
183         $contents = substr( $contents, $p );
184         # Unserialize
185         return unserialize( $contents );
188 function doUpdates() {
189         global $wgDeferredUpdateList;
190         foreach ( $wgDeferredUpdateList as $up ) { $up->doUpdate(); }