rss-bridge/bridges/GogsBridge.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

217 lines
6.9 KiB
PHP
Raw Normal View History

2019-06-21 20:52:51 +03:00
<?php
2019-06-21 20:52:51 +03:00
class GogsBridge extends BridgeAbstract
{
const NAME = 'Gogs';
const URI = 'https://gogs.io';
const DESCRIPTION = 'Returns the latest issues, commits or releases';
const MAINTAINER = 'logmanoriginal';
const CACHE_TIMEOUT = 300; // 5 minutes
2019-06-21 20:52:51 +03:00
const PARAMETERS = [
'global' => [
'host' => [
'name' => 'Host',
'exampleValue' => 'https://notabug.org',
2019-06-21 20:52:51 +03:00
'required' => true,
'title' => 'Host name with its protocol, without trailing slash',
2019-06-21 20:52:51 +03:00
],
'user' => [
'name' => 'Username',
'exampleValue' => 'PDModdingCommunity',
2019-06-21 20:52:51 +03:00
'required' => true,
'title' => 'User name as it appears in the URL',
],
'project' => [
'name' => 'Project name',
'exampleValue' => 'PD-Loader',
2019-06-21 20:52:51 +03:00
'required' => true,
'title' => 'Project name as it appears in the URL',
],
],
'Commits' => [
'branch' => [
'name' => 'Branch name',
'defaultValue' => 'master',
'required' => true,
'title' => 'Branch name as it appears in the URL',
],
2019-06-21 20:52:51 +03:00
],
'Issues' => [
'include_description' => [
'name' => 'Include issue description',
'type' => 'checkbox',
'title' => 'Activate to include the issue description',
],
2019-06-21 20:52:51 +03:00
],
'Single issue' => [
'issue' => [
'name' => 'Issue number',
'type' => 'number',
'exampleValue' => 100,
2019-06-21 20:52:51 +03:00
'required' => true,
'title' => 'Issue number from the issues list',
],
2019-06-21 20:52:51 +03:00
],
'Releases' => [],
];
2019-06-21 20:52:51 +03:00
private $title = '';
2019-06-21 20:52:51 +03:00
/**
* Note: detectParamters doesn't make sense for this bridge because there is
* no "single" host for this service. Anyone can host it.
*/
2019-06-21 20:52:51 +03:00
public function getURI()
{
switch ($this->queriedContext) {
2022-06-24 19:29:35 +03:00
case 'Commits':
2019-06-21 20:52:51 +03:00
return $this->getInput('host')
. '/' . $this->getInput('user')
. '/' . $this->getInput('project')
. '/commits/' . $this->getInput('branch');
2022-06-24 19:29:35 +03:00
case 'Issues':
2019-06-21 20:52:51 +03:00
return $this->getInput('host')
. '/' . $this->getInput('user')
. '/' . $this->getInput('project')
. '/issues/';
2022-06-24 19:29:35 +03:00
case 'Single issue':
2019-06-21 20:52:51 +03:00
return $this->getInput('host')
. '/' . $this->getInput('user')
. '/' . $this->getInput('project')
. '/issues/' . $this->getInput('issue');
2022-06-24 19:29:35 +03:00
case 'Releases':
2019-06-21 20:52:51 +03:00
return $this->getInput('host')
. '/' . $this->getInput('user')
. '/' . $this->getInput('project')
. '/releases/';
2019-06-21 20:52:51 +03:00
default:
return parent::getURI();
}
2019-06-21 20:52:51 +03:00
}
2019-06-21 20:52:51 +03:00
public function getName()
{
switch ($this->queriedContext) {
case 'Commits':
case 'Issues':
case 'Releases':
return $this->title . ' ' . $this->queriedContext;
case 'Single issue':
return $this->title . ' Issue ' . $this->getInput('issue');
default:
return parent::getName();
}
}
2019-06-21 20:52:51 +03:00
public function getIcon()
{
return 'https://gogs.io/img/favicon.ico';
}
2019-06-21 20:52:51 +03:00
public function collectData()
{
$html = getSimpleHTMLDOM($this->getURI());
2019-06-21 20:52:51 +03:00
$html = defaultLinkTo($html, $this->getURI());
2019-06-21 20:52:51 +03:00
$this->title = $html->find('[property="og:title"]', 0)->content;
2019-06-21 20:52:51 +03:00
switch ($this->queriedContext) {
2022-06-24 19:29:35 +03:00
case 'Commits':
2019-06-21 20:52:51 +03:00
$this->collectCommitsData($html);
2022-06-24 19:29:35 +03:00
break;
case 'Issues':
2019-06-21 20:52:51 +03:00
$this->collectIssuesData($html);
2022-06-24 19:29:35 +03:00
break;
case 'Single issue':
2019-06-21 20:52:51 +03:00
$this->collectSingleIssueData($html);
2022-06-24 19:29:35 +03:00
break;
case 'Releases':
2019-06-21 20:52:51 +03:00
$this->collectReleasesData($html);
2022-06-24 19:29:35 +03:00
break;
2019-06-21 20:52:51 +03:00
}
}
2019-06-21 20:52:51 +03:00
protected function collectCommitsData($html)
{
$commits = $html->find('#commits-table tbody tr')
or returnServerError('Unable to find commits');
2019-06-21 20:52:51 +03:00
foreach ($commits as $commit) {
$this->items[] = [
'uri' => $commit->find('a.sha', 0)->href,
'title' => $commit->find('.message span', 0)->plaintext,
'author' => $commit->find('.author', 0)->plaintext,
'timestamp' => $commit->find('.time-since', 0)->title,
'uid' => $commit->find('.sha', 0)->plaintext,
];
}
}
2019-06-21 20:52:51 +03:00
protected function collectIssuesData($html)
{
$issues = $html->find('.issue.list li')
or returnServerError('Unable to find issues');
2019-06-21 20:52:51 +03:00
foreach ($issues as $issue) {
$uri = $issue->find('a', 0)->href;
2019-06-21 20:52:51 +03:00
$item = [
'uri' => $uri,
'title' => $issue->find('.label', 0)->plaintext . ' | ' . $issue->find('a.title', 0)->plaintext,
'author' => $issue->find('.desc a', 0)->plaintext,
'timestamp' => $issue->find('.time-since', 0)->title,
'uid' => $issue->find('.label', 0)->plaintext,
];
2019-06-21 20:52:51 +03:00
if ($this->getInput('include_description')) {
$issue_html = getSimpleHTMLDOMCached($uri, 3600)
or returnServerError('Unable to load issue description');
2019-06-21 20:52:51 +03:00
$issue_html = defaultLinkTo($issue_html, $uri);
2019-06-21 20:52:51 +03:00
$item['content'] = $issue_html->find('.comment .markdown', 0);
}
2019-06-21 20:52:51 +03:00
$this->items[] = $item;
}
}
2019-06-21 20:52:51 +03:00
protected function collectSingleIssueData($html)
{
$comments = $html->find('.comments .comment')
or returnServerError('Unable to find comments');
2019-06-21 20:52:51 +03:00
foreach ($comments as $comment) {
$this->items[] = [
'uri' => $comment->find('a[href*="#issue"]', 0)->href,
'title' => $comment->find('span', 0)->plaintext,
'author' => $comment->find('.content a', 0)->plaintext,
'timestamp' => $comment->find('.time-since', 0)->title,
'content' => $comment->find('.markdown', 0),
];
}
2019-06-21 20:52:51 +03:00
$this->items = array_reverse($this->items);
}
2019-06-21 20:52:51 +03:00
protected function collectReleasesData($html)
{
$releases = $html->find('#release-list li')
or returnServerError('Unable to find releases');
2019-06-21 20:52:51 +03:00
foreach ($releases as $release) {
$this->items[] = [
'uri' => $release->find('a', 0)->href,
'title' => 'Release ' . $release->find('h4', 0)->plaintext,
];
}
}
}