fix timestamps, change multirow insert query into Database:insertArray()
[lhc/web/wiklou.git] / maintenance / InitialiseMessages.inc
1 <?php
2 /**
3 * Script to initialise the MediaWiki namespace
4 *
5 * This script is included from update.php and install.php. Do not run it
6 * by itself.
7 *
8 * @deprecated
9 * @package MediaWiki
10 * @subpackage Maintenance
11 */
12
13 /** */
14 function initialiseMessages( $overwrite = false, $messageArray = false ) {
15 global $wgLang, $wgScript, $wgServer, $wgAllMessagesEn;
16 global $wgOut, $wgArticle, $wgUser;
17 global $wgMessageCache, $wgMemc, $wgDBname, $wgUseMemCached;
18
19 # Initialise $wgOut and $wgUser for a command line script
20 $wgOut->disable();
21
22 $wgUser = new User;
23 $wgUser->setLoaded( true ); # Don't load from DB
24 $wgUser->setName( 'MediaWiki default' );
25
26 # Don't try to draw messages from the database we're initialising
27 $wgMessageCache->disable();
28
29 $fname = 'initialiseMessages';
30 $ns = NS_MEDIAWIKI;
31 # cur_user_text responsible for the modifications
32 # Don't change it unless you're prepared to update the DBs accordingly, otherwise the
33 # default messages won't be overwritte
34 $username = 'MediaWiki default';
35
36
37 print "Initialising \"MediaWiki\" namespace...\n";
38
39
40 $dbr =& wfGetDB( DB_SLAVE );
41 $dbw =& wfGetDB( DB_MASTER );
42 $cur = $dbr->tableName( 'cur' );
43
44 $timestamp = wfTimestampNow();
45 $invTimestamp = wfInvertTimestamp( $timestamp );
46
47 $sql = "SELECT cur_title,cur_is_new,cur_user_text FROM $cur WHERE cur_namespace=$ns AND cur_title IN(";
48
49 # Get keys from $wgAllMessagesEn, which is more complete than the local language
50 $first = true;
51 if ( $messageArray ) {
52 $sortedArray = $messageArray;
53 } else {
54 $sortedArray = $wgAllMessagesEn;
55 }
56
57 ksort( $sortedArray );
58
59 # SELECT all existing messages
60 # Can't afford to be locking all rows for update, this script can take quite a long time to complete
61 foreach ( $sortedArray as $key => $enMsg ) {
62 if ( $key == '' ) {
63 continue; // Skip odd members
64 }
65 if ( $first ) {
66 $first = false;
67 } else {
68 $sql .= ',';
69 }
70 $titleObj = Title::newFromText( $key );
71 $enctitle = $dbr->strencode($titleObj->getDBkey());
72 $sql .= "'$enctitle'";
73 }
74 $sql .= ')';
75 $res = $dbr->query( $sql );
76 $row = $dbr->fetchObject( $res );
77
78 # Read the results into an array
79 # Decide whether or not each one needs to be overwritten
80 $existingTitles = array();
81 while ( $row ) {
82 if ( $row->cur_user_text != $username ) {
83 $existingTitles[$row->cur_title] = 'keep';
84 } else {
85 $existingTitles[$row->cur_title] = 'chuck';
86 }
87
88 $row = $dbr->fetchObject( $res );
89 }
90
91 # Insert queries are done in one multi-row insert
92 # Here's the start of it:
93 $arr = array();
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 = $dbw->strencode( $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 $mwTitleObj = Title::makeTitle( NS_MEDIAWIKI, $title );
117 $article = new Article( $mwTitleObj );
118 $article->quickEdit( $message );
119 }
120 $doInsert = false;
121 } else {
122 array_push($arr,array(
123 cur_namespace=>$ns,
124 cur_title=>$title,
125 cur_text=>$dbencMsg,
126 cur_user_text=>$username,
127 cur_timestamp=>$dbw->timestamp($timestamp),
128 cur_restrictions=>'sysop',
129 cur_is_new=>1,
130 inverse_timestamp=>$invTimestamp,
131 cur_touched=>$dbw->timestamp($timestamp)));
132 }
133 }
134
135 $dbw->insertArray( $cur, $arr, $fname );
136
137 # Clear the relevant memcached key
138 print 'Clearing message cache...';
139 $wgMessageCache->clear();
140 print "Done.\n";
141 }
142
143 function loadLanguageFile( $filename )
144 {
145 $contents = file_get_contents( $filename );
146 # Remove header line
147 $p = strpos( $contents, "\n" ) + 1;
148 $contents = substr( $contents, $p );
149 # Unserialize
150 return unserialize( $contents );
151 }
152
153 function doUpdates() {
154 global $wgDeferredUpdateList;
155 foreach ( $wgDeferredUpdateList as $up ) { $up->doUpdate(); }
156 }
157
158 ?>