Reduce redundant hits to the page table for information on the current page; just...
[mediawiki.git] / maintenance / InitialiseMessages.inc
blob2973e518cc124887cf1971329dd9a20ef1a20d36
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  */
13 /** */
14 function initialiseMessages( $overwrite = false, $messageArray = false ) {
15         global $wgContLang, $wgContLanguageCode;
16         global $wgContLangClass, $wgAllMessagesEn;
17         global $wgDisableLangConversion;
18         global $wgForceUIMsgAsContentMsg;
19         global $wgLanguageNames;
20         global $IP;
22         # overwrite language conversion option so that all variants 
23         # of the messages are initialised
24         $wgDisableLangConversion = false;
26         if ( $messageArray ) {
27                 $sortedArray = $messageArray;
28         } else {
29                 $sortedArray = $wgAllMessagesEn;
30         }
31         
32         ksort( $sortedArray );
33         $messages=array();
35         $variants = $wgContLang->getVariants();
36         if(!in_array($wgContLanguageCode, $variants))
37                 $variants[]=$wgContLanguageCode;
39         foreach ($variants as $v) {
40                 $langclass = 'Language'. str_replace( '-', '_', ucfirst( $v ) );
41                 if( !class_exists($langclass) ) {
42                         die ("class $langclass not defined. perhaps you need to include the file $langclass.php in $wgContLangClass.php?");
43                 }
44                 $lang = new $langclass;
46                 if($v==$wgContLanguageCode)
47                         $suffix='';
48                 else
49                         $suffix="/$v";
50                 foreach ($sortedArray as $key => $msg) {
51                         $messages[$key.$suffix] = $lang->getMessage($key);
52                 }
53         }
55         require_once('languages/Names.php');
57     /*
58           initialize all messages in $wgForceUIMsgAsContentMsg for all
59           languages in Names.php
60     */
61         if( is_array( $wgForceUIMsgAsContentMsg ) ) {
62                 foreach( $wgForceUIMsgAsContentMsg as $uikey ) {
63                         foreach( $wgLanguageNames as $code => $name) {
64                                 if( $code == $wgContLanguageCode )
65                                         continue;
66                                 $msg = $wgContLang->getMessage( $uikey );
67                                 if( $msg )
68                                         $messages[$uikey. '/' . $code] = $msg;
69                         }
70                 }
71         }
72         initialiseMessagesReal( $overwrite, $messages );
75 /** */
76 function initialiseMessagesReal( $overwrite = false, $messageArray = false ) {
77         global $wgContLang, $wgScript, $wgServer, $wgAllMessagesEn;
78         global $wgOut, $wgArticle, $wgUser;
79         global $wgMessageCache, $wgMemc, $wgDBname, $wgUseMemCached;
81         # Initialise $wgOut and $wgUser for a command line script
82         $wgOut->disable();
84         $wgUser = new User;
85         $wgUser->setLoaded( true ); # Don't load from DB
86         $wgUser->setName( 'MediaWiki default' );
87         
88         # Don't try to draw messages from the database we're initialising
89         $wgMessageCache->disable();
90         $wgMessageCache->disableTransform();
92         $fname = 'initialiseMessages';
93         $ns = NS_MEDIAWIKI;
94         # cur_user_text responsible for the modifications
95         # Don't change it unless you're prepared to update the DBs accordingly, otherwise the 
96         # default messages won't be overwritte
97         $username = 'MediaWiki default';
99         
100         print "Initialising \"MediaWiki\" namespace...\n";
102         
103         $dbr =& wfGetDB( DB_SLAVE );
104         $dbw =& wfGetDB( DB_MASTER );
105         $page = $dbr->tableName( 'page' );
106         $revision = $dbr->tableName( 'revision' );
108         $timestamp = wfTimestampNow();
110         #$sql = "SELECT cur_title,cur_is_new,cur_user_text FROM $cur WHERE cur_namespace=$ns AND cur_title IN(";
111         $sql = "SELECT page_title,page_is_new,rev_user_text FROM $page, $revision WHERE
112                 page_namespace=$ns AND rev_page=page_id AND page_title IN(";
114         # Get keys from $wgAllMessagesEn, which is more complete than the local language
115         $first = true;
116         if ( $messageArray ) {
117                 $sortedArray = $messageArray;
118         } else {
119                 $sortedArray = $wgAllMessagesEn;
120         }
121         
122         ksort( $sortedArray );
124         # SELECT all existing messages
125         # Can't afford to be locking all rows for update, this script can take quite a long time to complete
126         foreach ( $sortedArray as $key => $enMsg ) {
127                 if ( $key == '' ) {
128                         continue; // Skip odd members
129                 }
130                 if ( $first ) {
131                         $first = false;
132                 } else {
133                         $sql .= ',';
134                 }
135                 $titleObj = Title::newFromText( $wgContLang->ucfirst( $key ) );
136                 $enctitle = $dbr->strencode($titleObj->getDBkey());
137                 $sql .= "'$enctitle'";
138         }
139         $sql .= ')';
140         $res = $dbr->query( $sql );
141         $row = $dbr->fetchObject( $res );
143         # Read the results into an array
144         # Decide whether or not each one needs to be overwritten
145         $existingTitles = array();
146         while ( $row ) {
147                 if ( $row->rev_user_text != $username ) {
148                         $existingTitles[$row->page_title] = 'keep';
149                 } else {
150                         $existingTitles[$row->page_title] = 'chuck';
151                 }
153                 $row = $dbr->fetchObject( $res );
154         }
156         # Insert queries are done in one multi-row insert
157         # Here's the start of it:
158         $arr = array();
159         $talk = $wgContLang->getNsText( NS_TALK );
160         $mwtalk = $wgContLang->getNsText( NS_MEDIAWIKI_TALK );
161         
162         # Process each message
163         foreach ( $sortedArray as $key => $enMsg ) {
164                 if ( $key == '' ) {
165                         continue; // Skip odd members
166                 }
167                 # Get message text
168                 if ( $messageArray ) {
169                         $message = $enMsg;
170                 } else {
171                         $message = wfMsgNoDBForContent( $key );
172                 }
173                 $titleObj = Title::newFromText( $wgContLang->ucfirst( $key ) );
174                 $title = $titleObj->getDBkey();
176                 # Update messages which already exist
177                 if ( array_key_exists( $title, $existingTitles ) ) {
178                         if ( $existingTitles[$title] == 'chuck' || $overwrite) {
179                                 # print "$title\n";
180                                 $mwTitleObj = Title::makeTitle( NS_MEDIAWIKI, $title );
181                                 $article = new Article( $mwTitleObj );
182                                 $article->quickEdit( $message );
183                         }
184                 } else {
185                         extract( $dbw->tableNames( 'text', 'page', 'revision' ) );
186                         $sql = "INSERT INTO $text (old_text, old_flags) VALUES ('" .
187                                 wfStrencode( $message ) .
188                                 "', '')";
189                         $dbw->query( $sql, $fname );
190                         $text_id = $dbw->insertID();
192                         $sql = "INSERT INTO $page (page_namespace, page_title, page_restrictions, page_counter, page_is_redirect,
193                                 page_is_new, page_random, page_touched, page_latest) VALUES (
194                                 {$ns}, '{$title}', 'sysop', 0, 0, 1, 0.5, '{$timestamp}', {$text_id} )";
195                         $dbw->query( $sql, $fname );
196                         $page_id = $dbw->insertID();
198                         $sql = "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text,
199                                                         rev_timestamp, rev_minor_edit)
200                                         VALUES ({$text_id}, {$page_id}, '', 0, '{$username}', '{$timestamp}', 0)";
201                         $dbw->query( $sql, $fname );
203                 }
204         }
206         # Clear the relevant memcached key
207         print 'Clearing message cache...';
208         $wgMessageCache->clear();
209         print "Done.\n";
212 /** */
213 function loadLanguageFile( $filename ) {
214         $contents = file_get_contents( $filename );
215         # Remove header line
216         $p = strpos( $contents, "\n" ) + 1;
217         $contents = substr( $contents, $p );
218         # Unserialize
219         return unserialize( $contents );
222 /** */
223 function doUpdates() {
224         global $wgDeferredUpdateList;
225         foreach ( $wgDeferredUpdateList as $up ) { $up->doUpdate(); }