Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
GetLYFiles
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 2
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 * @ingroup Maintenance
21 */
22
23use MediaWiki\Extension\Score\Score;
24
25if ( getenv( 'MW_INSTALL_PATH' ) ) {
26    $IP = getenv( 'MW_INSTALL_PATH' );
27} else {
28    $IP = __DIR__ . '/../../..';
29}
30
31require_once "$IP/maintenance/Maintenance.php";
32
33/**
34 * @ingroup Maintenance
35 */
36class GetLYFiles extends Maintenance {
37
38    public function __construct() {
39        parent::__construct();
40        $this->addDescription(
41            "Gets a count of the number of ly files and optionally writes them to disk"
42        );
43        $this->addOption(
44            'date',
45            'Get ly files that were created after this date (e.g. 20170101000000)',
46            true,
47            true
48        );
49
50        $this->addOption(
51            'outputdir',
52            'Saves ly files matching date to this directory',
53            false,
54            true
55        );
56        $this->requireExtension( "Score" );
57    }
58
59    public function execute() {
60        $backend = Score::getBackend();
61        $baseStoragePath = $backend->getRootStoragePath() . '/score-render';
62
63        $files = $backend->getFileList( [ 'dir' => $baseStoragePath, 'adviseStat' => true ] );
64        $count = iterator_count( $files );
65        $this->output( "Total files (all extensions): $count\n" );
66
67        $date = $this->getOption( 'date' );
68
69        $targetFiles = [];
70
71        foreach ( $files as $file ) {
72            $fullPath = $baseStoragePath . '/' . $file;
73
74            if (
75                pathinfo( $file, PATHINFO_EXTENSION ) === 'ly' &&
76                $backend->getFileTimestamp( [ 'src' => $fullPath ] ) >= $date
77            ) {
78                $targetFiles[] = $fullPath;
79            }
80        }
81
82        $targetFileCount = count( $targetFiles );
83
84        $this->output( "{$targetFileCount} ly files created on or after {$date}\n" );
85
86        if ( $this->hasOption( 'outputdir' ) ) {
87            $outputDir = $this->getOption( 'outputdir' );
88            $this->output( "Outputting ly files to {$outputDir}:\n" );
89
90            $count = 0;
91            foreach ( array_chunk( $targetFiles, 1000 ) as $chunk ) {
92                $fileContents = $backend->getFileContentsMulti(
93                    [
94                        'srcs' => $chunk,
95                        'parallelize' => true,
96                    ]
97                );
98
99                foreach ( $fileContents as $path => $contents ) {
100                    $pathNoPrefix = str_replace( $baseStoragePath . '/', '', $path );
101                    wfMkdirParents( $outputDir . '/' . dirname( $pathNoPrefix ) );
102                    file_put_contents( $outputDir . '/' . $pathNoPrefix, $contents );
103                }
104                $count += count( $chunk );
105                $this->output( "$count...\n" );
106            }
107
108            $this->output( "Done!\n" );
109        }
110    }
111
112}
113
114$maintClass = GetLYFiles::class;
115require_once RUN_MAINTENANCE_IF_MAIN;