}
$no_ddl = isset($_SERVER['argv'][2]) && $_SERVER['argv'][2] == '--no-ddl';
require_once $SYSTEM_ROOT . '/core/include/init.inc';
require_once SQ_INCLUDE_PATH . '/general_occasional.inc';
require_once SQ_FUDGE_PATH . '/db_extras/db_extras.inc';
error_reporting(E_ALL);
if (ini_get('memory_limit') != '-1') {
    ini_set('memory_limit', '-1');
}
$root_user =& $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
    echo "ERROR: Failed logging in as root user\n";
    exit;
}
//--        MAIN()        --//
$db = MatrixDAL::getDb();
$pgdb = MatrixDAL::getDbType() == 'pgsql';
// From test_message.php: fairly broad brush used here, we know that slon uses '<schema_name>_(logtrigger|denyaccess)_[0-9]', so we'll use that to sniff for slon and it's schema name. denyaccess indicates slave.
if ($pgdb && !$no_ddl) {
    $slon_schema_query = 'SELECT REGEXP_REPLACE(trigger_name, \'(_logtrigger_|_denyaccess_)[0-9]+\', \'\') FROM information_schema.triggers WHERE (trigger_name LIKE \'%_logtrigger_%\' OR trigger_name LIKE \'%_denyaccess_%\') limit 1';
    $result = MatrixDAL::executeSqlOne($slon_schema_query);
    if (!empty($result)) {
        echo_headline('*** WARNING ***');
        echo_headline('The database appears to be using Slony replication.');
        echo_headline('Consider using --no-ddl to use only DML commands or replication might be broken.');
        echo_headline('***************');
    }
}
$script_start = time();
echo_headline('TRUNCATING TREE');
if ($no_ddl) {
/**
* Finds an existing asset matching the exact type with the metadata or attribute value supplied
*
* @return void
* @access public
*/
function findAsset($root_asset_id, $asset_type_code, array $search)
{
    // Begin uberquery!
    $db = MatrixDAL::getDb();
    $search_type_attribute = isset($search['attribute']);
    $field_name = '';
    $field_value = '';
    if ($search_type_attribute) {
        $field_name = $search['attribute']['field'];
        $field_value = $search['attribute']['value'];
    } else {
        $field_name = $search['metadata']['field'];
        $field_value = $search['metadata']['value'];
    }
    $tree_id = '';
    // Grab a single tree ID so we can search our entire root asset
    $sql = 'SELECT t.treeid FROM sq_ast_lnk_tree t, sq_ast_lnk l WHERE l.linkid = t.linkid AND l.minorid = :root_asset_id LIMIT 1';
    try {
        $query = MatrixDAL::preparePdoQuery($sql);
        MatrixDAL::bindValueToPdo($query, 'root_asset_id', $root_asset_id);
        $tree_id = MatrixDAL::executePdoOne($query);
    } catch (Exception $e) {
        throw new Exception('Unable to search for an existing ' . $asset_type_code . ' asset: ' . $e->getMessage());
    }
    if ($tree_id == '') {
        return array();
    }
    // Query portion for restricting by attribute
    $attribute_sql_from = 'sq_ast_attr r, sq_ast_attr_val v ';
    // Query portion for restricting by metadata field value
    $metadata_sql_from = 'sq_ast_mdata_val m ';
    $sql = 'SELECT a.assetid, a.name ' . 'FROM sq_ast a, sq_ast_lnk l, sq_ast_lnk_tree t, ' . ($search_type_attribute ? $attribute_sql_from : $metadata_sql_from) . 'WHERE t.treeid LIKE :tree_id ' . 'AND l.linkid = t.linkid AND a.assetid = l.minorid ';
    if (!empty($asset_type_code)) {
        $sql .= 'AND a.type_code = :type_code ';
    }
    if ($search_type_attribute) {
        $sql .= ' AND v.assetid = a.assetid AND r.name = :field_name AND v.attrid = r.attrid AND v.custom_val = :field_val';
    } else {
        $sql .= ' AND m.assetid = a.assetid AND m.fieldid = :field_name AND m.value = :field_val';
    }
    try {
        $query = MatrixDAL::preparePdoQuery($sql);
        MatrixDAL::bindValueToPdo($query, 'tree_id', $tree_id . '%');
        MatrixDAL::bindValueToPdo($query, 'field_name', $field_name);
        MatrixDAL::bindValueToPdo($query, 'field_val', $field_value);
        if (!empty($asset_type_code)) {
            MatrixDAL::bindValueToPdo($query, 'type_code', $asset_type_code);
        }
        $matching_assets = MatrixDAL::executePdoAssoc($query, 0);
    } catch (Exception $e) {
        throw new Exception('Unable to search for an existing ' . $asset_type_code . ' asset: ' . $e->getMessage());
    }
    return $matching_assets;
}
/**
* Searches the Matrix System for an existing direct child asset which matches the specified name (and optionally, asset type)
* The asset IDs of matching assets are returned.
*
* @param int	$parent_id			The asset ID under which to search for direct matching child assets
* @param string	$asset_name			The asset name to match
* @param string	$asset_type_code	The asset type code to match (optional)
*
* @return array
* @access public
*/
function searchExistingAsset($parent_id, $asset_name, $asset_type_code = '')
{
    $db = MatrixDAL::getDb();
    $sql = 'SELECT l.minorid, a.name ' . 'FROM sq_ast_lnk l, sq_ast a ' . 'WHERE l.majorid = :majorid ';
    if (!empty($asset_type_code)) {
        $sql .= 'AND a.type_code = :type_code ';
    }
    $sql .= 'AND a.assetid = l.minorid ' . 'AND a.name = :asset_name';
    try {
        $query = MatrixDAL::preparePdoQuery($sql);
        MatrixDAL::bindValueToPdo($query, 'majorid', $parent_id);
        MatrixDAL::bindValueToPdo($query, 'asset_name', $asset_name);
        if (!empty($asset_type_code)) {
            MatrixDAL::bindValueToPdo($query, 'type_code', $asset_type_code);
        }
        $matching_assets = MatrixDAL::executePdoAssoc($query, 0);
    } catch (Exception $e) {
        throw new Exception('Unable to search for an existing ' . $asset_name . ' asset: ' . $e->getMessage());
    }
    return $matching_assets;
}
/**
* Gets the children of the root nodes in the correct order from highest in the tree first to the
* lowest. Taken from HIPO_Job_Update_Lookups->prepare().
*
* @return array
* @access public
*/
function getTreeSortedChildren($assetids)
{
    $db = MatrixDAL::getDb();
    $todo_normal = array();
    $todo_shadows = array();
    foreach ($assetids as $assetid) {
        // check if we are updating lookups for a shadow asset, or a bridge
        $id_parts = explode(':', $assetid);
        if (isset($id_parts[1])) {
            $todo_shadows = array_merge($todo_shadows, array_keys($GLOBALS['SQ_SYSTEM']->am->getChildren($assetid)));
        } else {
            $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
            if ($asset instanceof Bridge) {
                if (!method_exists($asset, 'getChildren')) {
                    trigger_localised_error('SYS0204', translate('Shadow asset handler "%s" can not get children'), E_USER_WARNING, $asset->name);
                } else {
                    $todo_shadows = array_merge($todo_shadows, array_keys($asset->getChildren($assetid)));
                }
            }
            $where = 'l.minorid = :assetid';
            $where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause($where, 't');
            $where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause($where, 'l');
            $sql = 'SELECT t.treeid
					FROM ' . SQ_TABLE_RUNNING_PREFIX . 'ast_lnk_tree t INNER JOIN ' . SQ_TABLE_RUNNING_PREFIX . 'ast_lnk l ON t.linkid = l.linkid
					' . $where;
            $sql = db_extras_modify_limit_clause($sql, MatrixDAL::getDbType(), 1);
            try {
                $query = MatrixDAL::preparePdoQuery($sql);
                MatrixDAL::bindValueToPdo($query, 'assetid', $assetid);
                $treeid = MatrixDAL::executePdoOne($query);
            } catch (Exception $e) {
                throw new Exception('Unable to get treeid for minorid: ' . $assetid . ' due to database error: ' . $e->getMessage());
            }
            $sql = 'SELECT l.minorid, MAX(LENGTH(t.treeid)) as length
					FROM ' . SQ_TABLE_RUNNING_PREFIX . 'ast_lnk_tree t
							 INNER JOIN ' . SQ_TABLE_RUNNING_PREFIX . 'ast_lnk l ON t.linkid = l.linkid
					';
            $where = 't.treeid LIKE :treeid
					  GROUP BY l.minorid ORDER BY length';
            $where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause($where, 't');
            $where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause($where, 'l');
            try {
                $query = MatrixDAL::preparePdoQuery($sql . $where);
                MatrixDAL::bindValueToPdo($query, 'treeid', $treeid . '%');
                $new_assets = MatrixDAL::executePdoAssoc($query);
            } catch (Exception $e) {
                throw new Exception('Unable to get minorids for treeid: ' . $treeid[0]['treeid'] . ' due to database error: ' . $e->getMessage());
            }
            $todo_normal = array_merge($todo_normal, $new_assets);
        }
        //end else
    }
    //end foreach
    // Make sure lower assets are done after higher ones
    usort($todo_normal, create_function('$a, $b', 'return $a[\'length\'] > $b[\'length\'];'));
    $todo_assetids = array();
    foreach ($todo_normal as $asset_info) {
        $todo_assetids[] = $asset_info['minorid'];
    }
    $todo_assetids = array_unique(array_merge($todo_assetids, $todo_shadows));
    return $todo_assetids;
}
Пример #5
0
    // Switch the db back if required.
    if ($restoreConnection) {
        $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
    }
}
if ($db_type == 'pgsql') {
    // fairly broad brush used here, we know that slon uses '<schema_name>_(logtrigger|denyaccess)_[0-9]', so we'll use that to sniff for slon and it's schema name. denyaccess indicates slave.
    $slon_schema_query = 'SELECT REGEXP_REPLACE(trigger_name, \'(_logtrigger|_denyaccess)(_[0-9]+)?\', \'\') FROM information_schema.triggers WHERE (trigger_name LIKE \'%_logtrigger%\' OR trigger_name LIKE \'%_denyaccess%\') limit 1';
    // {SLON_SCHEMA} is replaced after the schema name is worked out.
    $slon_schema_lag_query = 'SELECT cast(extract(epoch from st_lag_time) as int8) FROM {SLON_SCHEMA}.sl_status WHERE st_origin = (SELECT last_value FROM {SLON_SCHEMA}.sl_local_node_id)';
    $output .= 'slon:';
    $lag = 0;
    // Slon test
    $db_conf['dbslon'] = $db_conf['db2'];
    MatrixDAL::dbConnect($db_conf['dbslon'], 'dbslon');
    $db = MatrixDAL::getDb('dbslon');
    $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
    $start_time = time();
    $query = MatrixDAL::preparePdoQuery($slon_schema_query);
    try {
        $slon_schema = MatrixDAL::executePdoOne($query);
    } catch (Exception $e) {
        $slon_schema = '';
        $return_code = '500';
    }
    if (!empty($slon_schema)) {
        $output .= $slon_schema . ':';
        $query = MatrixDAL::preparePdoQuery(str_replace('{SLON_SCHEMA}', $slon_schema, $slon_schema_lag_query));
        try {
            $lag = MatrixDAL::executePdoOne($query);
        } catch (Exception $e) {