Marked authenticateUserData() as public
[mediawiki.git] / maintenance / InitialiseMessages.inc
blobd711aa7aa69cc284d7e6b8922d6c5cf75e14c6f5
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, $wgUseMemCached;
77         # Initialise $wgOut and $wgUser for a command line script
78         $wgOut->disable();
80         $wgUser = User::newFromName( 'MediaWiki default' );
81         if ( $wgUser->isAnon() ) {
82                 $wgUser->addToDatabase();
83         }
85         # Don't try to draw messages from the database we're initialising
86         $wgMessageCache->disable();
87         $wgMessageCache->disableTransform();
89         $fname = 'initialiseMessages';
90         $ns = NS_MEDIAWIKI;
91         # username responsible for the modifications
92         # Don't change it unless you're prepared to update the DBs accordingly, otherwise the
93         # default messages won't be overwritten
94         $username = 'MediaWiki default';
96         if ( !$outputCallback ) {
97                 # Print is not a function, and there doesn't appear to be any built-in 
98                 # workalikes, so let's just make our own anonymous function to do the 
99                 # same thing. 
100                 $outputCallback = create_function( '$s', 'print $s;' );
101         }
103         $outputCallback( "Initialising \"MediaWiki\" namespace for language code $wgLanguageCode...\n" );
105         # Check that the serialized data files are OK
106         if ( Language::isLocalisationOutOfDate( $wgLanguageCode ) ) {
107                 $outputCallback( "Warning: serialized data file may be out of date.\n" );
108         }
110         $dbr =& wfGetDB( DB_SLAVE );
111         $dbw =& wfGetDB( DB_MASTER );
112         $page = $dbr->tableName( 'page' );
113         $revision = $dbr->tableName( 'revision' );
115         $timestamp = wfTimestampNow();
117         $first = true;
118         if ( $messageArray ) {
119                 $sortedArray = $messageArray;
120         } else {
121                 $sortedArray = $wgContLang->getAllMessages();
122         }
124         ksort( $sortedArray );
126         # SELECT all existing messages
127         # Can't afford to be locking all rows for update, this script can take quite a long time to complete
128         $rows = array();
129         $nitems = count($sortedArray);
130         $maxitems = $dbr->maxListLen();
131         $pos = 0;
132         if ($maxitems)
133                 $chunks = array_chunk($sortedArray, $maxitems);
134         else
135                 $chunks = array($sortedArray);
137         foreach ($chunks as $chunk) {
138                 $first = true;
139                 $sql = "SELECT page_title,page_is_new,rev_user_text FROM $page, $revision WHERE
140                         page_namespace=$ns AND rev_id=page_latest AND page_title IN(";
142                 foreach ( $chunk as $key => $enMsg ) {
143                         if ( $key == '' ) {
144                                 continue; // Skip odd members
145                         }
146                         if ( $first ) {
147                                 $first = false;
148                         } else {
149                                 $sql .= ',';
150                         }
151                         $titleObj = Title::newFromText( $wgContLang->ucfirst( $key ) );
152                         $enctitle = $dbr->strencode($titleObj->getDBkey());
153                         $sql .= "'$enctitle'";
154                 }
156                 $sql .= ')';
157                 $res = $dbr->query( $sql );
158                 while ($row = $dbr->fetchObject($res))
159                         $rows[] = $row;
160         }
162         # Read the results into an array
163         # Decide whether or not each one needs to be overwritten
164         $existingTitles = array();
165         foreach ($rows as $row) {
166                 if ( $row->rev_user_text != $username  && $row->rev_user_text != 'Template namespace initialisation script' ) {
167                         $existingTitles[$row->page_title] = 'keep';
168                 } else {
169                         $existingTitles[$row->page_title] = 'chuck';
170                 }
171         }
173         # Insert queries are done in one multi-row insert
174         # Here's the start of it:
175         $arr = array();
176         $talk = $wgContLang->getNsText( NS_TALK );
177         $mwtalk = $wgContLang->getNsText( NS_MEDIAWIKI_TALK );
179         $numUpdated = 0;
180         $numKept = 0;
181         $numInserted = 0;
182         
183         # Merge these into a single transaction for speed
184         $dbw->begin();
186         # Process each message
187         foreach ( $sortedArray as $key => $message ) {
188                 if ( $key == '' ) {
189                         continue; // Skip odd members
190                 }
191                 # Get message text
192                 if ( !$messageArray ) {
193                         $message = wfMsgNoDBForContent( $key );
194                 }
195                 if ( is_null( $message ) ) {
196                         # This happens sometimes with out of date serialized data files
197                         $outputCallback( "Warning: Skipping null message $key\n" );
198                         continue;
199                 }
201                 $titleObj = Title::newFromText( $wgContLang->ucfirst( $key ), NS_MEDIAWIKI );
202                 $title = $titleObj->getDBkey();
204                 # Update messages which already exist
205                 if ( array_key_exists( $title, $existingTitles ) ) {
206                         if ( $existingTitles[$title] == 'chuck' || $overwrite) {
207                                 # Don't bother writing a new revision if we're the same
208                                 # as the current text!
209                                 $revision = Revision::newFromTitle( $titleObj );
210                                 if( is_null( $revision ) || $revision->getText() != $message ) {
211                                         $article = new Article( $titleObj );
212                                         $article->quickEdit( $message );
213                                         ++$numUpdated;
214                                 } else {
215                                         ++$numKept;
216                                 }
217                         } else {
218                                 ++$numKept;
219                         }
220                 } else {
221                         $article = new Article( $titleObj );
222                         $newid = $article->insertOn( $dbw );
223                         # FIXME: set restrictions
224                         $revision = new Revision( array(
225                                 'page'      => $newid,
226                                 'text'      => $message,
227                                 'user'      => 0,
228                                 'user_text' => $username,
229                                 'comment'   => '',
230                                 ) );
231                         $revid = $revision->insertOn( $dbw );
232                         $article->updateRevisionOn( $dbw, $revision );
233                         ++$numInserted;
234                 }
235         }
236         $dbw->commit();
238         # Clear the relevant memcached key
239         $wgMessageCache->clear();
240         $outputCallback( "Done. Updated: $numUpdated, inserted: $numInserted, kept: $numKept.\n" );
243 /** */
244 function loadLanguageFile( $filename ) {
245         $contents = file_get_contents( $filename );
246         # Remove header line
247         $p = strpos( $contents, "\n" ) + 1;
248         $contents = substr( $contents, $p );
249         # Unserialize
250         return unserialize( $contents );
253 /** */
254 function doUpdates() {
255         global $wgDeferredUpdateList;
256         foreach ( $wgDeferredUpdateList as $up ) { $up->doUpdate(); }