module_missing_message_fixer.drush.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * @file
  4. * Provide Drush integration for release building and dependency building.
  5. */
  6. use Drupal\module_missing_message_fixer\ModuleMissingMessageFixer;
  7. /**
  8. * Helper function to check for modules to fix.
  9. *
  10. * @param bool $return
  11. * If we are to return to rows or just print the list.
  12. *
  13. * @return array[]|null
  14. * An array of table rows, or NULL if $return === FALSE.
  15. */
  16. function module_missing_message_fixer_check_modules($return = FALSE) {
  17. if ($return) {
  18. return ModuleMissingMessageFixer::getTableRows();
  19. }
  20. $rows = [];
  21. // Use a key for the head row that is not a valid module name.
  22. $rows['*HEAD*'] = ModuleMissingMessageFixer::getTableHeader();
  23. $rows += ModuleMissingMessageFixer::getTableRows();
  24. // Print Table here instead of in the hook_command.
  25. $output = count($rows) > 1 ? drush_format_table($rows, TRUE) : 'No Missing Modules Found!!!';
  26. drush_print($output);
  27. return NULL;
  28. }
  29. /**
  30. * Implements hook_drush_help().
  31. *
  32. * @param string $section
  33. *
  34. * @return null|string
  35. */
  36. function module_missing_message_fixer_drush_help($section) {
  37. switch ($section) {
  38. case 'module-missing-message-fixer-list':
  39. return dt("Returns a list of modules that have missing messages.");
  40. case 'module-missing-message-fixer-fix':
  41. return dt("Fixes a specified module that has missing messages. (optional --all)");
  42. default:
  43. return NULL;
  44. }
  45. }
  46. /**
  47. * Implements hook_drush_command().
  48. */
  49. function module_missing_message_fixer_drush_command() {
  50. $items = [];
  51. $items['module-missing-message-fixer-list'] = array(
  52. 'description' => dt('Returns a list of modules that have missing messages.'),
  53. 'aliases' => array(
  54. 'mmmfl',
  55. ),
  56. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL
  57. );
  58. $items['module-missing-message-fixer-fix'] = array(
  59. 'description' => dt('Fixes modules that have missing messages.'),
  60. 'aliases' => array(
  61. 'mmmff',
  62. ),
  63. 'arguments' => array(
  64. 'name' => 'The name of the module to fix.',
  65. ),
  66. 'options' => array(
  67. 'all' => dt('Fixes all module missing messages'),
  68. ),
  69. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL
  70. );
  71. return $items;
  72. }
  73. /**
  74. * Drush command.
  75. *
  76. * Displays a list of modules that have missing messages.
  77. */
  78. function drush_module_missing_message_fixer_list() {
  79. module_missing_message_fixer_check_modules();
  80. }
  81. /**
  82. * Drush command.
  83. *
  84. * @param string $name
  85. * The name of the module to fix messages for.
  86. */
  87. function drush_module_missing_message_fixer_fix($name = NULL) {
  88. $modules = [];
  89. $rows = module_missing_message_fixer_check_modules(TRUE);
  90. if (drush_get_option('all') !== NULL) {
  91. if (!empty($rows)) {
  92. foreach ($rows as $row) {
  93. $modules[] = $row['name'];
  94. }
  95. }
  96. }
  97. elseif ($name !== NULL) {
  98. // If this exists in the table.
  99. if (strpos(json_encode($rows), $name)) {
  100. $modules[] = $name;
  101. }
  102. else {
  103. drush_log(dt('Module ' . $name . ' was not found.'), 'error');
  104. }
  105. }
  106. else {
  107. drush_log(dt('Missing input, provide module name or run with --all'), 'error');
  108. }
  109. // Delete if there is no modules.
  110. if (count($modules) > 0) {
  111. $query = \Drupal::database()->delete('key_value');
  112. $query->condition('collection', 'system.schema');
  113. $query->condition('name', $modules, 'IN');
  114. $query->execute();
  115. if (drush_get_option('all') !== NULL) {
  116. drush_log(dt('All missing references have been removed.'), 'success');
  117. }
  118. elseif ($name !== NULL) {
  119. if (in_array($name, $modules, TRUE)) {
  120. drush_log(dt('Reference to ' . $name . ' (if found) has been removed.'), 'success');
  121. }
  122. }
  123. }
  124. }