Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MULTISITE-15666: Add some drush commands (not ready to be merged!) #7

Open
wants to merge 7 commits into
base: fpfis-conf/d7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
295 changes: 295 additions & 0 deletions sites/all/drush/commands/custom/custom.drush.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
<?php

/**
* @file
* Drush commands for the module.
*/

/**
* Implements hook_drush_command().
*/
function custom_drush_command() {
$items = [];

$items['features-revert-site'] = [
'description' => 'Revert all enabled feature modules in your site folder.',
'options' => [
'force' => "Force revert even if Features assumes components' state are default.",
],
'drupal dependencies' => ['features'],
'aliases' => ['fr-site', 'frsi'],
];

$items['watchdog-smoketest'] = [
'description' => 'Crawl site and report watchdog errors and notifications back to user.',
'drupal dependencies' => ['dblog'],
'aliases' => ['wd-smoke', 'wdsm'],
'outputformat' => [
'default' => 'table',
'pipe-format' => 'var_export',
'field-labels' => array('wid' => 'ID', 'type' => 'Type', 'message' => 'Message', 'severity' => 'Severity', 'location' => 'Location', 'hostname' => 'Hostname', 'date' => 'Date', 'username' => 'Username'),
'fields-default' => array('type', 'severity', 'location', 'message'),
'column-widths' => array('type' => 8, 'severity' => 8),
'output-data-type' => 'format-table',
],
];

return $items;
}

/**
* Implements hook_drush_help().
*/
function custom_drush_help($section) {
switch ($section) {
case 'drush:features-revert-site':
return dt("Revert all enabled feature modules on your site that are located in the active site folder.");
break;
case 'drush:watchdog-smoketest':
return dt("Crawl site and report watchdog errors and notifications back to user.");
break;
}
return '';
}

/**
* Revert all enabled features to their definitions in code.
*/
function drush_custom_features_revert_site() {
// Before we can trigger other commands, we need to set our strict value to 0.
drush_set_option('strict', FALSE);

// Load required files.
module_load_include('inc', 'features', 'features.export');

$features = features_get_info('feature');
$features_to_revert = array();

foreach ($features as $feature) {
if ($feature->status == 1 && (substr($feature->filename, 0, 6) === "sites/")) {
$features_to_revert[] = $feature->name;
}
}

if (!empty($features_to_revert)) {
$dt_args = ['!modules' => implode(', ', $features_to_revert)];
drush_print(dt('The following modules will be reverted: !modules', $dt_args));
// Confirm that the user would like to continue. If not, simply abort.
if (!drush_confirm(dt('Do you really want to continue?'))) {
drush_user_abort('Aborting.');
}
drush_invoke('features-revert', $features_to_revert);
}
else {
drush_log(dt('Current state already matches defaults, aborting.'), 'ok');
}
}


/**
* Revert all enabled features to their definitions in code.
*/
function drush_custom_watchdog_smoketest() {
// Get cookie for root user.
global $user;
global $base_url;
$user = user_load(1);
$session_id = session_id();
// Did not find other way to login through cli
// $uli = user_pass_reset_url($user);
// $request = drupal_http_request($uli);
_drupal_session_write($session_id, '');
$options = [
'headers' => [
'Cookie' => session_name() . '=' . $session_id,
],
];

// Always add frontpage.
$paths = [
'/admin/config',
];

// Gather secondary urls.
$paths = generateUrlsByContentTypes($paths);
$paths = generateUrlsByTaxonomies($paths);
$paths = generateUrlsByViews($paths);
$paths = generateUrlsByPageManager($paths);

$count_paths = count($paths);
if ($count_paths >= 1) {
$last_wid = db_query('SELECT MAX(wid) FROM {watchdog}')->fetchField();
drush_log(dt('Visiting !count_paths paths as admin to check for watchdog messages.', array('!count_paths' => $count_paths)), 'ok');
foreach($paths as $path) {
$url = url($path, array('absolute' => TRUE));
$visit = drupal_http_request($url, $options);
}
}

_drupal_session_destroy($session_id);
$watchdog_messages = db_select('watchdog', 'wd')
->fields('wd')
->condition('wid', $last_wid, '>')
->condition('severity', 5, '<')
->execute()
->fetchAllAssoc('wid');

$table = array();
foreach ($watchdog_messages as $result) {
$row = core_watchdog_format_result($result, TRUE);
$table[$row->wid] = $row;
}

$count_watchdog = count($watchdog_messages);
if ($count_watchdog >= 1) {
drush_log(dt('Generated !count_watchdog watchdog entries of severity less than 5.', array('!count_watchdog' => $count_watchdog)), 'warning');
return $table;
}
else {
drush_log(dt('Generated !count_watchdog watchdog entries.', array('!count_watchdog' => $count_watchdog)), 'ok');
}
}

/**
* Generate the list of URL's based in the Content-types configuration.
*
* @return array
* List of URL's to test.
*/
function generateUrlsByContentTypes($paths)
{
$node_types = db_select('node_type', 'nt')
->fields('nt', ['type', 'name'])
->condition('nt.disabled', '0', '=')
->execute()
->fetchAll();
if (!empty($node_types)) {
foreach ($node_types as $node_type) {
$types[] = $node_type->type;
}
}
// Look for content in database.
$nodes = db_select('node', 'n')
->fields('n', array('nid', 'type'))
->condition('n.type', $types, 'IN')
->groupBy('n.type')
->condition('status', 0, '>')
->execute()
->fetchAll();
if (!empty($nodes)) {
foreach ($nodes as $node) {
$path = 'node/' . $node->nid;
if (drupal_valid_path($path)) {
$paths[] = url($path);
}
}
}
return $paths;
}
/**
* Generate the list of URL's based in the Taxonomy configuration.
*
* @return array
* List of URL's to test.
*/
function generateUrlsByTaxonomies($paths)
{
if (module_exists('taxonomy')) {
$taxonomies = db_select('taxonomy_term_data', 'ttd')
->fields('ttd', array('tid'))
->groupBy('ttd.vid')
->execute()
->fetchAll();
if (!empty($taxonomies)) {
foreach ($taxonomies as $taxonomy) {
$path = 'taxonomy/term/' . $taxonomy->tid;
if (drupal_valid_path($path)) {
$paths[] = url($path);
}
}
}
}
return $paths;
}
/**
* Generate the list of URL's based in the Search module.
*
* @return array
* List of URL's to test.
*/
function generateUrlsBySearch($paths)
{
if (module_exists('search') && drupal_valid_path('search')) {
$paths[] = url('search');
}
return $paths;
}
/**
* Generate the list of URL's based in the Views configuration.
*
* @return array
* List of URL's to test.
*/
function generateUrlsByViews($paths)
{
if (module_exists('views')) {

$all_views = views_get_all_views();
foreach ($all_views as $view) {

// Load all views displays to check for missing handlers.
if (module_exists('devel')) {

$views_debug = variable_get('views_devel_output', FALSE);
$views_region = variable_get('views_devel_region', 'watchdog');
variable_set('views_devel_output', TRUE);
variable_set('views_devel_region', 'message');

$displays = array_keys($view->display);
foreach($displays as $display) {
$view->execute($display);
$view = $view->clone_view();
}

variable_set('views_devel_output', $views_debug);
variable_set('views_devel_region', $views_region);
}

// Get the page views to visit later for detecting other watchdog entries.
if (empty($view->disabled)) {
foreach ($view->display as $display) {
if ($display->display_plugin == 'page') {
if (drupal_valid_path($display->display_options['path'])) {
$paths[] = url($display->display_options['path']);
}
}
}
}
}
}
return $paths;
}
/**
* Generate the list of URL's based in the Page Manager configuration.
*
* @return array
* List of URL's to test.
*/
function generateUrlsByPageManager($paths)
{
if (module_exists('page_manager')) {
$pages = db_select('page_manager_pages', 'pmp')
->fields('pmp', array('path'))
->execute()
->fetchAll();
if (!empty($pages)) {
foreach ($pages as $page) {
$path = $page->path;
if (drupal_valid_path($path)) {
$paths[] = url($path);
}
}
}
}
return $paths;
}
48 changes: 48 additions & 0 deletions sites/all/drush/commands/workbench_moderation_patch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
NextEuropa Workbench Moderation Fix
===================================
A Bug has been detected when updating sites from version 2.3 to 2.4 of the
platform.
It is related to the update of the "Workbench Moderation" module from
version 1.4 to 3.0.

The bug concerns any contents created with a published revision and a "draft"
one before the module upgrade.

It is described on the Drupal issue
https://www.drupal.org/project/workbench_moderation/issues/2958768:

"When a node (created prior to the Workbench Moderation 7.x-3.0 update) has a
published revision and a more-recent unpublished (e.g. Draft/Needs Review)
revision, any changes to the revision state of the more-recent revision changes
the published revision to unpublished, leaving no published revisions of the
node. Or just save the edited draft again and you will have the same behavior."

To fix this bug, we took the patch form this issue and adapted it into a drush
script in order to run it on each site independently. Indeed, the current
processes prevent us to run it through a platform "update" hook.

Usage
-----
To run this script please take these steps:
- Backup the database of the site, before starting the update
- Ensure the site run previously with the platform version 2.3
- Ensure the site is deployed with a platform version 2.4
- Ensure the Workbench Moderation "update" hook was run on version 2.4
- Run the script "drush wmpu"

The operation will log the results to the console and to drupal's watchdog,
identified by workbench_moderation_patch

Running simulation
-----
To run the update simulation script please take these steps:
- Backup the database of the site, before starting the update
- Ensure the site run previously with the platform version 2.3
- Ensure the site is deployed with a platform version 2.4
- Ensure the Workbench Moderation "update" hook was run on version 2.4
- Run the script "drush wms"

The operation will log the results to the console and to drupal's watchdog,
identified by workbench_moderation_patch, all messages will include at the start
of the line (Simulation run).

Loading