|
1 #!/usr/bin/perl |
|
2 #$Id$ |
|
3 # |
|
4 # cleanup after delinker |
|
5 # |
|
6 # (c)Peter [[grin]] Gervai, 2022 |
|
7 # cc-by-sa-4.0-int / gplv3+ |
|
8 # |
|
9 |
|
10 use strict; |
|
11 use warnings; |
|
12 use utf8; |
|
13 |
|
14 use File::Basename; |
|
15 use lib dirname(__FILE__); |
|
16 use BotSecrets; |
|
17 |
|
18 use MediaWiki::Bot qw(:constants); |
|
19 use DBI; |
|
20 |
|
21 binmode( STDOUT, ':utf8' ); |
|
22 |
|
23 $|=1; |
|
24 |
|
25 my ($db_name, $db_host, $db_port) = ("s52421__commonsdelinquent_p", "tools.db.svc.wikimedia.cloud", 3306); |
|
26 ## my ($db_user, $db_pw) = ("xxxxxxxxx", "xxxxxxxxxx"); |
|
27 |
|
28 &d("Start"); |
|
29 |
|
30 ## connect db |
|
31 my $dsn = "DBI:mysql:database=$db_name;host=$db_host;port=$db_port"; |
|
32 my $dbh = DBI->connect( $dsn, $db_user, $db_pw, { mysql_enable_utf8=>1, RaiseError=>0, AutoCommit=>0 } ); |
|
33 $dbh->{mysql_enable_utf8} = 1; |
|
34 |
|
35 ## prepare sql |
|
36 #my $sth = $dbh->prepare( "SELECT id,action,file,wiki,page,revision,done,note,replace_with_file FROM event WHERE action=? AND file=? AND done=? AND timestamp BETWEEN ? AND ?" ); |
|
37 my $sth = $dbh->prepare( "SELECT id,action,file,wiki,page,revision,done,note,replace_with_file FROM event WHERE action=? AND done=? AND timestamp BETWEEN ? AND ?" ); |
|
38 |
|
39 my $sth_update = $dbh->prepare( "UPDATE event SET done=? WHERE id=?" ); |
|
40 |
|
41 ## connect bot (enwp) |
|
42 #my ($bu,$bp) = ('xxxxxxxxxxxxx', 'xxxxxxxxxxxxxx' ); |
|
43 our $bot = MediaWiki::Bot->new({ |
|
44 host => 'en.wikipedia.org', |
|
45 login_data => { username => $bu, password=> $bp }, |
|
46 do_sul => 1, |
|
47 operator => 'grin', |
|
48 protocol => 'https', |
|
49 debug => 2, |
|
50 assert => 'user', |
|
51 }); |
|
52 my $last_wiki='huwiki'; |
|
53 |
|
54 die "Cannot login" unless $bot; |
|
55 |
|
56 ## rev |
|
57 my $revert_message = "Undoing CommonsDelinker bad replace, will be retried later."; |
|
58 |
|
59 ## prepare search |
|
60 my $action = 'replace'; |
|
61 #my $file = 'S-3A_MAD_DN-SC-87-05743.JPEG'; |
|
62 my $done = 127; |
|
63 my ($ts_from, $ts_to) = ('20220502000000', '20220506130000'); |
|
64 #my $res = $sth->execute( $action, $file, $done, $ts_from, $ts_to ); |
|
65 my $res = $sth->execute( $action, $done, $ts_from, $ts_to ); |
|
66 if( $dbh->err ) { |
|
67 die "Error doing SQL: " . $dbh->errstr; |
|
68 } |
|
69 print $sth->rows . " rows found.\n"; |
|
70 |
|
71 ## results |
|
72 while( my $a = $sth->fetchrow_hashref ) { |
|
73 for my $key (sort keys %$a) { |
|
74 print "$key=" . $$a{$key} . " "; |
|
75 } |
|
76 print "\n"; |
|
77 |
|
78 # check data |
|
79 if( $last_wiki ne $$a{wiki} ) { |
|
80 my $wikidata = &get_wikidata( $$a{wiki} ); |
|
81 next unless $wikidata; |
|
82 $bot->set_wiki( $wikidata ); |
|
83 } |
|
84 |
|
85 my $revid = $bot->get_last( $$a{page}, 'CommonsDelinker' ); |
|
86 if( !defined( $revid ) ) { |
|
87 &d("Revid is missing!! skipping $$a{wiki}:$$a{page}!!"); |
|
88 #&error("missing revid"); |
|
89 next; |
|
90 } |
|
91 |
|
92 if( $revid == $$a{revision} ) { |
|
93 &d(" Page unchanged, undo possible! REVERTING $$a{wiki}:$$a{page}"); |
|
94 |
|
95 # revert |
|
96 if( $bot->revert( $$a{page}, $$a{revision}, $revert_message ) ) { |
|
97 &d( "Success. Updating."); |
|
98 $sth_update->execute( 0, $$a{id} ); |
|
99 if( $dbh->err ) { |
|
100 &d( "*** Error updating db for $$a{wiki}:$$a{page} id=$$a{id}!!"); |
|
101 $dbh->commit; |
|
102 exit; |
|
103 } |
|
104 |
|
105 } else { |
|
106 &d( " Revert #1 failed, try to login."); |
|
107 if( $bot->login( { username=>$bu, password=>$bp } ) ) { |
|
108 if( $bot->revert( $$a{page}, $$a{revision}, $revert_message ) ) { |
|
109 &d( "Success^2. Updating."); |
|
110 $sth_update->execute( 0, $$a{id} ); |
|
111 if( $dbh->err ) { |
|
112 &d( "*** Error updating db for $$a{wiki}:$$a{page} id=$$a{id}!!"); |
|
113 $dbh->commit; |
|
114 exit; |
|
115 } |
|
116 } |
|
117 |
|
118 } else { |
|
119 &d( "Login failed into $$a{wiki}. Skipping"); |
|
120 &error("login failed"); |
|
121 $sth_update->execute( 43, $$a{id} ); |
|
122 } |
|
123 } |
|
124 |
|
125 } else { |
|
126 &d( " Page changed, oldid $$a{revision} - newid $revid; skipping (update db)."); |
|
127 $sth_update->execute( 42, $$a{id} ); |
|
128 if( $dbh->err ) { |
|
129 &d( "*** Error updating db for non-changed $$a{wiki}:$$a{page} id=$$a{id}!!"); |
|
130 } |
|
131 } |
|
132 |
|
133 $dbh->commit; |
|
134 } |
|
135 &d( "Commit."); |
|
136 $dbh->commit; |
|
137 |
|
138 exit; |
|
139 |
|
140 sub d { |
|
141 my ($s) = @_; |
|
142 print scalar(localtime) . " [$$] $s\n"; |
|
143 } |
|
144 |
|
145 |
|
146 sub error { |
|
147 return; # doesn't work |
|
148 my ($s) = @_; |
|
149 print "error: $s; " . $bot->{error}->{code} . "; " . $bot->{error}->{details} . "\n"; |
|
150 |
|
151 #use Data::Dumper; |
|
152 #die Dumper($bot); |
|
153 #exit; |
|
154 } |
|
155 |
|
156 ## decypher short wikinames |
|
157 sub get_wikidata { |
|
158 my ($name) = @_; |
|
159 |
|
160 &d("Decode $name"); |
|
161 |
|
162 my $host = $bot->db_to_domain( $name ); |
|
163 return { host => $host }; |
|
164 |
|
165 if( $name =~ /^(.{2,3}|simple)wiki$/ ) { |
|
166 return { host => "$1.wikipedia.org" }; |
|
167 } |
|
168 |
|
169 if( $name =~ /^(.{2,3})wikivoyage$/ ) { |
|
170 return { host => "$1.wikivoyage.org" }; |
|
171 } |
|
172 |
|
173 if( $name =~ /^(.{2,3})wikisource$/ ) { |
|
174 return { host => "$1.wikisource.org" }; |
|
175 } |
|
176 |
|
177 if( $name =~ /^(.{2,3})wikiquote$/ ) { |
|
178 return { host => "$1.wikiquote.org" }; |
|
179 } |
|
180 |
|
181 if( $name =~ /^(.{2,3})wikibooks$/ ) { |
|
182 return { host => "$1.wikibooks.org" }; |
|
183 } |
|
184 |
|
185 if( $name =~ /^(.{2,3})wiktionary$/ ) { |
|
186 return { host => "$1.wiktionary.org" }; |
|
187 } |
|
188 |
|
189 if( $name eq 'wikidatawiki' ) { |
|
190 # wikidata probably not fucked up |
|
191 return undef; |
|
192 } |
|
193 |
|
194 &d("*** $name not implemented yet!!! ***"); |
|
195 return undef; |
|
196 #die "decode '$name' isn't implemented yet."; |
|
197 } |
|
198 |
|
199 exit; |
|
200 |
|
201 my $article_name = 'a'; |
|
202 |
|
203 my $options = { revid => 13849803 }; |
|
204 my $txt = $bot->get_text($article_name, $options); |
|
205 die "error something" unless defined $options->{pageid}; |
|
206 warn "page doesn't exist" if $options->{pageid} == MediaWiki::Bot::PAGE_NONEXISTENT; |
|
207 print "Page length is ". length($txt) . "!\n"; |
|
208 |
|
209 my $pageid = $bot->get_id($article_name); |
|
210 die "error something else" unless defined $pageid; |
|
211 printf "Page id is %s\n", $pageid; |
|
212 |
|
213 # last _not_ by user |
|
214 my $revid = $bot->get_last($article_name,'no such user'); |
|
215 printf "Last revid is %s\n", $revid; |
|
216 |
|
217 $revid = $bot->get_last($article_name,'FoBe'); |
|
218 printf "Last revid-2 is %s\n", $revid; |
|
219 |
|
220 $options = { oldid=> 20300641, revid=>20300648 }; |
|
221 my $diff = $bot->diff($options); |
|
222 print "Diff: $diff\n"; |
|
223 |
|
224 ## commons |
|
225 $bot->set_wiki({ |
|
226 host => 'commons.wikimedia.org' |
|
227 }); |
|
228 die "Cannot login to commons" unless $bot; |
|
229 print "Logged over commons.\n"; |
|
230 |
|
231 $options = { revid=> 568734018, oldid=>628016329 }; |
|
232 $diff = $bot->diff($options); |
|
233 print "Diff: $diff\n"; |
|
234 |
|
235 ## a hiba: az elozo sor utolso szava + \n bekerult a replacementbe |
|
236 ## javitas: |
|
237 ## - ha ez az utolso edit |
|
238 ## - revert |
|
239 |
|
240 __END__ |
|
241 <tr> |
|
242 <td class="diff-marker" data-marker="−"></td> |
|
243 <td class="diff-deletedline diff-side-deleted"><div>File:Nestor Lakoba, Nikita Khrushchev, Lavrenti Beria and Aghasi Khanjian.jpg|Nestor Lakoba, Nikita Khrushchev, Lavrenti Beria and Aghasi Khanjian, <del class="diffchange diffchange-inline">1935</del></div></td> |
|
244 <td class="diff-marker" data-marker="+"></td> |
|
245 <td class="diff-addedline diff-side-added"><div>File:Nestor Lakoba, Nikita Khrushchev, Lavrenti Beria and Aghasi Khanjian.jpg|Nestor Lakoba, Nikita Khrushchev, Lavrenti Beria and Aghasi Khanjian,<ins class="diffchange diffchange-inline">|{{c|Georgy</ins> <ins class="diffchange diffchange-inline">Malenkov}} and Beria, 1940</ins></div></td> |
|
246 </tr> |
|
247 <tr> |
|
248 <td class="diff-marker" data-marker="−"></td> |
|
249 <td class="diff-deletedline diff-side-deleted"><div>File:Берия в суде.jpg|{{c|Georgy Malenkov}} and Beria, 1940</div></td> |
|
250 <td colspan="2" class="diff-empty diff-side-added"></td> |
|
251 </tr> |
|
252 |
|
253 |
|
254 ## generic revert |
|
255 ## - adatbazisbol ami done=42 |
|
256 ## - revision |
|
257 ## - ha az az utolso akkor: |
|
258 ## - revert |
|
259 ## - done=0 (pending) |
|
260 ## - ha nem akkor |
|
261 ## - done=666 (fixx it felix) |
|
262 ## |