Fix to improve speed on some systems
[lhc/web/wiklou.git] / maintenance / attribute.php
1 <?
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", "w" );
45 $sqlfile = fopen( "attribute.sql", "w" );
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 # Get old entries
56 print "Getting old entries";
57
58 $res = wfQuery( "SELECT old_namespace, old_title, old_id, old_timestamp FROM old WHERE old_user_text='$eSource'", DB_READ );
59 $row = wfFetchObject( $res );
60
61 if ( $row ) {
62 /*
63 if ( $row->old_title=='Votes_for_deletion' && $row->old_namespace == 4 ) {
64 # We don't have that long
65 break;
66 }
67 */
68 fwrite( $logfile, "**Old IDs: " );
69 fwrite( $sqlfile, "UPDATE old SET old_user=$uid, old_user_text='$eDest' WHERE old_id IN (\n" );
70
71 for ( $first=true; $row; $row = wfFetchObject( $res ) ) {
72 $ns = $wgLang->getNsText( $row->old_namespace );
73 if ( $ns ) {
74 $fullTitle = "$ns:{$row->old_title}";
75 } else {
76 $fullTitle = $row->old_title;
77 }
78 $url = "http://$lang.wikipedia.org/w/wiki.phtml?title=" . urlencode( $fullTitle );
79 $eTitle = wfStrencode( $row->old_title );
80 /*
81 # Find previous entry
82 $lastres = wfQuery( "SELECT old_id FROM old WHERE
83 old_title='$eTitle' AND old_namespace={$row->old_namespace} AND
84 old_timestamp<'{$row->old_timestamp}' ORDER BY inverse_timestamp LIMIT 1", DB_READ );
85 $lastrow = wfFetchObject( $lastres );
86 if ( $lastrow ) {
87 $last = $lastrow->old_id;
88 $url .= "&diff={$row->old_id}&oldid=$last";
89 } else {*/
90 $url .= "&oldid={$row->old_id}";
91 # }
92
93 # Output
94 fwrite( $sqlfile, " " );
95 if ( $first ) {
96 $first = false;
97 } else {
98 fwrite( $sqlfile, ", " );
99 fwrite( $logfile, ", " );
100 }
101
102 fwrite( $sqlfile, "{$row->old_id} -- $url\n" );
103 fwrite( $logfile, "[$url {$row->old_id}]" );
104
105 print ".";
106 }
107 fwrite( $sqlfile, ");\n" );
108 fwrite( $logfile, "\n" );
109 }
110 print "\n";
111
112 # Get cur entries
113 print "Getting cur entries";
114 $res = wfQuery( "SELECT cur_title, cur_namespace, cur_timestamp, cur_id FROM cur WHERE cur_user_text='$eSource'",
115 DB_READ );
116 $row = wfFetchObject( $res );
117 if ( $row ) {
118 fwrite( $sqlfile, "\n\nUPDATE cur SET cur_user=$uid, cur_user_text='$eDest' WHERE cur_id IN(\n" );
119 fwrite( $logfile, "**Cur entries:\n" );
120 for ( $first=true; $row; $row = wfFetchObject( $res ) ) {
121 $ns = $wgLang->getNsText( $row->cur_namespace );
122 if ( $ns ) {
123 $fullTitle = "$ns:{$row->cur_title}";
124 } else {
125 $fullTitle = $row->cur_title;
126 }
127 $url = "http://$lang.wikipedia.org/wiki/" . urlencode($fullTitle);
128 if ( $first ) {
129 fwrite( $sqlfile, " " );
130 $first = false;
131 } else {
132 fwrite( $sqlfile, " , " );
133 }
134 fwrite( $sqlfile, "{$row->cur_id} -- $url\n" );
135 fwrite( $logfile, "***[[$fullTitle]] {$row->cur_timestamp}\n" );
136 print ".";
137 }
138 fwrite( $sqlfile, ");\n" );
139 }
140 print "\n";
141
142 fclose( $sqlfile );
143 fclose( $logfile );
144
145 ?>