1<?php 2/** 3 * DokuWiki Plugin pagestats (Syntax Component) 4 * Allows using ~~PAGESTATSPAGE~~, ~~PAGESTATSMB~~, ~~MEDIASTATSPAGE~~, and ~~MEDIASTATSMB~~ to display stats. 5 */ 6 7if (!defined('DOKU_INC')) die(); 8 9class syntax_plugin_pagestats extends DokuWiki_Syntax_Plugin { 10 11 public function getType() { 12 return 'substition'; 13 } 14 15 public function getSort() { 16 return 999; 17 } 18 19 public function connectTo($mode) { 20 $this->Lexer->addSpecialPattern('~~PAGESTATSPAGE~~', $mode, 'plugin_pagestats'); 21 $this->Lexer->addSpecialPattern('~~PAGESTATSMB~~', $mode, 'plugin_pagestats'); 22 $this->Lexer->addSpecialPattern('~~MEDIASTATSPAGE~~', $mode, 'plugin_pagestats'); 23 $this->Lexer->addSpecialPattern('~~MEDIASTATSMB~~', $mode, 'plugin_pagestats'); 24 } 25 26 public function handle($match, $state, $pos, Doku_Handler $handler) { 27 return trim($match, '~'); // Gibt die genaue Syntax zurück 28 } 29 30public function render($mode, Doku_Renderer $renderer, $data) { 31 if ($mode !== 'xhtml') return false; 32 33 $dataPathPages = DOKU_INC . 'data/pages'; 34 $dataPathMedia = DOKU_INC . 'data/media'; 35 36 $stats = [ 37 'PAGESTATSPAGE' => $this->countFiles($dataPathPages, 'txt'), 38 'PAGESTATSMB' => $this->calculateSize($dataPathPages, 'txt'), 39 'MEDIASTATSPAGE' => $this->countFiles($dataPathMedia, ''), 40 'MEDIASTATSMB' => $this->calculateSize($dataPathMedia, '') 41 ]; 42 43 if (isset($stats[$data])) { 44 // Zahl direkt ausgeben 45 $value = $stats[$data]; 46 47 // "MB" bei Speicherangaben anhängen 48 if (in_array($data, ['PAGESTATSMB', 'MEDIASTATSMB'])) { 49 $value .= " MB"; 50 } 51 52 // Ausgabe in den Renderer einfügen 53 $renderer->doc .= hsc($value); 54 } 55 56 return true; 57} 58 59 private function countFiles($path, $extension) { 60 if (!is_dir($path)) return 0; 61 62 $count = 0; 63 $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); 64 foreach ($iterator as $file) { 65 if ($file->isFile() && ($extension === '' || $file->getExtension() === $extension)) { 66 $count++; 67 } 68 } 69 70 return $count; 71 } 72 73 private function calculateSize($path, $extension) { 74 if (!is_dir($path)) return 0; 75 76 $totalSize = 0; 77 $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); 78 foreach ($iterator as $file) { 79 if ($file->isFile() && ($extension === '' || $file->getExtension() === $extension)) { 80 $totalSize += $file->getSize(); 81 } 82 } 83 84 return round($totalSize / (1024 * 1024), 2); // In MB 85 } 86} 87