-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpublish.php
More file actions
187 lines (158 loc) · 5.4 KB
/
Copy pathpublish.php
File metadata and controls
187 lines (158 loc) · 5.4 KB
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
<?php
function apiSendAndReceive($apiData, $WIKIPEDIA_API_URL, &$cookieJar) {
$apiData['format'] = 'json'; // always get return data in JSON format
if ( ! isset($apiData['action']) ) {
throw new Error('Action is required.');
}
$ch = curl_init();
// always POST, never GET. 1) login requires POST. 2) edit -> text can be too long for GET
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiData));
$url = $WIKIPEDIA_API_URL;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // don't echo the output
curl_setopt($ch, CURLOPT_URL, $url); // set the URL
curl_setopt($ch, CURLOPT_USERAGENT, "[[w:User:Novem Linguae]]'s publish.php script. Concatenates .js files together and writes them to .js pages onwiki."); // set user agent
// Use in-memory cookie handling
if (!empty($cookieJar)) {
curl_setopt($ch, CURLOPT_COOKIE, $cookieJar);
}
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($curl, $header) use (&$cookieJar) {
if (preg_match('/^Set-Cookie:\s*(.*?);/i', $header, $matches)) {
$cookieJar .= $matches[1] . '; ';
}
return strlen($header);
});
$result = curl_exec($ch);
curl_close($ch);
$resultJson = json_decode($result, true);
if ( !$resultJson || isset($resultJson['error']) ) {
throw new Error('API returned an error message. ' . var_export($result, true));
}
return $resultJson;
}
function deleteImportStatements($str) {
return preg_replace('/import .*\n/m', '', $str);
}
function deleteExportStatements($str) {
return preg_replace('/^export /m', '', $str);
}
function deleteNoWikiTags($str) {
$str = preg_replace('/<nowiki>/', '', $str);
$str = preg_replace('/<\/nowiki>/', '', $str);
return $str;
}
function deleteRequireFunctions($str) {
return preg_replace('/^.*require\(.*$\n/m', '', $str);
}
function writeWikitextToWikipedia($WIKIPEDIA_API_URL, $WIKIPEDIA_USERNAME, $WIKIPEDIA_PASSWORD) {
$cookieJar = '';
// get login token
$apiData = [
'action' => 'query',
'meta' => 'tokens',
'type' => 'login',
];
$loginToken = apiSendAndReceive($apiData, $WIKIPEDIA_API_URL, $cookieJar)['query']['tokens']['logintoken'];
// log in to Wikipedia using bot key
$apiData = [
'action' => 'login',
'lgname' => $WIKIPEDIA_USERNAME,
'lgpassword' => $WIKIPEDIA_PASSWORD,
'lgtoken' => $loginToken,
];
$result = apiSendAndReceive($apiData, $WIKIPEDIA_API_URL, $cookieJar);
// get edit token
$apiData = [
'action' => 'query',
'meta' => 'tokens',
'type' => 'csrf',
];
$csrfToken = apiSendAndReceive($apiData, $WIKIPEDIA_API_URL, $cookieJar)['query']['tokens']['csrftoken'];
// make edit
$apiData = [
'action' => 'edit',
'title' => $_POST['pageTitle'],
'text' => $_POST['wikitext'],
'summary' => $_POST['editSummary'] . " (publish.php)",
'token' => $csrfToken,
];
$result = apiSendAndReceive($apiData, $WIKIPEDIA_API_URL, $cookieJar);
// Redirect to the diff page of the successful edit
if (isset($result['edit']['newrevid'])) {
$revisionId = $result['edit']['newrevid'];
$title = urlencode($_POST['pageTitle']);
header("Location: https://en.wikipedia.org/w/index.php?title=$title&diff=prev&oldid=$revisionId");
} else {
echo "<h1>Edit attempted, but onwiki code already matches the code we just attempted to write</h1>";
}
}
function generateWikitext($MAIN_FILE_PATH, $CLASSES_FOLDER_PATH) {
$wikitext = "// === Compiled with Novem Linguae's publish.php script ======================";
$files = scandir($CLASSES_FOLDER_PATH);
foreach ( $files as $fileName ) {
if ( $fileName === '.' ) {
continue;
}
if ( $fileName === '..' ) {
continue;
}
$path = $CLASSES_FOLDER_PATH . $fileName;
$classText = file_get_contents($path);
$wikitext .= "\n\n// === $path ======================================================\n\n" . $classText;
}
$wikitext .= "$(async function() {\n\n// === $MAIN_FILE_PATH ======================================================\n\n";
$wikitext .= file_get_contents($MAIN_FILE_PATH);
$wikitext .= "\n\n});";
$wikitext = deleteNoWikiTags($wikitext);
$wikitext = deleteImportStatements($wikitext);
$wikitext = deleteExportStatements($wikitext);
$wikitext = deleteRequireFunctions($wikitext);
$wikitext = "// <nowiki>\n\n" . $wikitext . "\n\n// </nowiki>";
return $wikitext;
}
ini_set('display_errors', 1);
error_reporting(E_ALL);
require_once('publish.config.php');
$formIsSubmitted = $_POST['submit'] ?? '';
if ( $formIsSubmitted ) {
$ABSOLUTE_PATH_TO_TEMP_DIRECTORY = sys_get_temp_dir();
writeWikitextToWikipedia($WIKIPEDIA_API_URL, $WIKIPEDIA_USERNAME, $WIKIPEDIA_PASSWORD);
die;
}
$wikitext = generateWikitext($MAIN_FILE_PATH, $CLASSES_FOLDER_PATH);
?>
<html>
<head>
<style>
textarea { width:800px; height:20em; }
input[type="text"] { width:800px; }
</style>
</head>
<body>
<form method="post">
<p>
Page Title:<br />
<input type="text" name="pageTitle" value="<?php echo htmlentities($PAGE_TITLE); ?>" />
</p>
<p>
Wikitext:<br />
<textarea name="wikitext"><?php echo htmlentities($wikitext); ?></textarea>
</p>
<p>
Edit Summary:<br />
<input type="text" name="editSummary" value="" />
</p>
<p>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
</body>
<script>
document.querySelector('[name="editSummary"]').focus();
document.addEventListener('keydown', async function(event) {
if ( event.altKey && event.shiftKey && event.key === 'S' ) {
document.querySelector('[name="submit"]').click();
}
});
</script>
</html>