}
    echo "\n\n";
}
// go trough each wysiwyg in the system and validate the links
$wysiwygids = $GLOBALS['SQ_SYSTEM']->am->getChildren($ROOT_ASSETID, 'content_type_wysiwyg', false);
foreach ($wysiwygids as $wysiwygid => $type_code_data) {
    $type_code = $type_code_data[0]['type_code'];
    $wysiwyg =& $GLOBALS['SQ_SYSTEM']->am->getAsset($wysiwygid, $type_code);
    $html = $wysiwyg->attr('html');
    // extract the ./?a=xx style links
    $e = '/\\.\\/\\?a=([0-9]+)/';
    $matches = array();
    preg_match_all($e, $html, $matches);
    $internal_assetids = $matches[1];
    foreach ($internal_assetids as $assetid) {
        printWYSIWYGName('WYSIWYG #' . $wysiwyg->id . ' - LINK #' . $assetid);
        if (!empty($assetid)) {
            $asset =& $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, '', true);
        } else {
            $asset = NULL;
        }
        if (is_null($asset)) {
            // the asset was invalid
            printUpdateStatus('INVALID');
        } else {
            if ($GLOBALS['SQ_SYSTEM']->am->assetInTrash($assetid, true)) {
                // the asset is in the trash
                printUpdateStatus('TRASH');
            } else {
                printUpdateStatus('OK');
            }
$ROOT_ASSETID = isset($_SERVER['argv'][2]) ? $_SERVER['argv'][2] : '1';
if ($ROOT_ASSETID == 1) {
    echo "\nWARNING: You are running this integrity checker on the whole system.\nThis is fine but:\n\tit may take a long time; and\n\tit will acquire locks on many of your assets (meaning you wont be able to edit content for a while)\n\n";
}
$root_user =& $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
// log in as root
if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
    echo "Failed loggin in as root user\n";
    exit;
}
// go trough each wysiwyg in the system, lock it, validate it, unlock it
$wysiwygids = $GLOBALS['SQ_SYSTEM']->am->getChildren($ROOT_ASSETID, 'content_type_wysiwyg', false);
foreach ($wysiwygids as $wysiwygid => $type_code_data) {
    $type_code = $type_code_data[0]['type_code'];
    $wysiwyg =& $GLOBALS['SQ_SYSTEM']->am->getAsset($wysiwygid, $type_code);
    printWYSIWYGName('WYSIWYG #' . $wysiwyg->id);
    // try to lock the WYSIWYG
    if (!$GLOBALS['SQ_SYSTEM']->am->acquireLock($wysiwyg->id, 'attributes')) {
        printUpdateStatus('LOCK');
        $GLOBALS['SQ_SYSTEM']->am->forgetAsset($wysiwyg);
        continue;
    }
    $old_html = $wysiwyg->attr('html');
    $new_html = preg_replace('|http[s]?://[^\\s]+(\\?a=[0-9]+)|', './\\1', $old_html);
    if (!$wysiwyg->setAttrValue('html', $new_html) || !$wysiwyg->saveAttributes()) {
        printUpdateStatus('FAILED');
        $GLOBALS['SQ_SYSTEM']->am->forgetAsset($container);
        continue;
    }
    // try to unlock the WYSIWYG
    if (!$GLOBALS['SQ_SYSTEM']->am->releaseLock($wysiwyg->id, 'attributes')) {