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