Fixed redirects, use getCurContentFields instead of getContentFields
[mediawiki.git] / maintenance / archives / moveCustomMessages.inc
blob5438194690a16e5e1f0393769a8be55e6eccfb8e
1 <?php
3 function isTemplateInitialised() {
4         $sql = "SELECT 1 FROM cur WHERE cur_namespace=" . NS_TEMPLATE . " LIMIT 1";
5         $res = wfQuery( $sql, DB_READ );
6         return wfNumRows( $res ) ? true : false;
9 function moveCustomMessages( $phase ) {
10         global $wgUser, $wgAllMessagesEn, $wgDeferredUpdateList, $wgLang;
11         global $targets, $template, $replaceCount;
13         $wgUser = new User;
14         $wgUser->setLoaded( true ); # Don't load from DB
15         $wgUser->setName( "Template namespace initialisation script" );
16         $wgUser->addRight( "bot" );
18         wfIgnoreSQLErrors( true );
20         # Compose DB key array
21         $dbkeys = array();
23         foreach ( $wgAllMessagesEn as $key => $enValue ) {
24                 $title = Title::newFromText( $key );
25                 $dbkeys[$title->getDBkey()] = 1;
26         }
28         $sql = "SELECT cur_id, cur_title FROM cur WHERE cur_namespace= " . NS_MEDIAWIKI;
29         $res = wfQuery( $sql, DB_READ );
31         # Compile target array
32         $targets = array();
33         while ( $row = wfFetchObject( $res ) ) {
34                 if ( !array_key_exists( $row->cur_title, $dbkeys ) ) {
35                         $targets[$row->cur_title] = 1;
36                 }
37         }
38         wfFreeResult( $res );
40         # Create redirects from destination to source
41         if ( $phase == 0 || $phase == 1 ) {
42                 print "Creating redirects\n";
43                 foreach ( $targets as $partial => $dummy ) {
44                         print "$partial...";
45                         $nt = Title::makeTitle( NS_TEMPLATE, $partial );
46                         $ot = Title::makeTitle( NS_MEDIAWIKI, $partial );
48                         if ( $nt->createRedirect( $ot, "" ) ) {
49                                 print "redirected\n";
50                         } else {
51                                 print "not redirected\n";
52                         }
53                 }
54                 if ( $phase == 0 ) {
55                         print "\nRedirects created. Update live script files now.\nPress ENTER to continue.\n\n";
56                         readconsole();
57                 }
58         }
60         # Move pages
61         if ( $phase == 0 || $phase == 2 ) {
62                 print "\nMoving pages...\n";
63                 foreach ( $targets as $partial => $dummy ) {
64                         wfQuery( "BEGIN", DB_WRITE );
65                         $ot = Title::makeTitle( NS_MEDIAWIKI, $partial );
66                         $nt = Title::makeTitle( NS_TEMPLATE, $partial );
67                         print "$partial...";
69                         if ( $ot->moveNoAuth( $nt ) === true ) {
70                                 print "moved\n";
71                         } else {
72                                 print "not moved\n";
73                         }
74                         # Do deferred updates
75                         while ( count( $wgDeferredUpdateList ) ) {
76                                 $up = array_pop( $wgDeferredUpdateList );
77                                 $up->doUpdate();
78                         }
79                         wfQuery( "COMMIT", DB_WRITE );
80                 }
81         }
83         # Convert text
84         if ( $phase == 0 || $phase == 3 ) {
85                 print "\nConverting text...\n";
86                 
87                 $parser = new Parser;
88                 $options = ParserOptions::newFromUser( $wgUser );
89                 $completedTitles = array();
90                 $titleChars = Title::legalChars();
91                 $mediaWiki = $wgLang->getNsText( NS_MEDIAWIKI );
92                 $template = $wgLang->getNsText( NS_TEMPLATE );
93                 $linkRegex = "/\[\[$mediaWiki:([$titleChars]*?)\]\]/";
94                 $msgRegex = "/{{msg:([$titleChars]*?)}}/";
96                 foreach ( $targets as $partial => $dummy ) {
97                         $dest = Title::makeTitle( NS_MEDIAWIKI, $partial );
98                         $linksTo = $dest->getLinksTo();
99                         foreach( $linksTo as $source ) {
100                                 wfQuery( "BEGIN", DB_WRITE );
101                                 $pdbk = $source->getPrefixedDBkey();
102                                 if ( !array_key_exists( $pdbk, $completedTitles ) ) {   
103                                         $completedTitles[$pdbk] = 1;
104                                         $id = $source->getArticleID();
105                                         $row = wfGetArray( 'cur', array( 'cur_text' ), 
106                                                 array( 'cur_id' => $source->getArticleID() ) );
107                                         $parser->startExternalParse( $source, $options, OT_WIKI );
108                                         $text = $parser->strip( $row->cur_text, $stripState, false );
109                                         # {{msg}} -> {{}}
110                                         $text = preg_replace( $msgRegex, "{{\$1}}", $text );
111                                         # [[MediaWiki:]] -> [[Template:]]
112                                         $text = preg_replace_callback( $linkRegex, "wfReplaceMediaWiki", $text );
113                                         $text = $parser->unstrip( $text, $stripState );
114                                         $text = $parser->unstripNoWiki( $text, $stripState );
115                                         if ( $text != $row->cur_text ) {
116                                                 print "$pdbk\n";
117                                                 $art = new Article( $source );
118                                                 $art->updateArticle( $text, "", false, false );
119                                                 # Do deferred updates
120                                                 while ( count( $wgDeferredUpdateList ) ) {
121                                                         $up = array_pop( $wgDeferredUpdateList );
122                                                         $up->doUpdate();
123                                                 }
124                                         } else {
125                                                 print "($pdbk)\n";
126                                         }
127                                 } 
128                                 wfQuery( "COMMIT", DB_WRITE );
129                         }
130                 }
131         }
135 #--------------------------------------------------------------------------------------------------------------
136 function wfReplaceMediaWiki( $m ) {
137         global $targets, $template, $replaceCount;
138         $title = Title::newFromText( $m[1] );
139         $partial = $title->getDBkey();
141         if ( array_key_exists( $partial, $targets ) ) {
142                 $text = "[[$template:{$m[1]}]]";
143         } else {
144                 $text = $m[0];
145         }
146         return $text;