2021-09-11 15:33:24 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @brief improve, a plugin for Dotclear 2
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-11 15:33:24 +00:00
|
|
|
* @package Dotclear
|
|
|
|
* @subpackage Plugin
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-11 15:33:24 +00:00
|
|
|
* @author Jean-Christian Denis and contributors
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-11 15:33:24 +00:00
|
|
|
* @copyright Jean-Christian Denis
|
|
|
|
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
|
|
|
|
*/
|
|
|
|
class ImproveActionGitshields extends ImproveAction
|
|
|
|
{
|
2021-11-01 21:17:44 +00:00
|
|
|
private $stop_scan = false;
|
2021-09-11 15:33:24 +00:00
|
|
|
protected $bloc_pattern = [
|
|
|
|
'remove' => '/\[!\[Release(.*)LICENSE\)/ms',
|
|
|
|
'target' => '/^([^\n]+)[\r\n|\n]{1,}/ms'
|
|
|
|
];
|
|
|
|
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%)',
|
2021-11-01 21:17:44 +00:00
|
|
|
'license' => '[![License](https://img.shields.io/github/license/%username%/%module%)](https://github.com/%username%/%module%/blob/master/LICENSE)'
|
2021-09-11 15:33:24 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
protected function init(): bool
|
|
|
|
{
|
|
|
|
$this->setProperties([
|
|
|
|
'id' => 'gitshields',
|
2021-09-15 21:48:58 +00:00
|
|
|
'name' => __('Shields badges'),
|
2021-09-11 15:33:24 +00:00
|
|
|
'desc' => __('Add and maintain shields.io badges to the REDAME.md file'),
|
|
|
|
'priority' => 380,
|
|
|
|
'config' => true,
|
|
|
|
'types' => ['plugin', 'theme']
|
|
|
|
]);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isConfigured(): bool
|
|
|
|
{
|
2021-09-15 21:48:58 +00:00
|
|
|
return !empty($this->getSetting('username'));
|
2021-09-11 15:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function configure($url): ?string
|
|
|
|
{
|
|
|
|
if (!empty($_POST['save']) && !empty($_POST['username'])) {
|
2021-09-15 21:48:58 +00:00
|
|
|
$this->setSettings([
|
2021-11-01 21:17:44 +00:00
|
|
|
'username' => (string) $_POST['username'],
|
2021-09-11 21:01:06 +00:00
|
|
|
'dotaddict' => !empty($_POST['dotaddict'])
|
|
|
|
]);
|
2021-09-11 15:33:24 +00:00
|
|
|
$this->redirect($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
return '
|
2021-09-11 21:01:06 +00:00
|
|
|
<p><label for="username">' . __('Your Github user name :') . '</label>' .
|
2021-09-15 21:48:58 +00:00
|
|
|
form::field('username', 60, 100, $this->getSetting('username')) . '
|
2021-09-11 20:29:42 +00:00
|
|
|
</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, !empty($this->getSetting('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>';
|
2021-09-11 15:33:24 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
public function openModule(): ?bool
|
2021-09-11 15:33:24 +00:00
|
|
|
{
|
|
|
|
$this->replaceInfo();
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
public function readFile(&$content): ?bool
|
2021-09-11 15:33:24 +00:00
|
|
|
{
|
2021-09-15 21:48:58 +00:00
|
|
|
if ($this->stop_scan || !preg_match('/(.*?)README\.md$/i', $this->path_full)) {
|
2021-09-11 15:33:24 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-11-01 21:17:44 +00:00
|
|
|
$clean = $this->deleteShieldsBloc($content);
|
|
|
|
$content = $this->writeShieldsBloc($clean);
|
2021-09-11 15:33:24 +00:00
|
|
|
$this->stop_scan = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function replaceInfo()
|
|
|
|
{
|
2021-09-11 21:01:06 +00:00
|
|
|
$bloc = [];
|
2021-11-01 21:17:44 +00:00
|
|
|
foreach ($this->bloc_content as $k => $v) {
|
2021-09-15 21:48:58 +00:00
|
|
|
if ($k == 'dotaddict' && empty($this->getSetting('dotaddict'))) {
|
2021-09-11 21:01:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-09-11 15:33:24 +00:00
|
|
|
$bloc[$k] = trim(str_replace(
|
2021-09-15 21:48:58 +00:00
|
|
|
[
|
2021-11-01 21:17:44 +00:00
|
|
|
'%username%',
|
|
|
|
'%module%',
|
|
|
|
'%dotclear%',
|
|
|
|
'%type%',
|
2021-09-15 21:48:58 +00:00
|
|
|
"\r\n", "\n"
|
|
|
|
],
|
|
|
|
[
|
2021-11-01 21:17:44 +00:00
|
|
|
$this->getSetting('username'),
|
|
|
|
$this->module['id'],
|
|
|
|
$dotclear = $this->getDotclearVersion(),
|
|
|
|
$this->module['type'],
|
2021-09-15 21:48:58 +00:00
|
|
|
'', ''
|
|
|
|
],
|
2021-09-11 15:33:24 +00:00
|
|
|
$v
|
|
|
|
));
|
|
|
|
}
|
|
|
|
$this->bloc = $bloc;
|
2021-09-15 21:48:58 +00:00
|
|
|
$this->setSuccess(__('Prepare custom shield info'));
|
2021-09-11 15:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getDotclearVersion()
|
|
|
|
{
|
|
|
|
$version = null;
|
2021-09-15 21:48:58 +00:00
|
|
|
if (!empty($this->module['requires']) && is_array($this->module['requires'])) {
|
|
|
|
foreach ($this->module['requires'] as $req) {
|
2021-09-11 15:33:24 +00:00
|
|
|
if (!is_array($req)) {
|
|
|
|
$req = [$req];
|
|
|
|
}
|
|
|
|
if ($req[0] == 'core') {
|
|
|
|
$version = $req[1];
|
2021-11-01 21:17:44 +00:00
|
|
|
|
2021-09-11 15:33:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-09-17 16:44:17 +00:00
|
|
|
} elseif (!empty($this->module['dc_min'])) {
|
|
|
|
$version = $this->module['dc_min'];
|
2021-09-11 15:33:24 +00:00
|
|
|
}
|
2021-11-01 21:17:44 +00:00
|
|
|
|
2021-09-11 15:33:24 +00:00
|
|
|
return $version ?: $this->core->getVersion('core');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function writeShieldsBloc($content)
|
|
|
|
{
|
2021-09-15 21:48:58 +00:00
|
|
|
$res = preg_replace(
|
2021-09-11 15:33:24 +00:00
|
|
|
$this->bloc_pattern['target'],
|
|
|
|
'$1' . "\n\n" . trim(implode("\n", $this->bloc)) . "\n\n",
|
|
|
|
$content,
|
2021-09-15 21:48:58 +00:00
|
|
|
1,
|
|
|
|
$count
|
2021-09-11 15:33:24 +00:00
|
|
|
);
|
2021-09-15 21:48:58 +00:00
|
|
|
if ($count) {
|
|
|
|
$this->setSuccess(__('Write new shield bloc'));
|
|
|
|
}
|
2021-11-01 21:17:44 +00:00
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
return $res;
|
2021-09-11 15:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function deleteShieldsBloc($content)
|
|
|
|
{
|
2021-09-15 21:48:58 +00:00
|
|
|
$res = preg_replace(
|
2021-09-11 15:33:24 +00:00
|
|
|
$this->bloc_pattern['remove'],
|
|
|
|
"\n\n",
|
2021-09-15 21:48:58 +00:00
|
|
|
$content,
|
|
|
|
1,
|
|
|
|
$count
|
2021-09-11 15:33:24 +00:00
|
|
|
);
|
2021-09-15 21:48:58 +00:00
|
|
|
if ($count) {
|
|
|
|
$this->setSuccess(__('Delete old shield bloc'));
|
|
|
|
}
|
2021-11-01 21:17:44 +00:00
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
return $res;
|
2021-09-11 15:33:24 +00:00
|
|
|
}
|
2021-11-01 21:17:44 +00:00
|
|
|
}
|