Community Documentation

drupal_bootstrap

5 bootstrap.inc drupal_bootstrap($phase)
6 bootstrap.inc drupal_bootstrap($phase)
7 bootstrap.inc drupal_bootstrap($phase = NULL, $new_phase = TRUE)
8 bootstrap.inc drupal_bootstrap($phase = NULL, $new_phase = TRUE)

A string describing a phase of Drupal to load. Each phase adds to the previous one, so invoking a later phase automatically runs the earlier phases too. The most important usage is that if you want to access the Drupal database from a script without loading anything else, you can include bootstrap.inc, and call drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE).

Parameters

$phase: A constant. Allowed values are: DRUPAL_BOOTSTRAP_CONFIGURATION: initialize configuration. DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE: try to call a non-database cache fetch routine. DRUPAL_BOOTSTRAP_DATABASE: initialize database layer. DRUPAL_BOOTSTRAP_ACCESS: identify and reject banned hosts. DRUPAL_BOOTSTRAP_SESSION: initialize session handling. DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE: load bootstrap.inc and module.inc, start the variable system and try to serve a page from the cache. DRUPAL_BOOTSTRAP_LANGUAGE: identify the language used on the page. DRUPAL_BOOTSTRAP_PATH: set $_GET['q'] to Drupal path of request. DRUPAL_BOOTSTRAP_FULL: Drupal is fully loaded, validate and fix input data.

▾ 5 functions call drupal_bootstrap()

drupal_install_system in includes/install.inc
Callback to install the system module.
init_theme in includes/theme.inc
Initialize the theme system by loading the theme.
install_main in ./install.php
The Drupal installation happens in a series of steps. We begin by verifying that the current environment meets our minimum requirements. We then go on to verify that settings.php is properly configured. From there we connect to the configured database…
install_tasks in ./install.php
Tasks performed after the database is initialized.
statistics_exit in modules/statistics/statistics.module
Implementation of hook_exit().

File

includes/bootstrap.inc, line 1107
Functions that need to be loaded on every Drupal request.

Code

<?php
function drupal_bootstrap($phase) {
  static $phases = array(DRUPAL_BOOTSTRAP_CONFIGURATION, DRUPAL_BOOTSTRAP_EARLY_PAGE_CACHE, DRUPAL_BOOTSTRAP_DATABASE, DRUPAL_BOOTSTRAP_ACCESS, DRUPAL_BOOTSTRAP_SESSION, DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE, DRUPAL_BOOTSTRAP_LANGUAGE, DRUPAL_BOOTSTRAP_PATH, DRUPAL_BOOTSTRAP_FULL), $phase_index = 0;

  while ($phase >= $phase_index && isset($phases[$phase_index])) {
    $current_phase = $phases[$phase_index];
    unset($phases[$phase_index++]);
    _drupal_bootstrap($current_phase);
  }
}
?>

Comments

I wanted to create an API

I wanted to create an API that can be called to create and edit nodes and this API would be called very frequently. My problem was that it was creating a high server load since Drupal was bootstrapped every time. Here are some stress test results I ran (times in seconds based on 100 page loads):

DRUPAL_BOOTSTRAP_DATABASE 0.014402948244654
DRUPAL_BOOTSTRAP_FULL 0.23315950355144
my custom code (see below) 0.04217075819921

As you can see, database access only is the fastest - but I wanted to use node functions and they would not be available in that mode. The full bootstrap gives me that, but it's too slow. So I created my own code:

<?php
chdir
('../../../../..'); //the Drupal root, relative to the directory of the path
require_once './includes/bootstrap.inc';
require_once
'./includes/common.inc';
require_once
'./includes/module.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
drupal_load('module', 'i18n'); // I had to load i18n, otherwise I got some errors. If you don't use it, remove this
module_invoke('i18n', 'boot');
drupal_load('module', 'node');
module_invoke('node', 'boot');

global
$user;
$user->uid = 1;
print_r($user);
$node = node_load(123);
print_r($node);
?>

This let's my super user create and edit nodes, with a load time of less than 1/5 of DRUPAL_BOOTSTRAP_FULL

And this will also fail

Because other modules should be able to inject data into $node via hook_nodeapi(). This code will not allow them to do that. You shouldn't avoid a full bootstrap in that case.

Thanks, olbion!

This example is great! People always just post snippets, it's very hard to find an example that actually shows all the requires and setup functions! I probably need to buy a book...

Login or register to post comments