Skip to content

Instantly share code, notes, and snippets.

@mkaatman
Last active October 2, 2015 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkaatman/2226848 to your computer and use it in GitHub Desktop.
Save mkaatman/2226848 to your computer and use it in GitHub Desktop.
Find and optionally delete dita files that are not referenced from the ditamap.
<?php
/**
* Find and optionally delete files that are not referenced from the ditamap as a topicref.
* Disclaimer: Not smart enough to look for keydef yet
*/
// Set this to the relative (to where you're executing script from) or absolute path
$dir_base = 'base/path/to/files/';
// Set this to directory name where dita files exist
$dir_read = 'user';
// Set this to relative or absolute path of ditamap
$ditamap = $dir_base.'your.ditamap';
$mapContents = file_get_contents($ditamap);
$sxe = new SimpleXMLElement($mapContents);
$result = $sxe->xpath('//@href');
echo "Files referenced in ditamap: ".count($result)."\n";
$actualFiles = scandir($dir_base.$dir_read);
echo "Files in directory: ".count($actualFiles)."\n";
$filesInMap = array();
while(list( , $node) = each($result)) {
$filesInMap[] = $node;
}
echo "\nFiles in directory but not in map: \n";
while(list( , $file) = each($actualFiles)){
if($file!='..'&&$file!='.'&&$file!='.svn'){
if(!array_search($dir_read."/".$file, $filesInMap)){
echo $file."\n";
//unlink($dir_base.$dir_read."/".$file);
}
}
}
@kkg58
Copy link

kkg58 commented May 10, 2014

This would be a lifesaver for me, but how do I actually use it? I don't use any code, but I'm not afraid to try. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment