(bug 769) OutputPage::permissionRequired() should suggest groups with the needed...
[mediawiki.git] / maintenance / InitialiseMessages.inc
blobab0f3c597ca0f8e9735b3b16aad43b9d957a788b
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, $outputCallback = false ) {
15         global $wgContLang, $wgContLanguageCode;
16         global $wgContLangClass;
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 = Language::getMessagesFor( 'en' );
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                 $lang = Language::factory( $v );
42                 if($v==$wgContLanguageCode)
43                         $suffix='';
44                 else
45                         $suffix="/$v";
46                 foreach ($sortedArray as $key => $msg) {
47                         $messages[$key.$suffix] = $lang->getMessage($key);
48                 }
49         }
51         require_once('languages/Names.php');
53     /*
54           initialize all messages in $wgForceUIMsgAsContentMsg for all
55           languages in Names.php
56     */
57         if( is_array( $wgForceUIMsgAsContentMsg ) ) {
58                 foreach( $wgForceUIMsgAsContentMsg as $uikey ) {
59                         foreach( $wgLanguageNames as $code => $name) {
60                                 if( $code == $wgContLanguageCode )
61                                         continue;
62                                 $msg = $wgContLang->getMessage( $uikey );
63                                 if( $msg )
64                                         $messages[$uikey. '/' . $code] = $msg;
65                         }
66                 }
67         }
68         initialiseMessagesReal( $overwrite, $messages, $outputCallback );
71 /** */
72 function initialiseMessagesReal( $overwrite = false, $messageArray = false, $outputCallback = false ) {
73         global $wgContLang, $wgScript, $wgServer, $wgLanguageCode;
74         global $wgOut, $wgArticle, $wgUser;
75         global $wgMessageCache, $wgMemc, $wgDBname, $wgUseMemCached;
77         # Initialise $wgOut and $wgUser for a command line script
78         $wgOut->disable();
80         $wgUser = new User;
81         $wgUser->setLoaded( true ); # Don't load from DB
82         $wgUser->setName( 'MediaWiki default' );
84         # Don't try to draw messages from the database we're initialising
85         $wgMessageCache->disable();
86         $wgMessageCache->disableTransform();
88         $fname = 'initialiseMessages';
89         $ns = NS_MEDIAWIKI;
90         # username responsible for the modifications
91         # Don't change it unless you're prepared to update the DBs accordingly, otherwise the
92         # default messages won't be overwritten
93         $username = 'MediaWiki default';
95         if ( !$outputCallback ) {
96                 # Print is not a function, and there doesn't appear to be any built-in 
97                 # workalikes, so let's just make our own anonymous function to do the 
98                 # same thing. 
99                 $outputCallback = create_function( '$s', 'print $s;' );
100         }
102         $outputCallback( "Initialising \"MediaWiki\" namespace for language code $wgLanguageCode...\n" );
104         # Check that the serialized data files are OK
105         if ( Language::isLocalisationOutOfDate( $wgLanguageCode ) ) {
106                 $outputCallback( "Warning: serialized data file may be out of date.\n" );
107         }
109         $dbr =& wfGetDB( DB_SLAVE );
110         $dbw =& wfGetDB( DB_MASTER );
111         $page = $dbr->tableName( 'page' );
112         $revision = $dbr->tableName( 'revision' );
114         $timestamp = wfTimestampNow();
116         $first = true;
117         if ( $messageArray ) {
118                 $sortedArray = $messageArray;
119         } else {
120                 $sortedArray = $wgContLang->getAllMessages();
121         }
123         ksort( $sortedArray );
125         # SELECT all existing messages
126         # Can't afford to be locking all rows for update, this script can take quite a long time to complete
127         $rows = array();
128         $nitems = count($sortedArray);
129         $maxitems = $dbr->maxListLen();
130         $pos = 0;
131         if ($maxitems)
132                 $chunks = array_chunk($sortedArray, $maxitems);
133         else
134                 $chunks = array($sortedArray);
136         foreach ($chunks as $chunk) {
137                 $first = true;
138                 $sql = "SELECT page_title,page_is_new,rev_user_text FROM $page, $revision WHERE
139                         page_namespace=$ns AND rev_id=page_latest AND page_title IN(";
141                 foreach ( $chunk as $key => $enMsg ) {
142                         if ( $key == '' ) {
143                                 continue; // Skip odd members
144                         }
145                         if ( $first ) {
146                                 $first = false;
147                         } else {
148                                 $sql .= ',';
149                         }
150                         $titleObj = Title::newFromText( $wgContLang->ucfirst( $key ) );
151                         $enctitle = $dbr->strencode($titleObj->getDBkey());
152                         $sql .= "'$enctitle'";
153                 }
155                 $sql .= ')';
156                 $res = $dbr->query( $sql );
157                 while ($row = $dbr->fetchObject($res))
158                         $rows[] = $row;
159         }
161         # Read the results into an array
162         # Decide whether or not each one needs to be overwritten
163         $existingTitles = array();
164         foreach ($rows as $row) {
165                 if ( $row->rev_user_text != $username  && $row->rev_user_text != 'Template namespace initialisation script' ) {
166                         $existingTitles[$row->page_title] = 'keep';
167                 } else {
168                         $existingTitles[$row->page_title] = 'chuck';
169                 }
170         }
172         # Insert queries are done in one multi-row insert
173         # Here's the start of it:
174         $arr = array();
175         $talk = $wgContLang->getNsText( NS_TALK );
176         $mwtalk = $wgContLang->getNsText( NS_MEDIAWIKI_TALK );
178         $numUpdated = 0;
179         $numKept = 0;
180         $numInserted = 0;
181         
182         # Merge these into a single transaction for speed
183         $dbw->begin();
185         # Process each message
186         foreach ( $sortedArray as $key => $message ) {
187                 if ( $key == '' ) {
188                         continue; // Skip odd members
189                 }
190                 # Get message text
191                 if ( !$messageArray ) {
192                         $message = wfMsgNoDBForContent( $key );
193                 }
194                 if ( is_null( $message ) ) {
195                         # This happens sometimes with out of date serialized data files
196                         $outputCallback( "Warning: Skipping null message $key\n" );
197                         continue;
198                 }
200                 $titleObj = Title::newFromText( $wgContLang->ucfirst( $key ), NS_MEDIAWIKI );
201                 $title = $titleObj->getDBkey();
203                 # Update messages which already exist
204                 if ( array_key_exists( $title, $existingTitles ) ) {
205                         if ( $existingTitles[$title] == 'chuck' || $overwrite) {
206                                 # Don't bother writing a new revision if we're the same
207                                 # as the current text!
208                                 $revision = Revision::newFromTitle( $titleObj );
209                                 if( is_null( $revision ) || $revision->getText() != $message ) {
210                                         $article = new Article( $titleObj );
211                                         $article->quickEdit( $message );
212                                         ++$numUpdated;
213                                 } else {
214                                         ++$numKept;
215                                 }
216                         } else {
217                                 ++$numKept;
218                         }
219                 } else {
220                         $article = new Article( $titleObj );
221                         $newid = $article->insertOn( $dbw );
222                         # FIXME: set restrictions
223                         $revision = new Revision( array(
224                                 'page'      => $newid,
225                                 'text'      => $message,
226                                 'user'      => 0,
227                                 'user_text' => $username,
228                                 'comment'   => '',
229                                 ) );
230                         $revid = $revision->insertOn( $dbw );
231                         $article->updateRevisionOn( $dbw, $revision );
232                         ++$numInserted;
233                 }
234         }
235         $dbw->commit();
237         # Clear the relevant memcached key
238         $wgMessageCache->clear();
239         $outputCallback( "Done. Updated: $numUpdated, inserted: $numInserted, kept: $numKept.\n" );
242 /** */
243 function loadLanguageFile( $filename ) {
244         $contents = file_get_contents( $filename );
245         # Remove header line
246         $p = strpos( $contents, "\n" ) + 1;
247         $contents = substr( $contents, $p );
248         # Unserialize
249         return unserialize( $contents );
252 /** */
253 function doUpdates() {
254         global $wgDeferredUpdateList;
255         foreach ( $wgDeferredUpdateList as $up ) { $up->doUpdate(); }