Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeleteOldScoreFiles
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2/**
3 * Deletes score files from storage
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24use MediaWiki\Extension\Score\Score;
25use MediaWiki\Maintenance\Maintenance;
26use MediaWiki\Status\Status;
27
28if ( getenv( 'MW_INSTALL_PATH' ) ) {
29    $IP = getenv( 'MW_INSTALL_PATH' );
30} else {
31    $IP = __DIR__ . '/../../..';
32}
33
34require_once "$IP/maintenance/Maintenance.php";
35
36/**
37 * Maintenance script that deletes old score files from storage
38 *
39 * @ingroup Maintenance
40 */
41class DeleteOldScoreFiles extends Maintenance {
42    public function __construct() {
43        parent::__construct();
44        $this->addDescription( "Deletes old score files from storage" );
45        $this->addOption(
46            "date",
47            'Delete score files that were created before this date (e.g. 20170101000000)',
48            true,
49            true
50        );
51        $this->requireExtension( "Score" );
52    }
53
54    public function execute() {
55        $backend = Score::getBackend();
56        $dir = $backend->getRootStoragePath() . '/score-render';
57
58        $filesToDelete = [];
59        $deleteDate = $this->getOption( 'date' );
60        foreach (
61            $backend->getFileList( [ 'dir' => $dir, 'adviseStat' => true ] ) as $file
62        ) {
63            $fullPath = $dir . '/' . $file;
64            $timestamp = $backend->getFileTimestamp( [ 'src' => $fullPath ] );
65            if ( $timestamp < $deleteDate ) {
66                $filesToDelete[] = [ 'op' => 'delete', 'src' => $fullPath, ];
67            }
68        }
69
70        $count = count( $filesToDelete );
71
72        if ( !$count ) {
73            $this->output( "No old score files to delete!\n" );
74            return;
75        }
76
77        $this->output( "$count old score files to be deleted.\n" );
78
79        $deletedCount = 0;
80        foreach ( array_chunk( $filesToDelete, 1000 ) as $chunk ) {
81            $ret = $backend->doQuickOperations( $chunk );
82
83            if ( $ret->isOK() ) {
84                $deletedCount += count( $chunk );
85                $this->output( "$deletedCount...\n" );
86            } else {
87                $status = Status::wrap( $ret );
88                $this->output( "Deleting old score files errored.\n" );
89                $this->error( $status->getWikiText( false, false, 'en' ) );
90            }
91        }
92
93        $this->output( "$deletedCount old score files deleted.\n" );
94    }
95}
96
97$maintClass = DeleteOldScoreFiles::class;
98require_once RUN_MAINTENANCE_IF_MAIN;