From acb127649c9c3f769b67360fa9906874a8b6169c Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Sat, 27 Mar 2004 05:48:24 +0000 Subject: [PATCH] Added text conversion phase, misc. improvements --- maintenance/archives/moveCustomMessages.php | 91 +++++++++++++++++---- 1 file changed, 74 insertions(+), 17 deletions(-) diff --git a/maintenance/archives/moveCustomMessages.php b/maintenance/archives/moveCustomMessages.php index e87ae2cfb3..244bd1c6a1 100644 --- a/maintenance/archives/moveCustomMessages.php +++ b/maintenance/archives/moveCustomMessages.php @@ -1,23 +1,24 @@ ] [skipredir] +# Usage: php moveCustomMessages.php [] [phase] +# Script works in three phases: +# 1. Create redirects from Template to MediaWiki namespace. Skip if you don't want them +# 2. Move pages from MediaWiki to Template namespace. +# 3. Convert the text to suit the new syntax chdir( ".." ); include_once( "commandLine.inc" ); -if ( @$argv[2] == "1" ) { - $doRedirects = true; - $doMove = false; -} elseif ( @$argv[2] == "2" ) { - $doRedirects = false; - $doMove = true; -} else { - $doRedirects = true; - $doMove = true; +$phase = 0; +if ( is_numeric( @$argv[2] ) && $argv[2] > 0) { + $phase = intval($argv[2]); } -$wgUser = User::newFromName( "Template namespace initialisation script" ); +$wgUser = new User; +$wgUser->setLoaded( true ); # Don't load from DB +$wgUser->setName( "Template namespace initialisation script" ); +$wgUser->addRight( "bot" ); # Compose DB key array global $wgAllMessagesEn; @@ -35,14 +36,14 @@ $res = wfQuery( $sql, DB_READ ); $targets = array(); while ( $row = wfFetchObject( $res ) ) { if ( !array_key_exists( $row->cur_title, $dbkeys ) ) { - $targets[] = $row->cur_title; + $targets[$row->cur_title] = 1; } } wfFreeResult( $res ); # Create redirects from destination to source -if ( $doRedirects ) { - foreach ( $targets as $partial ) { +if ( $phase == 0 || $phase == 1 ) { + foreach ( $targets as $partial => $dummy ) { print "$partial..."; $nt = Title::makeTitle( NS_TEMPLATE, $partial ); $ot = Title::makeTitle( NS_MEDIAWIKI, $partial ); @@ -53,15 +54,16 @@ if ( $doRedirects ) { print "not redirected\n"; } } - if ( $doMove ) { + if ( $phase == 0 ) { print "\nRedirects created. Update live script files now.\nPress ENTER to continue.\n\n"; readconsole(); } } # Move pages -if ( $doMove ) { - foreach ( $targets as $partial ) { +if ( $phase == 0 || $phase == 2 ) { + print "\n"; + foreach ( $targets as $partial => $dummy ) { $ot = Title::makeTitle( NS_MEDIAWIKI, $partial ); $nt = Title::makeTitle( NS_TEMPLATE, $partial ); print "$partial..."; @@ -79,4 +81,59 @@ if ( $doMove ) { } } +# Convert text +if ( $phase == 0 || $phase == 3 ) { + print "\n"; + + $parser = new Parser; + $options = ParserOptions::newFromUser( $wgUser ); + $completedTitles = array(); + $titleChars = Title::legalChars(); + $mediaWiki = $wgLang->getNsText( NS_MEDIAWIKI ); + $template = $wgLang->getNsText( NS_TEMPLATE ); + $linkRegex = "/\[\[$mediaWiki:([$titleChars]*?)\]\]/"; + $msgRegex = "/{{msg:([$titleChars]*?)}}/"; + + foreach ( $targets as $partial => $dummy ) { + $dest = Title::makeTitle( NS_TEMPLATE, $partial ); + $linksTo = $dest->getLinksTo(); + foreach( $linksTo as $source ) { + $pdbk = $source->getPrefixedDBkey(); + print "$pdbk..."; + if ( !array_key_exists( $pdbk, $completedTitles ) ) { + $completedTitles[$pdbk] = 1; + $id = $source->getArticleID(); + $row = wfGetArray( 'cur', array( 'cur_text' ), + array( 'cur_id' => $source->getArticleID() ) ); + $parser->startExternalParse( $source, $options, OT_WIKI ); + $text = $parser->strip( $row->cur_text, $stripState, false ); + # {{msg}} -> {{}} + $text = preg_replace( $msgRegex, "{{\$1}}", $text ); + # [[MediaWiki:]] -> [[Template:]] + $text = preg_replace_callback( $linkRegex, "wfReplaceMediaWiki", $text ); + $text = $parser->unstrip( $text, $stripState ); + if ( $text != $row->cur_text ) { + wfUpdateArray( 'cur', array( 'cur_text' => $text ), array( 'cur_id' => $id ) ); + print "modified\n"; + } else { + print "not modified\n"; + } + } + } + } +} + +#-------------------------------------------------------------------------------------------------------------- +function wfReplaceMediaWiki( $m ) { + global $targets, $template, $replaceCount; + $title = Title::newFromText( $m[1] ); + $partial = $title->getDBkey(); + + if ( array_key_exists( $partial, $targets ) ) { + $text = "[[$template:{$m[1]}]]"; + } else { + $text = $m[0]; + } + return $text; +} ?> -- 2.20.1