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