We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18668
    • 25 Posts
    I’m managing a MODx site that’s enormous (1202 published pages, 1561 page in total, most recent page is 1777), and the disk space it’s taking up is pretty sizeable too (around 1.5GB so far). I’m trying to optimise the site at the moment, and one of the things I’m trying to find out is which of the uploaded files are actually being used and which are ’orphaned’ - i.e. no page linking to them. Same with images. Does anyone know of an easy way of doing this? I can use the search function in the Manager to search for files and that brings up any pages relating to that file, but it would be a tedious process to go through each file individually to check! Is there are module around for this? Or a MySQL query that would do the job?

    Any ideas would be much appreciated smiley

    Matthew
      ******************
      Matthew Dawkins
      www.matthewdawkins.co.uk
      We make web sites for churches
      Follow me on Twitter: twitter.com/chapter9
      ******************
      • 25663 MODX Staff
      • 12,272 Posts
      That’d require a custom script. Good idea, but not currently available.
        Ryan Thrash, MODX Co-Founder
        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
        • 18668
        • 25 Posts
        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...
          ******************
          Matthew Dawkins
          www.matthewdawkins.co.uk
          We make web sites for churches
          Follow me on Twitter: twitter.com/chapter9
          ******************
          • 25663 MODX Staff
          • 12,272 Posts
          Very nice ... it’d be a great helper utility indeed. I don’t know that it needs to be a "true snippet" per se, but keep it up! laugh
            Ryan Thrash, MODX Co-Founder
            Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
            • 28033
            • 925 Posts
            If this could search in subfolders, this would be one kickass script. smiley
              My Snippets
              -> PopUpChunk v1.0
              • 3749
              • 24,544 Posts
              Looks like a potentially great snippet (although it might be better as a module). You could call it OrphanX.  wink

              PS: This should probably be in the "In Development" forum.

                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 18668
                • 25 Posts
                Yes, I expect the In Development forum would probably be more appropriate now, but originally it was just a question as to whether anything like this existed, hence my choice!  Still, if a moderator can move it, I’m sure it would be appreciated...

                As for progress, I’ve made some.  I’ve put the core code into a function, so that I can call it multiple times if necessary - I did try a recursive approach to call the function from the function when it encountered a directory, but the script timed out because I have too much in my folders!  So now it just lists the subfolders and allows you to navigate to them by clicking, a bit like the File Manager in MODx.  I also found that I needed to search for %20 characters too (in addition to spaces), as some files were being missed out.

                I’ve still got some tidying up to do, but I expect I’ll probably post the results up here before too long.  And if I can figure out how to make it into a module that would be even better! I’m just still surprised that no one has thought of this before...
                  ******************
                  Matthew Dawkins
                  www.matthewdawkins.co.uk
                  We make web sites for churches
                  Follow me on Twitter: twitter.com/chapter9
                  ******************
                  • 25663 MODX Staff
                  • 12,272 Posts
                  Hey Matthew, playing around with this this weekend by chance? smiley
                    Ryan Thrash, MODX Co-Founder
                    Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                    • 3749
                    • 24,544 Posts
                    As long as you’re walking through the whole site, a nice addition might be a dead link checker. smiley
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 20413
                      • 2,877 Posts
                        @hawproductions | http://mrhaw.com/

                        Infograph: MODX Advanced Install in 7 steps:
                        http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

                        Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
                        http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower