module.inc 45.2 KB
Newer Older
1
2
<?php

3
4
5
6
7
/**
 * @file
 * API for loading and interacting with Drupal modules.
 */

8
9
use Drupal\Component\Graph\Graph;

10
/**
11
 * Loads all enabled modules.
12
 *
13
 * @param bool $bootstrap
14
 *   Whether to load only the reduced set of modules loaded in "bootstrap mode"
15
16
17
18
19
20
21
22
 *   for cached pages. See bootstrap.inc. Pass NULL to only check the current
 *   status without loading of modules.
 * @param bool $reset
 *   (optional) Internal use only. Whether to reset the internal statically
 *   cached flag of whether modules have been loaded. If TRUE, all modules are
 *   (re)loaded in the same call. Used by the testing framework to override and
 *   persist a limited module list for the duration of a unit test (in which no
 *   module system exists).
23
 *
24
25
26
 * @return bool
 *   A Boolean indicating whether all modules have been loaded. This means all
 *   modules; the load status of bootstrap modules cannot be checked.
27
 */
28
29
30
31
32
function module_load_all($bootstrap = FALSE, $reset = FALSE) {
  static $has_run = FALSE;

  if ($reset) {
    $has_run = FALSE;
33
  }
34

35
36
  // Unless $boostrap is NULL, load the requested set of modules.
  if (isset($bootstrap) && !$has_run) {
37
38
    $type = $bootstrap ? 'bootstrap' : 'module_enabled';
    foreach (module_list($type) as $module) {
39
40
41
42
      drupal_load('module', $module);
    }
    // $has_run will be TRUE if $bootstrap is FALSE.
    $has_run = !$bootstrap;
43
  }
44
  return $has_run;
45
46
}

47
/**
48
49
 * Returns a list of currently active modules.
 *
50
51
 * Acts as a wrapper around system_list(), returning either a list of all
 * enabled modules, or just modules needed for bootstrap.
52
 *
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 * The returned module list is always based on system_list(). The only exception
 * to that is when a fixed list of modules has been passed in previously, in
 * which case system_list() is omitted and the fixed list is always returned in
 * subsequent calls until manually reverted via module_list_reset().
 *
 * @param string $type
 *   The type of list to return:
 *   - module_enabled: All enabled modules.
 *   - bootstrap: All enabled modules required for bootstrap.
 * @param array $fixed_list
 *   (optional) An array of module names to override the list of modules. This
 *   list will persist until the next call with a new $fixed_list passed in.
 *   Primarily intended for internal use (e.g., in install.php and update.php).
 *   Use module_list_reset() to undo the $fixed_list override.
67
68
 * @param bool $reset
 *   (optional) Whether to reset/remove the $fixed_list.
69
70
 *
 * @return array
71
72
 *   An associative array whose keys and values are the names of the modules in
 *   the list.
73
74
 *
 * @see module_list_reset()
75
 */
76
77
78
79
80
81
82
83
84
85
86
87
88
89
function module_list($type = 'module_enabled', array $fixed_list = NULL, $reset = FALSE) {
  // This static is only used for $fixed_list. It must not be a drupal_static(),
  // since any call to drupal_static_reset() in unit tests would cause an
  // attempt to retrieve the list of modules from the database (which does not
  // exist).
  static $module_list;

  if ($reset) {
    $module_list = NULL;
    // Do nothing if no $type and no $fixed_list have been passed.
    if (!isset($type) && !isset($fixed_list)) {
      return;
    }
  }
90
91
92
93
94
95
96
97
98
99
100
101

  // The list that will be be returned. Separate from $module_list in order
  // to not duplicate the static cache of system_list().
  $list = $module_list;

  if (isset($fixed_list)) {
    $module_list = array();
    foreach ($fixed_list as $name => $module) {
      drupal_get_filename('module', $name, $module['filename']);
      $module_list[$name] = $name;
    }
    $list = $module_list;
102
  }
103
  elseif (!isset($module_list)) {
104
    $list = system_list($type);
105
  }
106
107
108
  return $list;
}

109
110
111
112
113
114
/**
 * Reverts an enforced fixed list of module_list().
 *
 * Subsequent calls to module_list() will no longer use a fixed list.
 */
function module_list_reset() {
115
  module_list(NULL, NULL, TRUE);
116
117
}

118
/**
119
 * Builds a list of bootstrap modules and enabled modules and themes.
120
121
 *
 * @param $type
122
123
124
125
 *   The type of list to return:
 *   - module_enabled: All enabled modules.
 *   - bootstrap: All enabled modules required for bootstrap.
 *   - theme: All themes.
126
127
 *
 * @return
128
 *   An associative array of modules or themes, keyed by name. For $type
129
130
131
 *   'bootstrap' and 'module_enabled', the array values equal the keys.
 *   For $type 'theme', the array values are objects representing the
 *   respective database row, with the 'info' property already unserialized.
132
133
134
 *
 * @see module_list()
 * @see list_themes()
135
136
137
138
139
 *
 * @todo There are too many layers/levels of caching involved for system_list()
 *   data. Consider to add a config($name, $cache = TRUE) argument to allow
 *   callers like system_list() to force-disable a possible configuration
 *   storage controller cache or some other way to circumvent it/take it over.
140
141
142
143
 */
function system_list($type) {
  $lists = &drupal_static(__FUNCTION__);

144
145
146
147
  // For bootstrap modules, attempt to fetch the list from cache if possible.
  // if not fetch only the required information to fire bootstrap hooks
  // in case we are going to serve the page from cache.
  if ($type == 'bootstrap') {
148
149
150
    if (isset($lists['bootstrap'])) {
      return $lists['bootstrap'];
    }
151
    if ($cached = cache('bootstrap')->get('bootstrap_modules')) {
152
153
154
      $bootstrap_list = $cached->data;
    }
    else {
155
      $bootstrap_list = state()->get('system.module.bootstrap') ?: array();
156
      cache('bootstrap')->set('bootstrap_modules', $bootstrap_list);
157
    }
158
    // To avoid a separate database lookup for the filepath, prime the
159
160
    // drupal_get_filename() static cache for bootstrap modules only.
    // The rest is stored separately to keep the bootstrap module cache small.
161
162
163
    foreach ($bootstrap_list as $name => $filename) {
      drupal_classloader_register($name, dirname($filename));
      drupal_get_filename('module', $name, $filename);
164
165
166
167
168
169
    }
    // We only return the module names here since module_list() doesn't need
    // the filename itself.
    $lists['bootstrap'] = array_keys($bootstrap_list);
  }
  // Otherwise build the list for enabled modules and themes.
170
  elseif (!isset($lists['module_enabled'])) {
171
    if ($cached = cache('bootstrap')->get('system_list')) {
172
173
174
175
176
177
178
179
180
181
182
183
184
      $lists = $cached->data;
    }
    else {
      $lists = array(
        'module_enabled' => array(),
        'theme' => array(),
        'filepaths' => array(),
      );
      // The module name (rather than the filename) is used as the fallback
      // weighting in order to guarantee consistent behavior across different
      // Drupal installations, which might have modules installed in different
      // locations in the file system. The ordering here must also be
      // consistent with the one used in module_implements().
185
186
187
      $enabled_modules = config('system.module')->get('enabled');
      $module_files = state()->get('system.module.files');
      foreach ($enabled_modules as $name => $weight) {
188
        // Build a list of all enabled modules.
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
        $lists['module_enabled'][$name] = $name;
        // Build a list of filenames so drupal_get_filename can use it.
        $lists['filepaths'][] = array(
          'type' => 'module',
          'name' => $name,
          'filepath' => $module_files[$name],
        );
      }

      // Build a list of themes.
      $enabled_themes = config('system.theme')->get('enabled');
      // @todo Themes include all themes, including disabled/uninstalled. This
      //   system.theme.data state will go away entirely as soon as themes have
      //   a proper installation status.
      // @see http://drupal.org/node/1067408
      $theme_data = state()->get('system.theme.data');
      if (empty($theme_data)) {
        // @todo: system_list() may be called from _drupal_bootstrap_code() and
        // module_load_all(), in which case system.module is not loaded yet.
        // Prevent a filesystem scan in drupal_load() and include it directly.
        // @see http://drupal.org/node/1067408
        require_once DRUPAL_ROOT . '/core/modules/system/system.module';
        $theme_data = system_rebuild_theme_data();
      }
      foreach ($theme_data as $name => $theme) {
        $theme->status = (int) isset($enabled_themes[$name]);
        $lists['theme'][$name] = $theme;
216
        // Build a list of filenames so drupal_get_filename can use it.
217
218
219
220
221
222
        if (isset($enabled_themes[$name])) {
          $lists['filepaths'][] = array(
            'type' => 'theme',
            'name' => $name,
            'filepath' => $theme->filename,
          );
223
        }
224
      }
225
226
      // @todo Move into list_themes(). Read info for a particular requested
      //   theme from state instead.
227
228
229
      foreach ($lists['theme'] as $key => $theme) {
        if (!empty($theme->info['base theme'])) {
          // Make a list of the theme's base themes.
230
          require_once DRUPAL_ROOT . '/core/includes/theme.inc';
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
          $lists['theme'][$key]->base_themes = drupal_find_base_themes($lists['theme'], $key);
          // Don't proceed if there was a problem with the root base theme.
          if (!current($lists['theme'][$key]->base_themes)) {
            continue;
          }
          // Determine the root base theme.
          $base_key = key($lists['theme'][$key]->base_themes);
          // Add to the list of sub-themes for each of the theme's base themes.
          foreach (array_keys($lists['theme'][$key]->base_themes) as $base_theme) {
            $lists['theme'][$base_theme]->sub_themes[$key] = $lists['theme'][$key]->info['name'];
          }
          // Add the base theme's theme engine info.
          $lists['theme'][$key]->info['engine'] = $lists['theme'][$base_key]->info['engine'];
        }
        else {
          // A plain theme is its own base theme.
          $base_key = $key;
        }
        // Set the theme engine prefix.
        $lists['theme'][$key]->prefix = ($lists['theme'][$key]->info['engine'] == 'theme') ? $base_key : $lists['theme'][$key]->info['engine'];
      }
252
      cache('bootstrap')->set('system_list', $lists);
253
    }
254
    // To avoid a separate database lookup for the filepath, prime the
255
256
257
    // drupal_get_filename() static cache with all enabled modules and themes.
    foreach ($lists['filepaths'] as $item) {
      drupal_get_filename($item['type'], $item['name'], $item['filepath']);
258
      drupal_classloader_register($item['name'], dirname($item['filepath']));
259
260
261
262
263
264
    }
  }

  return $lists[$type];
}

265
/**
266
 * Resets all system_list() caches.
267
268
269
 */
function system_list_reset() {
  drupal_static_reset('system_list');
270
  drupal_static_reset('system_rebuild_module_data');
271
  drupal_static_reset('list_themes');
272
  cache('bootstrap')->deleteMultiple(array('bootstrap_modules', 'system_list'));
273
  cache()->delete('system_info');
274
275
276
277
278
279
280
281
  // Remove last known theme data state.
  // This causes system_list() to call system_rebuild_theme_data() on its next
  // invocation. When enabling a module that implements hook_system_info_alter()
  // to inject a new (testing) theme or manipulate an existing theme, then that
  // will cause system_list_reset() to be called, but theme data is not
  // necessarily rebuilt afterwards.
  // @todo Obsolete with proper installation status for themes.
  state()->delete('system.theme.data');
282
283
}

284
/**
285
 * Determines which modules require and are required by each module.
286
287
288
 *
 * @param $files
 *   The array of filesystem objects used to rebuild the cache.
289
 *
290
 * @return
291
292
293
294
295
 *   The same array with the new keys for each module:
 *   - requires: An array with the keys being the modules that this module
 *     requires.
 *   - required_by: An array with the keys being the modules that will not work
 *     without this module.
296
 */
297
function _module_build_dependencies($files) {
298
299
300
  foreach ($files as $filename => $file) {
    $graph[$file->name]['edges'] = array();
    if (isset($file->info['dependencies']) && is_array($file->info['dependencies'])) {
301
302
303
      foreach ($file->info['dependencies'] as $dependency) {
        $dependency_data = drupal_parse_dependency($dependency);
        $graph[$file->name]['edges'][$dependency_data['name']] = $dependency_data;
304
305
      }
    }
306
  }
307
308
  $graph_object = new Graph($graph);
  $graph = $graph_object->searchAndSort();
309
  foreach ($graph as $module => $data) {
310
    $files[$module]->required_by = isset($data['reverse_paths']) ? $data['reverse_paths'] : array();
311
312
313
    $files[$module]->requires = isset($data['paths']) ? $data['paths'] : array();
    $files[$module]->sort = $data['weight'];
  }
Steven Wittens's avatar
Steven Wittens committed
314
315
316
  return $files;
}

317
/**
318
 * Determines whether a given module exists.
319
320
321
 *
 * @param $module
 *   The name of the module (without the .module extension).
322
 *
323
324
325
 * @return
 *   TRUE if the module is both installed and enabled.
 */
326
function module_exists($module) {
327
  $list = module_list();
328
  return isset($list[$module]);
329
330
}

Steven Wittens's avatar
Steven Wittens committed
331
/**
332
 * Loads a module's installation hooks.
333
334
335
336
337
338
 *
 * @param $module
 *   The name of the module (without the .module extension).
 *
 * @return
 *   The name of the module's install file, if successful; FALSE otherwise.
Steven Wittens's avatar
Steven Wittens committed
339
340
341
 */
function module_load_install($module) {
  // Make sure the installation API is available
342
  include_once DRUPAL_ROOT . '/core/includes/install.inc';
Steven Wittens's avatar
Steven Wittens committed
343

344
  return module_load_include('install', $module);
345
346
347
}

/**
348
 * Loads a module include file.
349
 *
350
351
 * Examples:
 * @code
352
 *   // Load node.admin.inc from the node module.
353
 *   module_load_include('inc', 'node', 'node.admin');
354
 *   // Load content_types.inc from the node module.
355
 *   module_load_include('inc', 'node', 'content_types');
356
 * @endcode
357
 *
358
359
360
 * Do not use this function to load an install file, use module_load_install()
 * instead. Do not use this function in a global context since it requires
 * Drupal to be fully bootstrapped, use require_once DRUPAL_ROOT . '/path/file'
361
362
 * instead.
 *
363
364
365
366
367
 * @param $type
 *   The include file's type (file extension).
 * @param $module
 *   The module to which the include file belongs.
 * @param $name
368
369
 *   (optional) The base file name (without the $type extension). If omitted,
 *   $module is used; i.e., resulting in "$module.$type" by default.
370
371
372
 *
 * @return
 *   The name of the included file, if successful; FALSE otherwise.
373
374
 */
function module_load_include($type, $module, $name = NULL) {
375
  if (!isset($name)) {
376
377
378
    $name = $module;
  }

379
  if (function_exists('drupal_get_path')) {
380
381
382
383
384
    $file = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . "/$name.$type";
    if (is_file($file)) {
      require_once $file;
      return $file;
    }
385
  }
386
  return FALSE;
387
388
389
}

/**
390
 * Loads an include file for each enabled module.
391
392
393
394
395
 */
function module_load_all_includes($type, $name = NULL) {
  $modules = module_list();
  foreach ($modules as $module) {
    module_load_include($type, $module, $name);
Steven Wittens's avatar
Steven Wittens committed
396
397
398
399
  }
}

/**
400
 * Enables or installs a given list of modules.
Steven Wittens's avatar
Steven Wittens committed
401
 *
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
 * Definitions:
 * - "Enabling" is the process of activating a module for use by Drupal.
 * - "Disabling" is the process of deactivating a module.
 * - "Installing" is the process of enabling it for the first time or after it
 *   has been uninstalled.
 * - "Uninstalling" is the process of removing all traces of a module.
 *
 * Order of events:
 * - Gather and add module dependencies to $module_list (if applicable).
 * - For each module that is being enabled:
 *   - Install module schema and update system registries and caches.
 *   - If the module is being enabled for the first time or had been
 *     uninstalled, invoke hook_install() and add it to the list of installed
 *     modules.
 *   - Invoke hook_enable().
 * - Invoke hook_modules_installed().
 * - Invoke hook_modules_enabled().
419
 *
420
421
 * @param $module_list
 *   An array of module names.
422
423
424
425
 * @param $enable_dependencies
 *   If TRUE, dependencies will automatically be added and enabled in the
 *   correct order. This incurs a significant performance cost, so use FALSE
 *   if you know $module_list is already complete and in the correct order.
426
 *
427
428
 * @return
 *   FALSE if one or more dependencies are missing, TRUE otherwise.
429
430
431
432
433
 *
 * @see hook_install()
 * @see hook_enable()
 * @see hook_modules_installed()
 * @see hook_modules_enabled()
Steven Wittens's avatar
Steven Wittens committed
434
 */
435
function module_enable($module_list, $enable_dependencies = TRUE) {
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
  if ($enable_dependencies) {
    // Get all module data so we can find dependencies and sort.
    $module_data = system_rebuild_module_data();
    // Create an associative array with weights as values.
    $module_list = array_flip(array_values($module_list));

    while (list($module) = each($module_list)) {
      if (!isset($module_data[$module])) {
        // This module is not found in the filesystem, abort.
        return FALSE;
      }
      if ($module_data[$module]->status) {
        // Skip already enabled modules.
        unset($module_list[$module]);
        continue;
      }
      $module_list[$module] = $module_data[$module]->sort;

      // Add dependencies to the list, with a placeholder weight.
      // The new modules will be processed as the while loop continues.
456
      foreach (array_keys($module_data[$module]->requires) as $dependency) {
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
        if (!isset($module_list[$dependency])) {
          $module_list[$dependency] = 0;
        }
      }
    }

    if (!$module_list) {
      // Nothing to do. All modules already enabled.
      return TRUE;
    }

    // Sort the module list by pre-calculated weights.
    arsort($module_list);
    $module_list = array_keys($module_list);
  }

473
  // Required for module installation checks.
474
  include_once DRUPAL_ROOT . '/core/includes/install.inc';
475
476
477

  $modules_installed = array();
  $modules_enabled = array();
478
479
480
  $schema_store = drupal_container()->get('keyvalue')->get('system.schema');
  $module_config = config('system.module');
  $disabled_config = config('system.module.disabled');
481
  foreach ($module_list as $module) {
482
    // Only process modules that are not already enabled.
483
484
485
486
487
488
489
490
491
492
493
494
495
    $enabled = $module_config->get("enabled.$module") !== NULL;
    if (!$enabled) {
      $weight = $disabled_config->get($module);
      if ($weight === NULL) {
        $weight = 0;
      }
      $module_config
        ->set("enabled.$module", $weight)
        ->set('enabled', module_config_sort($module_config->get('enabled')))
        ->save();
      $disabled_config
        ->clear($module)
        ->save();
496
497
      // Load the module's code.
      drupal_load('module', $module);
498
      module_load_install($module);
499
500

      // Refresh the module list to include it.
501
      system_list_reset();
502
      module_implements_reset();
503
504
505
      _system_update_bootstrap_status();
      // Refresh the schema to include it.
      drupal_get_schema(NULL, TRUE);
506
507
      // Update the theme registry to include it.
      drupal_theme_rebuild();
508
509
510

      // Allow modules to react prior to the installation of a module.
      module_invoke_all('modules_preinstall', array($module));
511

512
513
514
      // Clear the entity info cache before importing new configuration.
      entity_info_cache_clear();

515
516
517
      // Now install the module if necessary.
      if (drupal_get_installed_schema_version($module, TRUE) == SCHEMA_UNINSTALLED) {
        drupal_install_schema($module);
518
519
520

        // Set the schema version to the number of the last update provided
        // by the module.
521
        $versions = drupal_get_schema_versions($module);
522
523
        $version = $versions ? max($versions) : SCHEMA_INSTALLED;

524
        // Install default configuration of the module.
525
        config_install_default_config('module', $module);
526

527
528
529
530
531
532
533
        // If the module has no current updates, but has some that were
        // previously removed, set the version to the value of
        // hook_update_last_removed().
        if ($last_removed = module_invoke($module, 'update_last_removed')) {
          $version = max($version, $last_removed);
        }
        drupal_set_installed_schema_version($module, $version);
534
535
        // Allow the module to perform install tasks.
        module_invoke($module, 'install');
536
537
        // Record the fact that it was installed.
        $modules_installed[] = $module;
538
        watchdog('system', '%module module installed.', array('%module' => $module), WATCHDOG_INFO);
539
      }
540

541
      // Allow modules to react prior to the enabling of a module.
542
      entity_info_cache_clear();
543
544
      module_invoke_all('modules_preenable', array($module));

545
546
547
548
549
      // Enable the module.
      module_invoke($module, 'enable');

      // Record the fact that it was enabled.
      $modules_enabled[] = $module;
550
      watchdog('system', '%module module enabled.', array('%module' => $module), WATCHDOG_INFO);
551
    }
552
553
  }

554
  // If any modules were newly installed, invoke hook_modules_installed().
555
  if (!empty($modules_installed)) {
556
    module_invoke_all('modules_installed', $modules_installed);
Steven Wittens's avatar
Steven Wittens committed
557
  }
558

559
560
561
  // If any modules were newly enabled, invoke hook_modules_enabled().
  if (!empty($modules_enabled)) {
    module_invoke_all('modules_enabled', $modules_enabled);
562
  }
563
564

  return TRUE;
Steven Wittens's avatar
Steven Wittens committed
565
566
567
}

/**
568
 * Disables a given set of modules.
Steven Wittens's avatar
Steven Wittens committed
569
 *
570
571
 * @param $module_list
 *   An array of module names.
572
573
574
575
 * @param $disable_dependents
 *   If TRUE, dependent modules will automatically be added and disabled in the
 *   correct order. This incurs a significant performance cost, so use FALSE
 *   if you know $module_list is already complete and in the correct order.
Steven Wittens's avatar
Steven Wittens committed
576
 */
577
578
579
580
581
582
583
function module_disable($module_list, $disable_dependents = TRUE) {
  if ($disable_dependents) {
    // Get all module data so we can find dependents and sort.
    $module_data = system_rebuild_module_data();
    // Create an associative array with weights as values.
    $module_list = array_flip(array_values($module_list));

584
    $profile = drupal_get_profile();
585
586
587
588
589
590
591
592
593
594
595
    while (list($module) = each($module_list)) {
      if (!isset($module_data[$module]) || !$module_data[$module]->status) {
        // This module doesn't exist or is already disabled, skip it.
        unset($module_list[$module]);
        continue;
      }
      $module_list[$module] = $module_data[$module]->sort;

      // Add dependent modules to the list, with a placeholder weight.
      // The new modules will be processed as the while loop continues.
      foreach ($module_data[$module]->required_by as $dependent => $dependent_data) {
596
        if (!isset($module_list[$dependent]) && $dependent != $profile) {
597
598
599
600
601
602
603
604
605
606
          $module_list[$dependent] = 0;
        }
      }
    }

    // Sort the module list by pre-calculated weights.
    asort($module_list);
    $module_list = array_keys($module_list);
  }

607
  $invoke_modules = array();
608

609
610
  $module_config = config('system.module');
  $disabled_config = config('system.module.disabled');
611
612
613
614
  foreach ($module_list as $module) {
    if (module_exists($module)) {
      module_load_install($module);
      module_invoke($module, 'disable');
615
616
617
618
619
620
      $disabled_config
        ->set($module, $module_config->get($module))
        ->save();
      $module_config
        ->clear("enabled.$module")
        ->save();
621
      $invoke_modules[] = $module;
622
      watchdog('system', '%module module disabled.', array('%module' => $module), WATCHDOG_INFO);
623
    }
Steven Wittens's avatar
Steven Wittens committed
624
  }
625
626

  if (!empty($invoke_modules)) {
627
    // Refresh the module list to exclude the disabled modules.
628
    system_list_reset();
629
    module_implements_reset();
630
    entity_info_cache_clear();
631
    // Invoke hook_modules_disabled before disabling modules,
632
633
    // so we can still call module hooks to get information.
    module_invoke_all('modules_disabled', $invoke_modules);
634
    _system_update_bootstrap_status();
635
636
    // Update the theme registry to remove the newly-disabled module.
    drupal_theme_rebuild();
Steven Wittens's avatar
Steven Wittens committed
637
638
639
  }
}

640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/**
 * Uninstalls a given list of modules.
 *
 * @param $module_list
 *   The modules to uninstall.
 * @param $uninstall_dependents
 *   If TRUE, the function will check that all modules which depend on the
 *   passed-in module list either are already uninstalled or contained in the
 *   list, and it will ensure that the modules are uninstalled in the correct
 *   order. This incurs a significant performance cost, so use FALSE if you
 *   know $module_list is already complete and in the correct order.
 *
 * @return
 *   FALSE if one or more dependent modules are missing from the list, TRUE
 *   otherwise.
 */
function module_uninstall($module_list = array(), $uninstall_dependents = TRUE) {
  if ($uninstall_dependents) {
    // Get all module data so we can find dependents and sort.
    $module_data = system_rebuild_module_data();
    // Create an associative array with weights as values.
    $module_list = array_flip(array_values($module_list));

    $profile = drupal_get_profile();
    while (list($module) = each($module_list)) {
      if (!isset($module_data[$module]) || drupal_get_installed_schema_version($module) == SCHEMA_UNINSTALLED) {
        // This module doesn't exist or is already uninstalled, skip it.
        unset($module_list[$module]);
        continue;
      }
      $module_list[$module] = $module_data[$module]->sort;

      // If the module has any dependents which are not already uninstalled and
      // not included in the passed-in list, abort. It is not safe to uninstall
      // them automatically because uninstalling a module is a destructive
      // operation.
      foreach (array_keys($module_data[$module]->required_by) as $dependent) {
        if (!isset($module_list[$dependent]) && drupal_get_installed_schema_version($dependent) != SCHEMA_UNINSTALLED && $dependent != $profile) {
          return FALSE;
        }
      }
    }

    // Sort the module list by pre-calculated weights.
    asort($module_list);
    $module_list = array_keys($module_list);
  }

688
  $storage = drupal_container()->get('config.storage');
689
690
  $schema_store = drupal_container()->get('keyvalue')->get('system.schema');
  $disabled_config = config('system.module.disabled');
691
692
693
694
695
696
697
698
699
700
701
702
703
  foreach ($module_list as $module) {
    // Uninstall the module.
    module_load_install($module);
    module_invoke($module, 'uninstall');
    drupal_uninstall_schema($module);

    // Remove all configuration belonging to the module.
    $config_names = $storage->listAll($module . '.');
    foreach ($config_names as $config_name) {
      config($config_name)->delete();
    }

    watchdog('system', '%module module uninstalled.', array('%module' => $module), WATCHDOG_INFO);
704
705
    $schema_store->delete($module);
    $disabled_config->clear($module);
706
  }
707
708
  $disabled_config->save();
  drupal_get_installed_schema_version(NULL, TRUE);
709
710
711
712
713
714
715
716
717

  if (!empty($module_list)) {
    // Call hook_module_uninstall to let other modules act
    module_invoke_all('modules_uninstalled', $module_list);
  }

  return TRUE;
}

718
719
/**
 * @defgroup hooks Hooks
Dries's avatar
   
Dries committed
720
721
 * @{
 * Allow modules to interact with the Drupal core.
722
723
 *
 * Drupal's module system is based on the concept of "hooks". A hook is a PHP
724
725
726
 * function that is named foo_bar(), where "foo" is the name of the module
 * (whose filename is thus foo.module) and "bar" is the name of the hook. Each
 * hook has a defined set of parameters and a specified result type.
727
 *
728
729
730
 * To extend Drupal, a module need simply implement a hook. When Drupal wishes
 * to allow intervention from modules, it determines which modules implement a
 * hook and calls that hook in all enabled modules that implement it.
731
732
733
 *
 * The available hooks to implement are explained here in the Hooks section of
 * the developer documentation. The string "hook" is used as a placeholder for
734
735
736
 * the module name in the hook definitions. For example, if the module file is
 * called example.module, then hook_help() as implemented by that module would
 * be defined as example_help().
737
 *
738
739
740
741
 * The example functions included are not part of the Drupal core, they are
 * just models that you can modify. Only the hooks implemented within modules
 * are executed when running Drupal.
 *
742
 * See also @link themeable the themeable group page. @endlink
743
744
745
 */

/**
746
 * Determines whether a module implements a hook.
747
748
749
750
751
 *
 * @param $module
 *   The name of the module (without the .module extension).
 * @param $hook
 *   The name of the hook (e.g. "help" or "menu").
752
 *
753
754
755
756
757
 * @return
 *   TRUE if the module is both installed and enabled, and the hook is
 *   implemented in that module.
 */
function module_hook($module, $hook) {
758
759
760
761
762
763
764
765
766
767
768
769
770
771
  $function = $module . '_' . $hook;
  if (function_exists($function)) {
    return TRUE;
  }
  // If the hook implementation does not exist, check whether it may live in an
  // optional include file registered via hook_hook_info().
  $hook_info = module_hook_info();
  if (isset($hook_info[$hook]['group'])) {
    module_load_include('inc', $module, $module . '.' . $hook_info[$hook]['group']);
    if (function_exists($function)) {
      return TRUE;
    }
  }
  return FALSE;
772
773
}

Dries's avatar
   
Dries committed
774
/**
775
 * Determines which modules are implementing a hook.
Dries's avatar
   
Dries committed
776
777
 *
 * @param $hook
778
 *   The name of the hook (e.g. "help" or "menu").
779
 *
Dries's avatar
   
Dries committed
780
781
 * @return
 *   An array with the names of the modules which are implementing this hook.
782
 *
783
 * @see module_implements_write_cache()
Dries's avatar
   
Dries committed
784
 */
785
function module_implements($hook) {
786
  // Use the advanced drupal_static() pattern, since this is called very often.
787
788
789
790
791
  static $drupal_static_fast;
  if (!isset($drupal_static_fast)) {
    $drupal_static_fast['implementations'] = &drupal_static(__FUNCTION__);
  }
  $implementations = &$drupal_static_fast['implementations'];
792

793
794
  // Fetch implementations from cache.
  if (empty($implementations)) {
795
    $implementations = cache('bootstrap')->get('module_implements');
796
797
798
799
800
801
802
803
    if ($implementations === FALSE) {
      $implementations = array();
    }
    else {
      $implementations = $implementations->data;
    }
  }

804
  if (!isset($implementations[$hook])) {
805
806
807
    // The hook is not cached, so ensure that whether or not it has
    // implementations, that the cache is updated at the end of the request.
    $implementations['#write_cache'] = TRUE;
808
    $hook_info = module_hook_info();
809
    $implementations[$hook] = array();
810
    foreach (module_list() as $module) {
811
      $include_file = isset($hook_info[$hook]['group']) && module_load_include('inc', $module, $module . '.' . $hook_info[$hook]['group']);
812
813
814
      // Since module_hook() may needlessly try to load the include file again,
      // function_exists() is used directly here.
      if (function_exists($module . '_' . $hook)) {
815
        $implementations[$hook][$module] = $include_file ? $hook_info[$hook]['group'] : FALSE;
816
817
      }
    }
818
819
820
821
822
    // Allow modules to change the weight of specific implementations but avoid
    // an infinite loop.
    if ($hook != 'module_implements_alter') {
      drupal_alter('module_implements', $implementations[$hook], $hook);
    }
823
824
  }
  else {
825
826
827
828
829
830
    foreach ($implementations[$hook] as $module => $group) {
      // If this hook implementation is stored in a lazy-loaded file, so include
      // that file first.
      if ($group) {
        module_load_include('inc', $module, "$module.$group");
      }
831
      // It is possible that a module removed a hook implementation without the
832
833
834
835
836
      // implementations cache being rebuilt yet, so we check whether the
      // function exists on each request to avoid undefined function errors.
      // Since module_hook() may needlessly try to load the include file again,
      // function_exists() is used directly here.
      if (!function_exists($module . '_' . $hook)) {
837
838
839
840
        // Clear out the stale implementation from the cache and force a cache
        // refresh to forget about no longer existing hook implementations.
        unset($implementations[$hook][$module]);
        $implementations['#write_cache'] = TRUE;
Dries's avatar
   
Dries committed
841
842
      }
    }
843
  }
Dries's avatar
   
Dries committed
844

845
  return array_keys($implementations[$hook]);
846
847
848
}

/**
849
 * Regenerates the stored list of hook implementations.
850
851
852
853
854
855
856
 */
function module_implements_reset() {
  // We maintain a persistent cache of hook implementations in addition to the
  // static cache to avoid looping through every module and every hook on each
  // request. Benchmarks show that the benefit of this caching outweighs the
  // additional database hit even when using the default database caching
  // backend and only a small number of modules are enabled. The cost of the
857
858
859
860
  // cache('bootstrap')->get() is more or less constant and reduced further when
  // non-database caching backends are used, so there will be more significant
  // gains when a large number of modules are installed or hooks invoked, since
  // this can quickly lead to module_hook() being called several thousand times
861
862
863
864
865
866
  // per request.
  drupal_static_reset('module_implements');
  cache('bootstrap')->set('module_implements', array());
  drupal_static_reset('module_hook_info');
  drupal_static_reset('drupal_alter');
  cache('bootstrap')->delete('hook_info');
867
868
869
}

/**
870
871
872
873
874
875
876
877
 * Retrieves a list of hooks that are declared through hook_hook_info().
 *
 * @return
 *   An associative array whose keys are hook names and whose values are an
 *   associative array containing a group name. The structure of the array
 *   is the same as the return value of hook_hook_info().
 *
 * @see hook_hook_info()
878
879
 */
function module_hook_info() {
880
881
882
883
884
885
  // When this function is indirectly invoked from bootstrap_invoke_all() prior
  // to all modules being loaded, we do not want to cache an incomplete
  // hook_hook_info() result, so instead return an empty array. This requires
  // bootstrap hook implementations to reside in the .module file, which is
  // optimal for performance anyway.
  if (!module_load_all(NULL)) {
886
887
888
    return array();
  }
  $hook_info = &drupal_static(__FUNCTION__);
889

890
891
  if (!isset($hook_info)) {
    $hook_info = array();
892
    $cache = cache('bootstrap')->get('hook_info');
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
    if ($cache === FALSE) {
      // Rebuild the cache and save it.
      // We can't use module_invoke_all() here or it would cause an infinite
      // loop.
      foreach (module_list() as $module) {
        $function = $module . '_hook_info';
        if (function_exists($function)) {
          $result = $function();
          if (isset($result) && is_array($result)) {
            $hook_info = array_merge_recursive($hook_info, $result);
          }
        }
      }
      // We can't use drupal_alter() for the same reason as above.
      foreach (module_list() as $module) {
        $function = $module . '_hook_info_alter';
        if (function_exists($function)) {
          $function($hook_info);
        }
      }
913
      cache('bootstrap')->set('hook_info', $hook_info);
914
915
916
917
918
919
920
    }
    else {
      $hook_info = $cache->data;
    }
  }

  return $hook_info;
921
922
}

923
924
925
926
927
928
929
/**
 * Writes the hook implementation cache.
 *
 * @see module_implements()
 */
function module_implements_write_cache() {
  $implementations = &drupal_static('module_implements');
930
931
932
933
  // Check whether we need to write the cache. We do not want to cache hooks
  // which are only invoked on HTTP POST requests since these do not need to be
  // optimized as tightly, and not doing so keeps the cache entry smaller.
  if (isset($implementations['#write_cache']) && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD')) {
934
    unset($implementations['#write_cache']);
935
    cache('bootstrap')->set('module_implements', $implementations);
936
937
938
  }
}

939
/**
940
 * Invokes a hook in a particular module.
941
942
943
944
945
946
947
 *
 * @param $module
 *   The name of the module (without the .module extension).
 * @param $hook
 *   The name of the hook to invoke.
 * @param ...
 *   Arguments to pass to the hook implementation.
948
 *
949
950
951
 * @return
 *   The return value of the hook implementation.
 */
952
function module_invoke($module, $hook) {
953
  $args = func_get_args();
954
  // Remove $module and $hook from the arguments.
955
  unset($args[0], $args[1]);
956
  if (module_hook($module, $hook)) {
957
    return call_user_func_array($module . '_' . $hook, $args);
958
959
  }
}
960

961
/**
962
 * Invokes a hook in all enabled modules that implement it.
963
964
965
966
967
 *
 * @param $hook
 *   The name of the hook to invoke.
 * @param ...
 *   Arguments to pass to the hook.
968
 *
969
970
971
972
 * @return
 *   An array of return values of the hook implementations. If modules return
 *   arrays from their implementations, those are merged into one array.
 */
973
function module_invoke_all($hook) {
974
  $args = func_get_args();
975
  // Remove $hook from the arguments.
976
  unset($args[0]);
977
  $return = array();
978
  foreach (module_implements($hook) as $module) {
979
    $function = $module . '_' . $hook;
980
    if (function_exists($function)) {
981
982
983
984
      $result = call_user_func_array($function, $args);
      if (isset($result) && is_array($result)) {
        $return = array_merge_recursive($return, $result);
      }
985
      elseif (isset($result)) {
986
987
        $return[] = $result;
      }
988
    }
989
990
991
992
993
994
  }

  return $return;
}

/**
Dries's avatar
   
Dries committed
995
 * @} End of "defgroup hooks".
996
997
 */

998
/**
999
 * Returns an array of modules required by core.
1000
 */