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

moveCustomMessages.inc

Go to the documentation of this file.
00001 <?php 00002 00003 function isTemplateInitialised() { 00004 $sql = "SELECT 1 FROM cur WHERE cur_namespace=" . NS_TEMPLATE . " LIMIT 1"; 00005 $res = wfQuery( $sql, DB_READ ); 00006 return wfNumRows( $res ) ? true : false; 00007 } 00008 00009 function moveCustomMessages( $phase ) { 00010 global $wgUser, $wgAllMessagesEn, $wgDeferredUpdateList, $wgLang; 00011 global $targets, $template, $replaceCount; 00012 00013 $wgUser = new User; 00014 $wgUser->setLoaded( true ); # Don't load from DB 00015 $wgUser->setName( "Template namespace initialisation script" ); 00016 $wgUser->addRight( "bot" ); 00017 00018 wfIgnoreSQLErrors( true ); 00019 00020 # Compose DB key array 00021 $dbkeys = array(); 00022 00023 foreach ( $wgAllMessagesEn as $key => $enValue ) { 00024 $title = Title::newFromText( $key ); 00025 $dbkeys[$title->getDBkey()] = 1; 00026 } 00027 00028 $sql = "SELECT cur_id, cur_title FROM cur WHERE cur_namespace= " . NS_MEDIAWIKI; 00029 $res = wfQuery( $sql, DB_READ ); 00030 00031 # Compile target array 00032 $targets = array(); 00033 while ( $row = wfFetchObject( $res ) ) { 00034 if ( !array_key_exists( $row->cur_title, $dbkeys ) ) { 00035 $targets[$row->cur_title] = 1; 00036 } 00037 } 00038 wfFreeResult( $res ); 00039 00040 # Create redirects from destination to source 00041 if ( $phase == 0 || $phase == 1 ) { 00042 print "Creating redirects\n"; 00043 foreach ( $targets as $partial => $dummy ) { 00044 print "$partial..."; 00045 $nt = Title::makeTitle( NS_TEMPLATE, $partial ); 00046 $ot = Title::makeTitle( NS_MEDIAWIKI, $partial ); 00047 00048 if ( $nt->createRedirect( $ot, "" ) ) { 00049 print "redirected\n"; 00050 } else { 00051 print "not redirected\n"; 00052 } 00053 } 00054 if ( $phase == 0 ) { 00055 print "\nRedirects created. Update live script files now.\nPress ENTER to continue.\n\n"; 00056 readconsole(); 00057 } 00058 } 00059 00060 # Move pages 00061 if ( $phase == 0 || $phase == 2 ) { 00062 print "\nMoving pages...\n"; 00063 foreach ( $targets as $partial => $dummy ) { 00064 wfQuery( "BEGIN", DB_WRITE ); 00065 $ot = Title::makeTitle( NS_MEDIAWIKI, $partial ); 00066 $nt = Title::makeTitle( NS_TEMPLATE, $partial ); 00067 print "$partial..."; 00068 00069 if ( $ot->moveNoAuth( $nt ) === true ) { 00070 print "moved\n"; 00071 } else { 00072 print "not moved\n"; 00073 } 00074 # Do deferred updates 00075 while ( count( $wgDeferredUpdateList ) ) { 00076 $up = array_pop( $wgDeferredUpdateList ); 00077 $up->doUpdate(); 00078 } 00079 wfQuery( "COMMIT", DB_WRITE ); 00080 } 00081 } 00082 00083 # Convert text 00084 if ( $phase == 0 || $phase == 3 ) { 00085 print "\nConverting text...\n"; 00086 00087 $parser = new Parser; 00088 $options = ParserOptions::newFromUser( $wgUser ); 00089 $completedTitles = array(); 00090 $titleChars = Title::legalChars(); 00091 $mediaWiki = $wgLang->getNsText( NS_MEDIAWIKI ); 00092 $template = $wgLang->getNsText( NS_TEMPLATE ); 00093 $linkRegex = "/\[\[$mediaWiki:([$titleChars]*?)\]\]/"; 00094 $msgRegex = "/{{msg:([$titleChars]*?)}}/"; 00095 00096 foreach ( $targets as $partial => $dummy ) { 00097 $dest = Title::makeTitle( NS_MEDIAWIKI, $partial ); 00098 $linksTo = $dest->getLinksTo(); 00099 foreach( $linksTo as $source ) { 00100 wfQuery( "BEGIN", DB_WRITE ); 00101 $pdbk = $source->getPrefixedDBkey(); 00102 if ( !array_key_exists( $pdbk, $completedTitles ) ) { 00103 $completedTitles[$pdbk] = 1; 00104 $id = $source->getArticleID(); 00105 $row = wfGetArray( 'cur', array( 'cur_text' ), 00106 array( 'cur_id' => $source->getArticleID() ) ); 00107 $parser->startExternalParse( $source, $options, OT_WIKI ); 00108 $text = $parser->strip( $row->cur_text, $stripState, false ); 00109 # {{msg}} -> {{}} 00110 $text = preg_replace( $msgRegex, "{{\$1}}", $text ); 00111 # [[MediaWiki:]] -> [[Template:]] 00112 $text = preg_replace_callback( $linkRegex, "wfReplaceMediaWiki", $text ); 00113 $text = $parser->unstrip( $text, $stripState ); 00114 $text = $parser->unstripNoWiki( $text, $stripState ); 00115 if ( $text != $row->cur_text ) { 00116 print "$pdbk\n"; 00117 $art = new Article( $source ); 00118 $art->updateArticle( $text, "", false, false ); 00119 # Do deferred updates 00120 while ( count( $wgDeferredUpdateList ) ) { 00121 $up = array_pop( $wgDeferredUpdateList ); 00122 $up->doUpdate(); 00123 } 00124 } else { 00125 print "($pdbk)\n"; 00126 } 00127 } 00128 wfQuery( "COMMIT", DB_WRITE ); 00129 } 00130 } 00131 } 00132 } 00133 00134 00135 #-------------------------------------------------------------------------------------------------------------- 00136 function wfReplaceMediaWiki( $m ) { 00137 global $targets, $template, $replaceCount; 00138 $title = Title::newFromText( $m[1] ); 00139 $partial = $title->getDBkey(); 00140 00141 if ( array_key_exists( $partial, $targets ) ) { 00142 $text = "[[$template:{$m[1]}]]"; 00143 } else { 00144 $text = $m[0]; 00145 } 00146 return $text; 00147 } 00148 00149 ?>

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