improve/inc/lib.improve.action.dcstore.php

191 lines
5.7 KiB
PHP
Raw Normal View History

2021-09-01 23:35:23 +00:00
<?php
/**
* @brief improve, a plugin for Dotclear 2
2021-11-01 21:17:44 +00:00
*
2021-09-01 23:35:23 +00:00
* @package Dotclear
2021-09-06 21:36:50 +00:00
* @subpackage Plugin
2021-11-01 21:17:44 +00:00
*
2021-09-01 23:35:23 +00:00
* @author Jean-Christian Denis and contributors
2021-11-01 21:17:44 +00:00
*
2021-09-01 23:35:23 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
class ImproveActionDcstore extends ImproveAction
{
protected function init(): bool
{
$this->setProperties([
2021-11-01 21:17:44 +00:00
'id' => 'dcstore',
'name' => __('Store file'),
'desc' => __('Re-create dcstore.xml file according to _define.php variables'),
2021-09-01 23:35:23 +00:00
'priority' => 420,
2021-11-01 21:17:44 +00:00
'config' => true,
'types' => ['plugin', 'theme']
2021-09-01 23:35:23 +00:00
]);
return true;
}
public function isConfigured(): bool
{
return !empty($this->getSetting('pattern'));
2021-09-01 23:35:23 +00:00
}
public function configure($url): ?string
{
if (!empty($_POST['save']) && !empty($_POST['dcstore_pattern'])) {
$this->setSettings('pattern', (string) $_POST['dcstore_pattern']);
2021-09-01 23:35:23 +00:00
$this->redirect($url);
}
2021-11-01 21:17:44 +00:00
return
2021-09-01 23:35:23 +00:00
'<p class="info">' . __('File will be overwritten if it exists') . '</p>' .
2021-11-01 21:17:44 +00:00
'<p><label class="classic" for="dcstore_pattern">' .
2021-09-01 23:35:23 +00:00
__('Predictable URL to zip file on the external repository') . '<br />' .
form::field('dcstore_pattern', 160, 255, $this->getSetting('pattern')) . '</label>' .
2021-09-01 23:35:23 +00:00
'</p>' .
2021-11-01 21:17:44 +00:00
'<p class="form-note">' .
sprintf(__('You can use wildcards %s'), '%author%, %type%, %id%, %version%.') .
2021-09-01 23:35:23 +00:00
'<br /> ' .
2021-11-01 21:17:44 +00:00
__('For exemple on github https://github.com/MyGitName/%id%/releases/download/v%version%/%type%-%id%.zip') .
2021-09-01 23:35:23 +00:00
'<br />' .
__('Note on github, you must create a release and join to it the module zip file.') . '
</p>';
}
public function openModule(): ?bool
2021-09-01 23:35:23 +00:00
{
$content = $this->generateXML();
if ($this->hasError()) {
2021-09-01 23:35:23 +00:00
return false;
}
2021-11-01 21:17:44 +00:00
2021-09-01 23:35:23 +00:00
try {
files::putContent($this->module['sroot'] . '/dcstore.xml', $content);
$this->setSuccess(__('Write dcstore.xml file.'));
2021-11-01 21:17:44 +00:00
} catch (Exception $e) {
$this->setError(__('Failed to write dcstore.xml file'));
2021-09-01 23:35:23 +00:00
return false;
}
return true;
}
public function generateXML()
2021-09-01 23:35:23 +00:00
{
$xml = ['<modules xmlns:da="http://dotaddict.org/da/">'];
$rsp = new xmlTag('module');
2021-09-01 23:35:23 +00:00
# id
if (empty($this->module['id'])) {
$this->setError(__('unkow module id'));
2021-09-01 23:35:23 +00:00
}
$rsp->id = $this->module['id'];
2021-09-01 23:35:23 +00:00
# name
if (empty($this->module['oname'])) {
$this->setError(__('unknow module name'));
2021-09-01 23:35:23 +00:00
}
$rsp->name($this->module['oname']);
2021-09-01 23:35:23 +00:00
# version
if (empty($this->module['version'])) {
$this->setError(__('unknow module version'));
2021-09-01 23:35:23 +00:00
}
$rsp->version($this->module['version']);
2021-09-01 23:35:23 +00:00
# author
if (empty($this->module['author'])) {
$this->setError(__('unknow module author'));
2021-09-01 23:35:23 +00:00
}
$rsp->author($this->module['author']);
2021-09-01 23:35:23 +00:00
# desc
if (empty($this->module['desc'])) {
$this->setError(__('unknow module description'));
2021-09-01 23:35:23 +00:00
}
$rsp->desc($this->module['desc']);
2021-09-01 23:35:23 +00:00
# repository
if (empty($this->module['repository'])) {
$this->setError(__('no repository set in _define.php'));
2021-09-01 23:35:23 +00:00
}
# file
$file_pattern = $this->parseFilePattern();
2021-09-01 23:35:23 +00:00
if (empty($file_pattern)) {
$this->setError(__('no zip file pattern set in configuration'));
2021-09-01 23:35:23 +00:00
}
$rsp->file($file_pattern);
2021-09-01 23:35:23 +00:00
# da dc_min or requires core
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') {
$this->module['dc_min'] = $req[1];
2021-11-01 21:17:44 +00:00
break;
}
}
}
if (empty($this->module['dc_min'])) {
$this->setWarning(__('no minimum dotclear version'));
2021-09-01 23:35:23 +00:00
} else {
$rsp->insertNode(new xmlTag('da:dcmin', $this->module['dc_min']));
2021-09-01 23:35:23 +00:00
}
# da details
if (empty($this->module['details'])) {
$this->setWarning(__('no details URL'));
2021-09-01 23:35:23 +00:00
} else {
$rsp->insertNode(new xmlTag('da:details', $this->module['details']));
2021-09-01 23:35:23 +00:00
}
# da sshot
//$rsp->insertNode(new xmlTag('da:sshot', $this->module['sshot']));
2021-09-01 23:35:23 +00:00
# da section
if (!empty($this->module['section'])) {
$rsp->insertNode(new xmlTag('da:section', $this->module['section']));
}
2021-09-01 23:35:23 +00:00
# da support
if (empty($this->module['support'])) {
$this->setWarning(__('no support URL'));
2021-09-01 23:35:23 +00:00
} else {
$rsp->insertNode(new xmlTag('da:support', $this->module['support']));
2021-09-01 23:35:23 +00:00
}
# da tags
//$rsp->insertNode(new xmlTag('da:tags', $this->module['tags']));
2021-09-01 23:35:23 +00:00
$res = new xmlTag('modules', $rsp);
$res->insertAttr('xmlns:da', 'http://dotaddict.org/da/');
2021-09-01 23:35:23 +00:00
return str_replace('><', ">\n<", $res->toXML());
2021-09-01 23:35:23 +00:00
}
private function parseFilePattern()
2021-09-01 23:35:23 +00:00
{
return text::tidyURL(str_replace(
[
'%type%',
'%id%',
'%version%',
'%author%'
],
[
$this->module['type'],
$this->module['id'],
$this->module['version'],
$this->module['author']
2021-09-01 23:35:23 +00:00
],
$this->getSetting('pattern')
2021-09-01 23:35:23 +00:00
));
}
2021-11-01 21:17:44 +00:00
}