forked from FlominatorTM/wikiblame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikiblame.php
1103 lines (981 loc) · 38 KB
/
wikiblame.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php header('Content-Type: text/html; charset=utf-8');
// Emulate the header BigPipe sends so we can test through Varnish.
header('Surrogate-Control: BigPipe/1.0');
// Explicitly disable caching so Varnish and other upstreams won't cache.
header("Cache-Control: no-cache, must-revalidate");
// Setting this header instructs Nginx to disable fastcgi_buffering and disable
// gzip for this request.
header('X-Accel-Buffering: no');
//last four lines come from https://www.jeffgeerling.com/blog/2016/streaming-php-disabling-output-buffering-php-apache-nginx-and-varnish
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="icon" href="WikiBlame.png" type="image/png">
<link rel="shortcut icon" href="WikiBlame.png" type="image/png">
<title>WikiBlame</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head><?php
include("shared_inc/language.inc.php");
include("shared_inc/wiki_functions.inc.php");
prevent_automatic_escaping_of_input_strings();
$inc_dir = "wikiblame_inc";
//get the language file and decide whether rtl oder ltr is used
$user_lang = read_language();
get_language('en', $inc_dir); //not translated messages will be printed in English
get_language($user_lang, $inc_dir);
fill_variables($user_lang);
$the_months = get_months($messages);
$allowedRevisionsPerCall = 50;
if ($user != '') {
$allowedRevisionsPerCall = -1;
}
$js_messages = [
'get_less_versions' => $messages['get_less_versions'],
'paste_url' => $messages['paste_url'],
'no_valid_url' => $messages['no_valid_url'],
'please_wait' => $messages['please_wait'],
];
$is_exp = stristr($_SERVER["PHP_SELF"], 'wikiblame.exp.php') !== false;
?>
<body dir="<?= $text_dir ?>" <?= $is_exp ? 'class="exp"' : '' ?> data-allowedVersionsPerCall="<?= $allowedRevisionsPerCall ?>">
<script type="text/javascript">
var i18n = <?= json_encode($js_messages, JSON_UNESCAPED_UNICODE) ?>;
</script>
<form method="get" name="mainform">
<div class="textalign-end">
<?= $messages['ui_lang'] . "\n" ?>
<br>
<?php language_selection($user_lang, $inc_dir); ?>
</div>
<h1>WikiBlame</h1><!-- Design by Elian -->
<table>
<tr>
<td>
<label for="lang">
<?php echo $messages['lang'] ?>
</label>
</td>
<td>
<input type="text" name="lang" id="lang" value="<?php echo $lang; ?>"> (<?php echo $messages['lang_example']; ?>)
</td>
</tr>
<tr>
<td>
<label for="project">
<?php echo $messages['project'] ?>
</label>
</td>
<td>
<input type="text" name="project" id="project" value="<?php echo $project; ?>"> (<?php echo $messages['project_example']; ?>)
</td>
</tr>
<tr>
<td>
<label for="tld">
<?php echo $messages['tld'] ?>
</label>
</td>
<td>
<input type="text" name="tld" id="tld" value="<?php echo $tld; ?>"> (<?php echo $messages['tld_example']; ?>)
</td>
</tr>
<tr>
<td>
<label for="article">
<?php echo $messages['article']; ?>
</label>
</td>
<td>
<input type="text" name="article" id="article" value="<?php echo htmlspecialchars($article); ?>" <?php if (!$article) echo 'autofocus'; ?>>
<input type="button" id="from_url" value="<?php echo $messages['from_url']; ?>">
</td>
</tr>
<tr>
<td>
<label for="needle">
<?php echo $messages['needle'] ?>
</label>
</td>
<td>
<input type="text" name="needle" id="needle" value="<?php echo htmlspecialchars($needle); ?>" <?php if ($article) echo 'autofocus'; ?>>
</td>
</tr>
<tr>
<td>
<label for="skipversions">
<?php echo $messages['skipversions'] ?>
</label>
</td>
<td>
<input type="number" name="skipversions" id="skipversions" value="<?php echo $skipversions; ?>">
</td>
</tr>
<tr>
<td>
<label for="ignorefirst">
<?php echo $messages['ignorefirst'] ?>
</label>
</td>
<td>
<input type="number" name="ignorefirst" id="ignorefirst" value="<?php echo $ignorefirst; ?>">
</td>
</tr>
<tr>
<td>
<label for="limit">
<?php echo $messages['limit'] ?>
</label>
</td>
<td>
<input type="number" name="limit" id="limit" value="<?php echo $limit; ?>">
</td>
</tr>
<tr>
<td>
<?php echo $messages['start_date'] ?>
</td>
<td>
<?php datedrop_with_months("", "off", false, 2001, date("Y"), $_REQUEST['offjahr'], $_REQUEST['offmon'], $_REQUEST['offtag'], $the_months, $messages['date_format']); ?>
<input type="button" id="resetdate" value="<?php echo $messages['reset']; ?>">
</td>
<tr>
<td><?php echo $messages['search_method'] ?></td>
<td>
<input type="radio" name="searchmethod" id="linear" value="lin" <?php if ($use_binary_search != true) echo checked; ?>>
<label for="linear">
<?php echo $messages['linear'] ?>
</label>
<input type="radio" name="searchmethod" id="int" value="int" <?php if ($use_binary_search == true) echo checked; ?>>
<label for="int">
<a href="<?php echo $messages['binary_in_wp'] ?>"><?php echo $messages['binary'] ?></a>
</label>
</td>
</tr>
<tr>
<td><?php echo $messages['order'] ?></td>
<td>
<input type="radio" name="order" id="desc" value="desc" <?php if ($asc != true) echo checked; ?>>
<label for="desc">
<?php echo $messages['newest_first'] ?>
</label>
<input type="radio" name="order" id="asc" value="asc" <?php if ($asc == true) echo checked; ?>>
<label for="asc">
<?php echo $messages['oldest_first'] ?>
</label>
</td>
</tr>
<tr class="checkboxrow">
<td>
<input type="checkbox" name="binary_search_inverse" id="binary_search_inverse" <?php if ($binary_search_inverse) echo checked; ?>>
</td>
<td>
<label for="binary_search_inverse">
<?php echo $messages['binary_search_inverse'] ?>
</label>
</td>
</tr>
<tr class="checkboxrow">
<td>
<input type="checkbox" name="ignore_minors" id="ignore_minors" <?php if ($ignore_minors == true) echo checked; ?>>
</td>
<td>
<label for="ignore_minors">
<?php echo $messages['ignore_minors'] ?>
</label>
</td>
</tr>
<tr class="checkboxrow">
<td>
<input type="checkbox" name="force_wikitags" id="force_wikitags" <?php if ($force_wikitags == "on") echo checked; ?>>
</td>
<td>
<label for="force_wikitags">
<?php echo $messages['force_wikitags'] ?>
</label>
</td>
</tr>
<tr id="submitrow">
<td colspan="2">
<input name="start" id="start" type="submit" value="<?php echo $messages['start'] ?>">
<input name="user" id="user" type="hidden" value="<?php echo $user ?>">
</td>
</tr>
</table>
</form>
<hr>
<footer>
<a href='<?php echo $messages['manual_link'] ?>'><?php echo $messages['manual'] ?></a> -
<a href='<?php echo $messages['contact_link'] ?>'><?php echo $messages['contact'] ?></a> -
<a href="https://github.com/FlominatorTM/wikiblame"><?php echo $messages['source_code'] ?></a> -
<a href="https://translatewiki.net/wiki/Translating:WikiBlame"><?php echo $messages['help_translating'] ?></a> -
<a href="https://de.wikipedia.org/wiki/Benutzer:Flominator">by Flominator</a>
</footer>
<?php
if ($needle != "") {
//$needle = needle_regex($needle); necessary if you work with html, which is currently not the case
check_options(); // stops script, when wrong options are used
check_revision_date_format($messages);
if (!$use_binary_search && $_REQUEST['user'] == "") {
check_calls_from_this_ip($limit, $ignorefirst, $skipversions);
}
//@TODO: create a method from this
if ($tags_present) {
$msg = str_replace('_ARTICLELINK_', "<a href=\"https://" . $server . "/wiki/" . $article . "\">$article</a>", $messages['search_in_progress_wikitags']);
} else {
$msg = str_replace('_ARTICLELINK_', "<a href=\"https://" . $server . "/wiki/" . $article . "\">$article</a>", $messages['search_in_progress_text']);
}
$msg = str_replace('_NEEDLE_', htmlspecialchars($needle), $msg);
echo "<div class=\"results\">\n";
echo "$msg<br>\n";
$exec_time = do_search();
echo '<br>' . $exec_time;
echo '<br><br><small>' . get_url($_REQUEST['offjahr'], $_REQUEST['offmon'], $_REQUEST['offtag']) . '</small></div>';
}
function fill_variables($user_lang)
{
global $article, $articleenc, $needle, $lang, $project, $server,
$use_binary_search, $limit, $ignorefirst, $skipversions, $ignore_minors, $offset, $binary_search_inverse, $asc,
$user, $force_wikitags, $tags_present; //todo: probably $tags_present doesn't need to be exposed
$article = str_replace('_', ' ', $_REQUEST['article']);
$articleenc = name_in_url($article);
$needle = trim($_REQUEST['needle']);
$lang = correct_language_mistakes($_REQUEST['lang']);
if ($lang == "") {
$lang = $user_lang;
}
$project = $_REQUEST['project'];
if ($project == "") {
$project = "wikipedia";
}
$tld = $_REQUEST['tld'];
if ($tld == "") {
$tld = "org";
}
if ($lang == "blank") {
$server = "$project.$tld";
} else {
$server = "$lang.$project.$tld";
}
$use_binary_search = true;
if ($_REQUEST['searchmethod'] == "lin") {
$use_binary_search = false;
}
$limit = $_REQUEST['limit'];
if ($limit == "") {
if ($use_binary_search) {
$limit = 500;
} else {
$limit = 50;
}
}
$ignorefirst = $_REQUEST['ignorefirst'];
if ($ignorefirst == "") {
$ignorefirst = 0;
}
$skipversions = $_REQUEST['skipversions'];
if ($skipversions == "") {
$skipversions = 0;
}
$ignore_minors = $_REQUEST['ignore_minors'];
if ($ignore_minors == "on") {
$ignore_minors = true;
} else {
$ignore_minors = false;
}
//Offset = YYYYMMDDmmhhss
$offset = $_REQUEST['offjahr'];
$offset .= str_pad($_REQUEST['offmon'], 2, '0', STR_PAD_LEFT);
$offset .= str_pad($_REQUEST['offtag'], 2, '0', STR_PAD_LEFT);
$offhour = str_pad($_REQUEST['offhour'], 2, '0', STR_PAD_LEFT);
if ($offhour == "00") {
$offhour = 23;
} else {
$offhour = str_pad(Get_UTC_Hours($offhour, $server), 2, '0', STR_PAD_LEFT);
}
$offmin = str_pad($_REQUEST['offmin'], 2, '0', STR_PAD_LEFT);
if ($offmin == "00") {
$offmin = 55;
}
$offset .= $offhour . $offmin . '00';
if (strlen($offset) < 12) {
$offset = strftime("%Y%m%d%H%M%S");
}
if ($_REQUEST['binary_search_inverse'] == "on") {
$binary_search_inverse = true;
} else {
$binary_search_inverse = false;
}
$asc = false;
if ($_REQUEST['order'] == "asc") {
if (!$use_binary_search) {
$asc = true;
}
}
$user = $_REQUEST['user'];
$force_wikitags = $_REQUEST['force_wikitags'];
if ($force_wikitags == "on") {
$tags_present = true;
} else {
$force_wikitags = "off";
$tags_present = wikitags_present();
}
}
function do_search()
{
global $versions, $articleenc, $offset, $needle_ever_found, $binary_search_retries, $use_binary_search, $skipversions, $ignorefirst, $messages;
$beginning = time(); //for benchmarking reasons
$versions = get_all_versions($articleenc, $offset);
log_search();
$needle_ever_found = false;
$binary_search_retries = 4;
if (count($versions) > 0) {
if ($use_binary_search) {
binary_search_from_earliest_index($versions);
} else {
checkversions($versions, $skipversions, $ignorefirst);
}
}
$finished = time() - $beginning;
$exec_time = str_replace('_EXECUTIONTIME_', $finished, $messages['execution_time']);
if (!$needle_ever_found) {
echo "<br>";
echo $messages['not_found_at_all'] . "\n";
$finished .= "_nf";
}
log_search($finished);
return $exec_time;
}
function check_revision_date_format($messages)
{
if (
!stristr($messages['revision_date_format'], '%d') //day
|| !stristr($messages['revision_date_format'], '%B') //monthname
|| !stristr($messages['revision_date_format'], '%Y') //year
|| !stristr($messages['revision_date_format'], '%H') //hour
|| !stristr($messages['revision_date_format'], '%M')
) //minute
{
//fallback to en in case of fucked up format
$messages['revision_date_format'] = "%H:%M, %d %B %Y";
}
}
function get_all_versions($articleenc, $offset)
{
global $limit, $server, $user_lang;
$historyurl = "https://" . $server . "/wiki/" . $articleenc . "?action=history&limit=$limit&offset=$offset&uselang=en"; //$user_lang"
$history = curl_request($historyurl);
//echo "<hr><pre>$history</pre><hr>";
return listversions($history);
}
//takes the requested history page, extracts links to the revisions and puts them into an array that is returned
//in default (meaning $asc!=true) index 0 contains the latest revision
function listversions($history)
{
global $articleenc, $asc, $messages, $ignore_minors, $deleted_revisions;
$searchterm = "name=\"diff\" "; //assumes that the history begins at the first occurrence of name="diff" /> <!--removed />-->
$versionen = array(); //array to store the links in
$deleted_revisions = 0;
$revision_html_blocks = explode($searchterm, $history);
/*
result in $revision_html_blocks are parts of the revision history that look like this (without line wraps)
id="mw-diff-64569839" />
<a href="/w/index.php?title=Hinterzarten&oldid=64569839" title="Hinterzarten">11:27, 16. Sep. 2009</a>
<span class='history-user'>
<a href="/wiki/Benutzer:TXiKiBoT" title="Benutzer:TXiKiBoT" class="mw-userlink">TXiKiBoT</a>
<span class="mw-usertoollinks">(<a href="/wiki/Benutzer_Diskussion:TXiKiBoT" title="Benutzer Diskussion:TXiKiBoT">Diskussion</a> |
<a href="/wiki/Spezial:Beitr%C3%A4ge/TXiKiBoT" title="Spezial:Beiträge/TXiKiBoT">Beiträge</a>)
</span>
</span>
<abbr class="minor" title="Kleine Änderung">K</abbr>
<span class="history-size">(10.740 Bytes)</span>
<span class="comment">(Bot: Ergänze: <a href="https://vi.wikipedia.org/wiki/Hinterzarten" class="extiw" title="vi:Hinterzarten">vi:Hinterzarten</a>)</span> (<span class="mw-history-undo"><a href="/w/index.php?title=Hinterzarten&action=edit&undoafter=64556690&undo=64569839" title="Hinterzarten">entfernen</a></span>) </span> <small><span class='fr-hist-autoreviewed plainlinks'>[<a href="https://de.wikipedia.org/w/index.php?title=Hinterzarten&stableid=64569839" class="external text" rel="nofollow">automatisch gesichtet</a>]</span></small></li> <li><span class='flaggedrevs-color-1'>(<a href="/w/index.php?title=Hinterzarten&diff=64569839&oldid=64556690" title="Hinterzarten">Aktuell</a>) (<a href="/w/index.php?title=Hinterzarten&diff=64556690&oldid=63484457" title="Hinterzarten">Vorherige</a>) <input type="radio" value="64556690" checked="checked" name="oldid" id="mw-oldid-64556690" /><input type="radio" value="64556690"
</li> */
if (count($revision_html_blocks) == 1) {
//only one revision and therefor no diff radio buttons
//simply split at some string that makes the next <a tag the right one
$revision_html_blocks = explode("pagehistory", $history);
}
//iterate over the parts
for ($block_i = 1; $block_i < count($revision_html_blocks); $block_i++) {
//find the beginning of the a tag
$start_pos_of_a = strpos($revision_html_blocks[$block_i], "<a");
//find the closing sequence of the a tag
$pos_of_closed_a = strpos($revision_html_blocks[$block_i], "</a>");
$length_between_both = $pos_of_closed_a - $start_pos_of_a;
$is_minor = stristr($revision_html_blocks[$block_i], '<abbr class="minoredit');
//extract the link from the current part like this one:
$one_version = substr($revision_html_blocks[$block_i], $start_pos_of_a, $length_between_both);
//result: <a href="/w/index.php?title=Hinterzarten&oldid=64569839" title="Hinterzarten">11:27, 16. Sep. 2009
$is_deleted_revision = stristr($one_version, 'mw-userlink'); //there is no revision link
if ($is_deleted_revision) {
$deleted_revisions++;
} else {
if ($ignore_minors) {
if (!$is_minor) {
add_one_version($one_version, $versions);
} else {
//echo "ignored a minor change";
}
} else {
add_one_version($one_version, $versions);
}
}
}
if ($asc == true) {
//echo "reversing the list";
$versions = array_reverse($versions);
}
echo str_replace('_NUMBEROFVERSIONS_', count($versions), $messages['versions_found']) . '<br>';
return $versions;
//regular expression that could be used to extract data from the revision links somewhen
//!oldid=(\d+)".*>([^<]+)</a>.*>([^<]+)</a>! 1=date, 2=revid 3=user
}
function add_one_version($one_version, &$versions)
{
global $server, $the_months, $messages;
//echo "one version: " . htmlspecialchars($one_version);
$offset_parts = extract_date_parts_from_history_link($one_version);
$month = $offset_parts[2];
$day = $offset_parts[1];
$year = $offset_parts[3];
$hour = substr($offset_parts[0], 0, 2);
$minute = substr($offset_parts[0], 3, 2);
$offset = $year . $month . $day . $hour . $minute;
$timestamp = mktime($hour, $minute, 0, $month, $day, $year);
$month_localized = $the_months[$month - 1];
$pattern = str_replace('%B', $month_localized, $messages['revision_date_format']);
$date_localized = strftime($pattern, $timestamp);
$id = idfromurl($one_version);
$versions[] = array(
'offset' => $offset,
'timestamp' => $timestamp,
'id' => $id,
'local_date' => $date_localized,
'checked' => false,
'found' => false
);
}
function checkversions($versions, $skipversions, $ignorefirst)
{
global $server, $needle, $needle_ever_found, $limit, $articleenc, $the_months;
$version_counter = 0;
echo "<ul>";
for ($i = 0; $i < count($versions); $i++) {
echo "<li>" . get_diff_link($versions[$i]);
if ($ignorefirst == 0) {
if ($version_counter == 0) {
if (needle_in_version($needle, $versions, $i)) {
echo " <span class=\"found\">OOO</span>\n";
$needle_ever_found = true;
} else {
echo " <span class=\"notfound\">XXX</span>\n";
}
start_over_here($versions[$i]['offset'], $skipversions);
$version_counter = $skipversions;
} else {
echo " <span class=\"skipped\">???</span>\n";
$version_counter--;
}
} else {
echo " <span class=\"skipped\">???</span>\n";
$ignorefirst--;
}
echo "</li>\n";
}
echo "</ul>";
flush();
}
function idfromurl($url)
{
$pos = strpos($url, "oldid=");
$endpos = strpos($url, "\"", $pos + 6);
$id = substr($url, $pos + 6, $endpos - ($pos + 6));
return $id;
}
function get_revision($id)
{
set_time_limit(60);
global $server, $articleenc, $tags_present;
if ($tags_present) {
$url = "https://$server/w/api.php?format=json&formatversion=2&action=query&prop=revisions&revids=$id&rvprop=content&rvslots=main";
$json = json_decode(curl_request($url), true);
$versionpage = $json['query']['pages'][0]['revisions'][0]['slots']['main']['content'];
} else {
$url = "https://" . $server . "/w/api.php?action=parse&disableeditsection=&formatversion=2&prop=text&oldid=" . $id . "&format=json";
if (stristr($server, "fandom")) {
$url = str_replace("/w/", "/", $url);
}
$json = json_decode(curl_request($url));
$versionpage = strip_tags($json->parse->text);
}
return $versionpage;
}
//generate link to start a new search with the date of this revision
function start_over_here($offset, $skip = 0, $link_text = "")
{
global $messages, $limit;
if ($link_text == "") //poor man's default parameter
{
$link_text = $messages['start_here'];
}
$parts = str_split($offset, 2);
$hour = $parts[4];
$minute = $parts[5];
$day = $parts[3];
$month = $parts[2];
$year = $parts[0] . $parts[1];
$theUrl = get_url($year, $month, $day, $hour, $minute, false);
if ($skip != 0) {
$theUrl = str_replace("limit=$limit", "limit=$skip", $theUrl);
}
echo "<a href=\"" . $theUrl . "\">[" . $link_text . "]</a>";
}
function get_month_number($month_text)
{
$months['January'] = '01';
$months['February'] = '02';
$months['March'] = '03';
$months['April'] = '04';
$months['May'] = '05';
$months['June'] = '06';
$months['July'] = '07';
$months['August'] = '08';
$months['September'] = '09';
$months['October'] = '10';
$months['November'] = '11';
$months['December'] = '12';
return $months[$month_text];
}
// 0: time, 1: day, 2: month, 3: year
function extract_date_parts_from_history_link($versionpage)
{
$ret = false;
// every revision history link contains a text like this:
//<a href="/w/index.php?title=Hinterzarten&oldid=151152765" class="mw-changeslist-date" title="Hinterzarten">17:28, 6 February 2016
$strBegin = ">";
$beginning = strrpos($versionpage, $strBegin);
if ($beginning > 0) //this is not the current revision (which looks different)
{
//extract date from revision text
$strDate = substr($versionpage, $beginning + strlen($strBegin));
$dateParts = explode(' ', trim($strDate));
$dateParts[1] = str_pad($dateParts[1], 2, '0', STR_PAD_LEFT);
$dateParts[2] = get_month_number($dateParts[2]);
$ret = $dateParts;
}
return $ret;
}
function log_search($time = "started")
{
global $article, $needle, $lang, $project, $asc, $use_binary_search, $server, $limit, $skipversions, $get_version_time, $versions, $offset, $user, $user_lang;
$logfile = "log/wikiblame_" . strftime("%Y-%m-%d") . ".csv";
if (!file_exists($logfile)) {
$header = "Day;";
$header .= "Time;";
$header .= "IP (Client);";
$header .= "UI Language;";
$header .= "Needle;";
$header .= "Article;";
$header .= "Language;";
$header .= "Project;";
$header .= "Offset;";
$header .= "Wanted Versions;";
$header .= "Found Versions;";
$header .= "Skipped Versions;";
$header .= "Linear/Interpolated;";
$header .= "User;";
$header .= "Execution-Time;";
$header .= "Get-Version-Time;";
$header .= "User-Agent;";
$header .= "URL;";
}
if ($file = fopen($logfile, "a")) {
fputs($file, $header);
fputs($file, strftime("%Y-%m-%d") . ";");
fputs($file, strftime("%H:%M") . ";");
fputs($file, "\"" . $_SERVER['REMOTE_ADDR'] . "\";");
fputs($file, $user_lang . ";");
fputs($file, "\"" . $needle . "\";");
fputs($file, "\"" . $article . "\";");
fputs($file, $lang . ";");
fputs($file, $project . ";");
fputs($file, "'$offset;");
fputs($file, $limit . ";");
fputs($file, count($versions) . ";");
fputs($file, $skipversions . ";");
if ($use_binary_search) {
fputs($file, "interpolated;");
} else {
fputs($file, "linear;");
}
fputs($file, $time . ";");
fputs($file, $get_version_time . ";");
fputs($file, '"' . $_SERVER['HTTP_USER_AGENT'] . '"' . ";");
fputs($file, str_replace('&', '&', get_url($year, $month, $day, $hour, $min, true)) . ";\n");
fclose($file);
}
}
//translates wiki syntax to html
function needle_regex($needle)
{
$needle = preg_replace('#\'\'\'(.*)\'\'\'#is', '<b>$1</b>', $needle); //bold
$needle = preg_replace('#\'\'(.*)\'\'#is', '<i>$1</i>', $needle); //italic
$needle = str_replace('[[', '<a.*>', $needle); //link
$needle = str_replace(']]', '</a>', $needle); //end of link
$needle = preg_replace('#\n\*([^\n\r]*)#is', '<li>$1</li>', $needle); //list items
return ($needle);
}
function needle_in_version($needle, &$versions, $index)
{
if (!$versions[$index]['checked']) {
$rev_text = get_revision($versions[$index]['id']);
$found = stristr($rev_text, $needle);
$versions[$index]['checked'] = true;
$versions[$index]['found'] = !(!$found); //to have the boolean instead of the text
}
return $versions[$index]['found'];
}
function check_if_found_in_earliest_version($needle, $versions, $earliest_index, $youngest_version = false)
{
global $messages;
//checking first/earliest revision => highest array index
$found_in_earliest_revision = needle_in_version($needle, $versions, $earliest_index);
if ($found_in_earliest_revision) {
$revLink = get_diff_link($versions[$earliest_index]);
if (!$youngest_version) {
//highest array index
$msg = str_replace('__NEEDLE__', '<b>' . htmlspecialchars($needle) . '</b>', $messages['first_version_present']);
} else {
//lowest array index
$msg = str_replace('__NEEDLE__', '<b>' . htmlspecialchars($needle) . '</b>', $messages['latest_version_present']);
}
echo (str_replace('__REVISIONLINK__', $revLink, $msg)) . '<br>';
}
flush();
return $found_in_earliest_revision;
}
function binary_search($middle, $from)
{
global $needle, $versions, $server, $messages, $binary_search_inverse, $binary_search_retries, $needle_ever_found, $limit, $articleenc, $deleted_revisions;
//echo "binary_search(".$middle.",".$from.")";
flush();
if ($middle < 0) {
if ($from != 2 && $from != 1) {
log_search("first_version");
//echo('<br>'.$messages['first_version']);
return;
} else {
$middle = 0;
}
}
$earliest_index = count($versions) - 1;
if ($from == $earliest_index && !$binary_search_inverse) {
if (check_if_found_in_earliest_version($needle, $versions, $earliest_index)) {
if ((count($versions) + $deleted_revisions) == $limit) {
//there might be revisions before
echo $messages['earlier_versions_available'] . ' ';
//start_over_here($versions[$earliest_index]['offset']);
$offset = $versions[$earliest_index]['offset'];
$versions = get_all_versions($articleenc, $offset);
binary_search_from_earliest_index($versions);
}
$needle_ever_found = true;
return; //either the new search was started or it was present in the initial version"
}
}
if ($middle == $from) {
log_search("no_differences");
if ($binary_search_inverse) {
if (check_if_found_in_earliest_version($needle, $versions, 0, true)) {
$needle_ever_found = true;
//searching for removal, brick wall, found in latest version
// return
} else if (check_if_found_in_earliest_version($needle, $versions, $earliest_index)) {
$needle_ever_found = true;
//must have been removed between earliest and where we just checked
$middle = floor($from + ($earliest_index - $from) / 2);
binary_search($middle, $from);
} else {
if ($from > 0) {
//start at a later revision (maybe it was not even inserted at this point of $from)
echo $messages['inverse_restart'] . '<br>';
binary_search(floor($from / 4), floor($from / 2));
} else {
$needle_ever_found = true; //actually it was not, but like this the message "try again" will disappear and the search will be over anyway
echo str_replace('_NUMBEROFVERSIONS_', count($versions), $messages['inverse_stuck']) . '<br>';
start_over_here($versions[$earliest_index]['offset'], 0, $messages['inverse_earliest']);
echo '<br>';
}
}
} else //looking for insertion => maybe we hit an edit war
{
/*
quick and dirty hack to fix this endless loop:
wikiblame.php?user_lang=en&lang=fr&project=wikipedia&article=Modèle%3AMéta+bandeau+d'événement+récent&needle=<u>&skipversions=0&ignorefirst=0&limit=50&offmon=10&offtag=23&offjahr=2017&searchmethod=int&order=desc&force_wikitags=on
*/
if (count($versions) == 1) {
if (check_if_found_in_earliest_version($needle, $versions, 0, false)) {
return;
} else {
$binary_search_retries = 0;
}
}
if ($binary_search_retries > 0) {
if (needle_in_version($needle, $versions, $middle)) {
//was present here already, remove later revisions
clear_array_until_including($versions, $middle + 1); //middle+1 might be the inserting revision
echo str_replace('_NUMBEROFVERSIONS_', count($versions), $messages['versions_found']) . '<br>';
binary_search_from_earliest_index($versions);
} else {
echo $messages['dead_end'] . '<br><br>';
echo $messages['once_more'] . '<br>';
$binary_search_retries--; //this only is applied after the recursion :(
binary_search($middle, $from - $binary_search_retries);
log_search("retry");
}
} else {
echo $messages['binary_enough'];
log_search("enough, done");
}
}
} else {
//echo "Checking differences between ".get_diff_link($middle)." between $middle and ". ($middle+1)." starting from $from : ";
//echo $messages['search_in_progress'];
$test_msg = str_replace('_FIRSTDATEVERSION_', get_diff_link($versions[$middle]), $messages['binary_test']);
$test_msg = str_replace('_FIRSTNUMBER_', $middle, $test_msg);
$test_msg = str_replace('_SECONDNUMBER_', $middle + 1, $test_msg);
$test_msg = str_replace('_SOURCENUMBER_', $from, $test_msg);
echo $test_msg;
/* Revision list looks like this:
[0]: 18. Jan. 2011 21:00 (current revision)
[1]: 18. Jan. 2011 19:00
[2]: 18. Jan. 2011 17:00
[3]: 15. Jan. 2011 15:00 */
$in_right = needle_in_version($needle, $versions, $middle);
$in_left = needle_in_version($needle, $versions, $middle + 1);
$step_length = abs(($from - $middle) / 2);
if ($in_right and $in_left) {
$needle_ever_found = true;
echo "<span class=\"found\">OO</span>\n";
//start_over_here($versions[$middle]['offset'], 0, 0);
echo "<br>";
if ($binary_search_inverse) {
//looking for removal => found in both => must have been removed later => remove the rest
$first_to_remove = $middle + 2; //$middle + 1 was checked and might be needed for output
$count = count($versions);
if ($count > $first_to_remove) {
echo str_replace('_NUMBEROFVERSIONS_', $count - $first_to_remove, $messages['delete_from_here']) . '<br><br>';
clear_array_starting_at($versions, $first_to_remove);
echo str_replace('_NUMBEROFVERSIONS_', count($versions), $messages['versions_found']) . '<br>';
binary_search_from_earliest_index($versions);
} else {
//prevent endless loops in the last four revisions
binary_search(floor($middle - 1), $middle);
}
} else {
//looking for insertion => found in both => must have been added earlier => check earlier versions => higher index in history array
binary_search(floor($middle + $step_length), $middle);
}
} else {
if (!$in_right and !$in_left) {
echo "<span class=\"notfound\">XX</span>\n";
//start_over_here($versions[$middle]['offset']);
echo "<br>";
if ($binary_search_inverse) {
//looking for removal => not found in any of both => must have been removed earlier => higher index in history array
binary_search(floor($middle + $step_length), $middle);
} else {
//looking for insertion => not found in any of both => look later => lower index in history array
binary_search(floor($middle - $step_length), $middle);
}
} else {
$left_version = get_old_link($versions[$middle + 1]);
$right_version = get_old_link($versions[$middle]);
if (!$in_left and $in_right) //XO
{
$needle_ever_found = true;
echo "<span class=\"notfound\">X</span>";
echo "<span class=\"found\">O</span><br>\n";
$insertion_found = str_replace('LEFT_VERSION', $left_version, $messages['insertion_found']);
echo str_replace('RIGHT_VERSION', $right_version, $insertion_found) . ': ';
if ($binary_search_inverse) {
//looking for removal
//was inserted at version "right"
//removal must have happend afterwards = more right = lower indexes
$first_to_remove = $middle + 1;
$count = count($versions);
echo '<br>' . str_replace('_NUMBEROFVERSIONS_', $count - $first_to_remove, $messages['delete_from_here']) . '<br><br>';
clear_array_starting_at($versions, $middle + 1);
echo str_replace('_NUMBEROFVERSIONS_', count($versions), $messages['versions_found']) . '<br>';
binary_search_from_earliest_index($versions);
return; //new search started, therefore we don't continue with this one
}
} else {
$needle_ever_found = true;
echo "<span class=\"found\">O</span>";
echo "<span class=\"notfound\">X</span><br>\n";
$deletion_found = str_replace('LEFT_VERSION', $left_version, $messages['deletion_found']);
echo str_replace('RIGHT_VERSION', $right_version, $deletion_found) . ': ';
if (!$binary_search_inverse) {
//was inserted before it got deleted
clear_array_until_including($versions, $middle + 1); //middle+1 might be the inserting revision
echo str_replace('_NUMBEROFVERSIONS_', count($versions), $messages['versions_found']) . '<br>';
binary_search_from_earliest_index($versions);
return; //new search started, therefore we don't continue with this one
}
}
$difflink = get_diff_link($versions[$middle]);
$end_of_opening_a = strpos($difflink, '>');
echo substr($difflink, 0, $end_of_opening_a + 1) . '<b>' . $messages['here'] . '</b></a>';
echo "<br>";
start_over_here($versions[$middle]['offset']);
}
}
}
}
function clear_array_starting_at(&$versions, $first_to_remove)
{
$end = count($versions);
for ($i = $first_to_remove; $i < $end; $i++) {
unset($versions[$i]);
}
}
function clear_array_until_including(&$versions, $last_to_remove)
{
global $messages;
//forget everything later than this version and do re-indexing
$first_to_remove = 0;
$end = count($versions) - $last_to_remove;
echo "<br>";
echo str_replace('_NUMBEROFVERSIONS_', $last_to_remove - $first_to_remove, $messages['delete_until_here']) . '<br><br>';
for ($i = $first_to_remove; $i < $end; $i++) {
$old_index = $last_to_remove + $i;
$new_index = $i;
//echo "$old_index => $new_index<br>";
$versions[$new_index] = $versions[$old_index];
}
clear_array_starting_at($versions, $end);
}
function binary_search_from_earliest_index($versions)
{
$earliest_index = count($versions);
binary_search(floor($earliest_index / 2), $earliest_index - 1);
}
function get_diff_link($version, $order = "prev")
{
global $server, $articleenc;
$link = 'https://' . $server . '/w/index.php?title=' . $articleenc . '&diff=prev&oldid=' . $version['id'];
return '<a href="' . $link . '">' . $version['local_date'] . '</a>';
}
function get_old_link($version)
{
global $server, $articleenc;
$link = 'https://' . $server . '/w/index.php?title=' . $articleenc . '&oldid=' . $version['id'];
return '<a href="' . $link . '">' . $version['local_date'] . '</a>';
}
function check_options()
{
global $skipversions, $limit, $ignorefirst, $messages;
$msg = str_replace('__VERSIONSTOSEARCH__', $limit, $messages['wrong_skips']);
if ($skipversions >= $limit) {
$msg = str_replace('__VERSIONSTOSKIP__', $skipversions, $msg);
echo '<div class="inputerror" data-fieldid="skipversions">' . $msg . '</div>';
die();
}
if ($ignorefirst >= $limit) {
$msg = str_replace('__VERSIONSTOSKIP__', $ignorefirst, $msg);
echo '<div class="inputerror" data-fieldid="ignorefirst">' . $msg . '</div>';