Neues Modul: Qualitätsmanagement
All checks were successful
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m46s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m51s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m2s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m55s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 4m15s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 4m16s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 3m44s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 3m46s
CI / Build-Artefakte aktuell (pull_request) Successful in 52s

- Kategorien mit farbigen Ordnerkacheln; Farbe und Symbol je Ordner
  (Auswahl oder eigener Upload, SVG bereinigt), an Unterordner vererbt
- Ordnerseite: Einführungstext, Ansprechpartner (Profilfelder wählbar),
  Unterordner, Liste mit Dokument, Version, Stand, Größe, Aktion
- Einträge: Datei (Lesefassung + bearbeitbare Vorlage), Link,
  Online-Formular; neue Fassungen archivieren die bisherigen
- PDF-Betrachter im Portal (pdf.js)
- Eigene Volltextsuche (PDF, Word, Excel, PowerPoint, OpenDocument)
  mit Textausschnitt; optionale Texterkennung per Tesseract
- Rollen-Ausschluss für das ganze Modul, Pflegerecht qm.manage,
  Zielgruppen je Kategorie
- Vorbereitet: Wiedervorlage, Verantwortliche, Lesebestätigungen
- Abhängigkeit smalot/pdfparser

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-26 15:50:23 +00:00
parent f78cc96be7
commit b3a057e27c
218 changed files with 13478 additions and 358 deletions

View file

@ -0,0 +1,163 @@
<?php
namespace Inhaus\Plugins\Qm\Text;
use App\Support\Settings;
use Imagick;
use Throwable;
/**
* Texterkennung (OCR) für eingescannte PDFs – optional, wenn der Server sie kann:
* Tesseract (Programm „tesseract“ mit deutschen Sprachdaten) und ein Umwandler für
* PDF-Seiten in Bilder (Programm „pdftoppm“ aus poppler-utils oder PHP-Imagick mit
* Ghostscript). Auf Shared Hosting meist nicht vorhanden – dann bleiben solche
* Dateien in der Suche nur über ihren Titel auffindbar.
*
* Einrichtung (Debian/Ubuntu): apt install tesseract-ocr tesseract-ocr-deu poppler-utils
*/
class Ocr
{
public const SETTING_ENABLED = 'qm.ocr.enabled';
public const SETTING_BINARY = 'qm.ocr.tesseract';
public const MAX_PAGES = 40;
public function __construct(private readonly Settings $settings) {}
public function enabled(): bool
{
return (bool) $this->settings->get(self::SETTING_ENABLED, false);
}
/**
* Was auf diesem Server vorhanden ist.
*
* @return array{exec: bool, // Programme dürfen aufgerufen werden (proc_open)
* tesseract: ?string, languages: list<string>, rasterizer: ?string}
*/
public function status(): array
{
$exec = function_exists('proc_open') && ! in_array('proc_open', array_map('trim', explode(',', (string) ini_get('disable_functions'))), true);
$tesseract = null;
$languages = [];
if ($exec) {
$binary = $this->binary();
$version = $this->run([$binary, '--version']);
if ($version !== null && str_contains(strtolower($version), 'tesseract')) {
$tesseract = trim(strtok($version, "\n"));
$languages = array_values(array_filter(array_map('trim', array_slice(explode("\n", (string) $this->run([$binary, '--list-langs'])), 1))));
}
}
return ['exec' => $exec, 'tesseract' => $tesseract, 'languages' => $languages, 'rasterizer' => $this->rasterizer($exec)];
}
public function available(): bool
{
$status = $this->status();
return $status['tesseract'] !== null && $status['rasterizer'] !== null;
}
/**
* Text einer PDF-Datei per Texterkennung.
*/
public function recognize(string $pdf): string
{
$dir = sys_get_temp_dir().'/inhaus-ocr-'.bin2hex(random_bytes(6));
@mkdir($dir, 0700, true);
try {
$images = $this->rasterize($pdf, $dir);
$languages = in_array('deu', $this->status()['languages'], true) ? 'deu+eng' : 'eng';
$text = '';
foreach ($images as $image) {
$text .= $this->run([$this->binary(), $image, 'stdout', '-l', $languages, '--psm', '1'])."\n";
}
return TextExtractor::normalize($text);
} finally {
array_map('unlink', glob($dir.'/*') ?: []);
@rmdir($dir);
}
}
private function binary(): string
{
$binary = trim((string) $this->settings->get(self::SETTING_BINARY, '')) ?: 'tesseract';
// Nur ein Programmname oder ein absoluter Pfad – keine Parameter.
return preg_match('#^(/[\w./-]+|[\w.-]+)$#', $binary) ? $binary : 'tesseract';
}
private function rasterizer(bool $exec): ?string
{
if ($exec && $this->run(['pdftoppm', '-v']) !== null) {
return 'pdftoppm';
}
try {
if (class_exists(Imagick::class) && in_array('PDF', Imagick::queryFormats('PDF'), true)) {
return 'imagick';
}
} catch (Throwable) {
// Imagick ohne PDF-Unterstützung
}
return null;
}
/**
* @return list<string> Bilddateien der Seiten
*/
private function rasterize(string $pdf, string $dir): array
{
if ($this->rasterizer(true) === 'pdftoppm') {
$this->run(['pdftoppm', '-r', '300', '-gray', '-png', '-l', (string) self::MAX_PAGES, $pdf, $dir.'/seite']);
} else {
$imagick = new Imagick;
$imagick->setResolution(300, 300);
$imagick->readImage($pdf.'[0-'.(self::MAX_PAGES - 1).']');
foreach ($imagick as $index => $page) {
$page->setImageFormat('png');
$page->writeImage(sprintf('%s/seite-%03d.png', $dir, $index + 1));
}
$imagick->clear();
}
$images = glob($dir.'/seite*.png') ?: [];
natsort($images);
return array_values($images);
}
/**
* Programm ohne Shell aufrufen; null, wenn es nicht läuft.
*
* @param list<string> $command
*/
protected function run(array $command): ?string
{
if (! function_exists('proc_open')) {
return null;
}
$process = @proc_open($command, [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes);
if (! is_resource($process)) {
return null;
}
$output = stream_get_contents($pipes[1]).stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
return proc_close($process) === 0 ? (string) $output : null;
}
}

View file

@ -0,0 +1,120 @@
<?php
namespace Inhaus\Plugins\Qm\Text;
use Smalot\PdfParser\Parser;
use Throwable;
use ZipArchive;
/**
* Text aus Dokumenten für die Volltextsuche – in reinem PHP, läuft also auch auf
* Shared Hosting. Eingescannte PDFs ohne Textebene liefern nichts; die übernimmt
* (falls eingerichtet) die Texterkennung (Ocr).
*/
class TextExtractor
{
/** Mehr Text braucht die Suche nicht. */
public const MAX_CHARS = 2_000_000;
/**
* @return array{text: ?string, status: string} status: ok | empty | unsupported | failed
*/
public function extract(string $path, string $extension): array
{
try {
$text = match (strtolower($extension)) {
'pdf' => $this->pdf($path),
'docx' => $this->xmlInZip($path, ['word/document.xml', 'word/header1.xml', 'word/footer1.xml']),
'xlsx' => $this->xmlInZip($path, ['xl/sharedStrings.xml']),
'pptx' => $this->xmlInZip($path, $this->zipEntries($path, '#^ppt/slides/slide\d+\.xml$#')),
'odt', 'ods', 'odp' => $this->xmlInZip($path, ['content.xml']),
'txt', 'csv' => (string) file_get_contents($path, length: self::MAX_CHARS),
default => null,
};
} catch (Throwable $e) {
logger()->warning("QM: Text aus {$path} nicht lesbar: ".$e->getMessage());
return ['text' => null, 'status' => 'failed'];
}
if ($text === null) {
return ['text' => null, 'status' => 'unsupported'];
}
$text = self::normalize($text);
return ['text' => $text === '' ? null : $text, 'status' => $text === '' ? 'empty' : 'ok'];
}
public static function normalize(string $text): string
{
$text = mb_convert_encoding($text, 'UTF-8', 'UTF-8');
$text = preg_replace('/[^\P{C}\n\t]+/u', ' ', $text) ?? '';
$text = preg_replace('/[ \t\x{00A0}]+/u', ' ', $text) ?? '';
$text = preg_replace('/\s*\n\s*/', "\n", $text) ?? '';
return mb_substr(trim($text), 0, self::MAX_CHARS);
}
private function pdf(string $path): string
{
$parser = new Parser;
return $parser->parseFile($path)->getText();
}
/**
* @param list<string> $entries
*/
private function xmlInZip(string $path, array $entries): string
{
$zip = new ZipArchive;
if ($zip->open($path) !== true) {
throw new \RuntimeException('Keine gültige Office-Datei.');
}
$text = '';
foreach ($entries as $entry) {
$xml = $zip->getFromName($entry);
if ($xml !== false) {
// Absätze und Zellen als Zeilen, dann Tags entfernen.
$xml = preg_replace('#</(w:p|a:p|text:p|text:h|si|table:table-cell)>#', "\n", $xml);
$text .= html_entity_decode(strip_tags((string) $xml), ENT_QUOTES | ENT_XML1, 'UTF-8')."\n";
}
}
$zip->close();
return $text;
}
/**
* @return list<string>
*/
private function zipEntries(string $path, string $pattern): array
{
$zip = new ZipArchive;
if ($zip->open($path) !== true) {
return [];
}
$names = [];
for ($i = 0; $i < $zip->numFiles; $i++) {
$name = (string) $zip->getNameIndex($i);
if (preg_match($pattern, $name)) {
$names[] = $name;
}
}
$zip->close();
natsort($names);
return array_values($names);
}
}