Tweak image img_name index updater to current tables.sql (primary key)
[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 $fname = 'isTemplateInitialised';
11
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;
15 }
16
17 function moveCustomMessages( $phase ) {
18 global $wgUser, $wgAllMessagesEn, $wgDeferredUpdateList, $wgLang;
19 global $targets, $template, $replaceCount;
20
21 $wgUser = new User;
22 $wgUser->setLoaded( true ); # Don't load from DB
23 $wgUser->setName( "Template namespace initialisation script" );
24 $wgUser->addRight( "bot" );
25
26 $dbw =& wfGetDB( DB_MASTER );
27
28 $dbw->ignoreErrors( true );
29
30 # Compose DB key array
31 $dbkeys = array();
32
33 foreach ( $wgAllMessagesEn as $key => $enValue ) {
34 $title = Title::newFromText( $key );
35 $dbkeys[$title->getDBkey()] = 1;
36 }
37
38 $res = $dbw->select( 'cur', array( 'cur_id', 'cur_title' ), array( 'cur_namespace' => NS_MEDIAWIKI ) );
39
40 # Compile target array
41 $targets = array();
42 while ( $row = $dbw->fetchObject( $res ) ) {
43 if ( !array_key_exists( $row->cur_title, $dbkeys ) ) {
44 $targets[$row->cur_title] = 1;
45 }
46 }
47 $dbw->freeResult( $res );
48
49 # Create redirects from destination to source
50 if ( $phase == 0 || $phase == 1 ) {
51 print "Creating redirects\n";
52 foreach ( $targets as $partial => $dummy ) {
53 print "$partial...";
54 $nt = Title::makeTitle( NS_TEMPLATE, $partial );
55 $ot = Title::makeTitle( NS_MEDIAWIKI, $partial );
56
57 if ( $nt->createRedirect( $ot, "" ) ) {
58 print "redirected\n";
59 } else {
60 print "not redirected\n";
61 }
62 }
63 if ( $phase == 0 ) {
64 print "\nRedirects created. Update live script files now.\nPress ENTER to continue.\n\n";
65 readconsole();
66 }
67 }
68
69 # Move pages
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 );
76 print "$partial...";
77
78 if ( $ot->moveNoAuth( $nt ) === true ) {
79 print "moved\n";
80 } else {
81 print "not moved\n";
82 }
83 # Do deferred updates
84 while ( count( $wgDeferredUpdateList ) ) {
85 $up = array_pop( $wgDeferredUpdateList );
86 $up->doUpdate();
87 }
88 $dbw->query( "COMMIT" );
89 }
90 }
91
92 # Convert text
93 if ( $phase == 0 || $phase == 3 ) {
94 print "\nConverting text...\n";
95
96 $parser = new Parser;
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]*?)}}/";
104
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 );
118 # {{msg}} -> {{}}
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 ) {
125 print "$pdbk\n";
126 $art = new Article( $source );
127 $art->updateArticle( $text, "", false, false );
128 # Do deferred updates
129 while ( count( $wgDeferredUpdateList ) ) {
130 $up = array_pop( $wgDeferredUpdateList );
131 $up->doUpdate();
132 }
133 } else {
134 print "($pdbk)\n";
135 }
136 }
137 $dbw->query( "COMMIT" );
138 }
139 }
140 }
141 }
142
143
144 #--------------------------------------------------------------------------------------------------------------
145 function wfReplaceMediaWiki( $m ) {
146 global $targets, $template, $replaceCount;
147 $title = Title::newFromText( $m[1] );
148 $partial = $title->getDBkey();
149
150 if ( array_key_exists( $partial, $targets ) ) {
151 $text = "[[$template:{$m[1]}]]";
152 } else {
153 $text = $m[0];
154 }
155 return $text;
156 }
157
158 ?>