1
0
This commit is contained in:
Richard Dern 2024-05-12 00:08:47 +02:00
parent 1d8d9683cc
commit 7c8a95fce1
3 changed files with 0 additions and 306 deletions

View File

@ -1,78 +0,0 @@
<?php
namespace App\Console\Commands\Bundle;
use App\Classes\Bundle;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
class DeadLinks extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bundle:dead-links
{ --c|clear : Clear current links.json file }
{ --r|recursive : Also check sub-bundles }
{ path? : Specific bundle for which to find dead links }';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Find dead links in bundles';
/**
* Execute the console command.
*/
public function handle()
{
$disk = Storage::disk(env('CONTENT_DISK'));
$path = $this->argument('path') ?? '/';
if ($this->option('recursive')) {
$bundles = Bundle::findBundles($disk, $path, true);
} else {
$bundles = [new Bundle($path, $disk)];
}
foreach ($bundles as $bundle) {
$bundle->load();
if ($this->option('clear')) {
$bundle->metadata('links')->clear();
$bundle->metadata('links')->save();
continue;
}
$deadLinks = [];
foreach ($bundle->metadata('links')->all() as $url => $data) {
if ($data['isDead']) {
$deadLinks[$url] = $data['reason'];
}
}
if (!empty($deadLinks)) {
$this->output->write(sprintf('Looking in %s... ', $bundle->getPath()));
$this->error('Dead links');
foreach ($deadLinks as $url => $reason) {
$this->line(sprintf(' - [%s]: %s', $url, $reason));
}
$this->newLine();
}
}
if ($this->option('clear')) {
$this->call('cache:clear');
$this->comment('links.json file(s) have been removed.');
$this->line('Remember to re-render bundles to check for dead links.');
}
}
}

View File

@ -1,41 +0,0 @@
<?php
namespace App\Console\Commands\Markdown;
use App\Services\Markdown\Linter;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
class Lint extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'markdown:lint { path : Path to the file to be formatted }';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Format markdown';
/**
* Execute the console command.
*/
public function handle()
{
Log::debug(sprintf('Linting %s', $this->argument('path')));
$content = Storage::disk(env('CONTENT_DISK'))->get($this->argument('path'));
$lint = new Linter($content);
$result = $lint->format();
if ($result !== $content) {
Storage::disk(env('CONTENT_DISK'))->put($this->argument('path'), $result);
}
}
}

View File

@ -1,187 +0,0 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use League\Flysystem\StorageAttributes;
class RestoreFiles extends Command
{
protected $signature = 'restore:files { path? : chemin de bundle }';
protected $description = 'Restaure les fichiers à partir de répertoires spécifiques';
protected $noBodyButAttachments = [];
public function __construct()
{
parent::__construct();
}
public function handle()
{
// Obtenez le disque à partir des variables d'environnement
$disk = Storage::disk(env('DIST_DISK', 'local'));
if (!empty($this->argument('path'))) {
$directories[] = $this->argument('path');
} else {
// Trouver tous les dossiers contenant un fichier `index.html`
$directories = $disk->listContents('/', true)
->filter(fn (StorageAttributes $attributes) => ($attributes->isDir() && $disk->exists($attributes->path() . '/index.html')))
->map(fn (StorageAttributes $attributes) => $attributes->path())
->toArray();
}
// Afficher les résultats
$this->info('Répertoires contenant un index.html :');
foreach ($directories as $folder) {
$this->processIndexHtml($disk, $folder);
}
$this->newLine();
$this->line('Récapitulatif');
$this->newLine();
foreach ($this->noBodyButAttachments as $path => $attachments) {
$this->newLine();
$this->line($path);
foreach ($attachments as $index => $file) {
$this->line(sprintf(' > %s', $file));
}
}
}
private function loadArticleMetadata($folder)
{
$jsonContent = Storage::disk(env('CONTENT_DISK'))->get($folder . '/data/index.json');
$data = json_decode($jsonContent, true);
return $data ?? []; // Retourne la référence de l'image d'en-tête si disponible
}
private function findAttachmentDetails($folder, $reference)
{
$types = ['images', 'videos', 'sounds']; // Les différents types d'attachments
foreach ($types as $type) {
$jsonPath = $folder . '/data/' . $type . '.json'; // Chemin vers le fichier JSON
if (Storage::disk(env('CONTENT_DISK'))->exists($jsonPath)) {
$jsonContent = Storage::disk(env('CONTENT_DISK'))->get($jsonPath);
$data = json_decode($jsonContent, true);
if (isset($data['files'][$reference])) {
return $data['files'][$reference]['filename']; // Retourne le 'filename' si la référence est trouvée
}
}
}
return null; // Retourne null si la référence n'est trouvée dans aucun fichier
}
private function extractReferencesFromMarkdown($markdownContent)
{
preg_match_all('/<x-attachment ref="([^"]+)" \/>/', $markdownContent, $matches);
return $matches[1]; // Retourne un tableau des références trouvées
}
private function processIndexHtml($disk, $folder)
{
$htmlContent = $disk->get($folder . '/index.html');
$markdownContent = Storage::disk(env('CONTENT_DISK'))->get($folder . '/index.md');
$hasNoBody = false;
$hasAttachments = false;
$attachments = [];
$this->newLine();
$this->line($folder);
if (empty($markdownContent)) {
$this->comment(' > Article has no body');
$hasNoBody = true;
}
$references = $this->extractReferencesFromMarkdown($markdownContent);
$cover = $this->loadArticleMetadata($folder)['cover'] ?? null;
$logo = $this->loadArticleMetadata($folder)['logo'] ?? null;
if ($cover) {
array_unshift($references, $cover);
}
if ($logo) {
array_push($references, $logo);
}
$dom = new \DOMDocument();
// Charger le HTML et supprimer les erreurs pour éviter les avertissements
@$dom->loadHTML($htmlContent);
$xpath = new \DOMXPath($dom);
$figureNodes = $xpath->query('//figure');
$index = 0;
foreach ($figureNodes as $index => $figure) {
// Traiter les images
$imageLinks = (new \DOMXPath($dom))->query('.//a[starts-with(@href, "/storage")]', $figure);
$this->processLinks($imageLinks, $references, $index, $folder, $attachments, 'image');
// Traiter les vidéos
$videoSources = (new \DOMXPath($dom))->query('.//video/source[starts-with(@src, "/storage")]', $figure);
$this->processLinks($videoSources, $references, $index, $folder, $attachments, 'video');
// Traiter les sons
$audioSources = (new \DOMXPath($dom))->query('.//audio/source[starts-with(@src, "/storage")]', $figure);
$this->processLinks($audioSources, $references, $index, $folder, $attachments, 'audio');
}
foreach ($attachments as $source => $destination) {
$copied = false;
$contents = file_get_contents($source);
if (!empty($contents)) {
$copied = Storage::disk(env('CONTENT_DISK'))->put($destination, $contents);
}
if ($copied) {
$this->info(sprintf(' > %s > %s', $source, $destination));
} else {
$this->error(sprintf(' > %s > %s', $source, $destination));
}
}
if ($hasAttachments && $hasNoBody) {
$this->noBodyButAttachments[$folder] = $attachments;
}
}
private function processLinks($nodes, $references, &$index, $folder, &$attachments, $type)
{
foreach ($nodes as $node) {
$pathAttribute = ($type === 'image' ? 'href' : 'src');
$href = $node->getAttribute($pathAttribute);
$realPath = Str::replace('/storage', 'storage/app/public', $href);
$reference = $references[$index] ?? 'Unknown';
if ($reference !== 'Unknown') {
$filename = $this->findAttachmentDetails($folder, $reference);
$filename = $folder . '/data/' . $filename;
} else {
$filename = $folder . '/data/attachments/' . $type . 's/' . basename($realPath);
}
// if (empty($attachments[$realPath]) || !in_array($realPath, $attachments[$reference])) {
$attachments[$realPath] = $filename;
// }
$index++;
}
}
}