From dd7a5f132a627ca7928de477332871a839e01149 Mon Sep 17 00:00:00 2001 From: Sam Wilson Date: Sun, 18 Dec 2022 14:46:08 +0800 Subject: [PATCH] Maintenance script to delete old diagrams files Add new maintenance script to delete diagram files older than 30 days. Bug: #50 --- includes/Diagrams.php | 19 +++++++---- maintenance/deleteOldDiagramsFiles.php | 45 ++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 maintenance/deleteOldDiagramsFiles.php diff --git a/includes/Diagrams.php b/includes/Diagrams.php index 197dbb1..63dccc3 100644 --- a/includes/Diagrams.php +++ b/includes/Diagrams.php @@ -35,12 +35,9 @@ protected function formatError( $error ) { } /** - * @param string $commandName The command to render the graph with. - * @param string $input The graph source. - * @param array $params Parameter to the wikitext tag (caption, format, etc.). - * @return string HTML to display the image and image map. + * @return LocalRepo */ - public function renderLocally( string $commandName, string $input, array $params ) { + public function getDiagramsRepo(): LocalRepo { $localRepo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo(); $diagramsRepo = new LocalRepo( [ 'class' => 'LocalRepo', @@ -55,11 +52,21 @@ public function renderLocally( string $commandName, string $input, array $params 'deletedHashLevels' => 0, 'zones' => [ 'public' => [ - 'directory' => '/diagrams', + 'directory' => 'diagrams', ], ], ] ); + return $diagramsRepo; + } + /** + * @param string $commandName The command to render the graph with. + * @param string $input The graph source. + * @param array $params Parameter to the wikitext tag (caption, format, etc.). + * @return string HTML to display the image and image map. + */ + public function renderLocally( string $commandName, string $input, array $params ) { + $diagramsRepo = $this->getDiagramsRepo(); $outputFormats = [ 'image' => $params['format'] ?? 'png' ]; if ( $commandName !== 'plantuml' ) { // Add image map output where it's supported. diff --git a/maintenance/deleteOldDiagramsFiles.php b/maintenance/deleteOldDiagramsFiles.php new file mode 100644 index 0000000..92b75ce --- /dev/null +++ b/maintenance/deleteOldDiagramsFiles.php @@ -0,0 +1,45 @@ +requireExtension( 'Diagrams' ); + $this->addDescription( 'Delete old Diagrams files.' ); + $this->addOption( 'ttl', 'Delete files older than this number of days. Default 30 days.', false, true ); + } + + public function execute(): void { + $services = MediaWikiServices::getInstance(); + $diagrams = new Diagrams( false, $services->getShellCommandFactory() ); + $repo = $diagrams->getDiagramsRepo(); + $deletedCount = 0; + $ttl = $this->getOption( 'ttl', 30 ) * 60 * 60 * 24; + $ttlTime = wfTimestampNow() - $ttl; + $repo->enumFiles( static function ( string $filePath ) use ( &$deletedCount, $repo, $ttlTime ) { + if ( $repo->getFileTimestamp( $filePath ) > $ttlTime ) { + return; + } + $deleted = $repo->getBackend()->delete( [ 'src' => $filePath ] ); + if ( $deleted->isOK() ) { + $deletedCount++; + } + } ); + $this->output( "Deleted $deletedCount files.\n" ); + } +} + +$maintClass = DeleteOldDiagramsFiles::class; +require_once RUN_MAINTENANCE_IF_MAIN;