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