Drupal investigation

system.install 70KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the system module.
  5. */
  6. use Drupal\Component\Utility\Crypt;
  7. use Drupal\Component\Utility\Environment;
  8. use Drupal\Component\FileSystem\FileSystem;
  9. use Drupal\Component\Utility\OpCodeCache;
  10. use Drupal\Core\Path\AliasStorage;
  11. use Drupal\Core\Url;
  12. use Drupal\Core\Database\Database;
  13. use Drupal\Core\DrupalKernel;
  14. use Drupal\Core\Site\Settings;
  15. use Drupal\Core\StreamWrapper\PrivateStream;
  16. use Drupal\Core\StreamWrapper\PublicStream;
  17. use Drupal\system\SystemRequirements;
  18. use Symfony\Component\HttpFoundation\Request;
  19. /**
  20. * Implements hook_requirements().
  21. */
  22. function system_requirements($phase) {
  23. global $install_state;
  24. $requirements = [];
  25. // Report Drupal version
  26. if ($phase == 'runtime') {
  27. $requirements['drupal'] = [
  28. 'title' => t('Drupal'),
  29. 'value' => \Drupal::VERSION,
  30. 'severity' => REQUIREMENT_INFO,
  31. 'weight' => -10,
  32. ];
  33. // Display the currently active installation profile, if the site
  34. // is not running the default installation profile.
  35. $profile = drupal_get_profile();
  36. if ($profile != 'standard') {
  37. $info = system_get_info('module', $profile);
  38. $requirements['install_profile'] = [
  39. 'title' => t('Installation profile'),
  40. 'value' => t('%profile_name (%profile-%version)', [
  41. '%profile_name' => $info['name'],
  42. '%profile' => $profile,
  43. '%version' => $info['version']
  44. ]),
  45. 'severity' => REQUIREMENT_INFO,
  46. 'weight' => -9
  47. ];
  48. }
  49. // Warn if any experimental modules are installed.
  50. $experimental = [];
  51. $enabled_modules = \Drupal::moduleHandler()->getModuleList();
  52. foreach ($enabled_modules as $module => $data) {
  53. $info = system_get_info('module', $module);
  54. if ($info['package'] === 'Core (Experimental)') {
  55. $experimental[$module] = $info['name'];
  56. }
  57. }
  58. if (!empty($experimental)) {
  59. $requirements['experimental'] = [
  60. 'title' => t('Experimental modules enabled'),
  61. 'value' => t('Experimental modules found: %module_list. <a href=":url">Experimental modules</a> are provided for testing purposes only. Use at your own risk.', ['%module_list' => implode(', ', $experimental), ':url' => 'https://www.drupal.org/core/experimental']),
  62. 'severity' => REQUIREMENT_WARNING,
  63. ];
  64. }
  65. }
  66. // Web server information.
  67. $software = \Drupal::request()->server->get('SERVER_SOFTWARE');
  68. $requirements['webserver'] = [
  69. 'title' => t('Web server'),
  70. 'value' => $software,
  71. ];
  72. // Tests clean URL support.
  73. if ($phase == 'install' && $install_state['interactive'] && !isset($_GET['rewrite']) && strpos($software, 'Apache') !== FALSE) {
  74. // If the Apache rewrite module is not enabled, Apache version must be >=
  75. // 2.2.16 because of the FallbackResource directive in the root .htaccess
  76. // file. Since the Apache version reported by the server is dependent on the
  77. // ServerTokens setting in httpd.conf, we may not be able to determine if a
  78. // given config is valid. Thus we are unable to use version_compare() as we
  79. // need have three possible outcomes: the version of Apache is greater than
  80. // 2.2.16, is less than 2.2.16, or cannot be determined accurately. In the
  81. // first case, we encourage the use of mod_rewrite; in the second case, we
  82. // raise an error regarding the minimum Apache version; in the third case,
  83. // we raise a warning that the current version of Apache may not be
  84. // supported.
  85. $rewrite_warning = FALSE;
  86. $rewrite_error = FALSE;
  87. $apache_version_string = 'Apache';
  88. // Determine the Apache version number: major, minor and revision.
  89. if (preg_match('/Apache\/(\d+)\.?(\d+)?\.?(\d+)?/', $software, $matches)) {
  90. $apache_version_string = $matches[0];
  91. // Major version number
  92. if ($matches[1] < 2) {
  93. $rewrite_error = TRUE;
  94. }
  95. elseif ($matches[1] == 2) {
  96. if (!isset($matches[2])) {
  97. $rewrite_warning = TRUE;
  98. }
  99. elseif ($matches[2] < 2) {
  100. $rewrite_error = TRUE;
  101. }
  102. elseif ($matches[2] == 2) {
  103. if (!isset($matches[3])) {
  104. $rewrite_warning = TRUE;
  105. }
  106. elseif ($matches[3] < 16) {
  107. $rewrite_error = TRUE;
  108. }
  109. }
  110. }
  111. }
  112. else {
  113. $rewrite_warning = TRUE;
  114. }
  115. if ($rewrite_warning) {
  116. $requirements['apache_version'] = [
  117. 'title' => t('Apache version'),
  118. 'value' => $apache_version_string,
  119. 'severity' => REQUIREMENT_WARNING,
  120. 'description' => t('Due to the settings for ServerTokens in httpd.conf, it is impossible to accurately determine the version of Apache running on this server. The reported value is @reported, to run Drupal without mod_rewrite, a minimum version of 2.2.16 is needed.', ['@reported' => $apache_version_string]),
  121. ];
  122. }
  123. if ($rewrite_error) {
  124. $requirements['Apache version'] = [
  125. 'title' => t('Apache version'),
  126. 'value' => $apache_version_string,
  127. 'severity' => REQUIREMENT_ERROR,
  128. 'description' => t('The minimum version of Apache needed to run Drupal without mod_rewrite enabled is 2.2.16. See the <a href=":link">enabling clean URLs</a> page for more information on mod_rewrite.', [':link' => 'http://drupal.org/node/15365']),
  129. ];
  130. }
  131. if (!$rewrite_error && !$rewrite_warning) {
  132. $requirements['rewrite_module'] = [
  133. 'title' => t('Clean URLs'),
  134. 'value' => t('Disabled'),
  135. 'severity' => REQUIREMENT_WARNING,
  136. 'description' => t('Your server is capable of using clean URLs, but it is not enabled. Using clean URLs gives an improved user experience and is recommended. <a href=":link">Enable clean URLs</a>', [':link' => 'http://drupal.org/node/15365']),
  137. ];
  138. }
  139. }
  140. // Test PHP version and show link to phpinfo() if it's available
  141. $phpversion = $phpversion_label = phpversion();
  142. if (function_exists('phpinfo')) {
  143. if ($phase === 'runtime') {
  144. $phpversion_label = t('@phpversion (<a href=":url">more information</a>)', ['@phpversion' => $phpversion, ':url' => (new Url('system.php'))->toString()]);
  145. }
  146. $requirements['php'] = [
  147. 'title' => t('PHP'),
  148. 'value' => $phpversion_label,
  149. ];
  150. }
  151. else {
  152. $requirements['php'] = [
  153. 'title' => t('PHP'),
  154. 'value' => $phpversion_label,
  155. 'description' => t('The phpinfo() function has been disabled for security reasons. To see your server\'s phpinfo() information, change your PHP settings or contact your server administrator. For more information, <a href=":phpinfo">Enabling and disabling phpinfo()</a> handbook page.', [':phpinfo' => 'https://www.drupal.org/node/243993']),
  156. 'severity' => REQUIREMENT_INFO,
  157. ];
  158. }
  159. if (version_compare($phpversion, DRUPAL_MINIMUM_PHP) < 0) {
  160. $requirements['php']['description'] = t('Your PHP installation is too old. Drupal requires at least PHP %version.', ['%version' => DRUPAL_MINIMUM_PHP]);
  161. $requirements['php']['severity'] = REQUIREMENT_ERROR;
  162. // If PHP is old, it's not safe to continue with the requirements check.
  163. return $requirements;
  164. }
  165. // Suggest to update to at least 5.5.21 or 5.6.5 for disabling multiple
  166. // statements.
  167. if (($phase === 'install' || \Drupal::database()->driver() === 'mysql') && !SystemRequirements::phpVersionWithPdoDisallowMultipleStatements($phpversion)) {
  168. $requirements['php'] = [
  169. 'title' => t('PHP (multiple statement disabling)'),
  170. 'value' => $phpversion_label,
  171. 'description' => t('PHP versions higher than 5.6.5 or 5.5.21 provide built-in SQL injection protection for mysql databases. It is recommended to update.'),
  172. 'severity' => REQUIREMENT_INFO,
  173. ];
  174. }
  175. // Test for PHP extensions.
  176. $requirements['php_extensions'] = [
  177. 'title' => t('PHP extensions'),
  178. ];
  179. $missing_extensions = [];
  180. $required_extensions = [
  181. 'date',
  182. 'dom',
  183. 'filter',
  184. 'gd',
  185. 'hash',
  186. 'json',
  187. 'pcre',
  188. 'pdo',
  189. 'session',
  190. 'SimpleXML',
  191. 'SPL',
  192. 'tokenizer',
  193. 'xml',
  194. ];
  195. foreach ($required_extensions as $extension) {
  196. if (!extension_loaded($extension)) {
  197. $missing_extensions[] = $extension;
  198. }
  199. }
  200. if (!empty($missing_extensions)) {
  201. $description = t('Drupal requires you to enable the PHP extensions in the following list (see the <a href=":system_requirements">system requirements page</a> for more information):', [
  202. ':system_requirements' => 'https://www.drupal.org/requirements',
  203. ]);
  204. // We use twig inline_template to avoid twig's autoescape.
  205. $description = [
  206. '#type' => 'inline_template',
  207. '#template' => '{{ description }}{{ missing_extensions }}',
  208. '#context' => [
  209. 'description' => $description,
  210. 'missing_extensions' => [
  211. '#theme' => 'item_list',
  212. '#items' => $missing_extensions,
  213. ],
  214. ],
  215. ];
  216. $requirements['php_extensions']['value'] = t('Disabled');
  217. $requirements['php_extensions']['severity'] = REQUIREMENT_ERROR;
  218. $requirements['php_extensions']['description'] = $description;
  219. }
  220. else {
  221. $requirements['php_extensions']['value'] = t('Enabled');
  222. }
  223. if ($phase == 'install' || $phase == 'runtime') {
  224. // Check to see if OPcache is installed.
  225. if (!OpCodeCache::isEnabled()) {
  226. $requirements['php_opcache'] = [
  227. 'value' => t('Not enabled'),
  228. 'severity' => REQUIREMENT_WARNING,
  229. 'description' => t('PHP OPcode caching can improve your site\'s performance considerably. It is <strong>highly recommended</strong> to have <a href="http://php.net/manual/opcache.installation.php" target="_blank">OPcache</a> installed on your server.'),
  230. ];
  231. }
  232. else {
  233. $requirements['php_opcache']['value'] = t('Enabled');
  234. }
  235. $requirements['php_opcache']['title'] = t('PHP OPcode caching');
  236. }
  237. if ($phase != 'update') {
  238. // Test whether we have a good source of random bytes.
  239. $requirements['php_random_bytes'] = [
  240. 'title' => t('Random number generation'),
  241. ];
  242. try {
  243. $bytes = random_bytes(10);
  244. if (strlen($bytes) != 10) {
  245. throw new \Exception(t('Tried to generate 10 random bytes, generated @count', ['@count' => strlen($bytes)]));
  246. }
  247. $requirements['php_random_bytes']['value'] = t('Successful');
  248. }
  249. catch (\Exception $e) {
  250. // If /dev/urandom is not available on a UNIX-like system, check whether
  251. // open_basedir restrictions are the cause.
  252. $open_basedir_blocks_urandom = FALSE;
  253. if (DIRECTORY_SEPARATOR === '/' && !@is_readable('/dev/urandom')) {
  254. $open_basedir = ini_get('open_basedir');
  255. if ($open_basedir) {
  256. $open_basedir_paths = explode(PATH_SEPARATOR, $open_basedir);
  257. $open_basedir_blocks_urandom = !array_intersect(['/dev', '/dev/', '/dev/urandom'], $open_basedir_paths);
  258. }
  259. }
  260. $args = [
  261. ':drupal-php' => 'https://www.drupal.org/docs/7/system-requirements/php#csprng',
  262. '%exception_message' => $e->getMessage(),
  263. ];
  264. if ($open_basedir_blocks_urandom) {
  265. $requirements['php_random_bytes']['description'] = t('Drupal is unable to generate highly randomized numbers, which means certain security features like password reset URLs are not as secure as they should be. Instead, only a slow, less-secure fallback generator is available. The most likely cause is that open_basedir restrictions are in effect and /dev/urandom is not on the whitelist. See the <a href=":drupal-php">system requirements</a> page for more information. %exception_message', $args);
  266. }
  267. else {
  268. $requirements['php_random_bytes']['description'] = t('Drupal is unable to generate highly randomized numbers, which means certain security features like password reset URLs are not as secure as they should be. Instead, only a slow, less-secure fallback generator is available. See the <a href=":drupal-php">system requirements</a> page for more information. %exception_message', $args);
  269. }
  270. $requirements['php_random_bytes']['value'] = t('Less secure');
  271. $requirements['php_random_bytes']['severity'] = REQUIREMENT_ERROR;
  272. }
  273. }
  274. if ($phase == 'install' || $phase == 'update') {
  275. // Test for PDO (database).
  276. $requirements['database_extensions'] = [
  277. 'title' => t('Database support'),
  278. ];
  279. // Make sure PDO is available.
  280. $database_ok = extension_loaded('pdo');
  281. if (!$database_ok) {
  282. $pdo_message = t('Your web server does not appear to support PDO (PHP Data Objects). Ask your hosting provider if they support the native PDO extension. See the <a href=":link">system requirements</a> page for more information.', [
  283. ':link' => 'https://www.drupal.org/requirements/pdo',
  284. ]);
  285. }
  286. else {
  287. // Make sure at least one supported database driver exists.
  288. $drivers = drupal_detect_database_types();
  289. if (empty($drivers)) {
  290. $database_ok = FALSE;
  291. $pdo_message = t('Your web server does not appear to support any common PDO database extensions. Check with your hosting provider to see if they support PDO (PHP Data Objects) and offer any databases that <a href=":drupal-databases">Drupal supports</a>.', [
  292. ':drupal-databases' => 'https://www.drupal.org/requirements/database',
  293. ]);
  294. }
  295. // Make sure the native PDO extension is available, not the older PEAR
  296. // version. (See install_verify_pdo() for details.)
  297. if (!defined('PDO::ATTR_DEFAULT_FETCH_MODE')) {
  298. $database_ok = FALSE;
  299. $pdo_message = t('Your web server seems to have the wrong version of PDO installed. Drupal requires the PDO extension from PHP core. This system has the older PECL version. See the <a href=":link">system requirements</a> page for more information.', [
  300. ':link' => 'https://www.drupal.org/requirements/pdo#pecl',
  301. ]);
  302. }
  303. }
  304. if (!$database_ok) {
  305. $requirements['database_extensions']['value'] = t('Disabled');
  306. $requirements['database_extensions']['severity'] = REQUIREMENT_ERROR;
  307. $requirements['database_extensions']['description'] = $pdo_message;
  308. }
  309. else {
  310. $requirements['database_extensions']['value'] = t('Enabled');
  311. }
  312. }
  313. else {
  314. // Database information.
  315. $class = Database::getConnection()->getDriverClass('Install\\Tasks');
  316. $tasks = new $class();
  317. $requirements['database_system'] = [
  318. 'title' => t('Database system'),
  319. 'value' => $tasks->name(),
  320. ];
  321. $requirements['database_system_version'] = [
  322. 'title' => t('Database system version'),
  323. 'value' => Database::getConnection()->version(),
  324. ];
  325. }
  326. // Test PHP memory_limit
  327. $memory_limit = ini_get('memory_limit');
  328. $requirements['php_memory_limit'] = [
  329. 'title' => t('PHP memory limit'),
  330. 'value' => $memory_limit == -1 ? t('-1 (Unlimited)') : $memory_limit,
  331. ];
  332. if (!Environment::checkMemoryLimit(DRUPAL_MINIMUM_PHP_MEMORY_LIMIT, $memory_limit)) {
  333. $description = [];
  334. if ($phase == 'install') {
  335. $description['phase'] = t('Consider increasing your PHP memory limit to %memory_minimum_limit to help prevent errors in the installation process.', ['%memory_minimum_limit' => DRUPAL_MINIMUM_PHP_MEMORY_LIMIT]);
  336. }
  337. elseif ($phase == 'update') {
  338. $description['phase'] = t('Consider increasing your PHP memory limit to %memory_minimum_limit to help prevent errors in the update process.', ['%memory_minimum_limit' => DRUPAL_MINIMUM_PHP_MEMORY_LIMIT]);
  339. }
  340. elseif ($phase == 'runtime') {
  341. $description['phase'] = t('Depending on your configuration, Drupal can run with a %memory_limit PHP memory limit. However, a %memory_minimum_limit PHP memory limit or above is recommended, especially if your site uses additional custom or contributed modules.', ['%memory_limit' => $memory_limit, '%memory_minimum_limit' => DRUPAL_MINIMUM_PHP_MEMORY_LIMIT]);
  342. }
  343. if (!empty($description['phase'])) {
  344. if ($php_ini_path = get_cfg_var('cfg_file_path')) {
  345. $description['memory'] = t('Increase the memory limit by editing the memory_limit parameter in the file %configuration-file and then restart your web server (or contact your system administrator or hosting provider for assistance).', ['%configuration-file' => $php_ini_path]);
  346. }
  347. else {
  348. $description['memory'] = t('Contact your system administrator or hosting provider for assistance with increasing your PHP memory limit.');
  349. }
  350. $handbook_link = t('For more information, see the online handbook entry for <a href=":memory-limit">increasing the PHP memory limit</a>.', [':memory-limit' => 'https://www.drupal.org/node/207036']);
  351. $description = [
  352. '#type' => 'inline_template',
  353. '#template' => '{{ description_phase }} {{ description_memory }} {{ handbook }}',
  354. '#context' => [
  355. 'description_phase' => $description['phase'],
  356. 'description_memory' => $description['memory'],
  357. 'handbook' => $handbook_link,
  358. ],
  359. ];
  360. $requirements['php_memory_limit']['description'] = $description;
  361. $requirements['php_memory_limit']['severity'] = REQUIREMENT_WARNING;
  362. }
  363. }
  364. // Test configuration files and directory for writability.
  365. if ($phase == 'runtime') {
  366. $conf_errors = [];
  367. // Find the site path. Kernel service is not always available at this point,
  368. // but is preferred, when available.
  369. if (\Drupal::hasService('kernel')) {
  370. $site_path = \Drupal::service('site.path');
  371. }
  372. else {
  373. $site_path = DrupalKernel::findSitePath(Request::createFromGlobals());
  374. }
  375. // Allow system administrators to disable permissions hardening for the site
  376. // directory. This allows additional files in the site directory to be
  377. // updated when they are managed in a version control system.
  378. if (Settings::get('skip_permissions_hardening')) {
  379. $conf_errors[] = t('Protection disabled');
  380. // If permissions hardening is disabled, then only show a warning for a
  381. // writable file, as a reminder, rather than an error.
  382. $file_protection_severity = REQUIREMENT_WARNING;
  383. }
  384. else {
  385. // In normal operation, writable files or directories are an error.
  386. $file_protection_severity = REQUIREMENT_ERROR;
  387. if (!drupal_verify_install_file($site_path, FILE_NOT_WRITABLE, 'dir')) {
  388. $conf_errors[] = t("The directory %file is not protected from modifications and poses a security risk. You must change the directory's permissions to be non-writable.", ['%file' => $site_path]);
  389. }
  390. }
  391. foreach (['settings.php', 'settings.local.php', 'services.yml'] as $conf_file) {
  392. $full_path = $site_path . '/' . $conf_file;
  393. if (file_exists($full_path) && (Settings::get('skip_permissions_hardening') || !drupal_verify_install_file($full_path, FILE_EXIST | FILE_READABLE | FILE_NOT_WRITABLE))) {
  394. $conf_errors[] = t("The file %file is not protected from modifications and poses a security risk. You must change the file's permissions to be non-writable.", ['%file' => $full_path]);
  395. }
  396. }
  397. if (!empty($conf_errors)) {
  398. if (count($conf_errors) == 1) {
  399. $description = $conf_errors[0];
  400. }
  401. else {
  402. // We use twig inline_template to avoid double escaping.
  403. $description = [
  404. '#type' => 'inline_template',
  405. '#template' => '{{ configuration_error_list }}',
  406. '#context' => [
  407. 'configuration_error_list' => [
  408. '#theme' => 'item_list',
  409. '#items' => $conf_errors,
  410. ],
  411. ],
  412. ];
  413. }
  414. $requirements['configuration_files'] = [
  415. 'value' => t('Not protected'),
  416. 'severity' => $file_protection_severity,
  417. 'description' => $description,
  418. ];
  419. }
  420. else {
  421. $requirements['configuration_files'] = [
  422. 'value' => t('Protected'),
  423. ];
  424. }
  425. $requirements['configuration_files']['title'] = t('Configuration files');
  426. }
  427. // Test the contents of the .htaccess files.
  428. if ($phase == 'runtime') {
  429. // Try to write the .htaccess files first, to prevent false alarms in case
  430. // (for example) the /tmp directory was wiped.
  431. file_ensure_htaccess();
  432. $htaccess_files['public://.htaccess'] = [
  433. 'title' => t('Public files directory'),
  434. 'directory' => drupal_realpath('public://'),
  435. ];
  436. if (PrivateStream::basePath()) {
  437. $htaccess_files['private://.htaccess'] = [
  438. 'title' => t('Private files directory'),
  439. 'directory' => drupal_realpath('private://'),
  440. ];
  441. }
  442. $htaccess_files['temporary://.htaccess'] = [
  443. 'title' => t('Temporary files directory'),
  444. 'directory' => drupal_realpath('temporary://'),
  445. ];
  446. foreach ($htaccess_files as $htaccess_file => $info) {
  447. // Check for the string which was added to the recommended .htaccess file
  448. // in the latest security update.
  449. if (!file_exists($htaccess_file) || !($contents = @file_get_contents($htaccess_file)) || strpos($contents, 'Drupal_Security_Do_Not_Remove_See_SA_2013_003') === FALSE) {
  450. $url = 'https://www.drupal.org/SA-CORE-2013-003';
  451. $requirements[$htaccess_file] = [
  452. 'title' => $info['title'],
  453. 'value' => t('Not fully protected'),
  454. 'severity' => REQUIREMENT_ERROR,
  455. 'description' => t('See <a href=":url">@url</a> for information about the recommended .htaccess file which should be added to the %directory directory to help protect against arbitrary code execution.', [':url' => $url, '@url' => $url, '%directory' => $info['directory']]),
  456. ];
  457. }
  458. }
  459. }
  460. // Report cron status.
  461. if ($phase == 'runtime') {
  462. $cron_config = \Drupal::config('system.cron');
  463. // Cron warning threshold defaults to two days.
  464. $threshold_warning = $cron_config->get('threshold.requirements_warning');
  465. // Cron error threshold defaults to two weeks.
  466. $threshold_error = $cron_config->get('threshold.requirements_error');
  467. // Determine when cron last ran.
  468. $cron_last = \Drupal::state()->get('system.cron_last');
  469. if (!is_numeric($cron_last)) {
  470. $cron_last = \Drupal::state()->get('install_time', 0);
  471. }
  472. // Determine severity based on time since cron last ran.
  473. $severity = REQUIREMENT_INFO;
  474. if (REQUEST_TIME - $cron_last > $threshold_error) {
  475. $severity = REQUIREMENT_ERROR;
  476. }
  477. elseif (REQUEST_TIME - $cron_last > $threshold_warning) {
  478. $severity = REQUIREMENT_WARNING;
  479. }
  480. // Set summary and description based on values determined above.
  481. $summary = t('Last run @time ago', ['@time' => \Drupal::service('date.formatter')->formatTimeDiffSince($cron_last)]);
  482. $requirements['cron'] = [
  483. 'title' => t('Cron maintenance tasks'),
  484. 'severity' => $severity,
  485. 'value' => $summary,
  486. ];
  487. if ($severity != REQUIREMENT_INFO) {
  488. $requirements['cron']['description'][] = [
  489. [
  490. '#markup' => t('Cron has not run recently.'),
  491. '#suffix' => ' ',
  492. ],
  493. [
  494. '#markup' => t('For more information, see the online handbook entry for <a href=":cron-handbook">configuring cron jobs</a>.', [':cron-handbook' => 'https://www.drupal.org/cron']),
  495. '#suffix' => ' ',
  496. ],
  497. ];
  498. }
  499. $requirements['cron']['description'][] = [
  500. [
  501. '#type' => 'link',
  502. '#prefix' => '(',
  503. '#title' => t('more information'),
  504. '#suffix' => ')',
  505. '#url' => Url::fromRoute('system.cron_settings'),
  506. ],
  507. [
  508. '#prefix' => '<span class="cron-description__run-cron">',
  509. '#suffix' => '</span>',
  510. '#type' => 'link',
  511. '#title' => t('Run cron'),
  512. '#url' => Url::fromRoute('system.run_cron'),
  513. ],
  514. ];
  515. }
  516. if ($phase != 'install') {
  517. $filesystem_config = \Drupal::config('system.file');
  518. $directories = [
  519. PublicStream::basePath(),
  520. // By default no private files directory is configured. For private files
  521. // to be secure the admin needs to provide a path outside the webroot.
  522. PrivateStream::basePath(),
  523. file_directory_temp(),
  524. ];
  525. }
  526. // During an install we need to make assumptions about the file system
  527. // unless overrides are provided in settings.php.
  528. if ($phase == 'install') {
  529. $directories = [];
  530. if ($file_public_path = Settings::get('file_public_path')) {
  531. $directories[] = $file_public_path;
  532. }
  533. else {
  534. // If we are installing Drupal, the settings.php file might not exist yet
  535. // in the intended site directory, so don't require it.
  536. $request = Request::createFromGlobals();
  537. $site_path = DrupalKernel::findSitePath($request);
  538. $directories[] = $site_path . '/files';
  539. }
  540. if ($file_private_path = Settings::get('file_private_path')) {
  541. $directories[] = $file_private_path;
  542. }
  543. if (!empty($GLOBALS['config']['system.file']['path']['temporary'])) {
  544. $directories[] = $GLOBALS['config']['system.file']['path']['temporary'];
  545. }
  546. else {
  547. // If the temporary directory is not overridden use an appropriate
  548. // temporary path for the system.
  549. $directories[] = FileSystem::getOsTemporaryDirectory();
  550. }
  551. }
  552. // Check the config directory if it is defined in settings.php. If it isn't
  553. // defined, the installer will create a valid config directory later, but
  554. // during runtime we must always display an error.
  555. if (!empty($GLOBALS['config_directories'])) {
  556. foreach (array_keys(array_filter($GLOBALS['config_directories'])) as $type) {
  557. $directory = config_get_config_directory($type);
  558. // If we're installing Drupal try and create the config sync directory.
  559. if (!is_dir($directory) && $phase == 'install') {
  560. file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  561. }
  562. if (!is_dir($directory)) {
  563. if ($phase == 'install') {
  564. $description = t('An automated attempt to create the directory %directory failed, possibly due to a permissions problem. To proceed with the installation, either create the directory and modify its permissions manually or ensure that the installer has the permissions to create it automatically. For more information, see INSTALL.txt or the <a href=":handbook_url">online handbook</a>.', ['%directory' => $directory, ':handbook_url' => 'https://www.drupal.org/server-permissions']);
  565. }
  566. else {
  567. $description = t('The directory %directory does not exist.', ['%directory' => $directory]);
  568. }
  569. $requirements['config directory ' . $type] = [
  570. 'title' => t('Configuration directory: %type', ['%type' => $type]),
  571. 'description' => $description,
  572. 'severity' => REQUIREMENT_ERROR,
  573. ];
  574. }
  575. }
  576. }
  577. if ($phase != 'install' && (empty($GLOBALS['config_directories']) || empty($GLOBALS['config_directories'][CONFIG_SYNC_DIRECTORY]) )) {
  578. $requirements['config directories'] = [
  579. 'title' => t('Configuration directories'),
  580. 'value' => t('Not present'),
  581. 'description' => t('Your %file file must define the $config_directories variable as an array containing the names of directories in which configuration files can be found. It must contain a %sync_key key.', ['%file' => $site_path . '/settings.php', '%sync_key' => CONFIG_SYNC_DIRECTORY]),
  582. 'severity' => REQUIREMENT_ERROR,
  583. ];
  584. }
  585. $requirements['file system'] = [
  586. 'title' => t('File system'),
  587. ];
  588. $error = '';
  589. // For installer, create the directories if possible.
  590. foreach ($directories as $directory) {
  591. if (!$directory) {
  592. continue;
  593. }
  594. if ($phase == 'install') {
  595. file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  596. }
  597. $is_writable = is_writable($directory);
  598. $is_directory = is_dir($directory);
  599. if (!$is_writable || !$is_directory) {
  600. $description = '';
  601. $requirements['file system']['value'] = t('Not writable');
  602. if (!$is_directory) {
  603. $error = t('The directory %directory does not exist.', ['%directory' => $directory]);
  604. }
  605. else {
  606. $error = t('The directory %directory is not writable.', ['%directory' => $directory]);
  607. }
  608. // The files directory requirement check is done only during install and runtime.
  609. if ($phase == 'runtime') {
  610. $description = t('You may need to set the correct directory at the <a href=":admin-file-system">file system settings page</a> or change the current directory\'s permissions so that it is writable.', [':admin-file-system' => \Drupal::url('system.file_system_settings')]);
  611. }
  612. elseif ($phase == 'install') {
  613. // For the installer UI, we need different wording. 'value' will
  614. // be treated as version, so provide none there.
  615. $description = t('An automated attempt to create this directory failed, possibly due to a permissions problem. To proceed with the installation, either create the directory and modify its permissions manually or ensure that the installer has the permissions to create it automatically. For more information, see INSTALL.txt or the <a href=":handbook_url">online handbook</a>.', [':handbook_url' => 'https://www.drupal.org/server-permissions']);
  616. $requirements['file system']['value'] = '';
  617. }
  618. if (!empty($description)) {
  619. $description = [
  620. '#type' => 'inline_template',
  621. '#template' => '{{ error }} {{ description }}',
  622. '#context' => [
  623. 'error' => $error,
  624. 'description' => $description,
  625. ],
  626. ];
  627. $requirements['file system']['description'] = $description;
  628. $requirements['file system']['severity'] = REQUIREMENT_ERROR;
  629. }
  630. }
  631. else {
  632. // This function can be called before the config_cache table has been
  633. // created.
  634. if ($phase == 'install' || file_default_scheme() == 'public') {
  635. $requirements['file system']['value'] = t('Writable (<em>public</em> download method)');
  636. }
  637. else {
  638. $requirements['file system']['value'] = t('Writable (<em>private</em> download method)');
  639. }
  640. }
  641. }
  642. // See if updates are available in update.php.
  643. if ($phase == 'runtime') {
  644. $requirements['update'] = [
  645. 'title' => t('Database updates'),
  646. 'value' => t('Up to date'),
  647. ];
  648. // Check installed modules.
  649. $has_pending_updates = FALSE;
  650. foreach (\Drupal::moduleHandler()->getModuleList() as $module => $filename) {
  651. $updates = drupal_get_schema_versions($module);
  652. if ($updates !== FALSE) {
  653. $default = drupal_get_installed_schema_version($module);
  654. if (max($updates) > $default) {
  655. $has_pending_updates = TRUE;
  656. break;
  657. }
  658. }
  659. }
  660. if (!$has_pending_updates) {
  661. /** @var \Drupal\Core\Update\UpdateRegistry $post_update_registry */
  662. $post_update_registry = \Drupal::service('update.post_update_registry');
  663. $missing_post_update_functions = $post_update_registry->getPendingUpdateFunctions();
  664. if (!empty($missing_post_update_functions)) {
  665. $has_pending_updates = TRUE;
  666. }
  667. }
  668. if ($has_pending_updates) {
  669. $requirements['update']['severity'] = REQUIREMENT_ERROR;
  670. $requirements['update']['value'] = t('Out of date');
  671. $requirements['update']['description'] = t('Some modules have database schema updates to install. You should run the <a href=":update">database update script</a> immediately.', [':update' => \Drupal::url('system.db_update')]);
  672. }
  673. $requirements['entity_update'] = [
  674. 'title' => t('Entity/field definitions'),
  675. 'value' => t('Up to date'),
  676. ];
  677. // Verify that no entity updates are pending.
  678. if ($change_list = \Drupal::entityDefinitionUpdateManager()->getChangeSummary()) {
  679. $build = [];
  680. foreach ($change_list as $entity_type_id => $changes) {
  681. $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id);
  682. $build[] = [
  683. '#theme' => 'item_list',
  684. '#title' => $entity_type->getLabel(),
  685. '#items' => $changes,
  686. ];
  687. }
  688. $entity_update_issues = \Drupal::service('renderer')->renderPlain($build);
  689. $requirements['entity_update']['severity'] = REQUIREMENT_ERROR;
  690. $requirements['entity_update']['value'] = t('Mismatched entity and/or field definitions');
  691. $requirements['entity_update']['description'] = t('The following changes were detected in the entity type and field definitions. @updates', ['@updates' => $entity_update_issues]);
  692. }
  693. }
  694. // Verify the update.php access setting
  695. if ($phase == 'runtime') {
  696. if (Settings::get('update_free_access')) {
  697. $requirements['update access'] = [
  698. 'value' => t('Not protected'),
  699. 'severity' => REQUIREMENT_ERROR,
  700. 'description' => t('The update.php script is accessible to everyone without authentication check, which is a security risk. You must change the @settings_name value in your settings.php back to FALSE.', ['@settings_name' => '$settings[\'update_free_access\']']),
  701. ];
  702. }
  703. else {
  704. $requirements['update access'] = [
  705. 'value' => t('Protected'),
  706. ];
  707. }
  708. $requirements['update access']['title'] = t('Access to update.php');
  709. }
  710. // Display an error if a newly introduced dependency in a module is not resolved.
  711. if ($phase == 'update') {
  712. $profile = drupal_get_profile();
  713. $files = system_rebuild_module_data();
  714. foreach ($files as $module => $file) {
  715. // Ignore disabled modules and installation profiles.
  716. if (!$file->status || $module == $profile) {
  717. continue;
  718. }
  719. // Check the module's PHP version.
  720. $name = $file->info['name'];
  721. $php = $file->info['php'];
  722. if (version_compare($php, PHP_VERSION, '>')) {
  723. $requirements['php']['description'] .= t('@name requires at least PHP @version.', ['@name' => $name, '@version' => $php]);
  724. $requirements['php']['severity'] = REQUIREMENT_ERROR;
  725. }
  726. // Check the module's required modules.
  727. foreach ($file->requires as $requirement) {
  728. $required_module = $requirement['name'];
  729. // Check if the module exists.
  730. if (!isset($files[$required_module])) {
  731. $requirements["$module-$required_module"] = [
  732. 'title' => t('Unresolved dependency'),
  733. 'description' => t('@name requires this module.', ['@name' => $name]),
  734. 'value' => t('@required_name (Missing)', ['@required_name' => $required_module]),
  735. 'severity' => REQUIREMENT_ERROR,
  736. ];
  737. continue;
  738. }
  739. // Check for an incompatible version.
  740. $required_file = $files[$required_module];
  741. $required_name = $required_file->info['name'];
  742. $version = str_replace(\Drupal::CORE_COMPATIBILITY . '-', '', $required_file->info['version']);
  743. $compatibility = drupal_check_incompatibility($requirement, $version);
  744. if ($compatibility) {
  745. $compatibility = rtrim(substr($compatibility, 2), ')');
  746. $requirements["$module-$required_module"] = [
  747. 'title' => t('Unresolved dependency'),
  748. 'description' => t('@name requires this module and version. Currently using @required_name version @version', ['@name' => $name, '@required_name' => $required_name, '@version' => $version]),
  749. 'value' => t('@required_name (Version @compatibility required)', ['@required_name' => $required_name, '@compatibility' => $compatibility]),
  750. 'severity' => REQUIREMENT_ERROR,
  751. ];
  752. continue;
  753. }
  754. }
  755. }
  756. }
  757. // Test Unicode library
  758. include_once DRUPAL_ROOT . '/core/includes/unicode.inc';
  759. $requirements = array_merge($requirements, unicode_requirements());
  760. if ($phase == 'runtime') {
  761. // Check for update status module.
  762. if (!\Drupal::moduleHandler()->moduleExists('update')) {
  763. $requirements['update status'] = [
  764. 'value' => t('Not enabled'),
  765. 'severity' => REQUIREMENT_WARNING,
  766. 'description' => t('Update notifications are not enabled. It is <strong>highly recommended</strong> that you enable the Update Manager module from the <a href=":module">module administration page</a> in order to stay up-to-date on new releases. For more information, <a href=":update">Update status handbook page</a>.', [
  767. ':update' => 'https://www.drupal.org/documentation/modules/update',
  768. ':module' => \Drupal::url('system.modules_list'),
  769. ]),
  770. ];
  771. }
  772. else {
  773. $requirements['update status'] = [
  774. 'value' => t('Enabled'),
  775. ];
  776. }
  777. $requirements['update status']['title'] = t('Update notifications');
  778. if (Settings::get('rebuild_access')) {
  779. $requirements['rebuild access'] = [
  780. 'title' => t('Rebuild access'),
  781. 'value' => t('Enabled'),
  782. 'severity' => REQUIREMENT_ERROR,
  783. 'description' => t('The rebuild_access setting is enabled in settings.php. It is recommended to have this setting disabled unless you are performing a rebuild.'),
  784. ];
  785. }
  786. }
  787. // See if trusted hostnames have been configured, and warn the user if they
  788. // are not set.
  789. if ($phase == 'runtime') {
  790. $trusted_host_patterns = Settings::get('trusted_host_patterns');
  791. if (empty($trusted_host_patterns)) {
  792. $requirements['trusted_host_patterns'] = [
  793. 'title' => t('Trusted Host Settings'),
  794. 'value' => t('Not enabled'),
  795. 'description' => t('The trusted_host_patterns setting is not configured in settings.php. This can lead to security vulnerabilities. It is <strong>highly recommended</strong> that you configure this. See <a href=":url">Protecting against HTTP HOST Header attacks</a> for more information.', [':url' => 'https://www.drupal.org/node/1992030']),
  796. 'severity' => REQUIREMENT_ERROR,
  797. ];
  798. }
  799. else {
  800. $requirements['trusted_host_patterns'] = [
  801. 'title' => t('Trusted Host Settings'),
  802. 'value' => t('Enabled'),
  803. 'description' => t('The trusted_host_patterns setting is set to allow %trusted_host_patterns', ['%trusted_host_patterns' => join(', ', $trusted_host_patterns)]),
  804. ];
  805. }
  806. }
  807. // Check xdebug.max_nesting_level, as some pages will not work if it is too
  808. // low.
  809. if (extension_loaded('xdebug')) {
  810. // Setting this value to 256 was considered adequate on Xdebug 2.3
  811. // (see http://bugs.xdebug.org/bug_view_page.php?bug_id=00001100)
  812. $minimum_nesting_level = 256;
  813. $current_nesting_level = ini_get('xdebug.max_nesting_level');
  814. if ($current_nesting_level < $minimum_nesting_level) {
  815. $requirements['xdebug_max_nesting_level'] = [
  816. 'title' => t('Xdebug settings'),
  817. 'value' => t('xdebug.max_nesting_level is set to %value.', ['%value' => $current_nesting_level]),
  818. 'description' => t('Set <code>xdebug.max_nesting_level=@level</code> in your PHP configuration as some pages in your Drupal site will not work when this setting is too low.', ['@level' => $minimum_nesting_level]),
  819. 'severity' => REQUIREMENT_ERROR,
  820. ];
  821. }
  822. }
  823. // Warning for httpoxy on IIS with affected PHP versions
  824. // @see https://www.drupal.org/node/2783079
  825. if (strpos($software, 'Microsoft-IIS') !== FALSE
  826. && (
  827. version_compare(PHP_VERSION, '5.5.38', '<')
  828. || (version_compare(PHP_VERSION, '5.6.0', '>=') && version_compare(PHP_VERSION, '5.6.24', '<'))
  829. || (version_compare(PHP_VERSION, '7.0.0', '>=') && version_compare(PHP_VERSION, '7.0.9', '<'))
  830. )) {
  831. $dom = new \DOMDocument('1.0', 'UTF-8');
  832. $webconfig = file_get_contents('web.config');
  833. // If you are here the web.config file must - of course - be well formed.
  834. // But the PHP DOM component will throw warnings on some XML compliant
  835. // stuff, so silently parse the configuration file.
  836. @$dom->loadHTML($webconfig);
  837. $httpoxy_rewrite = FALSE;
  838. foreach ($dom->getElementsByTagName('rule') as $rule) {
  839. foreach ($rule->attributes as $attr) {
  840. if (@$attr->name == 'name' && @$attr->nodeValue == 'Erase HTTP_PROXY') {
  841. $httpoxy_rewrite = TRUE;
  842. break 2;
  843. }
  844. }
  845. }
  846. if (!$httpoxy_rewrite) {
  847. $requirements['iis_httpoxy_protection'] = [
  848. 'title' => t('IIS httpoxy protection'),
  849. 'value' => t('Your PHP runtime version is affected by the httpoxy vulnerability.'),
  850. 'description' => t('Either update your PHP runtime version or uncomment the "Erase HTTP_PROXY" rule in your web.config file and add HTTP_PROXY to the allowed headers list. See more details in the <a href=":link">security advisory</a>.', [':link' => 'https://www.drupal.org/SA-CORE-2016-003']),
  851. 'severity' => REQUIREMENT_ERROR,
  852. ];
  853. }
  854. }
  855. // Installations on Windows can run into limitations with MAX_PATH if the
  856. // Drupal root directory is too deep in the filesystem. Generally this shows
  857. // up in cached Twig templates and other public files with long directory or
  858. // file names. There is no definite root directory depth below which Drupal is
  859. // guaranteed to function correctly on Windows. Since problems are likely
  860. // with more than 100 characters in the Drupal root path, show an error.
  861. if (substr(PHP_OS, 0, 3) == 'WIN') {
  862. $depth = strlen(realpath(DRUPAL_ROOT . '/' . PublicStream::basePath()));
  863. if ($depth > 120) {
  864. $requirements['max_path_on_windows'] = [
  865. 'title' => t('Windows installation depth'),
  866. 'description' => t('The public files directory path is %depth characters. Paths longer than 120 characters will cause problems on Windows.', ['%depth' => $depth]),
  867. 'severity' => REQUIREMENT_ERROR,
  868. ];
  869. }
  870. }
  871. return $requirements;
  872. }
  873. /**
  874. * Implements hook_install().
  875. */
  876. function system_install() {
  877. // Populate the cron key state variable.
  878. $cron_key = Crypt::randomBytesBase64(55);
  879. \Drupal::state()->set('system.cron_key', $cron_key);
  880. // Populate the site UUID and default name (if not set).
  881. $site = \Drupal::configFactory()->getEditable('system.site');
  882. $site->set('uuid', \Drupal::service('uuid')->generate());
  883. if (!$site->get('name')) {
  884. $site->set('name', 'Drupal');
  885. }
  886. $site->save(TRUE);
  887. }
  888. /**
  889. * Implements hook_schema().
  890. */
  891. function system_schema() {
  892. $schema['key_value'] = [
  893. 'description' => 'Generic key-value storage table. See the state system for an example.',
  894. 'fields' => [
  895. 'collection' => [
  896. 'description' => 'A named collection of key and value pairs.',
  897. 'type' => 'varchar_ascii',
  898. 'length' => 128,
  899. 'not null' => TRUE,
  900. 'default' => '',
  901. ],
  902. 'name' => [
  903. 'description' => 'The key of the key-value pair. As KEY is a SQL reserved keyword, name was chosen instead.',
  904. 'type' => 'varchar_ascii',
  905. 'length' => 128,
  906. 'not null' => TRUE,
  907. 'default' => '',
  908. ],
  909. 'value' => [
  910. 'description' => 'The value.',
  911. 'type' => 'blob',
  912. 'not null' => TRUE,
  913. 'size' => 'big',
  914. ],
  915. ],
  916. 'primary key' => ['collection', 'name'],
  917. ];
  918. $schema['key_value_expire'] = [
  919. 'description' => 'Generic key/value storage table with an expiration.',
  920. 'fields' => [
  921. 'collection' => [
  922. 'description' => 'A named collection of key and value pairs.',
  923. 'type' => 'varchar_ascii',
  924. 'length' => 128,
  925. 'not null' => TRUE,
  926. 'default' => '',
  927. ],
  928. 'name' => [
  929. // KEY is an SQL reserved word, so use 'name' as the key's field name.
  930. 'description' => 'The key of the key/value pair.',
  931. 'type' => 'varchar_ascii',
  932. 'length' => 128,
  933. 'not null' => TRUE,
  934. 'default' => '',
  935. ],
  936. 'value' => [
  937. 'description' => 'The value of the key/value pair.',
  938. 'type' => 'blob',
  939. 'not null' => TRUE,
  940. 'size' => 'big',
  941. ],
  942. 'expire' => [
  943. 'description' => 'The time since Unix epoch in seconds when this item expires. Defaults to the maximum possible time.',
  944. 'type' => 'int',
  945. 'not null' => TRUE,
  946. 'default' => 2147483647,
  947. ],
  948. ],
  949. 'primary key' => ['collection', 'name'],
  950. 'indexes' => [
  951. 'all' => ['name', 'collection', 'expire'],
  952. 'expire' => ['expire'],
  953. ],
  954. ];
  955. $schema['sequences'] = [
  956. 'description' => 'Stores IDs.',
  957. 'fields' => [
  958. 'value' => [
  959. 'description' => 'The value of the sequence.',
  960. 'type' => 'serial',
  961. 'unsigned' => TRUE,
  962. 'not null' => TRUE,
  963. ],
  964. ],
  965. 'primary key' => ['value'],
  966. ];
  967. $schema['sessions'] = [
  968. 'description' => "Drupal's session handlers read and write into the sessions table. Each record represents a user session, either anonymous or authenticated.",
  969. 'fields' => [
  970. 'uid' => [
  971. 'description' => 'The {users}.uid corresponding to a session, or 0 for anonymous user.',
  972. 'type' => 'int',
  973. 'unsigned' => TRUE,
  974. 'not null' => TRUE,
  975. ],
  976. 'sid' => [
  977. 'description' => "A session ID (hashed). The value is generated by Drupal's session handlers.",
  978. 'type' => 'varchar_ascii',
  979. 'length' => 128,
  980. 'not null' => TRUE,
  981. ],
  982. 'hostname' => [
  983. 'description' => 'The IP address that last used this session ID (sid).',
  984. 'type' => 'varchar_ascii',
  985. 'length' => 128,
  986. 'not null' => TRUE,
  987. 'default' => '',
  988. ],
  989. 'timestamp' => [
  990. 'description' => 'The Unix timestamp when this session last requested a page. Old records are purged by PHP automatically.',
  991. 'type' => 'int',
  992. 'not null' => TRUE,
  993. 'default' => 0,
  994. ],
  995. 'session' => [
  996. 'description' => 'The serialized contents of $_SESSION, an array of name/value pairs that persists across page requests by this session ID. Drupal loads $_SESSION from here at the start of each request and saves it at the end.',
  997. 'type' => 'blob',
  998. 'not null' => FALSE,
  999. 'size' => 'big',
  1000. ],
  1001. ],
  1002. 'primary key' => [
  1003. 'sid',
  1004. ],
  1005. 'indexes' => [
  1006. 'timestamp' => ['timestamp'],
  1007. 'uid' => ['uid'],
  1008. ],
  1009. 'foreign keys' => [
  1010. 'session_user' => [
  1011. 'table' => 'users',
  1012. 'columns' => ['uid' => 'uid'],
  1013. ],
  1014. ],
  1015. ];
  1016. // Create the url_alias table. The alias_storage service can auto-create its
  1017. // table, but this relies on exceptions being thrown. These exceptions will be
  1018. // thrown every request until an alias is created.
  1019. $schema['url_alias'] = AliasStorage::schemaDefinition();
  1020. return $schema;
  1021. }
  1022. /**
  1023. * Change two fields on the default menu link storage to be serialized data.
  1024. */
  1025. function system_update_8001(&$sandbox = NULL) {
  1026. $database = \Drupal::database();
  1027. $schema = $database->schema();
  1028. if ($schema->tableExists('menu_tree')) {
  1029. if (!isset($sandbox['current'])) {
  1030. // Converting directly to blob can cause problems with reading out and
  1031. // serializing the string data later on postgres, so rename the existing
  1032. // columns and create replacement ones to hold the serialized objects.
  1033. $old_fields = [
  1034. 'title' => [
  1035. 'description' => 'The text displayed for the link.',
  1036. 'type' => 'varchar',
  1037. 'length' => 255,
  1038. 'not null' => TRUE,
  1039. 'default' => '',
  1040. ],
  1041. 'description' => [
  1042. 'description' => 'The description of this link - used for admin pages and title attribute.',
  1043. 'type' => 'text',
  1044. 'not null' => FALSE,
  1045. ],
  1046. ];
  1047. foreach ($old_fields as $name => $spec) {
  1048. $schema->changeField('menu_tree', $name, 'system_update_8001_' . $name, $spec);
  1049. }
  1050. $spec = [
  1051. 'description' => 'The title for the link. May be a serialized TranslatableMarkup.',
  1052. 'type' => 'blob',
  1053. 'size' => 'big',
  1054. 'not null' => FALSE,
  1055. 'serialize' => TRUE,
  1056. ];
  1057. $schema->addField('menu_tree', 'title', $spec);
  1058. $spec = [
  1059. 'description' => 'The description of this link - used for admin pages and title attribute.',
  1060. 'type' => 'blob',
  1061. 'size' => 'big',
  1062. 'not null' => FALSE,
  1063. 'serialize' => TRUE,
  1064. ];
  1065. $schema->addField('menu_tree', 'description', $spec);
  1066. $sandbox['current'] = 0;
  1067. $sandbox['max'] = $database->query('SELECT COUNT(mlid) FROM {menu_tree}')
  1068. ->fetchField();
  1069. }
  1070. $menu_links = $database->queryRange('SELECT mlid, system_update_8001_title AS title, system_update_8001_description AS description FROM {menu_tree} ORDER BY mlid ASC', $sandbox['current'], $sandbox['current'] + 50)
  1071. ->fetchAllAssoc('mlid');
  1072. foreach ($menu_links as $menu_link) {
  1073. $menu_link = (array) $menu_link;
  1074. // Convert title and description to serialized strings.
  1075. $menu_link['title'] = serialize($menu_link['title']);
  1076. $menu_link['description'] = serialize($menu_link['description']);
  1077. $database->update('menu_tree')
  1078. ->fields($menu_link)
  1079. ->condition('mlid', $menu_link['mlid'])
  1080. ->execute();
  1081. $sandbox['current']++;
  1082. }
  1083. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['current'] / $sandbox['max']);
  1084. if ($sandbox['#finished'] >= 1) {
  1085. // Drop unnecessary fields from {menu_tree}.
  1086. $schema->dropField('menu_tree', 'system_update_8001_title');
  1087. $schema->dropField('menu_tree', 'title_arguments');
  1088. $schema->dropField('menu_tree', 'title_context');
  1089. $schema->dropField('menu_tree', 'system_update_8001_description');
  1090. }
  1091. return t('Menu links converted');
  1092. }
  1093. else {
  1094. return t('Menu link conversion skipped, because the {menu_tree} table did not exist yet.');
  1095. }
  1096. }
  1097. /**
  1098. * Removes the system.filter configuration.
  1099. */
  1100. function system_update_8002() {
  1101. \Drupal::configFactory()->getEditable('system.filter')->delete();
  1102. return t('The system.filter configuration has been moved to a container parameter, see default.services.yml for more information.');
  1103. }
  1104. /**
  1105. * Change the index on the {router} table.
  1106. */
  1107. function system_update_8003() {
  1108. $database = \Drupal::database();
  1109. $database->schema()->dropIndex('router', 'pattern_outline_fit');
  1110. $database->schema()->addIndex(
  1111. 'router',
  1112. 'pattern_outline_parts',
  1113. ['pattern_outline', 'number_parts'],
  1114. [
  1115. 'fields' => [
  1116. 'pattern_outline' => [
  1117. 'description' => 'The pattern',
  1118. 'type' => 'varchar',
  1119. 'length' => 255,
  1120. 'not null' => TRUE,
  1121. 'default' => '',
  1122. ],
  1123. 'number_parts' => [
  1124. 'description' => 'Number of parts in this router path.',
  1125. 'type' => 'int',
  1126. 'not null' => TRUE,
  1127. 'default' => 0,
  1128. 'size' => 'small',
  1129. ],
  1130. ],
  1131. ]
  1132. );
  1133. }
  1134. /**
  1135. * Add a (id, default_langcode, langcode) composite index to entities.
  1136. */
  1137. function system_update_8004() {
  1138. // \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema was changed in
  1139. // https://www.drupal.org/node/2261669 to include a (id, default_langcode,
  1140. // langcode) compound index, but this update function wasn't added until
  1141. // https://www.drupal.org/node/2542748. Regenerate the related schemas to
  1142. // ensure they match the currently expected status.
  1143. $manager = \Drupal::entityDefinitionUpdateManager();
  1144. foreach (array_keys(\Drupal::entityManager()
  1145. ->getDefinitions()) as $entity_type_id) {
  1146. // Only update the entity type if it already exists. This condition is
  1147. // needed in case new entity types are introduced after this update.
  1148. if ($entity_type = $manager->getEntityType($entity_type_id)) {
  1149. $manager->updateEntityType($entity_type);
  1150. }
  1151. }
  1152. }
  1153. /**
  1154. * Place local actions and tasks blocks in every theme.
  1155. */
  1156. function system_update_8005() {
  1157. // When block module is not installed, there is nothing that could be done
  1158. // except showing a warning.
  1159. if (!\Drupal::moduleHandler()->moduleExists('block')) {
  1160. return t('Block module is not enabled so local actions and tasks which have been converted to blocks, are not visible anymore.');
  1161. }
  1162. $config_factory = \Drupal::configFactory();
  1163. /** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler */
  1164. $theme_handler = \Drupal::service('theme_handler');
  1165. $custom_themes_installed = FALSE;
  1166. $message = NULL;
  1167. $langcode = \Drupal::service('language_manager')->getCurrentLanguage()->getId();
  1168. $local_actions_default_settings = [
  1169. 'plugin' => 'local_actions_block',
  1170. 'region' => 'content',
  1171. 'settings.label' => 'Primary admin actions',
  1172. 'settings.label_display' => 0,
  1173. 'settings.cache.max_age' => 0,
  1174. 'visibility' => [],
  1175. 'weight' => 0,
  1176. 'langcode' => $langcode,
  1177. ];
  1178. $tabs_default_settings = [
  1179. 'plugin' => 'local_tasks_block',
  1180. 'region' => 'content',
  1181. 'settings.label' => 'Tabs',
  1182. 'settings.label_display' => 0,
  1183. 'settings.cache.max_age' => 0,
  1184. 'visibility' => [],
  1185. 'weight' => 0,
  1186. 'langcode' => $langcode,
  1187. ];
  1188. foreach ($theme_handler->listInfo() as $theme) {
  1189. $theme_name = $theme->getName();
  1190. switch ($theme_name) {
  1191. case 'bartik':
  1192. $name = 'block.block.bartik_local_actions';
  1193. $values = [
  1194. 'id' => 'bartik_local_actions',
  1195. 'weight' => -1,
  1196. ] + $local_actions_default_settings;
  1197. _system_update_create_block($name, $theme_name, $values);
  1198. $name = 'block.block.bartik_local_tasks';
  1199. $values = [
  1200. 'id' => 'bartik_local_tasks',
  1201. 'weight' => -7,
  1202. ] + $tabs_default_settings;
  1203. _system_update_create_block($name, $theme_name, $values);
  1204. // Help region has been removed so all the blocks inside has to be moved
  1205. // to content region.
  1206. $weight = -6;
  1207. $blocks = [];
  1208. foreach ($config_factory->listAll('block.block.') as $block_config) {
  1209. $block = $config_factory->getEditable($block_config);
  1210. if ($block->get('theme') == 'bartik' && $block->get('region') == 'help') {
  1211. $blocks[] = $block;
  1212. }
  1213. }
  1214. // Sort blocks by block weight.
  1215. uasort($blocks, function ($a, $b) {
  1216. return $a->get('weight') - $b->get('weight');
  1217. });
  1218. // Move blocks to content region and set them in right order by their
  1219. // weight.
  1220. foreach ($blocks as $block) {
  1221. $block->set('region', 'content');
  1222. $block->set('weight', $weight++);
  1223. $block->save();
  1224. }
  1225. break;
  1226. case 'seven':
  1227. $name = 'block.block.seven_local_actions';
  1228. $values = [
  1229. 'id' => 'seven_local_actions',
  1230. 'weight' => -10,
  1231. ] + $local_actions_default_settings;
  1232. _system_update_create_block($name, $theme_name, $values);
  1233. $name = 'block.block.seven_primary_local_tasks';
  1234. $values = [
  1235. 'region' => 'header',
  1236. 'id' => 'seven_primary_local_tasks',
  1237. 'settings.label' => 'Primary tabs',
  1238. 'settings.primary' => TRUE,
  1239. 'settings.secondary' => FALSE,
  1240. ] + $tabs_default_settings;
  1241. _system_update_create_block($name, $theme_name, $values);
  1242. $name = 'block.block.seven_secondary_local_tasks';
  1243. $values = [
  1244. 'region' => 'pre_content',
  1245. 'id' => 'seven_secondary_local_tasks',
  1246. 'settings.label' => 'Secondary tabs',
  1247. 'settings.primary' => FALSE,
  1248. 'settings.secondary' => TRUE,
  1249. ] + $tabs_default_settings;
  1250. _system_update_create_block($name, $theme_name, $values);
  1251. break;
  1252. case 'stark':
  1253. $name = 'block.block.stark_local_actions';
  1254. $values = [
  1255. 'id' => 'stark_local_actions',
  1256. ] + $local_actions_default_settings;
  1257. _system_update_create_block($name, $theme_name, $values);
  1258. $name = 'block.block.stark_local_tasks';
  1259. $values = [
  1260. 'id' => 'stark_local_tasks',
  1261. ] + $tabs_default_settings;
  1262. _system_update_create_block($name, $theme_name, $values);
  1263. break;
  1264. case 'classy':
  1265. case 'stable':
  1266. // Don't place any blocks or trigger custom themes installed warning.
  1267. break;
  1268. default:
  1269. $custom_themes_installed = TRUE;
  1270. $name = 'block.block.' . $theme_name . '_local_actions';
  1271. $values = [
  1272. 'id' => $theme_name . '_local_actions',
  1273. 'weight' => -10,
  1274. ] + $local_actions_default_settings;
  1275. _system_update_create_block($name, $theme_name, $values);
  1276. $name = sprintf('block.block.%s_local_tasks', $theme_name);
  1277. $values = [
  1278. 'id' => $theme_name . '_local_tasks',
  1279. 'weight' => -20,
  1280. ] + $tabs_default_settings;
  1281. _system_update_create_block($name, $theme_name, $values);
  1282. break;
  1283. }
  1284. }
  1285. if ($custom_themes_installed) {
  1286. $message = t('Because your site has custom theme(s) installed, we had to set local actions and tasks blocks into the content region. Please manually review the block configurations and remove the removed variables from your templates.');
  1287. }
  1288. return $message;
  1289. }
  1290. /**
  1291. * Place branding blocks in every theme.
  1292. */
  1293. function system_update_8006() {
  1294. // When block module is not installed, there is nothing that could be done
  1295. // except showing a warning.
  1296. if (!\Drupal::moduleHandler()->moduleExists('block')) {
  1297. return t('Block module is not enabled so site branding elements, which have been converted to a block, are not visible anymore.');
  1298. }
  1299. /** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler */
  1300. $theme_handler = \Drupal::service('theme_handler');
  1301. $custom_themes_installed = FALSE;
  1302. $message = NULL;
  1303. $langcode = \Drupal::service('language_manager')->getCurrentLanguage()->getId();
  1304. $site_branding_default_settings = [
  1305. 'plugin' => 'system_branding_block',
  1306. 'region' => 'content',
  1307. 'settings.label' => 'Site branding',
  1308. 'settings.label_display' => 0,
  1309. 'visibility' => [],
  1310. 'weight' => 0,
  1311. 'langcode' => $langcode,
  1312. ];
  1313. foreach ($theme_handler->listInfo() as $theme) {
  1314. $theme_name = $theme->getName();
  1315. switch ($theme_name) {
  1316. case 'bartik':
  1317. $name = 'block.block.bartik_branding';
  1318. $values = [
  1319. 'id' => 'bartik_branding',
  1320. 'region' => 'header',
  1321. ] + $site_branding_default_settings;
  1322. _system_update_create_block($name, $theme_name, $values);
  1323. break;
  1324. case 'stark':
  1325. $name = 'block.block.stark_branding';
  1326. $values = [
  1327. 'id' => 'stark_branding',
  1328. 'region' => 'header',
  1329. ] + $site_branding_default_settings;
  1330. _system_update_create_block($name, $theme_name, $values);
  1331. break;
  1332. case 'seven':
  1333. case 'classy':
  1334. case 'stable':
  1335. // Don't place any blocks or trigger custom themes installed warning.
  1336. break;
  1337. default:
  1338. $custom_themes_installed = TRUE;
  1339. $name = sprintf('block.block.%s_branding', $theme_name);
  1340. $values = [
  1341. 'id' => sprintf('%s_branding', $theme_name),
  1342. 'region' => 'content',
  1343. 'weight' => '-50',
  1344. ] + $site_branding_default_settings;
  1345. _system_update_create_block($name, $theme_name, $values);
  1346. break;
  1347. }
  1348. }
  1349. if ($custom_themes_installed) {
  1350. $message = t('Because your site has custom theme(s) installed, we had to set the branding block into the content region. Please manually review the block configuration and remove the site name, slogan, and logo variables from your templates.');
  1351. }
  1352. return $message;
  1353. }
  1354. /**
  1355. * Helper function to create block configuration objects for an update.
  1356. *
  1357. * @param string $name
  1358. * The name of the config object.
  1359. * @param string $theme_name
  1360. * The name of the theme the block is associated with.
  1361. * @param array $values
  1362. * The block config values.
  1363. */
  1364. function _system_update_create_block($name, $theme_name, array $values) {
  1365. if (!\Drupal::service('config.storage')->exists($name)) {
  1366. $block = \Drupal::configFactory()->getEditable($name);
  1367. $values['uuid'] = \Drupal::service('uuid')->generate();
  1368. $values['theme'] = $theme_name;
  1369. $values['dependencies.theme'] = [$theme_name];
  1370. foreach ($values as $key => $value) {
  1371. $block->set($key, $value);
  1372. }
  1373. $block->save();
  1374. }
  1375. }
  1376. /**
  1377. * Set langcode fields to be ASCII-only.
  1378. */
  1379. function system_update_8007() {
  1380. $database = \Drupal::database();
  1381. $database_schema = $database->schema();
  1382. $entity_types = \Drupal::entityManager()->getDefinitions();
  1383. $schema = \Drupal::keyValue('entity.storage_schema.sql')->getAll();
  1384. $schema_copy = $schema;
  1385. foreach ($schema as $item_name => $item) {
  1386. list($entity_type_id, , ) = explode('.', $item_name);
  1387. if (!isset($entity_types[$entity_type_id])) {
  1388. continue;
  1389. }
  1390. foreach ($item as $table_name => $table_schema) {
  1391. foreach ($table_schema as $schema_key => $schema_data) {
  1392. if ($schema_key == 'fields') {
  1393. foreach ($schema_data as $field_name => $field_data) {
  1394. foreach ($field_data as $field_data_property => $field_data_value) {
  1395. // Langcode fields have the property 'is_ascii' set, instead
  1396. // they should have set the type to 'varchar_ascii'.
  1397. if ($field_data_property == 'is_ascii') {
  1398. unset($schema_copy[$item_name][$table_name]['fields'][$field_name]['is_ascii']);
  1399. $schema_copy[$item_name][$table_name]['fields'][$field_name]['type'] = 'varchar_ascii';
  1400. if ($database->driver() == 'mysql') {
  1401. $database_schema->changeField($table_name, $field_name, $field_name, $schema_copy[$item_name][$table_name]['fields'][$field_name]);
  1402. }
  1403. }
  1404. }
  1405. }
  1406. }
  1407. }
  1408. }
  1409. }
  1410. \Drupal::keyValue('entity.storage_schema.sql')->setMultiple($schema_copy);
  1411. $definitions = \Drupal::keyValue('entity.definitions.installed')->getAll();
  1412. $definitions_copy = $definitions;
  1413. foreach ($definitions as $item_name => $item_value) {
  1414. $suffix = '.field_storage_definitions';
  1415. if (substr($item_name, -strlen($suffix)) == $suffix) {
  1416. foreach ($item_value as $field_name => $field_definition) {
  1417. $reflection = new \ReflectionObject($field_definition);
  1418. $schema_property = $reflection->getProperty('schema');
  1419. $schema_property->setAccessible(TRUE);
  1420. $schema = $schema_property->getValue($field_definition);
  1421. if (isset($schema['columns']['value']['is_ascii'])) {
  1422. $schema['columns']['value']['type'] = 'varchar_ascii';
  1423. unset($schema['columns']['value']['is_ascii']);
  1424. }
  1425. $schema_property->setValue($field_definition, $schema);
  1426. $schema_property->setAccessible(FALSE);
  1427. $definitions_copy[$item_name][$field_name] = $field_definition;
  1428. }
  1429. }
  1430. }
  1431. \Drupal::keyValue('entity.definitions.installed')->setMultiple($definitions_copy);
  1432. }
  1433. /**
  1434. * Purge field schema data for uninstalled entity types.
  1435. */
  1436. function system_update_8008() {
  1437. $entity_types = \Drupal::entityManager()->getDefinitions();
  1438. /** @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface $schema */
  1439. $schema = \Drupal::keyValue('entity.storage_schema.sql');
  1440. foreach ($schema->getAll() as $key => $item) {
  1441. list($entity_type_id, ,) = explode('.', $key);
  1442. if (!isset($entity_types[$entity_type_id])) {
  1443. $schema->delete($key);
  1444. }
  1445. }
  1446. }
  1447. /**
  1448. * Add allowed attributes to existing html filters.
  1449. */
  1450. function system_update_8009() {
  1451. $default_mapping = [
  1452. '<a>' => '<a href hreflang>',
  1453. '<blockquote>' => '<blockquote cite>',
  1454. '<ol>' => '<ol start type>',
  1455. '<ul>' => '<ul type>',
  1456. '<img>' => '<img src alt height width>',
  1457. '<h2>' => '<h2 id>',
  1458. '<h3>' => '<h3 id>',
  1459. '<h4>' => '<h4 id>',
  1460. '<h5>' => '<h5 id>',
  1461. '<h6>' => '<h6 id>',
  1462. ];
  1463. $config_factory = \Drupal::configFactory();
  1464. foreach ($config_factory->listAll('filter.format') as $name) {
  1465. $allowed_html_mapping = $default_mapping;
  1466. $config = $config_factory->getEditable($name);
  1467. // The image alignment filter needs the data-align attribute.
  1468. $align_enabled = $config->get('filters.filter_align.status');
  1469. if ($align_enabled) {
  1470. $allowed_html_mapping['<img>'] = str_replace('>', ' data-align>', $allowed_html_mapping['<img>']);
  1471. }
  1472. // The image caption filter needs the data-caption attribute.
  1473. $caption_enabled = $config->get('filters.filter_caption.status');
  1474. if ($caption_enabled) {
  1475. $allowed_html_mapping['<img>'] = str_replace('>', ' data-caption>', $allowed_html_mapping['<img>']);
  1476. }
  1477. $allowed_html = $config->get('filters.filter_html.settings.allowed_html');
  1478. if (!empty($allowed_html)) {
  1479. $allowed_html = strtr($allowed_html, $allowed_html_mapping);
  1480. $config->set('filters.filter_html.settings.allowed_html', $allowed_html);
  1481. $config->save();
  1482. }
  1483. }
  1484. }
  1485. /**
  1486. * Place page title blocks in every theme.
  1487. */
  1488. function system_update_8010() {
  1489. // When block module is not installed, there is nothing that could be done
  1490. // except showing a warning.
  1491. if (!\Drupal::moduleHandler()->moduleExists('block')) {
  1492. return t('Block module is not enabled. The page title has been converted to a block, but default page title markup will still display at the top of the main content area.');
  1493. }
  1494. /** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler */
  1495. $theme_handler = \Drupal::service('theme_handler');
  1496. $custom_themes_installed = FALSE;
  1497. $message = NULL;
  1498. $langcode = \Drupal::service('language_manager')->getCurrentLanguage()->getId();
  1499. $page_title_default_settings = [
  1500. 'plugin' => 'page_title_block',
  1501. 'region' => 'content',
  1502. 'settings.label' => 'Page title',
  1503. 'settings.label_display' => 0,
  1504. 'visibility' => [],
  1505. 'weight' => -50,
  1506. 'langcode' => $langcode,
  1507. ];
  1508. foreach ($theme_handler->listInfo() as $theme) {
  1509. $theme_name = $theme->getName();
  1510. switch ($theme_name) {
  1511. case 'bartik':
  1512. $name = 'block.block.bartik_page_title';
  1513. $values = [
  1514. 'id' => 'bartik_page_title',
  1515. ] + $page_title_default_settings;
  1516. _system_update_create_block($name, $theme_name, $values);
  1517. break;
  1518. case 'stark':
  1519. $name = 'block.block.stark_page_title';
  1520. $values = [
  1521. 'id' => 'stark_page_title',
  1522. 'region' => 'content',
  1523. ] + $page_title_default_settings;
  1524. _system_update_create_block($name, $theme_name, $values);
  1525. break;
  1526. case 'seven':
  1527. $name = 'block.block.seven_page_title';
  1528. $values = [
  1529. 'id' => 'seven_page_title',
  1530. 'region' => 'header',
  1531. ] + $page_title_default_settings;
  1532. _system_update_create_block($name, $theme_name, $values);
  1533. break;
  1534. case 'classy':
  1535. $name = 'block.block.classy_page_title';
  1536. $values = [
  1537. 'id' => 'classy_page_title',
  1538. 'region' => 'content',
  1539. ] + $page_title_default_settings;
  1540. _system_update_create_block($name, $theme_name, $values);
  1541. break;
  1542. default:
  1543. $custom_themes_installed = TRUE;
  1544. $name = sprintf('block.block.%s_page_title', $theme_name);
  1545. $values = [
  1546. 'id' => sprintf('%s_page_title', $theme_name),
  1547. 'region' => 'content',
  1548. 'weight' => '-50',
  1549. ] + $page_title_default_settings;
  1550. _system_update_create_block($name, $theme_name, $values);
  1551. break;
  1552. }
  1553. }
  1554. if ($custom_themes_installed) {
  1555. $message = t('Because your site has custom theme(s) installed, we have placed the page title block in the content region. Please manually review the block configuration and remove the page title variables from your page templates.');
  1556. }
  1557. return $message;
  1558. }
  1559. /**
  1560. * Add secondary local tasks block to Seven (fixes system_update_8005).
  1561. */
  1562. function system_update_8011() {
  1563. $langcode = \Drupal::service('language_manager')->getCurrentLanguage()->getId();
  1564. $theme_name = 'seven';
  1565. $name = 'block.block.seven_secondary_local_tasks';
  1566. $values = [
  1567. 'plugin' => 'local_tasks_block',
  1568. 'region' => 'pre_content',
  1569. 'id' => 'seven_secondary_local_tasks',
  1570. 'settings.label' => 'Secondary tabs',
  1571. 'settings.label_display' => 0,
  1572. 'settings.primary' => FALSE,
  1573. 'settings.secondary' => TRUE,
  1574. 'visibility' => [],
  1575. 'weight' => 0,
  1576. 'langcode' => $langcode,
  1577. ];
  1578. _system_update_create_block($name, $theme_name, $values);
  1579. }
  1580. /**
  1581. * Enable automated cron module and move the config into it.
  1582. */
  1583. function system_update_8013() {
  1584. $config_factory = \Drupal::configFactory();
  1585. $system_cron_config = $config_factory->getEditable('system.cron');
  1586. if ($autorun = $system_cron_config->get('threshold.autorun')) {
  1587. // Install 'automated_cron' module.
  1588. \Drupal::service('module_installer')->install(['automated_cron'], FALSE);
  1589. // Copy 'autorun' value into the new module's 'interval' setting.
  1590. $config_factory->getEditable('automated_cron.settings')
  1591. ->set('interval', $autorun)
  1592. ->save(TRUE);
  1593. }
  1594. // Remove the 'autorun' key in system module config.
  1595. $system_cron_config
  1596. ->clear('threshold.autorun')
  1597. ->save(TRUE);
  1598. }
  1599. /**
  1600. * Install the Stable base theme if needed.
  1601. */
  1602. function system_update_8014() {
  1603. $theme_handler = \Drupal::service('theme_handler');
  1604. if ($theme_handler->themeExists('stable')) {
  1605. return;
  1606. }
  1607. $theme_handler->refreshInfo();
  1608. foreach ($theme_handler->listInfo() as $theme) {
  1609. // We first check that a base theme is set because if it's set to false then
  1610. // it's unset in \Drupal\Core\Extension\ThemeHandler::rebuildThemeData().
  1611. if (isset($theme->info['base theme']) && $theme->info['base theme'] == 'stable') {
  1612. $theme_handler->install(['stable']);
  1613. return;
  1614. }
  1615. }
  1616. }
  1617. /**
  1618. * Fix configuration overrides to not override non existing keys.
  1619. */
  1620. function system_update_8200(&$sandbox) {
  1621. $config_factory = \Drupal::configFactory();
  1622. if (!array_key_exists('config_names', $sandbox)) {
  1623. $sandbox['config_names'] = $config_factory->listAll();
  1624. $sandbox['max'] = count($sandbox['config_names']);
  1625. }
  1626. // Get a list of 50 to work on at a time.
  1627. $config_names_to_process = array_slice($sandbox['config_names'], 0, 50);
  1628. // Preload in a single query.
  1629. $config_factory->loadMultiple($config_names_to_process);
  1630. foreach ($config_names_to_process as $config_name) {
  1631. $config_factory->getEditable($config_name)->save();
  1632. }
  1633. // Update the list of names to process.
  1634. $sandbox['config_names'] = array_diff($sandbox['config_names'], $config_names_to_process);
  1635. $sandbox['#finished'] = empty($sandbox['config_names']) ? 1 : ($sandbox['max'] - count($sandbox['config_names'])) / $sandbox['max'];
  1636. }
  1637. /**
  1638. * Clear caches due to behavior change in DefaultPluginManager.
  1639. */
  1640. function system_update_8201() {
  1641. // Empty update to cause a cache rebuild.
  1642. }
  1643. /**
  1644. * Clear caches due to behavior change in MachineName element.
  1645. */
  1646. function system_update_8202() {
  1647. // Empty update to cause a cache rebuild.
  1648. }
  1649. /**
  1650. * Add detailed cron logging configuration.
  1651. */
  1652. function system_update_8300() {
  1653. \Drupal::configFactory()->getEditable('system.cron')
  1654. ->set('logging', 1)
  1655. ->save(TRUE);
  1656. }
  1657. /**
  1658. * Add install profile to core.extension configuration.
  1659. */
  1660. function system_update_8301() {
  1661. \Drupal::configFactory()->getEditable('core.extension')
  1662. ->set('profile', \Drupal::installProfile())
  1663. ->save();
  1664. }