Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 186 additions & 0 deletions bridges/AlpinePackagesBridge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?php

declare(strict_types=1);

class AlpinePackagesBridge extends BridgeAbstract
{
const NAME = 'Alpine Packages Bridge';
const MAINTAINER = 'rsd76';
const URI = 'https://pkgs.alpinelinux.org';
const DESCRIPTION = 'Bridge for getting Alpine packages versions';
const CACHE_TIMEOUT = 3600;

const PARAMETERS = [
[
'package' => [
'type' => 'text',
'name' => 'Package Name',
'required' => true,
'exampleValue' => 'curl',
'title' => 'Name of the package. Use * and ? as wildcards. Example: curl, curl-* or curl-???'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use a consistent package for all three examples you give? E.g.

Suggested change
'title' => 'Name of the package. Use * and ? as wildcards. Example: curl, curl-* or curl-???'
'title' => 'Name of the package. Use * and ? as wildcards. Example: curl-dev, curl-* or curl-???'

],
'branch' => [
'type' => 'text',
'name' => 'Package branch',
'required' => true,
'exampleValue' => 'v3.23',
'title' => 'Name of the branch. Like: edge, v3.23, v3.22, etc.'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'title' => 'Name of the branch. Like: edge, v3.23, v3.22, etc.'
'title' => 'Name of the branch. For example: edge, v3.23, v3.22, etc.'

],
'repository' => [
'type' => 'list',
'name' => 'Repository name',
'values' => [
'All' => 'all',
'Community' => 'community',
'Main' => 'main',
'Testing' => 'testing'
],
'defaultValue' => 'all',
'title' => 'Name of the repository.'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd delete that if it doesn't add any detail to the name above.

],
'architecture' => [
'type' => 'text',
'name' => 'Achitecture',
'exampleValue' => 'aarch64',
'title' => 'Architecture of the package. Like: aarch64, armhf, armv7, loongarch64, ppc64le, ppc64le, ppc64le, x86 and x86_64. Keep empty or use "all" for all.'
]
]
];

private function getADom($element)
{
return $element->find('a')[0];
}

private function getElementData($element)
{
$classes = [
'package',
'repo',
'arch',
'maintainer'
];
$noAhrefClasses = [
'branch',
'bdate'
];
$data = [];
// Get data from element which contains <a href=...>.
foreach ($classes as $class) {
$td = $this->getTdClassDom($element, $class);
$a = $this->getADom($td);
$data[$class] = trim($a->plaintext);
$data[$class . '-href'] = $a->href;
}
// Get data from element which only contains text.
foreach ($noAhrefClasses as $class) {
$td = $this->getTdClassDom($element, $class);
$data[$class] = trim($td->plaintext);
}
// Get version data in a <strong> element.
$td = $this->getTdClassDom($element, 'version');
$strong = $td->find('strong[class=hint--right hint--rounded text-success]')[0];
$data['version'] = trim($strong->plaintext);
return $data;
}

private function getTdClassDom($element, $class)
{
return $element->find('td[class=' . $class . ']')[0];
}

public function collectData()
{
$dom = getSimpleHTMLDOM($this->getUri());
$dom = defaultLinkTo($dom, self::URI);
$table = $dom->find('table[class=pure-table pure-table-striped]')[0];
$tbody = $table->find('tbody')[0];
$trs = $tbody->find('tr');
foreach ($trs as $tr) {
$itemData = $this->getElementData($tr);
$this->items[] = [
'title' => $itemData['package'] . '-' . $itemData['version'],
'uri' => $itemData['package-href'],
'timestamp' => strtotime($itemData['bdate']),
'uid' => trim($itemData['package']) . $itemData['version'] . $itemData['arch'] . $itemData['branch'] . $itemData['repo'],
'author' => $itemData['maintainer'],
'categories' => [
'arch: ' . $itemData['arch'],
'branch: ' . $itemData['branch'],
'repo: ' . $itemData['repo']
]
];
}
}

public function getName()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output is quite a mouthful. Can we develop a more compact representation for the feed name?
E.g. curl (branch edge, repo main, arch x86_64) - Alpine packages.
Also, we might skip a part if it is empty or "all."

{
$packageName = $this->getInput('package');
$branchName = $this->getInput('branch');
$repositoryName = $this->getInput('repository');
$architecture = $this->getInput('architecture');

$name = '';

if ($packageName) {
$packageName = strtolower($packageName);
$name = $packageName;
if ($branchName) {
$branchName = strtolower($branchName);
$name .= ' on branch ' . $branchName;
}
if ($repositoryName) {
$repositoryName = strtolower($repositoryName);
if ($repositoryName === 'all') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This handles "all" different from an empty specification. Please handle them the same (as in getUri below).

$name .= ' in all repositories';
} else {
$name .= ' in repository ' . $repositoryName;
}
}
if ($architecture) {
$architecture = strtolower($architecture);
if ($architecture === 'all') {
$name .= ' on all architectures';
} else {
$name .= ' on ' . $architecture;
}
}
$name .= ' - Alpine packages';
return $name;
}

return parent::getName();
}

public function getUri()
{
$package = $this->getInput('package');
$branch = $this->getInput('branch');
$repository = $this->getInput('repository');
$architecture = $this->getInput('architecture');

if ($package) {
$package = urlencode(strtolower(trim($package)));
}
if ($branch) {
$branch = strtolower(trim($branch));
}
if ($repository) {
$repository = strtolower($repository);
if ($repository === 'all') {
$repository = '';
}
}
if ($architecture) {
$architecture = strtolower(trim($architecture));
if ($architecture === 'all') {
$architecture = '';
}
}

if ($package && $branch) {
return self::URI . '/packages?name=' . $package . '&branch=' . $branch . '&repo=' . $repository . '&arch=' . $architecture . '&origin=&flagged=&maintainer=';
}
return self::URI;
}
}