Wiki-Quellcode von Missing Attachments
Version 1.1 von Holger Engels am 2026/11/03 09:50
Verstecke letzte Bearbeiter
| author | version | line-number | content |
|---|---|---|---|
![]() |
1.1 | 1 | {{groovy}} |
| 2 | import org.xwiki.store.filesystem.internal.FilesystemStoreTools | ||
| 3 | |||
| 4 | // ========================================== | ||
| 5 | // EINSTELLUNGEN: | ||
| 6 | // ========================================== | ||
| 7 | def dryRun = true // ZUERST auf true lassen zum Testen! Auf false setzen zum Ausführen. | ||
| 8 | def action = "create_dummy" // "create_dummy" (empfohlen) ODER "delete_from_doc" | ||
| 9 | // ========================================== | ||
| 10 | |||
| 11 | def fsAttachmentTool = services.component.getInstance(FilesystemStoreTools) | ||
| 12 | def currentWiki = xcontext.getDatabase() | ||
| 13 | def count = 0 | ||
| 14 | |||
| 15 | println "=== Automatische Prüfung fehlender Dateisystem-Anhänge ===" | ||
| 16 | println "Modus: **${action}** (Testlauf / dryRun: **${dryRun}**)\n" | ||
| 17 | println "|-Wiki|-Dokument|-Dateiname|-Aktion / Status" | ||
| 18 | |||
| 19 | for (def wiki in services.wiki.getAllIds()) { | ||
| 20 | xcontext.setDatabase(wiki) | ||
| 21 | |||
| 22 | // Alle Anhänge ermitteln, die laut DB im Dateisystem liegen müssen | ||
| 23 | def query = "select doc.fullName, attachment.filename from XWikiDocument doc, XWikiAttachment attachment where doc.id=attachment.docId and attachment.contentStore='file'" | ||
| 24 | def attachmentReferences = services.query.hql(query).execute() | ||
| 25 | |||
| 26 | for (def ref in attachmentReferences) { | ||
| 27 | def docFullName = ref[0] | ||
| 28 | def attachmentName = ref[1] | ||
| 29 | def attachmentStringRef = "${docFullName}@${attachmentName}" | ||
| 30 | |||
| 31 | try { | ||
| 32 | def attachmentRef = services.model.resolveAttachment(attachmentStringRef) | ||
| 33 | def attachmentFile = fsAttachmentTool.getAttachmentFileProvider(attachmentRef).getAttachmentContentBlob() | ||
| 34 | |||
| 35 | // Wenn die Datei auf der Festplatte fehlt: | ||
| 36 | if (!attachmentFile.exists()) { | ||
| 37 | count++ | ||
| 38 | def status = "Fehlt auf Platte" | ||
| 39 | |||
| 40 | if (!dryRun) { | ||
| 41 | if (action == "create_dummy") { | ||
| 42 | attachmentFile.getParentFile().mkdirs() | ||
| 43 | if (attachmentFile.createNewFile()) { | ||
| 44 | status = "Dummy (0 Bytes) erzeugt" | ||
| 45 | } else { | ||
| 46 | status = "Fehler beim Erzeugen" | ||
| 47 | } | ||
| 48 | } else if (action == "delete_from_doc") { | ||
| 49 | def doc = xwiki.getDocument(docFullName) | ||
| 50 | def att = doc.getAttachment(attachmentName) | ||
| 51 | if (att) { | ||
| 52 | doc.getAttachmentList().remove(att) | ||
| 53 | doc.save("Fehlenden Anhang automatisch entfernt", true) | ||
| 54 | status = "Aus Dokument ausgetragen" | ||
| 55 | } | ||
| 56 | } | ||
| 57 | } else { | ||
| 58 | status = "[Dry-Run] Würde repariert werden" | ||
| 59 | } | ||
| 60 | |||
| 61 | println "|${wiki}|[[${docFullName}]]|{{{${attachmentName}}}}|${status}" | ||
| 62 | } | ||
| 63 | } catch (Exception e) { | ||
| 64 | println "|${wiki}|${docFullName}|${attachmentName}|Fehler: ${e.message}" | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | // Kontext zurücksetzen | ||
| 70 | xcontext.setDatabase(currentWiki) | ||
| 71 | |||
| 72 | println "" | ||
| 73 | println "**Fertig! Insgesamt ${count} fehlende Dateien gefunden.**" | ||
| 74 | {{/groovy}} | ||
| 75 |
