if (empty($count)) {
            echo "NONE\n";
        } else {
            echo "There are {$count} remap(s) with invalid old URL.\n";
        }
        printRemaps($affected_result);
    }
}
/*
 * report invalid new url remaps
 */
if (getCLIArg('invalid_new_url')) {
    echo "\n## Invalid New URL ##\n";
    $sql = 'SELECT * FROM sq_ast_lookup_remap WHERE expires > :time OR expires is null';
    $query = MatrixDAL::preparePdoQuery($sql);
    $time = ts_iso8601(time());
    MatrixDAL::bindValueToPdo($query, 'time', $time);
    $result = MatrixDAL::executePdoAll($query);
    $affected_result = array();
    $root_urls = explode("\n", SQ_CONF_SYSTEM_ROOT_URLS);
    foreach ($result as $index => $url_info) {
        $new_url_parts = parse_url($url_info['remap_url']);
        $protocol = array_get_index($new_url_parts, 'scheme', NULL);
        if (!empty($protocol)) {
            $url = array_get_index($new_url_parts, 'host', '') . array_get_index($new_url_parts, 'path', '');
            //  does it match with root matrix url
            $is_matrix_url = FALSE;
            foreach ($root_urls as $root_url) {
                if (strpos($url, $root_url . '/') === 0 || $url === $root_url) {
                    $is_matrix_url = TRUE;
                    break;
/**
 * Update the sq_file_vers_history table in database. Set to_time for old record and add new record
 *
 * @param File_Versioning $file_versioning	the File_Versioning object
 * @param string $fileid	the fileid of the file to be updated
 * @param string $real_file	the real file path of the file
 * @param int $version		the new version of the file
 * @param string $extra_info
 * @return boolean	return TRUE if success; otherwise, return FALSE
 * @see _updateFile() in file_versioning.inc
 */
function _updateFileVersionHistory($file_versioning, $fileid, $real_file, $version, $extra_info = '')
{
    $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
    $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
    try {
        //If this version is already in version history table, but the to_date is set, report it.
        //This case is not likely to happen, just to make sure there is no duplicate version is set
        $db_version_info = $file_versioning->_getFileInfoAtVersion($fileid, $version);
        if (!empty($db_version_info)) {
            echo "ERROR: THIS FILE (FILEID = {$fileid}, VERSION = {$version}) IS NO LONGER USED SINCE {$db_version_info['to_date']}\n";
            return FALSE;
        }
        $now = time();
        $date = ts_iso8601($now);
        /*if (MatrixDAL::getDbType() == 'oci') {
        			$date = db_extras_todate(MatrixDAL::getDbType(), $date);
        		}*/
        $sql = 'UPDATE sq_file_vers_history
				SET to_date = :to_date
				WHERE fileid = :fileid
				  AND to_date IS NULL';
        try {
            if (MatrixDAL::getDbType() == 'oci') {
                $sql = str_replace(':to_date', db_extras_todate(MatrixDAL::getDbType(), ':to_date', FALSE), $sql);
            }
            $query = MatrixDAL::preparePdoQuery($sql);
            MatrixDAL::bindValueToPdo($query, 'fileid', $fileid);
            MatrixDAL::bindValueToPdo($query, 'to_date', $date);
            MatrixDAL::execPdoQuery($query);
        } catch (Exception $e) {
            throw new Exception('Unable to update version history for file ID ' . $fileid . ' due to database error: ' . $e->getMessage());
        }
        if (file_exists($real_file)) {
            $file_size = filesize($real_file);
            $md5 = md5_file($real_file);
            $sha1 = sha1_file($real_file);
            $removal = '0';
        } else {
            $file_size = 0;
            $md5 = '';
            $sha1 = '';
            $removal = '1';
        }
        $sql = 'INSERT INTO sq_file_vers_history
				(fileid, version, from_date, to_date, file_size, md5, sha1, removal, extra_info)
				VALUES
				(:fileid, :version, :from_date, :to_date, :file_size, :md5, :sha1, :removal, :extra_info)';
        try {
            if (MatrixDAL::getDbType() == 'oci') {
                $sql = str_replace(':from_date', db_extras_todate(MatrixDAL::getDbType(), ':from_date', FALSE), $sql);
            }
            $query = MatrixDAL::preparePdoQuery($sql);
            MatrixDAL::bindValueToPdo($query, 'fileid', $fileid);
            MatrixDAL::bindValueToPdo($query, 'version', $version);
            MatrixDAL::bindValueToPdo($query, 'from_date', $date);
            MatrixDAL::bindValueToPdo($query, 'to_date', NULL);
            MatrixDAL::bindValueToPdo($query, 'file_size', $file_size);
            MatrixDAL::bindValueToPdo($query, 'md5', $md5);
            MatrixDAL::bindValueToPdo($query, 'sha1', $sha1);
            MatrixDAL::bindValueToPdo($query, 'removal', $removal);
            MatrixDAL::bindValueToPdo($query, 'extra_info', $extra_info);
            MatrixDAL::execPdoQuery($query);
        } catch (Exception $e) {
            throw new Exception('Unable to insert version history for file ID ' . $fileid . ' due to database error: ' . $e->getMessage());
        }
        $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
        $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
    } catch (Exception $e) {
        echo "ERROR: " . $e->getMessage() . "\n";
        $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
        $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
        return FALSE;
    }
    return TRUE;
}