TimoKalker
New Creator

Confirming if a media element is still in use

Jump to solution

EN:

We have the requirement in the project context to write a script that checks whether media can still be deleted in the project context. However, we are not sure if just checking is sufficient or if further steps are needed.

During the verification, it is extremely important that NO media still in use are included in the list of unused media. If necessary, it is acceptable for a few unused media items not to be found.

The solution has to work for FS-Versions 2022.5 and 2023.5
_______
DE:
Wir haben die Anforderung im Projektkontext, per Script bei Medien zu überprüfen, ob diese noch im Projektkontext gelöscht werden können. Allerdings sind wir uns nicht sicher, ob das schon als Überprüfung reicht oder dort noch weitere Schritte nötig sind.
Bei der Überprüfung ist es extrem wichtig, dass KEINE Medien, die noch verwendet werden, in die Liste der ungenutzten Medien aufgenommen werden. Falls nötig, kann man also in kauf nehmen, dass ein paar ungenutzte Medien nicht gefunden werden.

Die Lösung soll auf den FS-Versionen 2023.5 und 2022.5 funktionieren.

_______

Hier der Code:

import de.espirit.firstspirit.access.project.Project;
import de.espirit.firstspirit.access.store.mediastore.Media;
import de.espirit.firstspirit.access.store.mediastore.MediaStoreRoot;

import java.util.Arrays;
import java.util.List;

public class CheckUnusedMediaElements extends AbstractCheck {

private final MediaStoreRoot mediaStore;
private final Project project;

public CheckUnusedMediaElements(MediaStoreRoot mediaStore, Project project){
this.mediaStore = mediaStore;
this.project = project;
}

@Override
protected void run() {
boolean noFinding = true;
List<String> list = Arrays.asList("js", "css");

for(Media currentMedia : mediaStore.getChildren(Media.class, true)){
if (currentMedia.hasIncomingReferences()) {
continue;
}
if (currentMedia.getType() == Media.FILE && list.contains(currentMedia.getFile(project.getMasterLanguage()).getExtension())) {
continue;
}

noFinding = false;
logger.log("The media element " + currentMedia.getUid() + " might be not in use", CheckUnusedMediaElements.class);
}
if(noFinding){
logger.log("no findings", CheckUnusedMediaElements.class);
}
}
}



Vielen Dank für euer Feedback, ob und was noch fehlt!/Thanks for your feedback

0 Kudos
1 Solution

Accepted Solutions
mbergmann
Crownpeak employee

Hi Timo,

usually such a check works fine. Using the reference graph to check for usages is in general the right approach.

There are some edge cases where that might be not enough because some (quite rare) ways of „using“ a media element are not reflected in the reference graph:

  • If a medium is only used by „computing“ its uid, e.g. something like $CMS_REF(media:"flag_"+set_language)$
    (but using the complete uid like in $CMS_REF(media:"flag_en")$ works!)
  • When there are mechanisms that rely on template logic to „find“ media elements that are not „directly“ referenced otherwise. An example would be some code that just works on a mediastore folder and iterates through its children to output links to them
  • Depending on your use case, you might want to perform the check both on the current AND release store to also find media elements that have already been deleted but their deletion has not been released
  • The check only uses the newest revision of the stores (current/release). This MIGHT be relevant if a user later restores a page that referenced the media in the past (but not now anymore)

Those cases are quite rare from my experience but I have come across them - so it would be good to check if your project(s) use such mechanisms.

Of course it is also important to tun that code with a user that has the permissions to access all media elements as only those will be checked.

Michael

View solution in original post

2 Replies
mbergmann
Crownpeak employee

Hi Timo,

usually such a check works fine. Using the reference graph to check for usages is in general the right approach.

There are some edge cases where that might be not enough because some (quite rare) ways of „using“ a media element are not reflected in the reference graph:

  • If a medium is only used by „computing“ its uid, e.g. something like $CMS_REF(media:"flag_"+set_language)$
    (but using the complete uid like in $CMS_REF(media:"flag_en")$ works!)
  • When there are mechanisms that rely on template logic to „find“ media elements that are not „directly“ referenced otherwise. An example would be some code that just works on a mediastore folder and iterates through its children to output links to them
  • Depending on your use case, you might want to perform the check both on the current AND release store to also find media elements that have already been deleted but their deletion has not been released
  • The check only uses the newest revision of the stores (current/release). This MIGHT be relevant if a user later restores a page that referenced the media in the past (but not now anymore)

Those cases are quite rare from my experience but I have come across them - so it would be good to check if your project(s) use such mechanisms.

Of course it is also important to tun that code with a user that has the permissions to access all media elements as only those will be checked.

Michael

I was unable to write a check for the first 2 cases with the 2 last ones being irrelevant for our usecase, however, I was able to check if a mediaElement has an URL and is thus used. Combined with a check for how old the media is to not falsely delete new media that was never published thus far, this is good enough. I wrote a test folder with both a computed and an indirectly used media, both are now seen as used after publishing the page.

Thank you!

0 Kudos