update.compare.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. <?php
  2. /**
  3. * @file
  4. * Code required only when comparing available updates to existing data.
  5. */
  6. use Drupal\update\UpdateFetcherInterface;
  7. use Drupal\update\UpdateManagerInterface;
  8. use Drupal\update\ModuleVersion;
  9. use Drupal\update\ProjectCoreCompatibility;
  10. /**
  11. * Determines version and type information for currently installed projects.
  12. *
  13. * Processes the list of projects on the system to figure out the currently
  14. * installed versions, and other information that is required before we can
  15. * compare against the available releases to produce the status report.
  16. *
  17. * @param $projects
  18. * Array of project information from
  19. * \Drupal\update\UpdateManager::getProjects().
  20. */
  21. function update_process_project_info(&$projects) {
  22. foreach ($projects as $key => $project) {
  23. // Assume an official release until we see otherwise.
  24. $install_type = 'official';
  25. $info = $project['info'];
  26. if (isset($info['version'])) {
  27. // Check for development snapshots
  28. if (preg_match('@(dev|HEAD)@', $info['version'])) {
  29. $install_type = 'dev';
  30. }
  31. // Figure out what the currently installed major version is. We need
  32. // to handle both contribution (e.g. "5.x-1.3", major = 1) and core
  33. // (e.g. "5.1", major = 5) version strings.
  34. $matches = [];
  35. if (preg_match('/^(\d+\.x-)?(\d+)\..*$/', $info['version'], $matches)) {
  36. $info['major'] = $matches[2];
  37. }
  38. elseif (!isset($info['major'])) {
  39. // This would only happen for version strings that don't follow the
  40. // drupal.org convention. We let contribs define "major" in their
  41. // .info.yml in this case, and only if that's missing would we hit this.
  42. $info['major'] = -1;
  43. }
  44. }
  45. else {
  46. // No version info available at all.
  47. $install_type = 'unknown';
  48. $info['version'] = t('Unknown');
  49. $info['major'] = -1;
  50. }
  51. // Finally, save the results we care about into the $projects array.
  52. $projects[$key]['existing_version'] = $info['version'];
  53. $projects[$key]['existing_major'] = $info['major'];
  54. $projects[$key]['install_type'] = $install_type;
  55. }
  56. }
  57. /**
  58. * Calculates the current update status of all projects on the site.
  59. *
  60. * The results of this function are expensive to compute, especially on sites
  61. * with lots of modules or themes, since it involves a lot of comparisons and
  62. * other operations. Therefore, we store the results. However, since this is not
  63. * the data about available updates fetched from the network, it is ok to
  64. * invalidate it somewhat quickly. If we keep this data for very long, site
  65. * administrators are more likely to see incorrect results if they upgrade to a
  66. * newer version of a module or theme but do not visit certain pages that
  67. * automatically clear this.
  68. *
  69. * @param array $available
  70. * Data about available project releases.
  71. *
  72. * @return
  73. * An array of installed projects with current update status information.
  74. *
  75. * @see update_get_available()
  76. * @see \Drupal\update\UpdateManager::getProjects()
  77. * @see update_process_project_info()
  78. * @see \Drupal\update\UpdateManagerInterface::projectStorage()
  79. * @see \Drupal\update\ProjectCoreCompatibility::setReleaseMessage()
  80. */
  81. function update_calculate_project_data($available) {
  82. // Retrieve the projects from storage, if present.
  83. $projects = \Drupal::service('update.manager')->projectStorage('update_project_data');
  84. // If $projects is empty, then the data must be rebuilt.
  85. // Otherwise, return the data and skip the rest of the function.
  86. if (!empty($projects)) {
  87. return $projects;
  88. }
  89. $projects = \Drupal::service('update.manager')->getProjects();
  90. update_process_project_info($projects);
  91. if (isset($projects['drupal']) && !empty($available['drupal'])) {
  92. // Calculate core status first so that it is complete before
  93. // \Drupal\update\ProjectCoreCompatibility::setReleaseMessage() is called
  94. // for each module below.
  95. update_calculate_project_update_status($projects['drupal'], $available['drupal']);
  96. if (isset($available['drupal']['releases'])) {
  97. $project_core_compatibility = new ProjectCoreCompatibility($projects['drupal'], $available['drupal']['releases']);
  98. }
  99. }
  100. foreach ($projects as $project => $project_info) {
  101. if (isset($available[$project])) {
  102. if ($project === 'drupal') {
  103. continue;
  104. }
  105. update_calculate_project_update_status($projects[$project], $available[$project]);
  106. // Inject the list of compatible core versions to show administrator(s)
  107. // which versions of core a given available update can be installed with.
  108. // Since individual releases of a project can be compatible with different
  109. // versions of core, and even multiple major versions of core (for
  110. // example, 8.9.x and 9.0.x), this list will hopefully help
  111. // administrator(s) know which available updates they can upgrade a given
  112. // project to.
  113. if (isset($project_core_compatibility)) {
  114. $project_core_compatibility->setReleaseMessage($projects[$project]);
  115. }
  116. }
  117. else {
  118. $projects[$project]['status'] = UpdateFetcherInterface::UNKNOWN;
  119. $projects[$project]['reason'] = t('No available releases found');
  120. }
  121. }
  122. // Give other modules a chance to alter the status (for example, to allow a
  123. // contrib module to provide fine-grained settings to ignore specific
  124. // projects or releases).
  125. \Drupal::moduleHandler()->alter('update_status', $projects);
  126. // Store the site's update status for at most 1 hour.
  127. \Drupal::keyValueExpirable('update')->setWithExpire('update_project_data', $projects, 3600);
  128. return $projects;
  129. }
  130. /**
  131. * Calculates the current update status of a specific project.
  132. *
  133. * This function is the heart of the update status feature. For each project it
  134. * is invoked with, it first checks if the project has been flagged with a
  135. * special status like "unsupported" or "insecure", or if the project node
  136. * itself has been unpublished. In any of those cases, the project is marked
  137. * with an error and the next project is considered.
  138. *
  139. * If the project itself is valid, the function decides what major release
  140. * series to consider. The project defines its currently supported branches in
  141. * its Drupal.org for the project, so the first step is to make sure the
  142. * development branch of the current version is still supported. If so, then the
  143. * major version of the current version is used. If the current version is not
  144. * in a supported branch, the next supported branch is used to determine the
  145. * major version to use. There's also a check to make sure that this function
  146. * never recommends an earlier release than the currently installed major
  147. * version.
  148. *
  149. * Given a target major version, the available releases are scanned looking for
  150. * the specific release to recommend (avoiding beta releases and development
  151. * snapshots if possible). For the target major version, the highest patch level
  152. * is found. If there is a release at that patch level with no extra ("beta",
  153. * etc.), then the release at that patch level with the most recent release date
  154. * is recommended. If every release at that patch level has extra (only betas),
  155. * then the latest release from the previous patch level is recommended. For
  156. * example:
  157. *
  158. * - 1.6-bugfix <-- recommended version because 1.6 already exists.
  159. * - 1.6
  160. *
  161. * or
  162. *
  163. * - 1.6-beta
  164. * - 1.5 <-- recommended version because no 1.6 exists.
  165. * - 1.4
  166. *
  167. * Also, the latest release from the same major version is looked for, even beta
  168. * releases, to display to the user as the "Latest version" option.
  169. * Additionally, the latest official release from any higher major versions that
  170. * have been released is searched for to provide a set of "Also available"
  171. * options.
  172. *
  173. * Finally, and most importantly, the release history continues to be scanned
  174. * until the currently installed release is reached, searching for anything
  175. * marked as a security update. If any security updates have been found between
  176. * the recommended release and the installed version, all of the releases that
  177. * included a security fix are recorded so that the site administrator can be
  178. * warned their site is insecure, and links pointing to the release notes for
  179. * each security update can be included (which, in turn, will link to the
  180. * official security announcements for each vulnerability).
  181. *
  182. * This function relies on the fact that the .xml release history data comes
  183. * sorted based on major version and patch level, then finally by release date
  184. * if there are multiple releases such as betas from the same major.patch
  185. * version (e.g., 5.x-1.5-beta1, 5.x-1.5-beta2, and 5.x-1.5). Development
  186. * snapshots for a given major version are always listed last.
  187. *
  188. * NOTE: This function *must* set a value for $project_data['status'] before
  189. * returning, or the rest of the Update Manager will break in unexpected ways.
  190. *
  191. * @param $project_data
  192. * An array containing information about a specific project.
  193. * @param $available
  194. * Data about available project releases of a specific project.
  195. */
  196. function update_calculate_project_update_status(&$project_data, $available) {
  197. foreach (['title', 'link'] as $attribute) {
  198. if (!isset($project_data[$attribute]) && isset($available[$attribute])) {
  199. $project_data[$attribute] = $available[$attribute];
  200. }
  201. }
  202. // If the project status is marked as something bad, there's nothing else
  203. // to consider.
  204. if (isset($available['project_status'])) {
  205. switch ($available['project_status']) {
  206. case 'insecure':
  207. $project_data['status'] = UpdateManagerInterface::NOT_SECURE;
  208. if (empty($project_data['extra'])) {
  209. $project_data['extra'] = [];
  210. }
  211. $project_data['extra'][] = [
  212. 'label' => t('Project not secure'),
  213. 'data' => t('This project has been labeled insecure by the Drupal security team, and is no longer available for download. Immediately disabling everything included by this project is strongly recommended!'),
  214. ];
  215. break;
  216. case 'unpublished':
  217. case 'revoked':
  218. $project_data['status'] = UpdateManagerInterface::REVOKED;
  219. if (empty($project_data['extra'])) {
  220. $project_data['extra'] = [];
  221. }
  222. $project_data['extra'][] = [
  223. 'label' => t('Project revoked'),
  224. 'data' => t('This project has been revoked, and is no longer available for download. Disabling everything included by this project is strongly recommended!'),
  225. ];
  226. break;
  227. case 'unsupported':
  228. $project_data['status'] = UpdateManagerInterface::NOT_SUPPORTED;
  229. if (empty($project_data['extra'])) {
  230. $project_data['extra'] = [];
  231. }
  232. $project_data['extra'][] = [
  233. 'label' => t('Project not supported'),
  234. 'data' => t('This project is no longer supported, and is no longer available for download. Disabling everything included by this project is strongly recommended!'),
  235. ];
  236. break;
  237. case 'not-fetched':
  238. $project_data['status'] = UpdateFetcherInterface::NOT_FETCHED;
  239. $project_data['reason'] = t('Failed to get available update data.');
  240. break;
  241. default:
  242. // Assume anything else (e.g. 'published') is valid and we should
  243. // perform the rest of the logic in this function.
  244. break;
  245. }
  246. }
  247. if (!empty($project_data['status'])) {
  248. // We already know the status for this project, so there's nothing else to
  249. // compute. Record the project status into $project_data and we're done.
  250. $project_data['project_status'] = $available['project_status'];
  251. return;
  252. }
  253. // Figure out the target major version.
  254. // Off Drupal.org, '0' could be a valid version string, so don't use empty().
  255. if (!isset($project_data['existing_version']) || $project_data['existing_version'] === '') {
  256. $project_data['status'] = UpdateFetcherInterface::UNKNOWN;
  257. $project_data['reason'] = t('Empty version');
  258. return;
  259. }
  260. try {
  261. $existing_major = ModuleVersion::createFromVersionString($project_data['existing_version'])->getMajorVersion();
  262. }
  263. catch (UnexpectedValueException $exception) {
  264. // If the version has an unexpected value we can't determine updates.
  265. $project_data['status'] = UpdateFetcherInterface::UNKNOWN;
  266. $project_data['reason'] = t('Invalid version: @existing_version', ['@existing_version' => $project_data['existing_version']]);
  267. return;
  268. }
  269. $supported_branches = [];
  270. if (isset($available['supported_branches'])) {
  271. $supported_branches = explode(',', $available['supported_branches']);
  272. }
  273. $is_in_supported_branch = function ($version) use ($supported_branches) {
  274. foreach ($supported_branches as $supported_branch) {
  275. if (strpos($version, $supported_branch) === 0) {
  276. return TRUE;
  277. }
  278. }
  279. return FALSE;
  280. };
  281. if ($is_in_supported_branch($project_data['existing_version'])) {
  282. // Still supported, stay at the current major version.
  283. $target_major = $existing_major;
  284. }
  285. elseif ($supported_branches) {
  286. // We know the current release is unsupported since it is not in
  287. // 'supported_branches' list. We should use the next valid supported
  288. // branch for the target major version.
  289. $project_data['status'] = UpdateManagerInterface::NOT_SUPPORTED;
  290. foreach ($supported_branches as $supported_branch) {
  291. try {
  292. $target_major = ModuleVersion::createFromSupportBranch($supported_branch)->getMajorVersion();
  293. }
  294. catch (UnexpectedValueException $exception) {
  295. continue;
  296. }
  297. }
  298. if (!isset($target_major)) {
  299. // If there are no valid support branches, use the current major.
  300. $target_major = $existing_major;
  301. }
  302. }
  303. else {
  304. // Malformed XML file? Stick with the current branch.
  305. $target_major = $existing_major;
  306. }
  307. // Make sure we never tell the admin to downgrade. If we recommended an
  308. // earlier version than the one they're running, they'd face an
  309. // impossible data migration problem, since Drupal never supports a DB
  310. // downgrade path. In the unfortunate case that what they're running is
  311. // unsupported, and there's nothing newer for them to upgrade to, we
  312. // can't print out a "Recommended version", but just have to tell them
  313. // what they have is unsupported and let them figure it out.
  314. $target_major = max($existing_major, $target_major);
  315. // If the project is marked as UpdateFetcherInterface::FETCH_PENDING, it
  316. // means that the data we currently have (if any) is stale, and we've got a
  317. // task queued up to (re)fetch the data. In that case, we mark it as such,
  318. // merge in whatever data we have (e.g. project title and link), and move on.
  319. if (!empty($available['fetch_status']) && $available['fetch_status'] == UpdateFetcherInterface::FETCH_PENDING) {
  320. $project_data['status'] = UpdateFetcherInterface::FETCH_PENDING;
  321. $project_data['reason'] = t('No available update data');
  322. $project_data['fetch_status'] = $available['fetch_status'];
  323. return;
  324. }
  325. // Defend ourselves from XML history files that contain no releases.
  326. if (empty($available['releases'])) {
  327. $project_data['status'] = UpdateFetcherInterface::UNKNOWN;
  328. $project_data['reason'] = t('No available releases found');
  329. return;
  330. }
  331. $recommended_version_without_extra = '';
  332. $recommended_release = NULL;
  333. foreach ($available['releases'] as $version => $release) {
  334. try {
  335. $release_module_version = ModuleVersion::createFromVersionString($release['version']);
  336. }
  337. catch (UnexpectedValueException $exception) {
  338. continue;
  339. }
  340. // First, if this is the existing release, check a few conditions.
  341. if ($project_data['existing_version'] === $version) {
  342. if (isset($release['terms']['Release type']) &&
  343. in_array('Insecure', $release['terms']['Release type'])) {
  344. $project_data['status'] = UpdateManagerInterface::NOT_SECURE;
  345. }
  346. elseif ($release['status'] == 'unpublished') {
  347. $project_data['status'] = UpdateManagerInterface::REVOKED;
  348. if (empty($project_data['extra'])) {
  349. $project_data['extra'] = [];
  350. }
  351. $project_data['extra'][] = [
  352. 'class' => ['release-revoked'],
  353. 'label' => t('Release revoked'),
  354. 'data' => t('Your currently installed release has been revoked, and is no longer available for download. Disabling everything included in this release or upgrading is strongly recommended!'),
  355. ];
  356. }
  357. elseif (isset($release['terms']['Release type']) &&
  358. in_array('Unsupported', $release['terms']['Release type'])) {
  359. $project_data['status'] = UpdateManagerInterface::NOT_SUPPORTED;
  360. if (empty($project_data['extra'])) {
  361. $project_data['extra'] = [];
  362. }
  363. $project_data['extra'][] = [
  364. 'class' => ['release-not-supported'],
  365. 'label' => t('Release not supported'),
  366. 'data' => t('Your currently installed release is now unsupported, and is no longer available for download. Disabling everything included in this release or upgrading is strongly recommended!'),
  367. ];
  368. }
  369. }
  370. // Other than the currently installed release, ignore unpublished, insecure,
  371. // or unsupported updates.
  372. elseif ($release['status'] == 'unpublished' ||
  373. !$is_in_supported_branch($release['version']) ||
  374. (isset($release['terms']['Release type']) &&
  375. (in_array('Insecure', $release['terms']['Release type']) ||
  376. in_array('Unsupported', $release['terms']['Release type'])))
  377. ) {
  378. continue;
  379. }
  380. $release_major_version = $release_module_version->getMajorVersion();
  381. // See if this is a higher major version than our target and yet still
  382. // supported. If so, record it as an "Also available" release.
  383. if ($release_major_version > $target_major) {
  384. if (!isset($project_data['also'])) {
  385. $project_data['also'] = [];
  386. }
  387. if (!isset($project_data['also'][$release_major_version])) {
  388. $project_data['also'][$release_major_version] = $version;
  389. $project_data['releases'][$version] = $release;
  390. }
  391. // Otherwise, this release can't matter to us, since it's neither
  392. // from the release series we're currently using nor the recommended
  393. // release. We don't even care about security updates for this
  394. // branch, since if a project maintainer puts out a security release
  395. // at a higher major version and not at the lower major version,
  396. // they must remove the lower version from the supported major
  397. // versions at the same time, in which case we won't hit this code.
  398. continue;
  399. }
  400. // Look for the 'latest version' if we haven't found it yet. Latest is
  401. // defined as the most recent version for the target major version.
  402. if (!isset($project_data['latest_version'])
  403. && $release_major_version == $target_major) {
  404. $project_data['latest_version'] = $version;
  405. $project_data['releases'][$version] = $release;
  406. }
  407. // Look for the development snapshot release for this branch.
  408. if (!isset($project_data['dev_version'])
  409. && $release_major_version == $target_major
  410. && $release_module_version->getVersionExtra() === 'dev') {
  411. $project_data['dev_version'] = $version;
  412. $project_data['releases'][$version] = $release;
  413. }
  414. if ($release_module_version->getVersionExtra()) {
  415. $release_version_without_extra = str_replace('-' . $release_module_version->getVersionExtra(), '', $release['version']);
  416. }
  417. else {
  418. $release_version_without_extra = $release['version'];
  419. }
  420. // Look for the 'recommended' version if we haven't found it yet (see
  421. // phpdoc at the top of this function for the definition).
  422. if (!isset($project_data['recommended'])
  423. && $release_major_version == $target_major) {
  424. if ($recommended_version_without_extra !== $release_version_without_extra) {
  425. $recommended_version_without_extra = $release_version_without_extra;
  426. $recommended_release = $release;
  427. }
  428. if ($release_module_version->getVersionExtra() === NULL) {
  429. $project_data['recommended'] = $recommended_release['version'];
  430. $project_data['releases'][$recommended_release['version']] = $recommended_release;
  431. }
  432. }
  433. // Stop searching once we hit the currently installed version.
  434. if ($project_data['existing_version'] === $version) {
  435. break;
  436. }
  437. // If we're running a dev snapshot and have a timestamp, stop
  438. // searching for security updates once we hit an official release
  439. // older than what we've got. Allow 100 seconds of leeway to handle
  440. // differences between the datestamp in the .info.yml file and the
  441. // timestamp of the tarball itself (which are usually off by 1 or 2
  442. // seconds) so that we don't flag that as a new release.
  443. if ($project_data['install_type'] == 'dev') {
  444. if (empty($project_data['datestamp'])) {
  445. // We don't have current timestamp info, so we can't know.
  446. continue;
  447. }
  448. elseif (isset($release['date']) && ($project_data['datestamp'] + 100 > $release['date'])) {
  449. // We're newer than this, so we can skip it.
  450. continue;
  451. }
  452. }
  453. // See if this release is a security update.
  454. if (isset($release['terms']['Release type'])
  455. && in_array('Security update', $release['terms']['Release type'])) {
  456. $project_data['security updates'][] = $release;
  457. }
  458. }
  459. // If we were unable to find a recommended version, then make the latest
  460. // version the recommended version if possible.
  461. if (!isset($project_data['recommended']) && isset($project_data['latest_version'])) {
  462. $project_data['recommended'] = $project_data['latest_version'];
  463. }
  464. if (isset($project_data['status'])) {
  465. // If we already know the status, we're done.
  466. return;
  467. }
  468. // If we don't know what to recommend, there's nothing we can report.
  469. // Bail out early.
  470. if (!isset($project_data['recommended'])) {
  471. $project_data['status'] = UpdateFetcherInterface::UNKNOWN;
  472. $project_data['reason'] = t('No available releases found');
  473. return;
  474. }
  475. // If we're running a dev snapshot, compare the date of the dev snapshot
  476. // with the latest official version, and record the absolute latest in
  477. // 'latest_dev' so we can correctly decide if there's a newer release
  478. // than our current snapshot.
  479. if ($project_data['install_type'] == 'dev') {
  480. if (isset($project_data['dev_version']) && $available['releases'][$project_data['dev_version']]['date'] > $available['releases'][$project_data['latest_version']]['date']) {
  481. $project_data['latest_dev'] = $project_data['dev_version'];
  482. }
  483. else {
  484. $project_data['latest_dev'] = $project_data['latest_version'];
  485. }
  486. }
  487. // Figure out the status, based on what we've seen and the install type.
  488. switch ($project_data['install_type']) {
  489. case 'official':
  490. if ($project_data['existing_version'] === $project_data['recommended'] || $project_data['existing_version'] === $project_data['latest_version']) {
  491. $project_data['status'] = UpdateManagerInterface::CURRENT;
  492. }
  493. else {
  494. $project_data['status'] = UpdateManagerInterface::NOT_CURRENT;
  495. }
  496. break;
  497. case 'dev':
  498. $latest = $available['releases'][$project_data['latest_dev']];
  499. if (empty($project_data['datestamp'])) {
  500. $project_data['status'] = UpdateFetcherInterface::NOT_CHECKED;
  501. $project_data['reason'] = t('Unknown release date');
  502. }
  503. elseif (($project_data['datestamp'] + 100 > $latest['date'])) {
  504. $project_data['status'] = UpdateManagerInterface::CURRENT;
  505. }
  506. else {
  507. $project_data['status'] = UpdateManagerInterface::NOT_CURRENT;
  508. }
  509. break;
  510. default:
  511. $project_data['status'] = UpdateFetcherInterface::UNKNOWN;
  512. $project_data['reason'] = t('Invalid info');
  513. }
  514. }