2d6f7650815b00a84246d739cc0a300b36be04d3
[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
32 print "Initialising \"MediaWiki\" namespace...\n";
33
34
35 $dbr =& wfGetDB( DB_SLAVE );
36 $dbw =& wfGetDB( DB_MASTER );
37 $cur = $dbr->tableName( 'cur' );
38
39 $sql = "SELECT cur_title,cur_is_new,cur_user_text FROM $cur WHERE cur_namespace=$ns AND cur_title IN(";
40
41 # Get keys from $wgAllMessagesEn, which is more complete than the local language
42 $first = true;
43 if ( $messageArray ) {
44 $sortedArray = $messageArray;
45 } else {
46 $sortedArray = $wgAllMessagesEn;
47 }
48
49 ksort( $sortedArray );
50
51 # SELECT all existing messages
52 # Can't afford to be locking all rows for update, this script can take quite a long time to complete
53 foreach ( $sortedArray as $key => $enMsg ) {
54 if ( $key == '' ) {
55 continue; // Skip odd members
56 }
57 if ( $first ) {
58 $first = false;
59 } else {
60 $sql .= ',';
61 }
62 $titleObj = Title::newFromText( $key );
63 $enctitle = $dbr->strencode($titleObj->getDBkey());
64 $sql .= "'$enctitle'";
65 }
66 $sql .= ')';
67 $res = $dbr->query( $sql );
68 $row = $dbr->fetchObject( $res );
69
70 # Read the results into an array
71 # Decide whether or not each one needs to be overwritten
72 $existingTitles = array();
73 while ( $row ) {
74 if ( $row->cur_user_text != $username ) {
75 $existingTitles[$row->cur_title] = 'keep';
76 } else {
77 $existingTitles[$row->cur_title] = 'chuck';
78 }
79
80 $row = $dbr->fetchObject( $res );
81 }
82
83 # Insert queries are done in one multi-row insert
84 # Here's the start of it:
85 $sql = "INSERT INTO $cur (cur_namespace, cur_title, cur_text,
86 cur_user_text, cur_timestamp, cur_restrictions,
87 cur_is_new, inverse_timestamp, cur_touched) VALUES ";
88 $first = true;
89 $talk = $wgLang->getNsText( NS_TALK );
90 $mwtalk = $wgLang->getNsText( NS_MEDIAWIKI_TALK );
91
92 # Process each message
93 foreach ( $sortedArray as $key => $enMsg ) {
94 if ( $key == '' ) {
95 continue; // Skip odd members
96 }
97 # Get message text
98 if ( $messageArray ) {
99 $message = $enMsg;
100 } else {
101 $message = wfMsgNoDB( $key );
102 }
103 $titleObj = Title::newFromText( $key );
104 $title = $titleObj->getDBkey();
105 $dbencMsg = $dbw->strencode( $message );
106
107 # Update messages which already exist
108 if ( array_key_exists( $title, $existingTitles ) ) {
109 if ( $existingTitles[$title] == 'chuck' || $overwrite) {
110 # print "$title\n";
111 $mwTitleObj = Title::makeTitle( NS_MEDIAWIKI, $title );
112 $article = new Article( $mwTitleObj );
113 $article->quickEdit( $message );
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
136 # Perform the insert query
137 if ( !$first ) {
138 $dbw->query( $sql, $fname );
139 }
140
141 # Clear the relevant memcached key
142 print 'Clearing message cache...';
143 $wgMessageCache->clear();
144 print "Done.\n";
145 }
146
147 function loadLanguageFile( $filename )
148 {
149 $contents = file_get_contents( $filename );
150 # Remove header line
151 $p = strpos( $contents, "\n" ) + 1;
152 $contents = substr( $contents, $p );
153 # Unserialize
154 return unserialize( $contents );
155 }
156
157 function doUpdates() {
158 global $wgDeferredUpdateList;
159 foreach ( $wgDeferredUpdateList as $up ) { $up->doUpdate(); }
160 }
161
162 ?>