Skip to content

Commit

Permalink
Maintenance script to delete old diagrams files
Browse files Browse the repository at this point in the history
Add new maintenance script to delete diagram files older than 30 days.

Bug: #50
  • Loading branch information
samwilson committed Jul 28, 2023
1 parent 6d25f39 commit dd7a5f1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
19 changes: 13 additions & 6 deletions includes/Diagrams.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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.
Expand Down
45 changes: 45 additions & 0 deletions maintenance/deleteOldDiagramsFiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

use MediaWiki\Extension\Diagrams\Diagrams;
use MediaWiki\MediaWikiServices;

$IP = getenv( 'MW_INSTALL_PATH' );
if ( $IP === false ) {
$IP = __DIR__ . '/../../..';
}
require_once "$IP/maintenance/Maintenance.php";

/**
* @ingroup Maintenance
*/
class DeleteOldDiagramsFiles extends Maintenance {

public function __construct() {
parent::__construct();
$this->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;

0 comments on commit dd7a5f1

Please sign in to comment.