Switch some messages to wikitext, html cleanup
[mediawiki.git] / maintenance / archives / moveCustomMessages.inc
blob83664dca0729e66d9dd6eb3417f6c2bc152a0e85
1 <?php
2 #### FIXME THIS FILE IS BADLY BROKEN FOR NEW SCHEMA
3 /**
4  * @deprecated
5  * @package MediaWiki
6  * @subpackage MaintenanceArchive
7  */
9 /** */
10 function isTemplateInitialised() {
11         global $wgAllMessagesEn;
12         $fname = 'isTemplateInitialised';
14         $dbw =& wfGetDB( DB_MASTER );
15         $n = $dbw->selectField( 'page', 'count(*)', array( 'page_namespace' => NS_MEDIAWIKI ) );
16         return $n > count( $wgAllMessagesEn ) ? false : true;
19 function moveCustomMessages( $phase ) {
20         global $wgUser, $wgAllMessagesEn, $wgDeferredUpdateList, $wgLang, $wgContLang;
21         global $targets, $template, $replaceCount;
23         $wgUser = new User;
24         $wgUser->setLoaded( true ); # Don't load from DB
25         $wgUser->setName( "Template namespace initialisation script" );
26         $wgUser->addRight( "bot" );
28         $dbw =& wfGetDB( DB_MASTER );
30         $dbw->ignoreErrors( true );
32         # Compose DB key array
33         $dbkeys = array();
35         foreach ( $wgAllMessagesEn as $key => $enValue ) {
36                 $title = Title::newFromText( $wgContLang->ucfirst( $key ) );
37                 $dbkeys[$title->getDBkey()] = 1;
38         }
40         $res = $dbw->select( 'page', array( 'page_id', 'page_title' ), array( 'page_namespace' => NS_MEDIAWIKI ) );
42         # Compile target array
43         $targets = array();
44         while ( $row = $dbw->fetchObject( $res ) ) {
45                 if ( !array_key_exists( $row->cur_title, $dbkeys ) ) {
46                         $targets[$row->cur_title] = 1;
47                 }
48         }
49         $dbw->freeResult( $res );
51         # Create redirects from destination to source
52         if ( $phase == 0 || $phase == 1 ) {
53                 print "Creating redirects\n";
54                 foreach ( $targets as $partial => $dummy ) {
55                         print "$partial...";
56                         $nt = Title::makeTitle( NS_TEMPLATE, $partial );
57                         $ot = Title::makeTitle( NS_MEDIAWIKI, $partial );
59                         if ( $nt->createRedirect( $ot, "" ) ) {
60                                 print "redirected\n";
61                         } else {
62                                 print "not redirected\n";
63                         }
64                 }
65                 if ( $phase == 0 ) {
66                         print "\nRedirects created. Update live script files now.\nPress ENTER to continue.\n\n";
67                         readconsole();
68                 }
69         }
71         # Move pages
72         if ( $phase == 0 || $phase == 2 ) {
73                 print "\nMoving pages...\n";
74                 foreach ( $targets as $partial => $dummy ) {
75                         $dbw->query( "BEGIN" );
76                         $ot = Title::makeTitle( NS_MEDIAWIKI, $partial );
77                         $nt = Title::makeTitle( NS_TEMPLATE, $partial );
78                         print "$partial...";
80                         if ( $ot->moveNoAuth( $nt ) === true ) {
81                                 print "moved\n";
82                         } else {
83                                 print "not moved\n";
84                         }
85                         # Do deferred updates
86                         while ( count( $wgDeferredUpdateList ) ) {
87                                 $up = array_pop( $wgDeferredUpdateList );
88                                 $up->doUpdate();
89                         }
90                         $dbw->query( "COMMIT" );
91                 }
92         }
94         # Convert text
95         if ( $phase == 0 || $phase == 3 ) {
96                 print "\nConverting text...\n";
97                 
98                 $parser = new Parser;
99                 $options = ParserOptions::newFromUser( $wgUser );
100                 $completedTitles = array();
101                 $titleChars = Title::legalChars();
102                 $mediaWiki = $wgLang->getNsText( NS_MEDIAWIKI );
103                 $template = $wgLang->getNsText( NS_TEMPLATE );
104                 $linkRegex = "/\[\[$mediaWiki:([$titleChars]*?)\]\]/";
105                 $msgRegex = "/{{msg:([$titleChars]*?)}}/";
107                 foreach ( $targets as $partial => $dummy ) {
108                         $dest = Title::makeTitle( NS_MEDIAWIKI, $partial );
109                         $linksTo = $dest->getLinksTo();
110                         foreach( $linksTo as $source ) {
111                                 $dbw->query( "BEGIN" );
112                                 $pdbk = $source->getPrefixedDBkey();
113                                 if ( !array_key_exists( $pdbk, $completedTitles ) ) {   
114                                         $completedTitles[$pdbk] = 1;
115                                         $id = $source->getArticleID();
116                                         $row = $dbw->selectRow( 'cur', array( 'cur_text' ), 
117                                                 array( 'cur_id' => $source->getArticleID() ) );
118                                         $parser->startExternalParse( $source, $options, OT_WIKI );
119                                         $text = $parser->strip( $row->cur_text, $stripState, false );
120                                         # {{msg}} -> {{}}
121                                         $text = preg_replace( $msgRegex, "{{\$1}}", $text );
122                                         # [[MediaWiki:]] -> [[Template:]]
123                                         $text = preg_replace_callback( $linkRegex, "wfReplaceMediaWiki", $text );
124                                         $text = $parser->unstrip( $text, $stripState );
125                                         $text = $parser->unstripNoWiki( $text, $stripState );
126                                         if ( $text != $row->cur_text ) {
127                                                 print "$pdbk\n";
128                                                 $art = new Article( $source );
129                                                 $art->updateArticle( $text, "", false, false );
130                                                 # Do deferred updates
131                                                 while ( count( $wgDeferredUpdateList ) ) {
132                                                         $up = array_pop( $wgDeferredUpdateList );
133                                                         $up->doUpdate();
134                                                 }
135                                         } else {
136                                                 print "($pdbk)\n";
137                                         }
138                                 } 
139                                 $dbw->query( "COMMIT" );
140                         }
141                 }
142         }
146 #--------------------------------------------------------------------------------------------------------------
147 function wfReplaceMediaWiki( $m ) {
148         global $targets, $template, $replaceCount;
149         $title = Title::newFromText( $m[1] );
150         $partial = $title->getDBkey();
152         if ( array_key_exists( $partial, $targets ) ) {
153                 $text = "[[$template:{$m[1]}]]";
154         } else {
155                 $text = $m[0];
156         }
157         return $text;