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\Status\Status;
26
27if ( getenv( 'MW_INSTALL_PATH' ) ) {
28    $IP = getenv( 'MW_INSTALL_PATH' );
29} else {
30    $IP = __DIR__ . '/../../..';
31}
32
33require_once "$IP/maintenance/Maintenance.php";
34
35/**
36 * Maintenance script that deletes old score files from storage
37 *
38 * @ingroup Maintenance
39 */
40class DeleteOldScoreFiles extends Maintenance {
41    public function __construct() {
42        parent::__construct();
43        $this->addDescription( "Deletes old score files from storage" );
44        $this->addOption(
45            "date",
46            'Delete score files that were created before this date (e.g. 20170101000000)',
47            true,
48            true
49        );
50        $this->requireExtension( "Score" );
51    }
52
53    public function execute() {
54        $backend = Score::getBackend();
55        $dir = $backend->getRootStoragePath() . '/score-render';
56
57        $filesToDelete = [];
58        $deleteDate = $this->getOption( 'date' );
59        foreach (
60            $backend->getFileList( [ 'dir' => $dir, 'adviseStat' => true ] ) as $file
61        ) {
62            $fullPath = $dir . '/' . $file;
63            $timestamp = $backend->getFileTimestamp( [ 'src' => $fullPath ] );
64            if ( $timestamp < $deleteDate ) {
65                $filesToDelete[] = [ 'op' => 'delete', 'src' => $fullPath, ];
66            }
67        }
68
69        $count = count( $filesToDelete );
70
71        if ( !$count ) {
72            $this->output( "No old score files to delete!\n" );
73            return;
74        }
75
76        $this->output( "$count old score files to be deleted.\n" );
77
78        $deletedCount = 0;
79        foreach ( array_chunk( $filesToDelete, 1000 ) as $chunk ) {
80            $ret = $backend->doQuickOperations( $chunk );
81
82            if ( $ret->isOK() ) {
83                $deletedCount += count( $chunk );
84                $this->output( "$deletedCount...\n" );
85            } else {
86                $status = Status::wrap( $ret );
87                $this->output( "Deleting old score files errored.\n" );
88                $this->error( $status->getWikiText( false, false, 'en' ) );
89            }
90        }
91
92        $this->output( "$deletedCount old score files deleted.\n" );
93    }
94}
95
96$maintClass = DeleteOldScoreFiles::class;
97require_once RUN_MAINTENANCE_IF_MAIN;