Finding unused attachments on page

Hi,

maybe you know the scenario. You have a page with loads of attachments.
You want to split it into several pages.
You copy the page x times, rename them and keep just the things you want in the editor.
Problem: you copied all the attachments of the original folder each and every time.

Is there a way of finding the unused attachments after cleaning up in the editor
and delete them? There is such thing for confluence i saw.

Cheers,
Tom

Hi. WDYM by “unused” ? Attachments can be used/be useful (for ex you give the link to the attachment to someone in an email) even if they’re not image attachments used in the content of xwiki pages or even if there are no links to them in wiki pages.

If you want to find usage of attachments in wiki pages, you’ll need to use some query to find if some wiki page content match the attachment name for example. Beware that you may also need to look into xobject textareas.

See https://extensions.xwiki.org/xwiki/bin/view/Extension/Query%20Module

You could also search on snippets.xwiki.org, maybe there’s something close to what you want or at least some examples of queries you can reuse.

Hi Vincent,

I’ll check the links you showed me, thx very much for that.

I’m wondering to be the only one pointing out to that problem. Maybe
its because of the fact that my pages have up to 500 picture-attachments.

If i decide to delete parts of the editor-content or split it into several pages
to reduce the vast amount of information a button like " delete the unused/nonlinked
attachments on this page" would be extremely powerful, dont you think?

If you copy pages with 500+ Attachments, delte the editor content and you simply dont
give a damn you waste your system with 500+ more pictures. Do that several times
and xwiki will show the white flag!:wink:

Cheers,
T.

wow, indeed first time I’m hearing someone using so many attachments… you’re stressing xwiki to the max (good!) :slight_smile:

Sure it’s interesting. I wouldn’t call it vital but certainly a nice to have. Note that this can be developed as an extension of XWiki and it doesn’t need to be in the core for you to have it.

It’s actually quite easy to write a small script to do this on the current page. You don’t even need a query for that. You can simply get the XDOM of the page. It returns your page content as structured content with Paragraph Blocks, Space Blocks, Link Blocks, etc. So it’s very easy to iterate over all found Links or Images and verify if the reference is local or not.

You could check https://rendering.xwiki.org/xwiki/bin/view/Main/GettingStarted#HExamples to see some examples which you could use in Groovy. To get the xdom of the page:

{{groovy}}
def xdom = xwiki.getDocument('reference to my doc').xdom
...
{{/groovy}}

Now you don’t need this and a simple search would be enough for your use case probably:

{{velocity}}
#set ($content = $xwiki.getDocument('reference to my doc').content)
## get attachments, see https://www.xwiki.org/xwiki/bin/view/Documentation/DevGuide/Scripting/APIGuide/#HListallattachmentsofapage
...

What would be interesting IMO is a generic “split page” refactoring action to split a page into 2 pages. The UI would need somehow to let the user pick which parts to keep and which parts to move among:

  • Content
  • XObjects
  • XClass
  • Attachments
  • (optional) Comments
  • (optional) History

You could create a jira at https://jira.xwiki.org to raise the idea.

Got a similar issue where people copy and paste images in pages, after deleting an image from an article,the attachment isn’t deleted. A simple script to remove these obsolete attachments would be very handy, would you be able to provide some instructions on how to achieve this, I’m not too familiar with the xwiki development environment. (Having just come across from mediawiki…)

I just like to revive that topic because its falling on my feet every single day.

I know there is still no official batchdeleting option but noone knows a workaround?
I mean, with all the love for xwiki, that should be a basic feature.

Hi @Torell

I gave you some links to write a small script snippet to do it. Were you able to progress? What step did you reach? Don’t hesitate to jump on the matrix chat to ask specific questions and you’ll get quick answers.

Alternatively would you be willing to sponsor the development of this feature? (asking since I know the XWiki SAS company is accepting to do sponsored work to help finance the development of XWiki).

Thanks

Note that the strategy I mentioned above is not going to work since you can use an attachment from another page and it would be too time-consuming to parse all pages of the wiki.

So the best strategy is to rely on a SOLR query to find if an attachment is used in the content of a page (see https://extensions.xwiki.org/xwiki/bin/view/Extension/Solr%20Search%20Query%20API).

Ideas:

  • Idea 1: it could be interesting to display a small icon next to each attachment when viewing the attachment list on page. That icon would indicate if the attachment is referenced or not.
  • Idea 2: Implement something like the Link Checker (finds broken links), see https://extensions.xwiki.org/xwiki/bin/view/Extension/Link%20Checker%20Application
  • Idea 3: Add a new tab in the Page Index UI to display “Orphaned Attachments”
  • Idea 4: Add a new column in the Page Index UI for the Attachment tab where you can filter on whether the attachment is used or not. Side note: could be a bit costly in perf (you incur the cost even when you don’t care about it, when listing attachments).

Somehow i totally oversaw your answers.
Thanks for your time, i will look into the options.

Cheers,
Torell