* Disable $wgAllowExternalImages by default.
[mediawiki.git] / maintenance / InitialiseMessages.inc
blobdc89a7916ed090f71c56a30f0a4e616eb1a57b2a
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         }
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                         wfDie( "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' );
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';
100         print "Initialising \"MediaWiki\" namespace...\n";
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         # Get keys from $wgAllMessagesEn, which is more complete than the local language
112         $first = true;
113         if ( $messageArray ) {
114                 $sortedArray = $messageArray;
115         } else {
116                 $sortedArray = $wgAllMessagesEn;
117         }
119         ksort( $sortedArray );
121         # SELECT all existing messages
122         # Can't afford to be locking all rows for update, this script can take quite a long time to complete
123         $rows = array();
124         $nitems = count($sortedArray);
125         $maxitems = $dbr->maxListLen();
126         $pos = 0;
127         if ($maxitems)
128                 $chunks = array_chunk($sortedArray, $maxitems);
129         else
130                 $chunks = array($sortedArray);
132         foreach ($chunks as $chunk) {
133                 $first = true;
134                 $sql = "SELECT page_title,page_is_new,rev_user_text FROM $page, $revision WHERE
135                         page_namespace=$ns AND rev_page=page_id AND page_title IN(";
137                 foreach ( $chunk as $key => $enMsg ) {
138                         if ( $key == '' ) {
139                                 continue; // Skip odd members
140                         }
141                         if ( $first ) {
142                                 $first = false;
143                         } else {
144                                 $sql .= ',';
145                         }
146                         $titleObj = Title::newFromText( $wgContLang->ucfirst( $key ) );
147                         $enctitle = $dbr->strencode($titleObj->getDBkey());
148                         $sql .= "'$enctitle'";
149                 }
151                 $sql .= ')';
152                 $res = $dbr->query( $sql );
153                 while ($row = $dbr->fetchObject($res))
154                         $rows[] = $row;
155         }
157         # Read the results into an array
158         # Decide whether or not each one needs to be overwritten
159         $existingTitles = array();
160         foreach ($rows as $row) {
161                 if ( $row->rev_user_text != $username  && $row->rev_user_text != 'Template namespace initialisation script' ) {
162                         $existingTitles[$row->page_title] = 'keep';
163                 } else {
164                         $existingTitles[$row->page_title] = 'chuck';
165                 }
166         }
168         # Insert queries are done in one multi-row insert
169         # Here's the start of it:
170         $arr = array();
171         $talk = $wgContLang->getNsText( NS_TALK );
172         $mwtalk = $wgContLang->getNsText( NS_MEDIAWIKI_TALK );
174         # Merge these into a single transaction for speed
175         $dbw->begin();
177         # Process each message
178         foreach ( $sortedArray as $key => $enMsg ) {
179                 if ( $key == '' ) {
180                         continue; // Skip odd members
181                 }
182                 # Get message text
183                 if ( $messageArray ) {
184                         $message = $enMsg;
185                 } else {
186                         $message = wfMsgNoDBForContent( $key );
187                 }
188                 $titleObj = Title::newFromText( $wgContLang->ucfirst( $key ), NS_MEDIAWIKI );
189                 $title = $titleObj->getDBkey();
191                 # Update messages which already exist
192                 if ( array_key_exists( $title, $existingTitles ) ) {
193                         if ( $existingTitles[$title] == 'chuck' || $overwrite) {
194                                 # Don't bother writing a new revision if we're the same
195                                 # as the current text!
196                                 $revision = Revision::newFromTitle( $titleObj );
197                                 if( is_null( $revision ) || $revision->getText() != $message ) {
198                                         $article = new Article( $titleObj );
199                                         $article->quickEdit( $message );
200                                 }
201                         }
202                 } else {
203                         $article = new Article( $titleObj );
204                         $newid = $article->insertOn( $dbw, 'sysop' );
205                         # FIXME: set restrictions
206                         $revision = new Revision( array(
207                                 'page'      => $newid,
208                                 'text'      => $message,
209                                 'user'      => 0,
210                                 'user_text' => $username,
211                                 'comment'   => '',
212                                 ) );
213                         $revid = $revision->insertOn( $dbw );
214                         $article->updateRevisionOn( $dbw, $revision );
215                 }
216         }
217         $dbw->commit();
219         # Clear the relevant memcached key
220         print 'Clearing message cache...';
221         $wgMessageCache->clear();
222         print "Done.\n";
225 /** */
226 function loadLanguageFile( $filename ) {
227         $contents = file_get_contents( $filename );
228         # Remove header line
229         $p = strpos( $contents, "\n" ) + 1;
230         $contents = substr( $contents, $p );
231         # Unserialize
232         return unserialize( $contents );
235 /** */
236 function doUpdates() {
237         global $wgDeferredUpdateList;
238         foreach ( $wgDeferredUpdateList as $up ) { $up->doUpdate(); }