module_missing_message_fixer.drush.inc 3.4 KB

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