bug fix
[mediawiki.git] / maintenance / attribute.php
bloba6dcf4a6734605e53e69d0adac50486f58cf0618
1 <?php
2 # Script for re-attributing edits
3 require_once( "commandLine.inc" );
5 # Parameters
6 if ( count( $args ) < 2 ) {
7 print "Not enough parameters\n";
8 if ( $wgWikiFarm ) {
9 print "Usage: php attribute.php <language> <site> <source> <destination>\n";
10 } else {
11 print "Usage: php attribute.php <source> <destination>\n";
13 exit;
16 $source = $args[0];
17 $dest = $args[1];
19 $eSource = wfStrencode( $source );
20 $eDest = wfStrencode( $dest );
22 # Get user id
23 $res = wfQuery( "SELECT user_id FROM user WHERE user_name='$eDest'", DB_READ );
24 $row = wfFetchObject( $res );
25 if ( !$row ) {
26 print "Warning: the target name \"$dest\" does not exist";
27 $uid = 0;
28 } else {
29 $uid = $row->user_id;
32 # Initialise files
33 $logfile = fopen( "attribute.log", "a" );
34 $sqlfile = fopen( "attribute.sql", "a" );
36 fwrite( $logfile, "* $source &rarr; $dest\n" );
38 fwrite( $sqlfile,
39 "-- Changing attribution SQL file
40 -- Generated with attribute.php
41 -- $source -> $dest ($uid)
42 ");
44 $omitTitle = "Wikipedia:Changing_attribution_for_an_edit";
46 # Get old entries
47 print "\nOld entries\n\n";
49 $res = wfQuery( "SELECT old_namespace, old_title, old_id, old_timestamp FROM old WHERE old_user_text='$eSource'", DB_READ );
50 $row = wfFetchObject( $res );
52 if ( $row ) {
54 if ( $row->old_title=='Votes_for_deletion' && $row->old_namespace == 4 ) {
55 # We don't have that long
56 break;
59 fwrite( $logfile, "**Old IDs: " );
60 fwrite( $sqlfile, "UPDATE old SET old_user=$uid, old_user_text='$eDest' WHERE old_id IN (\n" );
62 for ( $first=true; $row; $row = wfFetchObject( $res ) ) {
63 $ns = $wgLang->getNsText( $row->old_namespace );
64 if ( $ns ) {
65 $fullTitle = "$ns:{$row->old_title}";
66 } else {
67 $fullTitle = $row->old_title;
69 if ( $fullTitle == $omitTitle ) {
70 continue;
73 print "$fullTitle\n";
74 $url = "http://$lang.wikipedia.org/w/wiki.phtml?title=" . urlencode( $fullTitle );
75 $eTitle = wfStrencode( $row->old_title );
76 /*
77 # Find previous entry
78 $lastres = wfQuery( "SELECT old_id FROM old WHERE
79 old_title='$eTitle' AND old_namespace={$row->old_namespace} AND
80 old_timestamp<'{$row->old_timestamp}' ORDER BY inverse_timestamp LIMIT 1", DB_READ );
81 $lastrow = wfFetchObject( $lastres );
82 if ( $lastrow ) {
83 $last = $lastrow->old_id;
84 $url .= "&diff={$row->old_id}&oldid=$last";
85 } else {*/
86 $url .= "&oldid={$row->old_id}";
87 # }
89 # Output
90 fwrite( $sqlfile, " " );
91 if ( $first ) {
92 $first = false;
93 } else {
94 fwrite( $sqlfile, ", " );
95 fwrite( $logfile, ", " );
98 fwrite( $sqlfile, "{$row->old_id} -- $url\n" );
99 fwrite( $logfile, "[$url {$row->old_id}]" );
102 fwrite( $sqlfile, ");\n" );
103 fwrite( $logfile, "\n" );
106 # Get cur entries
107 print "\n\nCur entries\n\n";
109 $res = wfQuery( "SELECT cur_title, cur_namespace, cur_timestamp, cur_id FROM cur WHERE cur_user_text='$eSource'",
110 DB_READ );
111 $row = wfFetchObject( $res );
112 if ( $row ) {
113 fwrite( $sqlfile, "\n\nUPDATE cur SET cur_user=$uid, cur_user_text='$eDest' WHERE cur_id IN(\n" );
114 fwrite( $logfile, "**Cur entries:\n" );
115 for ( $first=true; $row; $row = wfFetchObject( $res ) ) {
116 $ns = $wgLang->getNsText( $row->cur_namespace );
117 if ( $ns ) {
118 $fullTitle = "$ns:{$row->cur_title}";
119 } else {
120 $fullTitle = $row->cur_title;
122 if ( $fullTitle == $omitTitle ) {
123 continue;
125 $url = "http://$lang.wikipedia.org/wiki/" . urlencode($fullTitle);
126 if ( $first ) {
127 fwrite( $sqlfile, " " );
128 $first = false;
129 } else {
130 fwrite( $sqlfile, " , " );
132 fwrite( $sqlfile, "{$row->cur_id} -- $url\n" );
133 fwrite( $logfile, "***[[$fullTitle]] {$row->cur_timestamp}\n" );
134 print "$fullTitle\n";
136 fwrite( $sqlfile, ");\n" );
138 print "\n";
140 fclose( $sqlfile );
141 fclose( $logfile );