Last active
October 2, 2015 10:18
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.