2 #### FIXME THIS FILE IS BADLY BROKEN FOR NEW SCHEMA
6 * @subpackage MaintenanceArchive
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;
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
35 foreach ( $wgAllMessagesEn as $key => $enValue ) {
36 $title = Title::newFromText( $wgContLang->ucfirst( $key ) );
37 $dbkeys[$title->getDBkey()] = 1;
40 $res = $dbw->select( 'page', array( 'page_id', 'page_title' ), array( 'page_namespace' => NS_MEDIAWIKI ) );
42 # Compile target array
44 while ( $row = $dbw->fetchObject( $res ) ) {
45 if ( !array_key_exists( $row->cur_title, $dbkeys ) ) {
46 $targets[$row->cur_title] = 1;
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 ) {
56 $nt = Title::makeTitle( NS_TEMPLATE, $partial );
57 $ot = Title::makeTitle( NS_MEDIAWIKI, $partial );
59 if ( $nt->createRedirect( $ot, "" ) ) {
62 print "not redirected\n";
66 print "\nRedirects created. Update live script files now.\nPress ENTER to continue.\n\n";
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 );
80 if ( $ot->moveNoAuth( $nt ) === true ) {
86 while ( count( $wgDeferredUpdateList ) ) {
87 $up = array_pop( $wgDeferredUpdateList );
90 $dbw->query( "COMMIT" );
95 if ( $phase == 0 || $phase == 3 ) {
96 print "\nConverting text...\n";
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 );
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 ) {
128 $art = new Article( $source );
129 $art->updateArticle( $text, "", false, false );
130 # Do deferred updates
131 while ( count( $wgDeferredUpdateList ) ) {
132 $up = array_pop( $wgDeferredUpdateList );
139 $dbw->query( "COMMIT" );
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]}]]";