1b9e659b4f53683aef0978efc0929f4ec7b78ba8
[lhc/web/wiklou.git] / maintenance / attribute.php
1 <?php
2
3 # Parameters
4
5 if ($argc < 4) {
6 print "Not enough parameters\n";
7 print "Usage: php attribute.php <lang> <source> <destination>\n";
8 exit;
9 }
10
11 $lang = $argv[1];
12 $source = $argv[2];
13 $dest = $argv[3];
14
15 # Initialisation
16
17 $wgCommandLineMode = true;
18 $DP = "../includes";
19
20 $sep = strchr( $include_path = ini_get( "include_path" ), ";" ) ? ";" : ":";
21 ini_set( "include_path", "$IP$sep$include_path" );
22
23 include_once( "/apache/htdocs/$lang/w/LocalSettings.php" );
24 include_once( "Setup.php" );
25
26 $wgTitle = Title::newFromText( "Changing attribution script" );
27 set_time_limit(0);
28 $wgCommandLineMode = true;
29
30 $eSource = wfStrencode( $source );
31 $eDest = wfStrencode( $dest );
32
33 # Get user id
34 $res = wfQuery( "SELECT user_id FROM user WHERE user_name='$eDest'", DB_READ );
35 $row = wfFetchObject( $res );
36 if ( !$row ) {
37 print "Warning: the target name \"$dest\" does not exist";
38 $uid = 0;
39 } else {
40 $uid = $row->user_id;
41 }
42
43 # Initialise files
44 $logfile = fopen( "attribute.log", "a" );
45 $sqlfile = fopen( "attribute.sql", "a" );
46
47 fwrite( $logfile, "* $source &rarr; $dest\n" );
48
49 fwrite( $sqlfile,
50 "-- Changing attribution SQL file
51 -- Generated with attribute.php
52 -- $source -> $dest ($uid)
53 ");
54
55 $omitTitle = "Wikipedia:Changing_attribution_for_an_edit";
56
57 # Get old entries
58 print "\nOld entries\n\n";
59
60 $res = wfQuery( "SELECT old_namespace, old_title, old_id, old_timestamp FROM old WHERE old_user_text='$eSource'", DB_READ );
61 $row = wfFetchObject( $res );
62
63 if ( $row ) {
64 /*
65 if ( $row->old_title=='Votes_for_deletion' && $row->old_namespace == 4 ) {
66 # We don't have that long
67 break;
68 }
69 */
70 fwrite( $logfile, "**Old IDs: " );
71 fwrite( $sqlfile, "UPDATE old SET old_user=$uid, old_user_text='$eDest' WHERE old_id IN (\n" );
72
73 for ( $first=true; $row; $row = wfFetchObject( $res ) ) {
74 $ns = $wgLang->getNsText( $row->old_namespace );
75 if ( $ns ) {
76 $fullTitle = "$ns:{$row->old_title}";
77 } else {
78 $fullTitle = $row->old_title;
79 }
80 if ( $fullTitle == $omitTitle ) {
81 continue;
82 }
83
84 print "$fullTitle\n";
85 $url = "http://$lang.wikipedia.org/w/wiki.phtml?title=" . urlencode( $fullTitle );
86 $eTitle = wfStrencode( $row->old_title );
87 /*
88 # Find previous entry
89 $lastres = wfQuery( "SELECT old_id FROM old WHERE
90 old_title='$eTitle' AND old_namespace={$row->old_namespace} AND
91 old_timestamp<'{$row->old_timestamp}' ORDER BY inverse_timestamp LIMIT 1", DB_READ );
92 $lastrow = wfFetchObject( $lastres );
93 if ( $lastrow ) {
94 $last = $lastrow->old_id;
95 $url .= "&diff={$row->old_id}&oldid=$last";
96 } else {*/
97 $url .= "&oldid={$row->old_id}";
98 # }
99
100 # Output
101 fwrite( $sqlfile, " " );
102 if ( $first ) {
103 $first = false;
104 } else {
105 fwrite( $sqlfile, ", " );
106 fwrite( $logfile, ", " );
107 }
108
109 fwrite( $sqlfile, "{$row->old_id} -- $url\n" );
110 fwrite( $logfile, "[$url {$row->old_id}]" );
111
112 }
113 fwrite( $sqlfile, ");\n" );
114 fwrite( $logfile, "\n" );
115 }
116
117 # Get cur entries
118 print "\n\nCur entries\n\n";
119
120 $res = wfQuery( "SELECT cur_title, cur_namespace, cur_timestamp, cur_id FROM cur WHERE cur_user_text='$eSource'",
121 DB_READ );
122 $row = wfFetchObject( $res );
123 if ( $row ) {
124 fwrite( $sqlfile, "\n\nUPDATE cur SET cur_user=$uid, cur_user_text='$eDest' WHERE cur_id IN(\n" );
125 fwrite( $logfile, "**Cur entries:\n" );
126 for ( $first=true; $row; $row = wfFetchObject( $res ) ) {
127 $ns = $wgLang->getNsText( $row->cur_namespace );
128 if ( $ns ) {
129 $fullTitle = "$ns:{$row->cur_title}";
130 } else {
131 $fullTitle = $row->cur_title;
132 }
133 if ( $fullTitle == $omitTitle ) {
134 continue;
135 }
136 $url = "http://$lang.wikipedia.org/wiki/" . urlencode($fullTitle);
137 if ( $first ) {
138 fwrite( $sqlfile, " " );
139 $first = false;
140 } else {
141 fwrite( $sqlfile, " , " );
142 }
143 fwrite( $sqlfile, "{$row->cur_id} -- $url\n" );
144 fwrite( $logfile, "***[[$fullTitle]] {$row->cur_timestamp}\n" );
145 print "$fullTitle\n";
146 }
147 fwrite( $sqlfile, ");\n" );
148 }
149 print "\n";
150
151 fclose( $sqlfile );
152 fclose( $logfile );
153
154 ?>