r2859 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r2858‎ | r2859 | r2860 >
Date:05:48, 27 March 2004
Author:timstarling
Status:old
Tags:
Comment:
Added text conversion phase, misc. improvements
Modified paths:
  • /trunk/phase3/maintenance/archives/moveCustomMessages.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/archives/moveCustomMessages.php
@@ -1,23 +1,24 @@
22 <?php
33 # Move "custom messages" from the MediaWiki namespace to the Template namespace
4 -# Usage: php moveCustomMessages.php [<lang>] [skipredir]
 4+# Usage: php moveCustomMessages.php [<lang>] [phase]
55
 6+# Script works in three phases:
 7+# 1. Create redirects from Template to MediaWiki namespace. Skip if you don't want them
 8+# 2. Move pages from MediaWiki to Template namespace.
 9+# 3. Convert the text to suit the new syntax
610
711 chdir( ".." );
812 include_once( "commandLine.inc" );
913
10 -if ( @$argv[2] == "1" ) {
11 - $doRedirects = true;
12 - $doMove = false;
13 -} elseif ( @$argv[2] == "2" ) {
14 - $doRedirects = false;
15 - $doMove = true;
16 -} else {
17 - $doRedirects = true;
18 - $doMove = true;
 14+$phase = 0;
 15+if ( is_numeric( @$argv[2] ) && $argv[2] > 0) {
 16+ $phase = intval($argv[2]);
1917 }
2018
21 -$wgUser = User::newFromName( "Template namespace initialisation script" );
 19+$wgUser = new User;
 20+$wgUser->setLoaded( true ); # Don't load from DB
 21+$wgUser->setName( "Template namespace initialisation script" );
 22+$wgUser->addRight( "bot" );
2223
2324 # Compose DB key array
2425 global $wgAllMessagesEn;
@@ -35,14 +36,14 @@
3637 $targets = array();
3738 while ( $row = wfFetchObject( $res ) ) {
3839 if ( !array_key_exists( $row->cur_title, $dbkeys ) ) {
39 - $targets[] = $row->cur_title;
 40+ $targets[$row->cur_title] = 1;
4041 }
4142 }
4243 wfFreeResult( $res );
4344
4445 # Create redirects from destination to source
45 -if ( $doRedirects ) {
46 - foreach ( $targets as $partial ) {
 46+if ( $phase == 0 || $phase == 1 ) {
 47+ foreach ( $targets as $partial => $dummy ) {
4748 print "$partial...";
4849 $nt = Title::makeTitle( NS_TEMPLATE, $partial );
4950 $ot = Title::makeTitle( NS_MEDIAWIKI, $partial );
@@ -53,15 +54,16 @@
5455 print "not redirected\n";
5556 }
5657 }
57 - if ( $doMove ) {
 58+ if ( $phase == 0 ) {
5859 print "\nRedirects created. Update live script files now.\nPress ENTER to continue.\n\n";
5960 readconsole();
6061 }
6162 }
6263
6364 # Move pages
64 -if ( $doMove ) {
65 - foreach ( $targets as $partial ) {
 65+if ( $phase == 0 || $phase == 2 ) {
 66+ print "\n";
 67+ foreach ( $targets as $partial => $dummy ) {
6668 $ot = Title::makeTitle( NS_MEDIAWIKI, $partial );
6769 $nt = Title::makeTitle( NS_TEMPLATE, $partial );
6870 print "$partial...";
@@ -79,4 +81,59 @@
8082 }
8183 }
8284
 85+# Convert text
 86+if ( $phase == 0 || $phase == 3 ) {
 87+ print "\n";
 88+
 89+ $parser = new Parser;
 90+ $options = ParserOptions::newFromUser( $wgUser );
 91+ $completedTitles = array();
 92+ $titleChars = Title::legalChars();
 93+ $mediaWiki = $wgLang->getNsText( NS_MEDIAWIKI );
 94+ $template = $wgLang->getNsText( NS_TEMPLATE );
 95+ $linkRegex = "/\[\[$mediaWiki:([$titleChars]*?)\]\]/";
 96+ $msgRegex = "/{{msg:([$titleChars]*?)}}/";
 97+
 98+ foreach ( $targets as $partial => $dummy ) {
 99+ $dest = Title::makeTitle( NS_TEMPLATE, $partial );
 100+ $linksTo = $dest->getLinksTo();
 101+ foreach( $linksTo as $source ) {
 102+ $pdbk = $source->getPrefixedDBkey();
 103+ print "$pdbk...";
 104+ if ( !array_key_exists( $pdbk, $completedTitles ) ) {
 105+ $completedTitles[$pdbk] = 1;
 106+ $id = $source->getArticleID();
 107+ $row = wfGetArray( 'cur', array( 'cur_text' ),
 108+ array( 'cur_id' => $source->getArticleID() ) );
 109+ $parser->startExternalParse( $source, $options, OT_WIKI );
 110+ $text = $parser->strip( $row->cur_text, $stripState, false );
 111+ # {{msg}} -> {{}}
 112+ $text = preg_replace( $msgRegex, "{{\$1}}", $text );
 113+ # [[MediaWiki:]] -> [[Template:]]
 114+ $text = preg_replace_callback( $linkRegex, "wfReplaceMediaWiki", $text );
 115+ $text = $parser->unstrip( $text, $stripState );
 116+ if ( $text != $row->cur_text ) {
 117+ wfUpdateArray( 'cur', array( 'cur_text' => $text ), array( 'cur_id' => $id ) );
 118+ print "modified\n";
 119+ } else {
 120+ print "not modified\n";
 121+ }
 122+ }
 123+ }
 124+ }
 125+}
 126+
 127+#--------------------------------------------------------------------------------------------------------------
 128+function wfReplaceMediaWiki( $m ) {
 129+ global $targets, $template, $replaceCount;
 130+ $title = Title::newFromText( $m[1] );
 131+ $partial = $title->getDBkey();
 132+
 133+ if ( array_key_exists( $partial, $targets ) ) {
 134+ $text = "[[$template:{$m[1]}]]";
 135+ } else {
 136+ $text = $m[0];
 137+ }
 138+ return $text;
 139+}
83140 ?>

Status & tagging log