I’m one of those ’fusspots’ too that you speak of

So I made a little script to ease some of the work for me; just little snippets of code that I use but don’t want to make a module for:
<?php
// Set this to your MODx Directory
$base_path = '/var/www/modx/';
include($base_path.'manager/includes/config.inc.php');
$action = (string)$_REQUEST['action'];
if (!$rs = mysql_connect($database_server, $database_user, $database_password, true))
die('Unable to connect to server');
if (!mysql_select_db(trim($dbase, '`')))
die('Unable to select database');
switch ($action) {
case 'doc_groups':
$sql = 'SELECT id, document_group AS dg, document, count(*) as gc FROM '.$table_prefix.'document_groups
GROUP BY document, dg
ORDER BY gc DESC';
$q = mysql_query($sql, $rs);
if ($q === false)
die('Error in query');
while ($item = mysql_fetch_assoc($q)) {
if ($item['gc'] <= 1) {
break;
}
$sql = 'DELETE FROM '.$table_prefix.'document_groups WHERE id='.$item['id'];
echo $sql.'<br/>';
mysql_query($sql, $rs);
}
break;
case 'tmplvars':
$sql = 'SELECT id, tmplvarid, contentid, value, count(*) as tvcount FROM '.$table_prefix.'site_tmplvar_contentvalues GROUP BY tmplvarid, contentid';
$q = mysql_query($sql, $rs);
while ($item = mysql_fetch_assoc($q)) {
if ($item['tvcount'] <= 1) continue;
print_r($item);
echo "<br/>";
}
break;
case 'doc_id':
$table = (string)$_REQUEST['table'];
if (!$table) $table = 'site_content';
$sql = 'SELECT id FROM '.$table_prefix.$table.' ORDER BY id';
$q = mysql_query($sql, $rs);
$previd = 0;
echo '<pre>';
$output = 'Open IDs in '.$table_prefix.$table.':';
while ($item = mysql_fetch_assoc($q)) {
if ($item['id'] - $previd > 1) {
for ($i = ($previd+1); $i < $item['id']; $i++) {
$output .= ' '.$i;
if (strlen($output) > 140) {
echo $output."\n";
$output = '';
}
}
}
$previd = $item['id'];
}
echo $output.'</pre>';
break;
default:
echo "Select an action.<br/>";
break;
}
echo "Query Complete on table <tt>".$dbase."</tt><br/>";Access it through a browser like so: [tt]
http://www.example.com/cleanup.php?action=doc_id[/tt] Be sure to set the [tt]$base_path[/tt] appropriately in your environment
It has a couple other actions too:
- [tt]doc_groups[/tt] will look through the document_groups table for duplicate entries and delete them. I use database synchronization software and sometimes this table gets cluttered with duplicates.
- [tt]tmplvars[/tt] does the same thing on the site_tmplvar_contentvalues table for the same reason.
[li][tt]doc_id[/tt] examines the ’site_content’ table (by default) for non-sequential rows and reports them.
I use the [tt]doc_id[/tt] action in conjunction with phpMyAdmin to create new documents and recycle the IDs. This is just because I’m picky though, I don’t have any valid reason to be doing this...