rebuild.php 1.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

/**
 * @file
 * Rebuilds all Drupal caches even when Drupal itself does not work.
 *
 * Needs a token query argument which can be calculated using the
 * scripts/rebuild_token_calculator.sh script.
 *
 * @see drupal_rebuild()
 */

use Drupal\Component\Utility\Crypt;
14
use Drupal\Core\DrupalKernel;
15
use Drupal\Core\Site\Settings;
16
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19 20 21 22

// Change the directory to the Drupal root.
chdir('..');

23
$autoloader = require_once __DIR__ . '/../autoload.php';
24
require_once __DIR__ . '/includes/utility.inc';
25

26 27 28 29
$request = Request::createFromGlobals();
// Manually resemble early bootstrap of DrupalKernel::boot().
require_once __DIR__ . '/includes/bootstrap.inc';
DrupalKernel::bootEnvironment();
30 31 32 33 34 35 36 37 38

try {
  Settings::initialize(dirname(__DIR__), DrupalKernel::findSitePath($request), $autoloader);
}
catch (HttpExceptionInterface $e) {
  $response = new Response('', $e->getStatusCode());
  $response->prepare($request)->send();
  exit;
}
39

40
if (Settings::get('rebuild_access', FALSE) ||
41 42 43
  ($request->query->get('token') && $request->query->get('timestamp') &&
    ((REQUEST_TIME - $request->query->get('timestamp')) < 300) &&
    Crypt::hashEquals(Crypt::hmacBase64($request->query->get('timestamp'), Settings::get('hash_salt')), $request->query->get('token'))
44
  )) {
45
  // Clear the APCu cache to ensure APCu class loader is reset.
46 47 48
  if (function_exists('apcu_clear_cache')) {
    apcu_clear_cache();
  }
49
  drupal_rebuild($autoloader, $request);
50 51
  drupal_set_message('Cache rebuild complete.');
}
52 53
$base_path = dirname(dirname($request->getBaseUrl()));
header('Location: ' . $base_path);