Issue #1565138: Include lock.inc for Drupal > 7.13.
[project/registry_rebuild.git] / registry_rebuild.drush.inc
1 <?php
2 /**
3 * @file
4 * Provide Drush integration for release building and dependency building.
5 */
6
7 /**
8 * Implements hook_drush_help().
9 */
10 function registry_rebuild_drush_help($section) {
11 switch ($section) {
12 case 'drush:registry-rebuild':
13 return dt('Rebuild the registry or module cache in a Drupal install.');
14 }
15 }
16
17 /**
18 * Implements hook_drush_command().
19 */
20 function registry_rebuild_drush_command() {
21 $items = array();
22
23 $items['registry-rebuild'] = array(
24 'description' => 'Rebuild the registry table (for classes) and the system table (for module locations) in a Drupal install.',
25 'callback' => 'drush_registry_rebuild',
26 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap.
27 'options' => array(
28 'no-cache-clear' => 'Rebuild the registry only, do not clear caches.',
29 ),
30 'examples' => array(
31 'drush rr --no-cache-clear' => 'Rebuild the registry only, do not clear caches.',
32 ),
33 'aliases' => array('rr'),
34 );
35
36 return $items;
37 }
38
39 /**
40 * Rebuild the registry.
41 *
42 * Before calling this we need to be bootstrapped to DRUPAL_BOOTSTRAP_DATABASE.
43 */
44 function drush_registry_rebuild() {
45 define('MAINTENANCE_MODE', 'update');
46 ini_set('memory_limit', -1);
47 if (!drush_bootstrap_to_phase(DRUSH_BOOTSTRAP_DRUPAL_DATABASE)) {
48 return drush_set_error('DRUPAL_SITE_NOT_FOUND', dt('You need to specify an alias or run this command within a drupal site.'));
49 }
50 $include_dir = DRUPAL_ROOT . '/includes';
51 $module_dir = DRUPAL_ROOT . '/modules';
52 // Use core directory if it exists.
53 if (file_exists(DRUPAL_ROOT . '/core/includes/bootstrap.inc')) {
54 $include_dir = DRUPAL_ROOT . '/core/includes';
55 $module_dir = DRUPAL_ROOT . '/core/modules';
56 }
57
58 $includes = array(
59 $include_dir . '/bootstrap.inc',
60 $include_dir . '/common.inc',
61 $include_dir . '/entity.inc',
62 $module_dir . '/entity/entity.module',
63 $module_dir . '/entity/entity.controller.inc',
64 $module_dir . '/system/system.module',
65 $include_dir . '/database/query.inc',
66 $include_dir . '/database/select.inc',
67 $module_dir . '/entity/entity.module',
68 $include_dir . '/registry.inc',
69 $include_dir . '/module.inc',
70 $include_dir . '/menu.inc',
71 $include_dir . '/file.inc',
72 $include_dir . '/lock.inc',
73 );
74
75 foreach ($includes as $include) {
76 if (file_exists($include)) {
77 require_once($include);
78 }
79 }
80 if (drush_drupal_major_version() < 7) {
81 return drush_module_cache_rebuild();
82 }
83
84 // This section is not functionally important. It's just getting the
85 // registry_parsed_files() so that it can report the change.
86 $connection_info = Database::getConnectionInfo();
87 $driver = $connection_info['default']['driver'];
88 require_once $include_dir . '/database/' . $driver . '/query.inc';
89
90 $parsed_before = registry_get_parsed_files();
91
92 cache_clear_all('lookup_cache', 'cache_bootstrap');
93 cache_clear_all('variables', 'cache_bootstrap');
94 cache_clear_all('module_implements', 'cache_bootstrap');
95
96 registry_rebuild(); // Call Drupal 7+ registry_rebuild()
97 drush_log(dt('Registry has been rebuilt.'));
98
99 drush_log(dt('Bootstrapping to DRUPAL_BOOTSTRAP_FULL.'));
100 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
101 drush_log(dt('Doing registry_rebuild() in DRUPAL_BOOTSTRAP_FULL.'));
102
103 if (!drush_get_option('no-cache-clear')) {
104 db_query('TRUNCATE {cache}');
105 }
106
107 registry_rebuild();
108 $parsed_after = registry_get_parsed_files();
109
110 // Remove files which don't exist anymore.
111 $filenames = array();
112 foreach ($parsed_after as $filename => $file) {
113 if (!file_exists($filename)) {
114 $filenames[] = $filename;
115 }
116 }
117
118 if (!empty($filenames)) {
119 db_delete('registry_file')
120 ->condition('filename', $filenames)
121 ->execute();
122 db_delete('registry')
123 ->condition('filename', $filenames)
124 ->execute();
125
126 $dt_args = array(
127 '@files' => implode(', ', $filenames),
128 );
129 $singular = 'Manually deleted 1 stale file from the registry.';
130 $plural = 'Manually deleted @count stale files from the registry.';
131 drush_log(format_plural(count($filenames), $singular, $plural), 'success');
132 $singular = "A file has been declared in a module's .info, but could not be found. This is probably indicative of a bug. The missing file is @files.";
133 $plural = "@count files were declared in a module's .info, but could not be found. This is probably indicative of a bug. The missing files are @files.";
134 drush_log(format_plural(count($filenames), $singular, $plural, $dt_args), 'warning');
135 }
136
137 $parsed_after = registry_get_parsed_files();
138
139 $message = 'There were @parsed_before files in the registry before and @parsed_after files now.';
140 $dt_args = array(
141 '@parsed_before' => count($parsed_before),
142 '@parsed_after' => count($parsed_after),
143 );
144 drush_log(dt($message, $dt_args));
145
146 drush_log(dt('The registry has been rebuilt.'), 'success');
147
148 if (!drush_get_option('no-cache-clear')) {
149 drush_drupal_cache_clear_all();
150 }
151 else {
152 drush_log(dt('The caches have not been cleared. It is recommended you clear the Drupal caches as soon as possible.'), 'warning');
153 }
154 }
155
156 /**
157 * Rebuild for D6 (which really just means rebuilding the menu/system table).
158 */
159 function drush_module_cache_rebuild() {
160 drush_log(dt('menu_rebuild() in DRUSH_BOOTSTRAP_DRUPAL_DATABASE.'));
161 module_list(TRUE, FALSE);
162 module_rebuild_cache();
163 drush_log(dt('Module cache has been rebuilt.'));
164 drush_log(dt('Bootstrapping to DRUPAL_BOOTSTRAP_FULL.'));
165 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
166 if (!drush_get_option('no-cache-clear')) {
167 drush_drupal_cache_clear_all();
168 }
169 else {
170 drush_log(dt('The caches have not been cleared. It is recommended you clear the Drupal caches as soon as possible.'), 'warning');
171 }
172 }