Files
bachir 54c5733069 fix update-project-info: ecrit la version drupal.org (8.x-1.23, 8.x-3.x-dev) dans les .info.yml au lieu de la version composer normalisee (1.23.0) — sinon /admin/reports/updates affiche 'Non supportée !' sur tous les modules en 8.x-*
- tag: version = source.reference (tag git drupal.org), fallback URL dist ftp.drupal.org
- branche dev: version dev exacte retrouvée via release-history updates.drupal.org (8.x-3.x-dev, 2.0.x-dev...)
- ne ecrase plus une version existante correcte

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-11-22 21:39:21 +01:00

117 lines
4.3 KiB
PHP

<?php
/**
* @file
* Injecte 'project:' et 'version:' dans les .info.yml des packages drupal/*.
*
* - Les installs git (source) ne contiennent ni project: ni version: dans le
* .info.yml (seuls les tarballs packagés par drupal.org les ajoutent) : sans
* ça, le module update ne voit que le core dans /admin/reports/updates.
* - La version écrite doit être la version drupal.org (ex. '8.x-1.23',
* '8.x-3.x-dev'), PAS la version normalisée par composer (ex. '1.23.0') :
* sinon /admin/reports/updates affiche « Non supportée ! » en rouge.
* Le tag drupal.org est retrouvé via source.reference (tag git) ou l'URL
* dist ; pour les branches dev, via le release-history updates.drupal.org.
*
* Appelé via composer scripts post-install-cmd / post-update-cmd.
*/
$root = dirname(__DIR__);
$installedFile = $root . '/vendor/composer/installed.json';
if (!file_exists($installedFile)) {
return;
}
$installed = json_decode(file_get_contents($installedFile), TRUE);
$packages = $installed['packages'] ?? $installed;
/**
* Retourne la version drupal.org d'un package, ou NULL si indéterminable.
*/
function drupal_org_version(array $pkg): ?string {
$ref = $pkg['source']['reference'] ?? '';
// Un tag (ex. '8.x-1.23', '4.1.4', '2.0.0-beta14') — pas un hash de commit.
if ($ref !== '' && !preg_match('/^[0-9a-f]{40}$/i', $ref) && preg_match('/^[vV]?[0-9]/', $ref)) {
return ltrim($ref, 'vV');
}
// Sinon, essaie d'extraire le tag de l'URL dist (ftp.drupal.org/files/projects/<proj>-<tag>.zip).
$dist = $pkg['dist']['url'] ?? '';
if ($dist !== '' && preg_match('/-([0-9][^\/]*?)\.(zip|tar\.gz|tgz)$/', $dist, $m)) {
return $m[1];
}
// Branche dev (ex. 'dev-3.x', 'dev-2.0.x') : interroge le release-history
// drupal.org pour trouver la version dev exacte ('8.x-3.x-dev', '3.x-dev'…).
$version = $pkg['version'] ?? '';
if (preg_match('/^dev-(\d+(?:\.\d+)?\.x)$/', $version, $m)) {
$branch = $m[1];
$short = substr($pkg['name'] ?? '', 7);
$url = 'https://updates.drupal.org/release-history/' . $short . '/current';
$ctx = stream_context_create(['http' => ['timeout' => 5], 'https' => ['timeout' => 5]]);
$xml = @file_get_contents($url, false, $ctx);
if ($xml !== false && preg_match_all('/<version>([^<]+)<\/version>/', $xml, $mm)) {
$candidates = ['8.x-' . $branch . '-dev', $branch . '-dev'];
foreach ($candidates as $cand) {
if (in_array($cand, $mm[1], true)) {
return $cand;
}
}
// Le XML ne connaît aucune version dev de cette branche : on laisse tel quel.
}
return $version;
}
return $version !== '' ? $version : NULL;
}
$patched = 0;
foreach ($packages as $pkg) {
$name = $pkg['name'] ?? '';
if (!str_starts_with($name, 'drupal/')) {
continue;
}
$short = substr($name, 7);
$installPath = $pkg['install-path'] ?? NULL;
if (!$installPath) {
continue;
}
// install-path est relatif à vendor/composer/ (où réside installed.json).
$dir = $root . '/vendor/composer/' . $installPath;
if (!is_dir($dir)) {
continue;
}
// .info.yml principal du package : <dir>/<short>.info.yml, sinon le 1er trouvé.
$infoFile = $dir . '/' . $short . '.info.yml';
if (!file_exists($infoFile)) {
$found = glob($dir . '/*.info.yml');
if (!$found) {
continue;
}
$infoFile = $found[0];
}
$content = file_get_contents($infoFile);
$original = $content;
// project: en tête si absent.
if (!preg_match('/^project:/mi', $content)) {
$content = "project: '" . $short . "'\n" . $content;
}
// version: remplacée par la version drupal.org (jamais la version composer
// normalisée, ex. '1.23.0' au lieu de '8.x-1.23').
$version = drupal_org_version($pkg);
$content = preg_replace('/^version:.*$/mi', '', $content);
if ($version !== NULL && $version !== '') {
$content = trim($content) . "\n\nversion: '" . $version . "'\n";
}
if ($content !== $original) {
file_put_contents($infoFile, $content);
$patched++;
}
}
// Invalide le cache des projets update si Drupal est bootstrappable.
if (class_exists('Drupal')) {
try {
\Drupal::keyValue('update')->delete('update_project_projects');
\Drupal::keyValue('update')->delete('update_project_data');
}
catch (\Throwable $e) {
}
}
fwrite(STDOUT, "update-project-info: $patched .info.yml patchés\n");