-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAttachFiles.php
419 lines (378 loc) · 19.9 KB
/
AttachFiles.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
<?php
use MediaWiki\MediaWikiServices;
use Wikimedia\Mime\MimeMap;
class AttachFiles {
private static $fileTypes = [];
private static function initializeFileTypes() {
if (empty(self::$fileTypes)) {
self::$fileTypes["attachfiles-pdf-document"] = [];
self::$fileTypes["attachfiles-text-document"] = [];
self::$fileTypes["attachfiles-presentation"] = [];
self::$fileTypes["attachfiles-spreadsheet"] = [];
self::$fileTypes["attachfiles-document"] = [];
foreach (MimeMap::MEDIA_TYPES[MEDIATYPE_OFFICE] as $mimeType) {
if ($mimeType === "application/pdf" || $mimeType === "application/acrobat" || strpos($mimeType, "djvu") !== false) {
self::$fileTypes["attachfiles-pdf-document"][] = $mimeType;
} else if (strpos($mimeType, "word") !== false || strpos($mimeType, "text") !== false) {
self::$fileTypes["attachfiles-text-document"][] = $mimeType;
} else if (strpos($mimeType, "powerpoint") !== false || strpos($mimeType, "presentation") !== false) {
self::$fileTypes["attachfiles-presentation"][] = $mimeType;
} else if (strpos($mimeType, "excel") !== false || strpos($mimeType, "spreadsheet") !== false) {
self::$fileTypes["attachfiles-spreadsheet"][] = $mimeType;
} else {
self::$fileTypes["attachfiles-document"][] = $mimeType;
}
}
self::$fileTypes["attachfiles-text-document"][] = "text/plain";
self::$fileTypes["attachfiles-text-document"][] = "text";
self::$fileTypes["attachfiles-spreadsheet"][] = "text/csv";
self::$fileTypes["attachfiles-spreadsheet"][] = "text/tab-separated-values";
self::$fileTypes["attachfiles-image"] = array_merge(MimeMap::MEDIA_TYPES[MEDIATYPE_BITMAP], MimeMap::MEDIA_TYPES[MEDIATYPE_DRAWING]);
self::$fileTypes["attachfiles-audio-file"] = MimeMap::MEDIA_TYPES[MEDIATYPE_AUDIO];
// MediaWiki detects ogg/opus files always as application/ogg, regardless of whether they were actually audio or video.
// As a result, we made the decision to classify all ogg/opus files as audio, unfortunately.
self::$fileTypes["attachfiles-audio-file"][] = "application/ogg";
self::$fileTypes["attachfiles-video-file"] = MimeMap::MEDIA_TYPES[MEDIATYPE_VIDEO];
self::$fileTypes["attachfiles-archive-file"] = MimeMap::MEDIA_TYPES[MEDIATYPE_ARCHIVE];
}
}
private static function getFileType($file) {
// Initialize file type mapping.
self::initializeFileTypes();
$mimeType = $file->getMimeType();
// TODO: should we enable this?
// $mimeType = MimeMap::MIME_TYPE_ALIASES[$mimeType] ?? $mimeType;
foreach (self::$fileTypes as $fileType => $mimeTypes) {
if (in_array($mimeType, $mimeTypes)) {
return $fileType;
}
}
return "attachfiles-file";
}
private static function formatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, $precision) . ' ' . $units[$pow];
}
private static function generateJavaScript($context, $id, $namespace, $attachedFiles) {
// MediaWiki uses underscores instead of spaces for links.
$attachedFilesLink = str_replace(" ", "_", $attachedFiles);
$maxUploadSize = min(UploadBase::getMaxUploadSize("file"), UploadBase::getMaxPhpUploadSize());
$maxUploadSizeNice = self::formatBytes($maxUploadSize);
$deleteConfirm = $context->msg("attachfiles-delete-confirm");
$deleteReason = $context->msg("attachfiles-delete-reason");
// Custom message here because the default MediaWiki one refers to scary groups.
$deletePermissionError = $context->msg("attachfiles-delete-permission-error");
$uploadComment = $context->msg("attachfiles-upload-comment");
$uploadFileSizeError = $context->msg("attachfiles-upload-file-size-error");
$uploadInvalidNameError = $context->msg("attachfiles-upload-invalid-name-error");
$uploadExistsError = $context->msg("attachfiles-upload-exists-error");
// Custom message here because the default MediaWiki one refers to scary groups.
$uploadPermissionError = $context->msg("attachfiles-upload-permission-error");
return <<<EOD
function deleteFile(articleID, name) {
if (!confirm(`$deleteConfirm`)) {
return;
}
(new mw.Api()).postWithEditToken({
formatversion: 2,
action: "delete",
pageid: articleID,
reason: `$deleteReason`,
uselang: mw.config.get("wgUserLanguage"),
errorformat: "plaintext"
}).then(function(result) {
window.location.reload();
}).catch(function(code, details) {
if (code === "http") {
alert("HTTP error: " + details.exception);
} else if (code === "permissiondenied") {
alert(`$deletePermissionError`);
} else {
alert(details.errors[0].text);
}
});
}
function uploadFile() {
var form = document.getElementById("attachfiles_form");
var uploadButton = document.getElementById("attachfiles_upload");
uploadButton.disabled = true;
var maxUploadSize = $maxUploadSize;
var file = document.getElementById("attachfiles_file").files[0];
if (file.size > maxUploadSize) {
maxUploadSize = "$maxUploadSizeNice";
alert(`$uploadFileSizeError`);
uploadButton.disabled = false;
return;
}
var fileExtension = file.name.substring(file.name.lastIndexOf("."));
var displayName = document.getElementById("attachfiles_name").value;
var name = $id + "_" + displayName + fileExtension;
// First request: we don't ignore warnings now because we want to handle some of them.
(new mw.Api()).postWithEditToken({
formatversion: 2,
action: "upload",
file: file,
filename: name,
comment: `$uploadComment`,
uselang: mw.config.get("wgUserLanguage"),
errorformat: "plaintext",
attachfiles_id: $id,
attachfiles_displayname: displayName,
attachfiles_namespace: $namespace
}, {
contentType: "multipart/form-data",
timeout: 0
}).then(function(result) {
if (!result.upload.warnings) {
form.reset();
window.location.reload();
return;
}
if (result.upload.warnings.badfilename) {
// We don't allow bad file names.
alert(`$uploadInvalidNameError`);
uploadButton.disabled = false;
} else if (result.upload.warnings.exists) {
// We don't allow duplicate file names because this would overwrite the existing file.
alert(`$uploadExistsError`);
uploadButton.disabled = false;
} else {
fileKeyExtension = result.upload.filekey.substring(result.upload.filekey.lastIndexOf("."));
// Server modified the file.
if (fileKeyExtension !== fileExtension) {
name = $id + "_" + displayName + fileKeyExtension;
}
// Other warnings can be ignored at this time.
(new mw.Api()).postWithEditToken({
formatversion: 2,
action: "upload",
filekey: result.upload.filekey,
filename: name,
comment: `$uploadComment`,
ignorewarnings: true,
uselang: mw.config.get("wgUserLanguage"),
errorformat: "plaintext",
attachfiles_id: $id,
attachfiles_displayname: displayName,
attachfiles_namespace: $namespace
}, {
contentType: "multipart/form-data",
timeout: 0
}).then(function(result) {
form.reset();
window.location.reload();
}).catch(function(code, details) {
uploadButton.disabled = false;
if (code === "http") {
alert("HTTP error: " + details.exception);
} else if (code === "permissiondenied") {
alert(`$uploadPermissionError`);
} else {
alert(details.errors[0].text);
}
});
}
}).catch(function(code, details) {
uploadButton.disabled = false;
if (code === "http") {
alert("HTTP error: " + details.exception);
} else if (code === "permissiondenied") {
alert(`$uploadPermissionError`);
} else {
alert(details.errors[0].text);
}
});
}
var toc = document.getElementById("toc");
if (toc) {
var ul = toc.getElementsByTagName("ul")[0];
var tocnumber = ul.getElementsByClassName("toclevel-1").length + 1;
var li = document.createElement("li");
li.setAttribute("class", `toclevel-1 tocsection-\${tocnumber}`);
li.innerHTML = `<a href="#$attachedFilesLink"><span class="tocnumber">\${tocnumber}</span> <span class="toctext">$attachedFiles</span></a>`;
ul.appendChild(li);
}
EOD;
}
public static function onAPIGetAllowedParams($module, &$params, $flags) {
if ($module instanceof ApiMain) {
$params["attachfiles_id"] = [ApiBase::PARAM_TYPE => "integer"];
$params["attachfiles_displayname"] = [ApiBase::PARAM_TYPE => "string"];
$params["attachfiles_namespace"] = [ApiBase::PARAM_TYPE => "integer"];
}
}
public static function onLoadExtensionSchemaUpdates($updater) {
$dir = __DIR__ . "/sql";
$updater->addExtensionTable("attachfiles_attached", "$dir/attached.sql");
return true;
}
public static function onBeforePageDisplay($out, $skin) {
global $wgContentNamespaces;
global $wgAFIgnoredPages;
global $wgExtensionAssetsPath;
$title = $out->getTitle();
$id = $title->getArticleID();
$namespace = $title->getNamespace();
$context = RequestContext::getMain();
$action = Action::getActionName($context);
$user = $context->getUser();
$canDelete = $user->isAllowedAny("delete");
$canUpload = $user->isAllowedAny("upload");
if ($id > 0 && !in_array($title->getPrefixedText(), $wgAFIgnoredPages ?? []) && in_array($namespace, $wgContentNamespaces) && $action === "view") {
$attachedFiles = $context->msg("attachfiles-attached-files");
$out->addInlineScript(self::generateJavaScript($context, $id, $namespace, $attachedFiles));
$out->addWikiTextAsInterface("== $attachedFiles ==");
$services = MediaWikiServices::getInstance();
$lb = $services->getDBLoadBalancer();
$dbr = $lb->getConnectionRef(DB_REPLICA);
$res = $dbr->select("attachfiles_attached", ["filename", "displayname"], ["pageid" => $id], __METHOD__, []);
if ($res->numRows() > 0) {
$table = "<table class=\"wikitable sortable\">\n";
$table .= "<tbody>\n";
$table .= "<tr>";
$table .= "<th>" . $context->msg("attachfiles-file") . "</th>";
$table .= "<th>" . $context->msg("attachfiles-file-type") . "</th>";
$table .= "<th>" . $context->msg("attachfiles-upload-date") . "</th>";
$table .= "<th>" . $context->msg("attachfiles-uploader") . "</th>";
if ($canDelete) {
$table .= "<th class=\"unsortable\">" . $context->msg("attachfiles-delete") . "</th>";
}
$table .= "</tr>\n";
$tableRows = [];
foreach ($res as $row) {
$fileName = $row->filename;
$downloadName = substr($fileName, strpos($fileName, "_") + 1);
$file = $services->getRepoGroup()->findFile($fileName);
$fileType = self::getFileType($file);
$url = htmlentities($file->getUrl());
$displayName = htmlentities($row->displayname);
$timestamp = $file->getTimestamp();
if (method_exists($file, "getUploader")) {
$uploader = $file->getUploader();
$uploaderId = $uploader->getId();
$uploaderName = $uploader->getName();
} else {
$uploaderId = $file->getUser("id");
$uploaderName = $file->getUser("text");
}
$tableRow = "<tr>";
$tableRow .= "<td><img src=\"" . $wgExtensionAssetsPath . "/AttachFiles/icons/$fileType.png\" style=\"padding-right: 0.3em\"><a href=\"$url\" download=\"$downloadName\">$displayName</a></td>";
$tableRow .= "<td>" . $context->msg($fileType) . "</td>";
$tableRow .= "<td data-sort-value=\"$timestamp\">" . $context->getLanguage()->date($timestamp, true) . "</td>";
$tableRow .= "<td>" . Linker::userLink($uploaderId, $uploaderName) . "</td>";
if ($canDelete) {
$articleID = $file->getTitle()->getArticleID();
$tableRow .= "<td><a href=\"javascript:deleteFile($articleID, `$displayName`);\">" . $context->msg("attachfiles-delete") . "</a></td>";
}
$tableRow .= "</tr>\n";
$tableRows[$tableRow] = $timestamp;
}
// Make sure the rows in the table are sorted by ascending timestamp.
arsort($tableRows);
foreach ($tableRows as $row => $timestamp) {
$table .= $row;
}
$table .= "</tbody>\n";
$table .= "</table>\n";
$out->addHTML($table);
} else {
$out->addWikiMsg("attachfiles-no-attached-files");
}
if ($canUpload) {
$fileNamePlaceholder = $context->msg("attachfiles-file-name-placeholder");
$uploadSubmitText = $context->msg("attachfiles-upload");
$uploadForm = "<form id=\"attachfiles_form\" onsubmit=\"uploadFile(); return false;\">";
$uploadForm .= "<p style=\"line-height: 2.3em\">";
$uploadForm .= "<input type=\"file\" id=\"attachfiles_file\" required style=\"width: 25.5em\">";
$uploadForm .= "<br>";
$uploadForm .= "<input type=\"text\" id=\"attachfiles_name\" placeholder=\"$fileNamePlaceholder\" required style=\"width: 20em; margin-right: 0.5em\">";
$uploadForm .= "<input type=\"submit\" value=\"$uploadSubmitText\" id=\"attachfiles_upload\">";
$uploadForm .= "</p>";
$uploadForm .= "</form>\n";
$out->addHTML($uploadForm);
} else if (!$user->isRegistered()) {
$out->addWikiMsg("attachfiles-no-upload-permissions");
}
}
}
public static function onContentAlterParserOutput($content, $title, $parserOutput) {
global $wgContentNamespaces;
global $wgAFIgnoredPages;
$id = $title->getArticleID();
$namespace = $title->getNamespace();
if ($id > 0 && !in_array($title->getPrefixedText(), $wgAFIgnoredPages ?? []) && in_array($namespace, $wgContentNamespaces)) {
$services = MediaWikiServices::getInstance();
$lb = $services->getDBLoadBalancer();
$dbr = $lb->getConnectionRef(DB_REPLICA);
$res = $dbr->select("attachfiles_attached", ["filename"], ["pageid" => $id], __METHOD__, []);
foreach ($res as $row) {
// Add to the images of the page (prevents MediaWiki from deleting it from the imagelinks table).
$fileName = $row->filename;
$parserOutput->addImage($fileName);
}
}
}
public static function onUploadComplete($uploadBase) {
$context = RequestContext::getMain();
$id = $context->getRequest()->getIntOrNull("attachfiles_id");
$displayName = $context->getRequest()->getText("attachfiles_displayname");
$namespace = $context->getRequest()->getIntOrNull("attachfiles_namespace");
if (!isset($id) || !isset($displayName) || !isset($namespace)) {
// User uploaded from unkown source, so we ignore it.
return;
}
$file = $uploadBase->getLocalFile();
$services = MediaWikiServices::getInstance();
$lb = $services->getDBLoadBalancer();
$dbr = $lb->getConnectionRef(DB_PRIMARY);
$dbr->insert("attachfiles_attached", ["pageid" => $id, "filename" => $file->getName(), "displayname" => $displayName], __METHOD__, ["IGNORE"]);
$dbr->insert("imagelinks", ["il_from" => $id, "il_from_namespace" => $namespace, "il_to" => $file->getName()], __METHOD__, ["IGNORE"]);
}
public static function onFileDeleteComplete($file, $oldimage, $article, $user, $reason) {
$services = MediaWikiServices::getInstance();
$lb = $services->getDBLoadBalancer();
$dbr = $lb->getConnectionRef(DB_PRIMARY);
$dbr->delete("attachfiles_attached", ["filename" => $file->getName()]);
$dbr->delete("imagelinks", ["il_to" => $file->getName()]);
}
public static function onArticleDeleteComplete(&$article, &$user, $reason, $id, $content, $logEntry, $archivedRevisionCount) {
global $wgContentNamespaces;
global $wgAFIgnoredPages;
$title = $article->getTitle();
$namespace = $title->getNamespace();
if ($id > 0 && !in_array($title->getPrefixedText(), $wgAFIgnoredPages ?? []) && in_array($namespace, $wgContentNamespaces)) {
$context = RequestContext::getMain();
$deleteReason = $context->msg("attachfiles-delete-reason");
$services = MediaWikiServices::getInstance();
$lb = $services->getDBLoadBalancer();
$dbr = $lb->getConnectionRef(DB_REPLICA);
$res = $dbr->select("attachfiles_attached", ["filename"], ["pageid" => $id], __METHOD__, []);
foreach ($res as $row) {
$fileName = $row->filename;
$file = $services->getRepoGroup()->findFile($fileName);
$title = $file->getTitle();
$oldimage = null;
FileDeleteForm::doDelete($title, $file, $oldimage, $deleteReason, false, $user, []);
}
}
}
public static function onPageMoveCompleting($old, $new, $userIdentity, $pageid, $redirid, $reason, $revision) {
if ($old->inNamespace(NS_FILE)) {
$services = MediaWikiServices::getInstance();
$lb = $services->getDBLoadBalancer();
$dbr = $lb->getConnectionRef(DB_PRIMARY);
if ($new->inNamespace(NS_FILE)) {
$dbr->update("attachfiles_attached", ["filename" => $new->getDBkey()], ["filename" => $old->getDBkey()]);
$dbr->update("imagelinks", ["il_to" => $new->getDBkey()], ["il_to" => $old->getDBkey()]);
} else {
// Is it even possible to move a file page to a non-file page?
$dbr->delete("attachfiles_attached", ["filename" => $old->getDBkey()]);
$dbr->delete("imagelinks", ["il_to" => $old->getDBkey()]);
}
}
}
}