Okay, I’ve had a play around and I think I’ve got it working now. I’m using a snippet to call a series of MySQL queries comparing the contents of a particular folder to the database, searching the content, snippets, chunks, template and TVs. I haven’t made it parse subfolders yet, but the basic functionality is there and appears to be working. In case anyone else is interested in this, here is the code I’ve got at the moment:
<?php
/* Find orphaned files in MODx */
/* Cycle through all files (inc subfolders) and check whether they are used in the database in the content, chunks, snippets, templates or TVs */
/* directory to scan */
$dir = 'assets/images';
$output='';
if ($dirHandle = opendir($dir)) {
$output.='<table>';
$output.='<tr style="font-weight: bold;"><td>file</td><td>matches</td><td>pages</td><td>chunks</td><td>snippets</td><td>templates</td><td>tvs</td></tr>';
while (false !== ($file = readdir($dirHandle))) {
/* ignore thumbnails */
if (/*strpos($file,'.thumb')===false &&*/ strpos($file,'.')>0) {
$matchCount=0; $contentCount=0; $chunkCount=0; $snippetCount=0; $templateCount=0; $tvCount=0;
/* check for file in page content */
$contentQuery = $modx->db->select('*',$modx->getFullTableName('site_content'),'content LIKE "%'.$file.'%"');
if (mysql_num_rows($contentQuery)>0) $contentCount=mysql_num_rows($contentQuery);
/* check for file in chunks */
$chunkQuery = $modx->db->select('*',$modx->getFullTableName('site_htmlsnippets'),'snippet LIKE "%'.$file.'%"');
if (mysql_num_rows($chunkQuery)>0) $chunkCount=mysql_num_rows($chunkQuery);
/* check for file in snippets */
$snippetQuery = $modx->db->select('*',$modx->getFullTableName('site_snippets'),'snippet LIKE "%'.$file.'%"');
if (mysql_num_rows($snippetQuery)>0) $snippetCount=mysql_num_rows($snippetQuery);
/* check for file in templates */
$templateQuery = $modx->db->select('*',$modx->getFullTableName('site_templates'),'content LIKE "%'.$file.'%"');
if (mysql_num_rows($templateQuery)>0) $templateCount=mysql_num_rows($templateQuery);
/* check for file in TVs */
$tvQuery = $modx->db->select('*',$modx->getFullTableName('site_tmplvar_contentvalues'),'value LIKE "%'.$file.'%"');
if (mysql_num_rows($tvQuery)>0) $tvCount=mysql_num_rows($tvQuery);
/* add up */
$matchCount=$contentCount+$chunkCount+$snippetCount+$templateCount+$tvCount;
($matchCount==0)? $style='style="background: #ffe0e0;"' : $style='';
/* display it */
$output.='<tr '.$style.'>';
$output.='<td>'.$file.'</td>';
$output.='<td>'.$matchCount.'</td>';
$output.='<td>'.$contentCount.'</td>';
$output.='<td>'.$chunkCount.'</td>';
$output.='<td>'.$snippetCount.'</td>';
$output.='<td>'.$templateCount.'</td>';
$output.='<td>'.$tvCount.'</td>';
$output.='</tr>';
}
}
$output.='</table>';
}
else { $output='Sorry, couldn\'t open the folder.'; }
/* send output back */
return $output;
?>
Thoughts or comments for improvement always welcome! I don’t think it’s particularly efficient, as it has to do 5 queries for each file, but then again it’s not the sort of snippet that’s going to be run all the time, so server load shouldn’t be too bad! It’s a work in progress, and I guess if there are other people who might find it useful I might consider actually making it a proper snippet and putting it in the repository...