improve/inc/module/gitshields.php

206 lines
6.3 KiB
PHP
Raw Normal View History

<?php
/**
* @brief improve, a plugin for Dotclear 2
2021-11-01 21:17:44 +00:00
*
* @package Dotclear
* @subpackage Plugin
2021-11-01 21:17:44 +00:00
*
* @author Jean-Christian Denis and contributors
2021-11-01 21:17:44 +00:00
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace plugins\improve\module;
/* dotclear */
use dcCore;
/* improve */
use plugins\improve\action;
/* clearbricks */
use form;
/**
* Improve action module Github shields.io
*/
class gitshields extends action
{
/** @var string Username of git repo */
private $username = '';
/** @var boolean add Dotaddict shield */
private $dotaddict = false;
2021-11-07 23:59:52 +00:00
/** @var boolean Stop scaning files */
private $stop_scan = false;
/** @var array Parsed bloc */
private $blocs = [];
/** @var array Search patterns */
protected $bloc_pattern = [
'remove' => '/\[!\[Release(.*)LICENSE\)/ms',
'target' => '/^([^\n]+)[\r\n|\n]{1,}/ms',
];
2021-11-07 23:59:52 +00:00
/** @var array Shields patterns */
protected $bloc_content = [
2021-11-01 21:17:44 +00:00
'release' => '[![Release](https://img.shields.io/github/v/release/%username%/%module%)](https://github.com/%username%/%module%/releases)',
'date' => '[![Date](https://img.shields.io/github/release-date/%username%/%module%)](https://github.com/%username%/%module%/releases)',
'issues' => '[![Issues](https://img.shields.io/github/issues/%username%/%module%)](https://github.com/%username%/%module%/issues)',
'dotclear' => '[![Dotclear](https://img.shields.io/badge/dotclear-v%dotclear%-blue.svg)](https://fr.dotclear.org/download)',
2021-09-11 21:05:31 +00:00
'dotaddict' => '[![Dotaddict](https://img.shields.io/badge/dotaddict-official-green.svg)](https://%type%s.dotaddict.org/dc2/details/%module%)',
'license' => '[![License](https://img.shields.io/github/license/%username%/%module%)](https://github.com/%username%/%module%/blob/master/LICENSE)',
];
protected function init(): bool
{
$this->setProperties([
'id' => 'gitshields',
'name' => __('Shields badges'),
'description' => __('Add and maintain shields.io badges to the REDAME.md file'),
'priority' => 380,
'configurator' => true,
'types' => ['plugin', 'theme'],
]);
$username = $this->getSetting('username');
$this->username = is_string($username) ? $username : '';
$this->dotaddict = (bool) $this->getSetting('dotaddict');
return true;
}
public function isConfigured(): bool
{
return !empty($this->getSetting('username'));
}
public function configure($url): ?string
{
if (!empty($_POST['save']) && !empty($_POST['username'])) {
$this->setSettings([
2021-11-01 21:17:44 +00:00
'username' => (string) $_POST['username'],
'dotaddict' => !empty($_POST['dotaddict']),
2021-09-11 21:01:06 +00:00
]);
$this->redirect($url);
}
return '
2021-09-11 21:01:06 +00:00
<p><label for="username">' . __('Your Github user name :') . '</label>' .
form::field('username', 60, 100, $this->username) . '
</p><p class="form-note">' . __('Used in your Github URL: http://github.com/username/module_id.') . '<br />' .
2021-09-11 21:01:06 +00:00
__('If you have badges not created by this tool in the README.md file you should remove them manually.') . '</p>
2021-11-01 21:17:44 +00:00
<p><label for="dotaddict">' .
form::checkbox('dotaddict', 1, $this->dotaddict) . ' ' .
2021-09-11 21:01:06 +00:00
__('Include Dotaddict badge') . '</label>
</p><p class="form-note">' . __('If your plugin or theme is on Dotaddict, you can add a badge to link to its details in Dotaddict.') . '</p>';
}
public function openModule(): ?bool
{
$this->replaceInfo();
return null;
}
public function readFile(&$content): ?bool
{
if ($this->stop_scan || !preg_match('/(.*?)README\.md$/i', $this->path_full)) {
return null;
}
2021-11-01 21:17:44 +00:00
$clean = $this->deleteShieldsBloc($content);
$content = $this->writeShieldsBloc($clean);
$this->stop_scan = true;
return true;
}
2021-11-07 23:59:52 +00:00
private function replaceInfo(): void
{
2021-11-07 23:59:52 +00:00
$blocs = [];
2021-11-01 21:17:44 +00:00
foreach ($this->bloc_content as $k => $v) {
if ($k == 'dotaddict' && !$this->dotaddict) {
2021-09-11 21:01:06 +00:00
continue;
}
2021-11-07 23:59:52 +00:00
$blocs[$k] = trim(str_replace(
[
2021-11-01 21:17:44 +00:00
'%username%',
'%module%',
'%dotclear%',
'%type%',
"\r\n", "\n",
],
[
$this->username,
2021-11-01 21:17:44 +00:00
$this->module['id'],
$dotclear = $this->getDotclearVersion(),
$this->module['type'],
'', '',
],
$v
));
}
2021-11-07 23:59:52 +00:00
$this->blocs = $blocs;
$this->setSuccess(__('Prepare custom shield info'));
}
2021-11-07 23:59:52 +00:00
private function getDotclearVersion(): string
{
$version = null;
if (!empty($this->module['requires']) && is_array($this->module['requires'])) {
foreach ($this->module['requires'] as $req) {
if (!is_array($req)) {
$req = [$req];
}
if ($req[0] == 'core') {
$version = $req[1];
2021-11-01 21:17:44 +00:00
break;
}
}
2021-09-17 16:44:17 +00:00
} elseif (!empty($this->module['dc_min'])) {
$version = $this->module['dc_min'];
}
2021-11-01 21:17:44 +00:00
return $version ?: dcCore::app()->getVersion('core');
}
2021-11-07 23:59:52 +00:00
private function writeShieldsBloc(string $content): string
{
$res = preg_replace(
$this->bloc_pattern['target'],
2021-11-07 23:59:52 +00:00
'$1' . "\n\n" . trim(implode("\n", $this->blocs)) . "\n\n",
$content,
1,
$count
);
2021-11-07 23:59:52 +00:00
if ($count && $res) {
$this->setSuccess(__('Write new shield bloc'));
}
2021-11-01 21:17:44 +00:00
2021-11-07 23:59:52 +00:00
return (string) $res;
}
2021-11-07 23:59:52 +00:00
private function deleteShieldsBloc(string $content): string
{
$res = preg_replace(
$this->bloc_pattern['remove'],
"\n\n",
$content,
1,
$count
);
2021-11-07 23:59:52 +00:00
if ($count && $res) {
$this->setSuccess(__('Delete old shield bloc'));
}
2021-11-01 21:17:44 +00:00
2021-11-07 23:59:52 +00:00
return (string) $res;
}
2021-11-01 21:17:44 +00:00
}