Created
May 15, 2022 17:13
-
-
Save dshoreman/a86df88b8c479cd17c48489e173d4426 to your computer and use it in GitHub Desktop.
Fedora Package Group Lister
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Fedora\Packages; | |
use SimpleXMLElement; | |
use XMLReader; | |
class FedoraRelease | |
{ | |
const RELEASES_URI = 'https://archives.fedoraproject.org/pub/archive/fedora/linux/releases/'; | |
private array $groups = []; | |
private string $compsXML = ''; | |
public function __construct( | |
private int $version, | |
) { | |
echo "<h2>Fedora {$version} Package Groups</h2>"; | |
} | |
public function groups() | |
{ | |
return $this->groups; | |
} | |
public function loadCompsFor(string $edition) | |
{ | |
$uri = $this->findCompsFile(ucfirst(strtolower($edition)));; | |
$xml = XMLReader::XML(file_get_contents($uri)); | |
while ($xml->read()) { | |
if ($xml->name != 'group') { | |
continue; | |
} | |
$grp = PackageGroup::fromXml($xml); | |
if ($grp) { | |
$this->groups[] = $grp; | |
} | |
} | |
} | |
private function findCompsFile(string $edition) | |
{ | |
$baseUri = sprintf('%s%d/%s/x86_64/os/repodata/', self::RELEASES_URI, $this->version, $edition); | |
$index = file_get_contents($baseUri); | |
$m = preg_match('/<a href="([0-9a-f]+-comps-'.$edition.'.x86_64.xml)">/', $index, $matches); | |
if ($m) { | |
return $baseUri . $matches[1]; | |
} | |
} | |
} | |
class PackageGroup | |
{ | |
public function __construct( | |
private string $id, | |
private string $name, | |
private string $description, | |
private bool $default, | |
private bool $visible, | |
private PackageList $packages | |
) {} | |
public static function fromXml(XMLReader $x) | |
{ | |
$raw = simplexml_load_string($x->readOuterXml()); | |
$obj = (object) (array) $raw; | |
if (!isset($obj->id)) { | |
return; | |
} | |
return new self( | |
$obj->id, | |
$obj->name[0] ?? 'unknown', | |
$obj->description[0] ?? '', | |
(bool) $obj->default, | |
(string) $obj->uservisible === 'false' ? false : true, | |
new PackageList($raw->packagelist), | |
); | |
} | |
public function printHeader() | |
{ | |
echo '<div style="width: 320px; float: left; max-height: 500px; overflow: auto;">'; | |
echo sprintf('<h3 title="Package ID: %s">%s</h3>', $this->id, $this->name); | |
echo '<p>' . $this->description . '</p>'; | |
} | |
public function printPkglist() | |
{ | |
echo $this->packages . '</div>'; | |
} | |
} | |
class PackageList | |
{ | |
private array $packages; | |
public function __construct(SimpleXMLElement $tree) | |
{ | |
$requires = (array) $tree->packagereq; | |
foreach ($tree->packagereq as $req) { | |
$attrs = $req->attributes(); | |
if (isset($req['@attributes'])) { | |
unset($req['@attributes']); | |
} | |
$this->append(sprintf('%s %s', $attrs->type ? "[$attrs->type]" : '', $req)); | |
} | |
} | |
public function append(string $package) | |
{ | |
$this->packages[] = $package; | |
} | |
public function __toArray(): array | |
{ | |
return $this->packages; | |
} | |
public function __toString(): string | |
{ | |
return '<ul><li>' . implode('</li><li>', $this->packages) . '.</li></ul>'; | |
} | |
} | |
$release = new FedoraRelease((int) ($_GET['release'] ?? 36)); | |
$release->loadCompsFor($_GET['edition'] ?? 'server'); | |
foreach ($release->groups() as $group) { | |
$group->printHeader(); | |
$group->printPkglist(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment