Index: trunk/phase3/maintenance/archives/moveCustomMessages.php |
— | — | @@ -1,23 +1,24 @@ |
2 | 2 | <?php |
3 | 3 | # 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] |
5 | 5 | |
| 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 |
6 | 10 | |
7 | 11 | chdir( ".." ); |
8 | 12 | include_once( "commandLine.inc" ); |
9 | 13 | |
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]); |
19 | 17 | } |
20 | 18 | |
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" ); |
22 | 23 | |
23 | 24 | # Compose DB key array |
24 | 25 | global $wgAllMessagesEn; |
— | — | @@ -35,14 +36,14 @@ |
36 | 37 | $targets = array(); |
37 | 38 | while ( $row = wfFetchObject( $res ) ) { |
38 | 39 | if ( !array_key_exists( $row->cur_title, $dbkeys ) ) { |
39 | | - $targets[] = $row->cur_title; |
| 40 | + $targets[$row->cur_title] = 1; |
40 | 41 | } |
41 | 42 | } |
42 | 43 | wfFreeResult( $res ); |
43 | 44 | |
44 | 45 | # 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 ) { |
47 | 48 | print "$partial..."; |
48 | 49 | $nt = Title::makeTitle( NS_TEMPLATE, $partial ); |
49 | 50 | $ot = Title::makeTitle( NS_MEDIAWIKI, $partial ); |
— | — | @@ -53,15 +54,16 @@ |
54 | 55 | print "not redirected\n"; |
55 | 56 | } |
56 | 57 | } |
57 | | - if ( $doMove ) { |
| 58 | + if ( $phase == 0 ) { |
58 | 59 | print "\nRedirects created. Update live script files now.\nPress ENTER to continue.\n\n"; |
59 | 60 | readconsole(); |
60 | 61 | } |
61 | 62 | } |
62 | 63 | |
63 | 64 | # 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 ) { |
66 | 68 | $ot = Title::makeTitle( NS_MEDIAWIKI, $partial ); |
67 | 69 | $nt = Title::makeTitle( NS_TEMPLATE, $partial ); |
68 | 70 | print "$partial..."; |
— | — | @@ -79,4 +81,59 @@ |
80 | 82 | } |
81 | 83 | } |
82 | 84 | |
| 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 | +} |
83 | 140 | ?> |