5 * @subpackage MaintenanceArchive
9 function isTemplateInitialised() {
10 $fname = 'isTemplateInitialised';
12 $dbw =& wfGetDB( DB_MASTER );
13 $res = $dbw->select( 'cur', 1, array( 'cur_namespace' => NS_TEMPLATE ), $fname, array( 'LIMIT' => 1 ) );
14 return $dbw->numRows( $res ) ? true : false;
17 function moveCustomMessages( $phase ) {
18 global $wgUser, $wgAllMessagesEn, $wgDeferredUpdateList, $wgLang;
19 global $targets, $template, $replaceCount;
22 $wgUser->setLoaded( true ); # Don't load from DB
23 $wgUser->setName( "Template namespace initialisation script" );
24 $wgUser->addRight( "bot" );
26 $dbw =& wfGetDB( DB_MASTER );
28 $dbw->ignoreErrors( true );
30 # Compose DB key array
33 foreach ( $wgAllMessagesEn as $key => $enValue ) {
34 $title = Title::newFromText( $key );
35 $dbkeys[$title->getDBkey()] = 1;
38 $res = $dbw->select( 'cur', array( 'cur_id', 'cur_title' ), array( 'cur_namespace' => NS_MEDIAWIKI ) );
40 # Compile target array
42 while ( $row = $dbw->fetchObject( $res ) ) {
43 if ( !array_key_exists( $row->cur_title, $dbkeys ) ) {
44 $targets[$row->cur_title] = 1;
47 $dbw->freeResult( $res );
49 # Create redirects from destination to source
50 if ( $phase == 0 || $phase == 1 ) {
51 print "Creating redirects\n";
52 foreach ( $targets as $partial => $dummy ) {
54 $nt = Title::makeTitle( NS_TEMPLATE, $partial );
55 $ot = Title::makeTitle( NS_MEDIAWIKI, $partial );
57 if ( $nt->createRedirect( $ot, "" ) ) {
60 print "not redirected\n";
64 print "\nRedirects created. Update live script files now.\nPress ENTER to continue.\n\n";
70 if ( $phase == 0 || $phase == 2 ) {
71 print "\nMoving pages...\n";
72 foreach ( $targets as $partial => $dummy ) {
73 $dbw->query( "BEGIN" );
74 $ot = Title::makeTitle( NS_MEDIAWIKI, $partial );
75 $nt = Title::makeTitle( NS_TEMPLATE, $partial );
78 if ( $ot->moveNoAuth( $nt ) === true ) {
84 while ( count( $wgDeferredUpdateList ) ) {
85 $up = array_pop( $wgDeferredUpdateList );
88 $dbw->query( "COMMIT" );
93 if ( $phase == 0 || $phase == 3 ) {
94 print "\nConverting text...\n";
97 $options = ParserOptions::newFromUser( $wgUser );
98 $completedTitles = array();
99 $titleChars = Title::legalChars();
100 $mediaWiki = $wgLang->getNsText( NS_MEDIAWIKI );
101 $template = $wgLang->getNsText( NS_TEMPLATE );
102 $linkRegex = "/\[\[$mediaWiki:([$titleChars]*?)\]\]/";
103 $msgRegex = "/{{msg:([$titleChars]*?)}}/";
105 foreach ( $targets as $partial => $dummy ) {
106 $dest = Title::makeTitle( NS_MEDIAWIKI, $partial );
107 $linksTo = $dest->getLinksTo();
108 foreach( $linksTo as $source ) {
109 $dbw->query( "BEGIN" );
110 $pdbk = $source->getPrefixedDBkey();
111 if ( !array_key_exists( $pdbk, $completedTitles ) ) {
112 $completedTitles[$pdbk] = 1;
113 $id = $source->getArticleID();
114 $row = $dbw->selectRow( 'cur', array( 'cur_text' ),
115 array( 'cur_id' => $source->getArticleID() ) );
116 $parser->startExternalParse( $source, $options, OT_WIKI );
117 $text = $parser->strip( $row->cur_text, $stripState, false );
119 $text = preg_replace( $msgRegex, "{{\$1}}", $text );
120 # [[MediaWiki:]] -> [[Template:]]
121 $text = preg_replace_callback( $linkRegex, "wfReplaceMediaWiki", $text );
122 $text = $parser->unstrip( $text, $stripState );
123 $text = $parser->unstripNoWiki( $text, $stripState );
124 if ( $text != $row->cur_text ) {
126 $art = new Article( $source );
127 $art->updateArticle( $text, "", false, false );
128 # Do deferred updates
129 while ( count( $wgDeferredUpdateList ) ) {
130 $up = array_pop( $wgDeferredUpdateList );
137 $dbw->query( "COMMIT" );
144 #--------------------------------------------------------------------------------------------------------------
145 function wfReplaceMediaWiki( $m ) {
146 global $targets, $template, $replaceCount;
147 $title = Title::newFromText( $m[1] );
148 $partial = $title->getDBkey();
150 if ( array_key_exists( $partial, $targets ) ) {
151 $text = "[[$template:{$m[1]}]]";