including the new Defines.php
[mediawiki.git] / maintenance / archives / moveCustomMessages.inc
blobe4eadd5e70f5d49edfd89fb64a9f63414e508b15
1 <?php
3 function isTemplateInitialised() {
4         $fname = 'isTemplateInitialised';
6         $dbw =& wfGetDB( DB_MASTER );
7         $res = $dbw->select( 'cur', 1, array( 'cur_namespace' => NS_TEMPLATE ), $fname, array( 'LIMIT' => 1 ) );
8         return $dbw->numRows( $res ) ? true : false;
11 function moveCustomMessages( $phase ) {
12         global $wgUser, $wgAllMessagesEn, $wgDeferredUpdateList, $wgLang;
13         global $targets, $template, $replaceCount;
15         $wgUser = new User;
16         $wgUser->setLoaded( true ); # Don't load from DB
17         $wgUser->setName( "Template namespace initialisation script" );
18         $wgUser->addRight( "bot" );
20         $dbw =& wfGetDB( DB_MASTER );
22         $dbw->setIgnoreErrors( true );
24         # Compose DB key array
25         $dbkeys = array();
27         foreach ( $wgAllMessagesEn as $key => $enValue ) {
28                 $title = Title::newFromText( $key );
29                 $dbkeys[$title->getDBkey()] = 1;
30         }
32         $res = $dbw->select( 'cur', array( 'cur_id', 'cur_title' ), array( 'cur_namespace' => NS_MEDIAWIKI ) );
34         # Compile target array
35         $targets = array();
36         while ( $row = $dbw->fetchObject( $res ) ) {
37                 if ( !array_key_exists( $row->cur_title, $dbkeys ) ) {
38                         $targets[$row->cur_title] = 1;
39                 }
40         }
41         $dbw->freeResult( $res );
43         # Create redirects from destination to source
44         if ( $phase == 0 || $phase == 1 ) {
45                 print "Creating redirects\n";
46                 foreach ( $targets as $partial => $dummy ) {
47                         print "$partial...";
48                         $nt = Title::makeTitle( NS_TEMPLATE, $partial );
49                         $ot = Title::makeTitle( NS_MEDIAWIKI, $partial );
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         }
63         # Move pages
64         if ( $phase == 0 || $phase == 2 ) {
65                 print "\nMoving pages...\n";
66                 foreach ( $targets as $partial => $dummy ) {
67                         $dbw->query( "BEGIN" );
68                         $ot = Title::makeTitle( NS_MEDIAWIKI, $partial );
69                         $nt = Title::makeTitle( NS_TEMPLATE, $partial );
70                         print "$partial...";
72                         if ( $ot->moveNoAuth( $nt ) === true ) {
73                                 print "moved\n";
74                         } else {
75                                 print "not moved\n";
76                         }
77                         # Do deferred updates
78                         while ( count( $wgDeferredUpdateList ) ) {
79                                 $up = array_pop( $wgDeferredUpdateList );
80                                 $up->doUpdate();
81                         }
82                         $dbw->query( "COMMIT" );
83                 }
84         }
86         # Convert text
87         if ( $phase == 0 || $phase == 3 ) {
88                 print "\nConverting text...\n";
89                 
90                 $parser = new Parser;
91                 $options = ParserOptions::newFromUser( $wgUser );
92                 $completedTitles = array();
93                 $titleChars = Title::legalChars();
94                 $mediaWiki = $wgLang->getNsText( NS_MEDIAWIKI );
95                 $template = $wgLang->getNsText( NS_TEMPLATE );
96                 $linkRegex = "/\[\[$mediaWiki:([$titleChars]*?)\]\]/";
97                 $msgRegex = "/{{msg:([$titleChars]*?)}}/";
99                 foreach ( $targets as $partial => $dummy ) {
100                         $dest = Title::makeTitle( NS_MEDIAWIKI, $partial );
101                         $linksTo = $dest->getLinksTo();
102                         foreach( $linksTo as $source ) {
103                                 $dbw->query( "BEGIN" );
104                                 $pdbk = $source->getPrefixedDBkey();
105                                 if ( !array_key_exists( $pdbk, $completedTitles ) ) {   
106                                         $completedTitles[$pdbk] = 1;
107                                         $id = $source->getArticleID();
108                                         $row = $dbw->selectRow( 'cur', array( 'cur_text' ), 
109                                                 array( 'cur_id' => $source->getArticleID() ) );
110                                         $parser->startExternalParse( $source, $options, OT_WIKI );
111                                         $text = $parser->strip( $row->cur_text, $stripState, false );
112                                         # {{msg}} -> {{}}
113                                         $text = preg_replace( $msgRegex, "{{\$1}}", $text );
114                                         # [[MediaWiki:]] -> [[Template:]]
115                                         $text = preg_replace_callback( $linkRegex, "wfReplaceMediaWiki", $text );
116                                         $text = $parser->unstrip( $text, $stripState );
117                                         $text = $parser->unstripNoWiki( $text, $stripState );
118                                         if ( $text != $row->cur_text ) {
119                                                 print "$pdbk\n";
120                                                 $art = new Article( $source );
121                                                 $art->updateArticle( $text, "", false, false );
122                                                 # Do deferred updates
123                                                 while ( count( $wgDeferredUpdateList ) ) {
124                                                         $up = array_pop( $wgDeferredUpdateList );
125                                                         $up->doUpdate();
126                                                 }
127                                         } else {
128                                                 print "($pdbk)\n";
129                                         }
130                                 } 
131                                 $dbw->query( "COMMIT" );
132                         }
133                 }
134         }
138 #--------------------------------------------------------------------------------------------------------------
139 function wfReplaceMediaWiki( $m ) {
140         global $targets, $template, $replaceCount;
141         $title = Title::newFromText( $m[1] );
142         $partial = $title->getDBkey();
144         if ( array_key_exists( $partial, $targets ) ) {
145                 $text = "[[$template:{$m[1]}]]";
146         } else {
147                 $text = $m[0];
148         }
149         return $text;