244bd1c6a12c443b7b9890e4284e11fb4bb5f1e8
[lhc/web/wiklou.git] / maintenance / archives / moveCustomMessages.php
1 <?php
2 # Move "custom messages" from the MediaWiki namespace to the Template namespace
3 # Usage: php moveCustomMessages.php [<lang>] [phase]
4
5 # Script works in three phases:
6 # 1. Create redirects from Template to MediaWiki namespace. Skip if you don't want them
7 # 2. Move pages from MediaWiki to Template namespace.
8 # 3. Convert the text to suit the new syntax
9
10 chdir( ".." );
11 include_once( "commandLine.inc" );
12
13 $phase = 0;
14 if ( is_numeric( @$argv[2] ) && $argv[2] > 0) {
15 $phase = intval($argv[2]);
16 }
17
18 $wgUser = new User;
19 $wgUser->setLoaded( true ); # Don't load from DB
20 $wgUser->setName( "Template namespace initialisation script" );
21 $wgUser->addRight( "bot" );
22
23 # Compose DB key array
24 global $wgAllMessagesEn;
25 $dbkeys = array();
26
27 foreach ( $wgAllMessagesEn as $key => $enValue ) {
28 $title = Title::newFromText( $key );
29 $dbkeys[$title->getDBkey()] = 1;
30 }
31
32 $sql = "SELECT cur_id, cur_title FROM cur WHERE cur_namespace= " . NS_MEDIAWIKI;
33 $res = wfQuery( $sql, DB_READ );
34
35 # Compile target array
36 $targets = array();
37 while ( $row = wfFetchObject( $res ) ) {
38 if ( !array_key_exists( $row->cur_title, $dbkeys ) ) {
39 $targets[$row->cur_title] = 1;
40 }
41 }
42 wfFreeResult( $res );
43
44 # Create redirects from destination to source
45 if ( $phase == 0 || $phase == 1 ) {
46 foreach ( $targets as $partial => $dummy ) {
47 print "$partial...";
48 $nt = Title::makeTitle( NS_TEMPLATE, $partial );
49 $ot = Title::makeTitle( NS_MEDIAWIKI, $partial );
50
51 if ( $nt->createRedirect( $ot, "" ) ) {
52 print "redirected\n";
53 } else {
54 print "not redirected\n";
55 }
56 }
57 if ( $phase == 0 ) {
58 print "\nRedirects created. Update live script files now.\nPress ENTER to continue.\n\n";
59 readconsole();
60 }
61 }
62
63 # Move pages
64 if ( $phase == 0 || $phase == 2 ) {
65 print "\n";
66 foreach ( $targets as $partial => $dummy ) {
67 $ot = Title::makeTitle( NS_MEDIAWIKI, $partial );
68 $nt = Title::makeTitle( NS_TEMPLATE, $partial );
69 print "$partial...";
70
71 if ( $ot->moveNoAuth( $nt ) === true ) {
72 print "moved\n";
73 } else {
74 print "not moved\n";
75 }
76 # Do deferred updates
77 while ( count( $wgDeferredUpdateList ) ) {
78 $up = array_pop( $wgDeferredUpdateList );
79 $up->doUpdate();
80 }
81 }
82 }
83
84 # Convert text
85 if ( $phase == 0 || $phase == 3 ) {
86 print "\n";
87
88 $parser = new Parser;
89 $options = ParserOptions::newFromUser( $wgUser );
90 $completedTitles = array();
91 $titleChars = Title::legalChars();
92 $mediaWiki = $wgLang->getNsText( NS_MEDIAWIKI );
93 $template = $wgLang->getNsText( NS_TEMPLATE );
94 $linkRegex = "/\[\[$mediaWiki:([$titleChars]*?)\]\]/";
95 $msgRegex = "/{{msg:([$titleChars]*?)}}/";
96
97 foreach ( $targets as $partial => $dummy ) {
98 $dest = Title::makeTitle( NS_TEMPLATE, $partial );
99 $linksTo = $dest->getLinksTo();
100 foreach( $linksTo as $source ) {
101 $pdbk = $source->getPrefixedDBkey();
102 print "$pdbk...";
103 if ( !array_key_exists( $pdbk, $completedTitles ) ) {
104 $completedTitles[$pdbk] = 1;
105 $id = $source->getArticleID();
106 $row = wfGetArray( 'cur', array( 'cur_text' ),
107 array( 'cur_id' => $source->getArticleID() ) );
108 $parser->startExternalParse( $source, $options, OT_WIKI );
109 $text = $parser->strip( $row->cur_text, $stripState, false );
110 # {{msg}} -> {{}}
111 $text = preg_replace( $msgRegex, "{{\$1}}", $text );
112 # [[MediaWiki:]] -> [[Template:]]
113 $text = preg_replace_callback( $linkRegex, "wfReplaceMediaWiki", $text );
114 $text = $parser->unstrip( $text, $stripState );
115 if ( $text != $row->cur_text ) {
116 wfUpdateArray( 'cur', array( 'cur_text' => $text ), array( 'cur_id' => $id ) );
117 print "modified\n";
118 } else {
119 print "not modified\n";
120 }
121 }
122 }
123 }
124 }
125
126 #--------------------------------------------------------------------------------------------------------------
127 function wfReplaceMediaWiki( $m ) {
128 global $targets, $template, $replaceCount;
129 $title = Title::newFromText( $m[1] );
130 $partial = $title->getDBkey();
131
132 if ( array_key_exists( $partial, $targets ) ) {
133 $text = "[[$template:{$m[1]}]]";
134 } else {
135 $text = $m[0];
136 }
137 return $text;
138 }
139 ?>