-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Maintenance script to delete old diagrams files
Add new maintenance script to delete diagram files older than 30 days. Bug: #50
- Loading branch information
Showing
2 changed files
with
58 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |