Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

InitialiseMessages.inc

Go to the documentation of this file.
00001 <?php 00002 # Script to initialise the MediaWiki namespace 00003 00004 # This script is included from update.php and install.php. Do not run it 00005 # by itself. 00006 00007 function initialiseMessages( $overwrite = false, $messageArray = false ) { 00008 global $wgLang, $wgScript, $wgServer, $wgAllMessagesEn; 00009 global $wgOut, $wgArticle, $wgUser; 00010 global $wgMessageCache, $wgMemc, $wgDBname, $wgUseMemCached; 00011 00012 # Initialise $wgOut and $wgUser for a command line script 00013 $wgOut->disable(); 00014 00015 $wgUser = new User; 00016 $wgUser->setLoaded( true ); # Don't load from DB 00017 $wgUser->setName( 'MediaWiki default' ); 00018 00019 # Don't try to draw messages from the database we're initialising 00020 $wgMessageCache->disable(); 00021 00022 $fname = 'initialiseMessages'; 00023 $ns = NS_MEDIAWIKI; 00024 # cur_user_text responsible for the modifications 00025 # Don't change it unless you're prepared to update the DBs accordingly, otherwise the 00026 # default messages won't be overwritte 00027 $username = 'MediaWiki default'; 00028 00029 $timestamp = wfTimestampNow(); 00030 $invTimestamp = wfInvertTimestamp( $timestamp ); 00031 $navText = '{{int:allmessagestext}}'; 00032 $navText .= " 00033 00034 <table border=1 width=100%><tr><td> 00035 '''Name''' 00036 </td><td> 00037 '''Default text''' 00038 </td><td> 00039 '''Current text''' 00040 </td></tr>"; 00041 00042 print "Initialising \"MediaWiki\" namespace...\n"; 00043 $sql = "SELECT cur_title,cur_is_new,cur_user_text FROM cur WHERE cur_namespace=$ns AND cur_title IN("; 00044 00045 # Get keys from $wgAllMessagesEn, which is more complete than the local language 00046 $first = true; 00047 if ( $messageArray ) { 00048 $sortedArray = $messageArray; 00049 } else { 00050 $sortedArray = $wgAllMessagesEn; 00051 } 00052 00053 ksort( $sortedArray ); 00054 00055 # SELECT all existing messages 00056 foreach ( $sortedArray as $key => $enMsg ) { 00057 if ( $key == '' ) { 00058 continue; // Skip odd members 00059 } 00060 if ( $first ) { 00061 $first = false; 00062 } else { 00063 $sql .= ','; 00064 } 00065 $titleObj = Title::newFromText( $key ); 00066 $enctitle = wfStrencode($titleObj->getDBkey()); 00067 $sql .= "'$enctitle'"; 00068 } 00069 $sql .= ')'; 00070 $res = wfQuery( $sql, DB_READ ); 00071 $row = wfFetchObject( $res ); 00072 00073 # Read the results into an array 00074 # Decide whether or not each one needs to be overwritten 00075 $existingTitles = array(); 00076 while ( $row ) { 00077 if ( $row->cur_user_text != $username ) { 00078 $existingTitles[$row->cur_title] = 'keep'; 00079 } else { 00080 $existingTitles[$row->cur_title] = 'chuck'; 00081 } 00082 00083 $row = wfFetchObject( $res ); 00084 } 00085 00086 # Insert queries are done in one multi-row insert 00087 # Here's the start of it: 00088 $sql = "INSERT INTO cur (cur_namespace, cur_title, cur_text, 00089 cur_user_text, cur_timestamp, cur_restrictions, 00090 cur_is_new, inverse_timestamp, cur_touched) VALUES "; 00091 $first = true; 00092 $talk = $wgLang->getNsText( NS_TALK ); 00093 $mwtalk = $wgLang->getNsText( NS_MEDIAWIKI_TALK ); 00094 00095 # Process each message 00096 foreach ( $sortedArray as $key => $enMsg ) { 00097 if ( $key == '' ) { 00098 continue; // Skip odd members 00099 } 00100 # Get message text 00101 if ( $messageArray ) { 00102 $message = $enMsg; 00103 } else { 00104 $message = wfMsgNoDB( $key ); 00105 } 00106 $titleObj = Title::newFromText( $key ); 00107 $title = $titleObj->getDBkey(); 00108 $dbencMsg = wfStrencode( $message ); 00109 00110 # Update messages which already exist 00111 if ( array_key_exists( $title, $existingTitles ) ) { 00112 if ( $existingTitles[$title] == 'chuck' || $overwrite) { 00113 # print "$title\n"; 00114 $mwTitleObj = Title::makeTitle( NS_MEDIAWIKI, $title ); 00115 $article = new Article( $mwTitleObj ); 00116 $article->quickEdit( $message ); 00117 } 00118 $doInsert = false; 00119 } else { 00120 # Queue for insertion 00121 if ( $first ) { 00122 $first = false; 00123 } else { 00124 $sql .= ','; 00125 } 00126 $sql .= 00127 "($ns, 00128 '$title', 00129 '$dbencMsg', 00130 '$username', 00131 '$timestamp', 00132 'sysop', 00133 1, 00134 '$invTimestamp', 00135 '$timestamp')"; 00136 } 00137 00138 # Make table row for navigation page 00139 $message = wfEscapeWikiText( $message ); 00140 $navText .= 00141 "<tr><td> 00142 [$wgServer$wgScript?title=MediaWiki:$title&action=edit $key]<br> 00143 [[$mwtalk:$title|$talk]] 00144 </td><td> 00145 $message 00146 </td><td> 00147 {{int:$title}} 00148 </td></tr>"; 00149 } 00150 00151 # Perform the insert query 00152 if ( !$first ) { 00153 wfQuery( $sql, DB_WRITE, $fname ); 00154 } 00155 00156 # Write the navigation page 00157 00158 $navText .= '</table>'; 00159 $title = wfMsgNoDB( 'allmessages' ); 00160 $titleObj = Title::makeTitle( NS_WIKIPEDIA, $title ); 00161 $wgArticle = new Article( $titleObj ); 00162 $wgOut->disable(); 00163 $wgUser = User::newFromName( 'MediaWiki default' ); 00164 if ( $titleObj->getArticleID() ) { 00165 $wgArticle->updateArticle( $navText, '', 0, 0 ); 00166 } else { 00167 $wgArticle->insertNewArticle( $navText, '', 0, 0 ); 00168 } 00169 00170 # Clear the relevant memcached key 00171 if( $wgUseMemCached ) { 00172 print 'Clearing message cache...'; 00173 $wgMemc->delete( $wgDBname.':messages' ); 00174 print "Done.\n"; 00175 } 00176 } 00177 00178 function loadLanguageFile( $filename ) 00179 { 00180 $contents = file_get_contents( $filename ); 00181 # Remove header line 00182 $p = strpos( $contents, "\n" ) + 1; 00183 $contents = substr( $contents, $p ); 00184 # Unserialize 00185 return unserialize( $contents ); 00186 } 00187 00188 function doUpdates() { 00189 global $wgDeferredUpdateList; 00190 foreach ( $wgDeferredUpdateList as $up ) { $up->doUpdate(); } 00191 } 00192 00193 ?>

Generated on Tue Jun 29 23:40:03 2004 for Mediawiki by doxygen 1.3.7