-
Notifications
You must be signed in to change notification settings - Fork 24
/
gadgetapi.php
67 lines (55 loc) · 2.29 KB
/
gadgetapi.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
<?php
declare(strict_types=1);
// https://en.wikipedia.org/wiki/MediaWiki:Gadget-citations.js
set_time_limit(120);
try {
@header('Access-Control-Allow-Origin: *'); // Needed for gadget to work right
@header('Content-Type: text/json');
//Set up tool requirements
require_once 'setup.php';
if (!is_string(@$_POST['text']) || !is_string(@$_POST['summary'])) {
throw new Exception('not a string'); // @codeCoverageIgnore
}
$originalText = $_POST['text'];
$editSummary = $_POST['summary'];
unset($_GET, $_POST, $_REQUEST); // Memory minimize
if (strlen($originalText) < 6) {
throw new Exception('tiny page'); // @codeCoverageIgnore
} elseif (strlen($originalText) > 100000) { // will probably time-out otherwise, see https://en.wikipedia.org/wiki/Special:LongPages
throw new Exception('bogus huge page'); // @codeCoverageIgnore
} elseif (strlen($editSummary) > 5000) { // see https://en.wikipedia.org/wiki/Help:Edit_summary#The_500-character_limit
throw new Exception('bogus summary'); // @codeCoverageIgnore
}
//Expand text from postvars
$page = new Page();
ob_start(); // For some reason this is needed sometimes
$page->parse_text($originalText);
$page->expand_text();
ob_end_clean();
$newText = $page->parsed_text();
if ($newText === "") {
throw new Exception('text lost'); // @codeCoverageIgnore
}
//Modify edit summary to identify bot-assisted edits
if ($newText !== $originalText) {
if ($editSummary) {
$editSummary .= ' | '; // Add pipe if already something there.
}
$editSummary .= str_replace('Use this bot', 'Use this tool', $page->edit_summary()) . '| #UCB_Gadget ';
}
unset($originalText, $page);
/**
* @psalm-taint-escape html
* @psalm-taint-escape has_quotes
*/
$result = ['expandedtext' => $newText, 'editsummary' => $editSummary];
unset($newText, $editSummary);
ob_end_clean();
echo (string) @json_encode($result);
} catch (Throwable $e) { // @codeCoverageIgnore
@ob_end_clean(); // @codeCoverageIgnore
@ob_end_clean(); // @codeCoverageIgnore
@ob_end_clean(); // @codeCoverageIgnore
// Above is paranoid panic code. So paranoid that we even empty buffers two extra times
}
?>