Check missing filesystem attachments

Last modified by Camille Desmots on 2022/09/05 00:14

cogCheck filesystem attachments that are missing from disk and can not be downloaded
TypeXAR
CategoryApplication
Developed by

Eduard Moraru

Active Installs1
Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Installable with the Extension Manager

Description

In some rare cases, some page attachments can be displayed on a wiki page, but when trying to view or download them, you get an error that they can not be found. For some reason, the files were deleted from the filesystem, but the metadata is still present on the database.

This application helps you identify such compromised attachments on the main wiki and all subwikis.

checkMissingFilesystemAttachments.png

Programming rights are needed to run this app.

Source

In case you just want this as a snippet and can't/won't install it with Extension Manager or with XAR import, you can examine the source below:

Filesystem attachments that are still reported as existing in the wiki pages (i.e. with metadata still present on the database), but that are actually missing (i.e. somehow got deleted from disk). All subwikis are checked.

{{groovy}}
def fsAttachmentTool = services.component.getInstance(org.xwiki.store.filesystem.internal.FilesystemStoreTools);
def currentWiki = xcontext.getDatabase();
def count = 0;
println "|=Attachment File Name|=Document|=Wiki|=Attachment file Path"
for (def wiki in services.wiki.getAllIds()) {
 // Chance the context wiki.
 xcontext.setDatabase(wiki);

 def attachmentReferences = services.query.hql("select doc.fullName, attachment.filename from XWikiDocument doc, XWikiAttachment attachment where doc.id=attachment.docId and attachment.contentStore='file'").execute();

 for (def ref in attachmentReferences) {
   def docFullName = ref[0];
   def attachmentName = ref[1];
   def attachmentStringRef = "${docFullName}@${attachmentName}"
   def attachmentRef = services.model.resolveAttachment(attachmentStringRef)
   def attachmentFile = fsAttachmentTool.getAttachmentFileProvider(attachmentRef).getAttachmentContentFile();
   if (!attachmentFile.exists()) {
     println "|{{{${attachmentName}}}}|[[${docFullName}]]|${wiki}|{{{${attachmentFile}}}}"
     count++;
    }
  }
}
// Restore the context wiki.
xcontext.setDatabase(currentWiki);

println ""
println "Total: **${count}**";
{{/groovy}}

Prerequisites & Installation Instructions

We recommend using the Extension Manager to install this extension (Make sure that the text "Installable with the Extension Manager" is displayed at the top right location on this page to know if this extension can be installed with the Extension Manager). Note that installing Extensions when being offline is currently not supported and you'd need to use some complex manual method.

You can also use the following manual method, which is useful if this extension cannot be installed with the Extension Manager or if you're using an old version of XWiki that doesn't have the Extension Manager:

  1. Log in the wiki with a user having Administration rights
  2. Go to the Administration page and select the Import category
  3. Follow the on-screen instructions to upload the downloaded XAR
  4. Click on the uploaded XAR and follow the instructions
  5. You'll also need to install all dependent Extensions that are not already installed in your wiki
Tags:
    

Get Connected