<?php

# Copyright (c) 2012 John Reese
# Licensed under the MIT license
form_security_validate('plugin_Source_repo_delete');
access_ensure_global_level(plugin_config_get('manage_threshold'));
$f_repo_id = gpc_get_string('id');
$t_repo = SourceRepo::load($f_repo_id);
helper_ensure_confirmed(sprintf(plugin_lang_get('ensure_delete'), $t_repo->name), plugin_lang_get('delete_repository'));
SourceRepo::delete($t_repo->id);
form_security_purge('plugin_Source_repo_delete');
print_successful_redirect(plugin_page('index', true));
/**
 * preg_replace_callback function for working with VCS links.
 */
function Source_Changeset_Link_Callback($p_matches)
{
    $t_url_type = strtolower($p_matches[1]);
    $t_repo_name = $p_matches[2];
    $t_revision = $p_matches[3];
    $t_repo_table = plugin_table('repository', 'Source');
    $t_changeset_table = plugin_table('changeset', 'Source');
    $t_file_table = plugin_table('file', 'Source');
    $t_query = "SELECT c.* FROM {$t_changeset_table} AS c\n\t\t\t\tJOIN {$t_repo_table} AS r ON r.id=c.repo_id\n\t\t\t\tWHERE c.revision LIKE " . db_param() . '
				AND r.name LIKE ' . db_param();
    $t_result = db_query_bound($t_query, array($t_revision . '%', $t_repo_name . '%'), 1);
    if (db_num_rows($t_result) > 0) {
        $t_row = db_fetch_array($t_result);
        $t_changeset = new SourceChangeset($t_row['repo_id'], $t_row['revision'], $t_row['branch'], $t_row['timestamp'], $t_row['author'], $t_row['message'], $t_row['user_id']);
        $t_changeset->id = $t_row['id'];
        $t_repo = SourceRepo::load($t_changeset->repo_id);
        $t_vcs = SourceVCS::repo($t_repo);
        if ($t_url_type == "v") {
            $t_url = $t_vcs->url_changeset($t_repo, $t_changeset);
        } else {
            $t_url = plugin_page('view') . '&id=' . $t_changeset->id;
        }
        $t_name = string_display_line($t_repo->name . ' ' . $t_vcs->show_changeset($t_repo, $t_changeset));
        if (!is_blank($t_url)) {
            return '<a href="' . $t_url . '">' . $t_name . '</a>';
        }
        return $t_name;
    }
    return $p_matches[0];
}
    }
}
if (gpc_get_string('api_key') == plugin_config_get('api_key')) {
    $t_valid = true;
}
# Not validated by this point gets the boot!
if (!$t_valid) {
    die(plugin_lang_get('invalid_import_url'));
}
$f_repo_id = strtolower(gpc_get_string('id'));
# Load an array of repositories to be imported
if ($f_repo_id == 'all') {
    $t_repos = SourceRepo::load_all();
} elseif (is_numeric($f_repo_id)) {
    $t_repo_id = (int) $f_repo_id;
    $t_repos = array(SourceRepo::load($t_repo_id));
} else {
    $f_repo_name = $f_repo_id;
    $t_repos = array(SourceRepo::load_from_name($f_repo_name));
}
# Loop through all repos to be imported
foreach ($t_repos as $t_repo) {
    $t_vcs = SourceVCS::repo($t_repo);
    # keep checking for more changesets to import
    $t_repo->import_error = false;
    while (true) {
        # import the next batch of changesets
        $t_changesets = $t_vcs->import_latest($t_repo);
        # check for errors
        if (!is_array($t_changesets)) {
            $t_repo->import_error = true;