Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CheckFiles
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 2
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
182
1<?php
2
3require_once( dirname(dirname(dirname(dirname(__DIR__)))) . '/maintenance/Maintenance.php' );
4
5use MediaWiki\MediaWikiServices;
6
7/**
8 * This script checks if all files in DB have their
9 * counterpart on FileSystem
10 */
11class CheckFiles extends Maintenance {
12
13    function __construct() {
14        parent::__construct();
15
16        $this->requireExtension( 'NSFileRepo' );
17    }
18
19    function execute() {
20        $dbr = wfGetDB( DB_REPLICA );
21        print( "Using DB: " . $dbr->getDBName() ) . PHP_EOL;
22
23        $aImgNames = array();
24        $res = $dbr->select('image',
25            array( 'img_name' ),
26            array(),
27            __METHOD__
28        );
29
30        $repoGroup = MediaWikiServices::getInstance()->getRepoGroup();
31
32        foreach( $res as $row ) {
33            $sName = preg_replace( '/_/', ' ', $row->img_name );
34            $oTitle = Title::makeTitle( NS_FILE, $sName );
35
36            if( !$oTitle->exists() ) {
37                    print ( "Title for " . $sName . " does not exist" . PHP_EOL );
38                    continue;
39            }
40
41            if( $oTitle->getNamespace() !== NS_FILE ) {
42                    print ( "Title for " . $sName . " is not in NS_FILE" . PHP_EOL );
43                    continue;
44            }
45
46            $oFile = $repoGroup->findFile( $sName );
47            if( !$oFile || !$oFile->exists() ) {
48                    print( "File " . $sName . " does not exist!" . PHP_EOL );
49            }
50            $sFileLocalPath = $oFile->getLocalRefPath();
51            if( !$sFileLocalPath || !file_exists( $sFileLocalPath ) ) {
52                print( "Image " . $sName . " not found!" . PHP_EOL );
53            }
54        }
55
56        $res = $dbr->select('oldimage',
57            array( 'oi_name', 'oi_archive_name' ),
58            array(),
59            __METHOD__
60        );
61
62            foreach( $res as $row ) {
63            $oTitle = Title::makeTitle( NS_FILE, $row->oi_name );
64            $repo = $repoGroup->getRepo( 'local' );
65            $strippedName = NSLocalFile::getFilenameStripped( $row->oi_archive_name );
66            $file = OldLocalFile::newFromArchiveName( $oTitle, $repo, $strippedName );
67            if( !$file->getLocalRefPath() || !file_exists( $file->getLocalRefPath() ) ) {
68                $file = OldLocalFile::newFromArchiveName( $oTitle, $repo, $row->oi_archive_name );
69                print( "Archive file: " . $row->oi_archive_name . " not found" . PHP_EOL );
70                if( $file->getLocalRefPath() && file_exists( $file->getLocalRefPath() ) ) {
71                    print( "\t...but wrong version of this file exists: " . $file->getLocalRefPath() . PHP_EOL );
72                }
73            }
74        }
75    }
76}
77
78$maintClass = CheckFiles::class;
79require_once( RUN_MAINTENANCE_IF_MAIN );