Find broken links to external files

Find broken links to external files

Postby iainkewley » Tue Jan 21, 2014 8:53 pm

We maintain a large map, with links to thousands of files. From time to time these files get misplaced and the links break. We could do with a method of detecting these broken links, other than trawling through almost 8000 nodes manually. Any thoughts?

Iain
iainkewley
 
Posts: 65
Joined: Sat Dec 22, 2012 11:17 am

Re: Find broken links to external files

Postby seb4stien » Sat Jan 25, 2014 10:59 pm

Here is a small script that should do the job :

Code: Select all
// @ExecutionModes({ON_SELECTED_NODE_RECURSIVELY})

if (node.link && node.link.text.startsWith('file:')   ) {
   file = new File(node.link.text.substring(6))
   if (! file.exists()) {
      ui.informationMessage("Broken link for " + node.text)
      c.select(node)
   }
}


I added it to the script gallery on the wiki.
seb4stien
 
Posts: 32
Joined: Sat Jun 08, 2013 8:28 am

Re: Find broken links to external files

Postby boercher » Sun Jan 26, 2014 1:51 am

Here another script that works for all kind of links, except HTTPS and tests all links in a map:
Code: Select all
// @ExecutionModes({ON_SINGLE_NODE})
import org.freeplane.core.resources.ResourceController
import org.freeplane.features.mode.Controller
import org.freeplane.features.url.UrlManager
import org.freeplane.plugin.script.ScriptingPermissions


def URL getAbsoluteUrl(uri) {
    UrlManager urlManager = Controller.currentModeController.getExtension(UrlManager.class)
    return urlManager.getAbsoluteUrl(node.map.delegate, uri)
}

def scriptingPermissions = new ScriptingPermissions(ResourceController.resourceController.properties)
if (!scriptingPermissions.get(scriptingPermissions.RESOURCES_EXECUTE_SCRIPTS_WITHOUT_READ_RESTRICTION)
    || !scriptingPermissions.get(scriptingPermissions.RESOURCES_EXECUTE_SCRIPTS_WITHOUT_NETWORK_RESTRICTION)) {
    throw new RuntimeException("this scripts needs file read and network access permissions")
}

def broken = []
def countGood = 0
c.find{ it.link.uri != null }.each {
    try {
        def url = getAbsoluteUrl(it.link.uri)
        println "checking ${it.link.text}..."
        if (url.getProtocol() == 'https') {
            println "will not check ${it.link.text}"
        } else {
            def urlConnection = url.openConnection()
            urlConnection.connect()
            ++countGood
        }
    } catch (Exception e) {
        // e.printStackTrace();
        println "broken link: ${it.link.text}: ${e.message}"
        broken.add(it)
    }
}
if (broken) {
    def brokenLinksNode = node.map.root.createChild("broken links")
    broken.each {
        brokenLinksNode.createChild(it.text).link.node = it
    }
}
c.statusInfo = "found ${broken.size()} broken links and $countGood good links"


Volker
boercher
 
Posts: 644
Joined: Tue Jul 26, 2011 7:13 am

Re: Find broken links to external files

Postby boercher » Tue Jan 28, 2014 11:28 pm

Ian, have the script(s) done their job for you?

Volker
boercher
 
Posts: 644
Joined: Tue Jul 26, 2011 7:13 am

Re: Find broken links to external files

Postby iainkewley » Wed Jan 29, 2014 3:15 pm

Thanks. Sorry for the delay, was testing. Looks good, however it doesn't seem to pick up images that are supposed to be present but aren't

Iain
iainkewley
 
Posts: 65
Joined: Sat Dec 22, 2012 11:17 am

Re: Find broken links to external files

Postby boercher » Wed Jan 29, 2014 5:03 pm

Ok, there can be actually many links in a map at various locations. I have only dealt with the hyperlinks in the node core. The script hast to be extended for images, links in attributes and - possibly - some more.

Volker
boercher
 
Posts: 644
Joined: Tue Jul 26, 2011 7:13 am

Re: Find broken links to external files

Postby iainkewley » Wed Jan 29, 2014 8:43 pm

Volker

It's the images that are the key ones for us, as you know we're maintaining and updating a 7000+ node map! and most of the updates are as images, so it's very easy for one to go missing,

Iain
iainkewley
 
Posts: 65
Joined: Sat Dec 22, 2012 11:17 am

Re: Find broken links to external files

Postby boercher » Fri Jan 31, 2014 11:36 pm

Ian,

please checkout the revised checkLinks.groovy. It handles external images, too.

Volker
boercher
 
Posts: 644
Joined: Tue Jul 26, 2011 7:13 am

Re: Find broken links to external files

Postby iainkewley » Sat Feb 01, 2014 11:47 am

Volker

I get an error-see screenshot. I would post the log file but I can't seem to get the system to accept it, whatever file extension I try!

Iain
Attachments
Capture.JPG
(24.6 KiB) Downloaded 95 times
iainkewley
 
Posts: 65
Joined: Sat Dec 22, 2012 11:17 am

Re: Find broken links to external files

Postby boercher » Sun Feb 02, 2014 12:21 am

Oops, error fixed - please try again!

Volker
boercher
 
Posts: 644
Joined: Tue Jul 26, 2011 7:13 am

Next

Return to Open Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron