drupalorg-1.0.x-dev/drupalorg.install
drupalorg.install
<?php
/**
* @file
* Contains the database tables required by some migrations.
*/
use Drupal\Core\Database\Database;
use Drupal\drupalorg\Utilities\GitLabClientHelper;
/**
* Implements hook_requirements().
*/
function drupalorg_requirements($phase) {
$requirements = [];
if (!class_exists('\Gitlab\Client')) {
$requirements['drupalorg_gitlab_library'] = [
'title' => t('GitLab'),
'value' => t('DrupalOrg requires the "m4tthumphrey/php-gitlab-api" library.'),
'severity' => REQUIREMENT_ERROR,
];
}
elseif ($phase == 'runtime') {
$config = \Drupal::config('drupalorg.gitlab_settings');
$requirements['drupalorg_gitlab'] = [
'title' => t('GitLab'),
'value' => t('Configured correctly'),
'severity' => REQUIREMENT_OK,
];
if (
empty($config->get('host')) ||
empty($config->get('token'))
) {
$requirements['drupalorg_gitlab']['value'] = t('Host and Token not set<br>“host” and “token” need to be set in the setting files under "drupalorg.gitlab_settings" or via the configuration form.');
$requirements['drupalorg_gitlab']['severity'] = REQUIREMENT_ERROR;
return $requirements;
}
try {
$client = (new GitLabClientHelper())->client();
$gitlab_pat = $client->personal_access_tokens()->current();
$gitlab_user = $client->users()->user();
$expected_scopes = ['api', 'read_user', 'read_repository', 'sudo', 'admin_mode'];
sort($expected_scopes);
sort($gitlab_pat['scopes']);
if ($gitlab_pat['scopes'] != $expected_scopes) {
$requirements['drupalorg_gitlab']['value'] = t('Token is not granted expected %scopes scopes.', [
'%scopes' => implode(', ', $expected_scopes),
]);
$requirements['drupalorg_gitlab']['severity'] = REQUIREMENT_WARNING;
return $requirements;
}
$requirements['drupalorg_gitlab']['value'] = t('Connected as @name<br>Personal access token expires in @expires days.', [
'@name' => $gitlab_user['name'],
'@expires' => (new DateTimeImmutable('now'))->diff(new DateTimeImmutable($gitlab_pat['expires_at']))->days,
]);
}
catch (\Exception $e) {
$requirements['drupalorg_gitlab']['value'] = t('Personal access token not valid<br>Result from attempted GitLab API call: %message', [
'%message' => $e->getMessage(),
]);
$requirements['drupalorg_gitlab']['severity'] = REQUIREMENT_ERROR;
}
}
return $requirements;
}
/**
* Implements hook_schema().
*/
function drupalorg_schema() {
// Used to link social network profiles to now internal authmap entries.
$schema['project_usage_week_release'] = [
'description' => 'Drupal.org Project Usage Weekly Release Data.',
'fields' => [
'release_id' => [
'description' => 'Id of the release.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
],
'timestamp' => [
'description' => 'Timestamp within the week the data was collected.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
],
'count' => [
'description' => 'Number of uses counted for the release.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
],
'project_id' => [
'description' => 'Id of the project the release belongs to.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
],
],
'indexes' => [
'release_id' => [
'release_id',
],
'timestamp' => [
'timestamp',
],
'project_id' => [
'project_id',
],
],
'foreign keys' => [
'release_id' => [
'table' => 'node',
'columns' => [
'id' => 'id',
],
],
// 'project_id' => [
// 'table' => 'node',
// 'columns' => [
// 'id' => 'id',
// ],
// ],
],
'primary key' => [
'release_id',
'timestamp',
],
];
// Project maintainers direct migration.
$schema['drupalorg_project_maintainer'] = [
'description' => 'Users who have various per-project maintainer permissions.',
'fields' => [
'nid' => [
'description' => 'Foreign key: node.nid of the project.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'uid' => [
'description' => 'Foreign key: {users}.uid of a user with any project maintainer permissions.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'update_project' => [
'description' => 'Can this user update(edit) the given project and modify its settings.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'administer_maintainers' => [
'description' => 'Can this user manipulate the maintainers for the given project.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'write_to_vcs' => [
'description' => 'Allows a user to commit or push to the repository associated with this project.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'manage_releases' => [
'description' => 'Allows a user to create and update releases, and to control which branches are supported.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'maintain_issues' => [
'description' => 'Can this user maintain issues for the given project.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
],
'primary key' => [
'nid',
'uid',
],
];
// GitLab users direct migration.
$schema['drupalorg_gitlab_users'] = [
'description' => 'GitLab users mapping of IDs to Drupal.org user IDs.',
'fields' => [
'uid' => [
'description' => 'Foreign key: {users}.uid of a user mapped to a GitLab user id.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'gitlab_user_id' => [
'description' => 'GitLab user id',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'picture_timestamp' => [
'description' => 'Keeps track of user pictures being synced over to GitLab.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
],
'primary key' => [
'uid',
],
'unique keys' => [
'gitlab_user_id' => ['gitlab_user_id'],
],
];
$schema['drupalorg_project_repositories'] = [
'description' => 'Map between Drupal project and GitLab repository ID and path.',
'fields' => [
'drupal_project_nid' => [
'description' => 'Foreign key: node.nid of the project.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'gitlab_project_id' => [
'description' => 'Project ID within GitLab.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'gitlab_namespace' => [
'description' => 'Namespace the repository is in within GitLab.',
'type' => 'varchar',
'length' => 255,
],
'gitlab_project_name' => [
'description' => 'Namespace the repository is in within GitLab.',
'type' => 'varchar',
'length' => 255,
],
],
'primary key' => [
'drupal_project_nid',
],
'unique keys' => [
'gitlab_project_id' => ['gitlab_project_id'],
'gitlab_namespaced_project_name' => ['gitlab_namespace', 'gitlab_project_name'],
],
];
$schema['drupalorg_project_release_supported_versions'] = [
'description' => 'Stores information about which versions of a project are supported.',
'fields' => [
'nid' => [
'description' => 'The node ID of the project.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'branch' => [
'description' => 'The version string truncated at the last ".".',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
],
'supported' => [
'description' => 'A flag to indicate whether or not a given branch of a project is supported.',
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 1,
],
'recommended_release' => [
'description' => 'The release node ID of the recommended release node for this branch (the latest release without any "extra" version info such as "alpha1", also known as stable release).',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'latest_release' => [
'description' => 'The release node ID of the latest release node for this branch (even if it has "extra" version info such as "alpha1").',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'latest_security_release' => [
'description' => 'The release node ID of the latest release node marked as a "security update" for this branch.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
],
'primary key' => ['nid', 'branch'],
'unique keys' => [
'latest_release' => ['latest_release'],
'recommended_release' => ['recommended_release'],
],
'indexes' => [
'nid_supported' => ['nid', 'supported'],
'supported_recommended' => ['supported', 'recommended_release'],
],
];
$schema['drupalorg_node_deleted'] = [
'description' => 'Stores deleted nodes and associated data.',
'fields' => [
'nid' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The node id of the deleted node.',
],
'node_type' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => '',
'description' => 'The node type.',
],
'uid' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The {users}.uid who authored the node.',
],
'title' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'The node title.',
],
'created' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The time that the node was created, as a Unix timestamp.',
],
'changed' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The time that the node was last edited, as a Unix timestamp.',
],
'status' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 1,
'size' => 'tiny',
'description' => 'The published status of a node. (0 = Not Published, 1 = Published)',
],
'name' => [
'type' => 'varchar',
'length' => 60,
'not null' => FALSE,
'description' => "The node author's name. Uses {users}.name if the user is logged in.",
],
'language' => [
'description' => 'The {languages}.language of this node.',
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
],
'node_body' => [
'type' => 'text',
'size' => 'big',
'not null' => FALSE,
'description' => "The node body content",
],
'flag_count' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The number of times a node was flagged as spam.',
],
'deleted_by' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'unsigned' => TRUE,
'description' => 'The userid of the account that deleted the node.',
],
'deleted_on' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The time that the node was deleted, as a Unix timestamp.',
],
],
'primary key' => ['nid'],
];
return $schema;
}
/**
* Add project maintainers table for migration.
*/
function drupalorg_update_9001() {
$table_definition = drupalorg_schema()['project_maintainer'];
$schema = Database::getConnection()->schema();
$schema->createTable('project_maintainer', $table_definition);
}
/**
* Add drupalorg_project_repositories table for migration.
*/
function drupalorg_update_9002() {
$table_definition = drupalorg_schema()['drupalorg_project_repositories'];
$schema = Database::getConnection()->schema();
$schema->createTable('drupalorg_project_repositories', $table_definition);
}
/**
* Add drupalorg_project_release_supported_versions table for migration.
*/
function drupalorg_update_9003() {
$table_definition = drupalorg_schema()['drupalorg_project_release_supported_versions'];
$schema = Database::getConnection()->schema();
$schema->createTable('drupalorg_project_release_supported_versions', $table_definition);
}
/**
* Add drupalorg_gitlab_users table for migration.
*/
function drupalorg_update_10001() {
$table_definition = drupalorg_schema()['drupalorg_gitlab_users'];
$schema = Database::getConnection()->schema();
$schema->createTable('drupalorg_gitlab_users', $table_definition);
}
/**
* Add project maintainers table for migration with correct name and columns.
*/
function drupalorg_update_10002() {
$table_definition = drupalorg_schema()['drupalorg_project_maintainer'];
$schema = Database::getConnection()->schema();
// This was the initial table, with wrong name and not all columns.
$schema->dropTable('project_maintainer');
$schema->createTable('drupalorg_project_maintainer', $table_definition);
}
/**
* Ensure all section terms have menus after update.
*/
function drupalorg_update_10003() {
drupalorg_ensure_section_menus();
}
/**
* Ensure all section terms have menus after update.
*/
function drupalorg_update_10004() {
drupalorg_ensure_section_menus();
}
/**
* Add drupalorg_node_deleted table for migration.
*/
function drupalorg_update_10005() {
$table_definition = drupalorg_schema()['drupalorg_node_deleted'];
$schema = Database::getConnection()->schema();
$schema->createTable('drupalorg_node_deleted', $table_definition);
}
/**
* Adds new field to the maintainers table.
*/
function drupalorg_update_10006() {
$table_definition = drupalorg_schema()['drupalorg_project_maintainer'];
$schema = Database::getConnection()->schema();
$schema->addField('drupalorg_project_maintainer', 'maintain_issues', $table_definition['fields']['maintain_issues']);
}
