Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 64 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
FixOldImage | |
0.00% |
0 / 61 |
|
0.00% |
0 / 2 |
306 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 59 |
|
0.00% |
0 / 1 |
272 |
1 | <?php |
2 | |
3 | require_once( dirname(dirname(dirname(dirname(__DIR__)))) . '/maintenance/Maintenance.php' ); |
4 | |
5 | use MediaWiki\MediaWikiServices; |
6 | |
7 | class FixOldImage extends Maintenance { |
8 | protected $mDBName; |
9 | |
10 | function __construct() { |
11 | parent::__construct(); |
12 | |
13 | $this->requireExtension( 'NSFileRepo' ); |
14 | } |
15 | |
16 | function execute() { |
17 | $dbw = wfGetDB( DB_PRIMARY ); |
18 | print( "USING DB: " . $dbw->getDBName() . "\n" ); |
19 | $images = $dbw->select( 'oldimage', |
20 | array( 'oi_name', 'oi_archive_name' ), |
21 | array(), |
22 | __METHOD__ |
23 | ); |
24 | |
25 | $count = 0; |
26 | $log = ''; |
27 | $repo = MediaWikiServices::getInstance()->getRepoGroup()->getRepo( 'local' ); |
28 | |
29 | foreach( $images as $image ) { |
30 | $nameBits = explode( ':', $image->oi_name ); |
31 | $nameNS = ''; |
32 | $nameName = $image->oi_name; |
33 | if( count( $nameBits ) == 2 ) { |
34 | $nameNS = $nameBits[0]; |
35 | $nameName = $nameBits[1]; |
36 | } |
37 | |
38 | $archiveName = explode( '!', $image->oi_archive_name )[1]; |
39 | $archiveBits = explode( ':', $archiveName ); |
40 | $archiveNS = ''; |
41 | if( count( $archiveBits ) == 2 ) { |
42 | $archiveNS = $archiveBits[0]; |
43 | $archiveName = $archiveBits[1]; |
44 | } |
45 | |
46 | $title = Title::makeTitle( NS_FILE, $image->oi_name ); |
47 | if( !$title || !$title->exists() ) { |
48 | print( "Title wrong " . $image->oi_name . PHP_EOL ); |
49 | continue; |
50 | } |
51 | |
52 | $strippedName = NSLocalFile::getFilenameStripped( $image->oi_archive_name ); |
53 | |
54 | $file = OldLocalFile::newFromArchiveName( $title, $repo, $strippedName ); |
55 | if( !$file || !$file->exists() ) { |
56 | $file = OldLocalFile::newFromArchiveName( $title, $repo, $image->oi_archive_name ); |
57 | if( $file && $file->exists() && file_exists( $file->getLocalRefPath() ) ) { |
58 | $path = $file->getLocalRefPath(); |
59 | print( "Found wrong file on FS: " . $path . "..." ); |
60 | $baseDir = dirname( $path ); |
61 | $renamed = rename( $path, $baseDir . '/' . $strippedName ); |
62 | if( $renamed ) { |
63 | print("renamed" . PHP_EOL ); |
64 | $log .= "Renamed " . $path . " to " . $baseDir . '/' . $strippedName . "\n"; |
65 | } else { |
66 | print("failed to rename" . PHP_EOL ); |
67 | $log .= "Failed to rename " . $path . " to " . $baseDir . '/' . $strippedName . "\n"; |
68 | } |
69 | |
70 | } |
71 | } |
72 | |
73 | if( $nameNS === $archiveNS && $nameName == $archiveName ) { |
74 | continue; |
75 | } |
76 | |
77 | if( $nameNS != $archiveNS && $nameName != $archiveName ) { |
78 | continue; |
79 | } |
80 | |
81 | print( "NAME: ns-" . $nameNS . ' name-' . $nameName . ' ARCHIVE: ns-' . $archiveNS . ' name-' . $archiveName . "\n" ); |
82 | $log .= $nameNS . ";" . $nameName . ";" . $archiveNS . ";" . $archiveName . "\n"; |
83 | $dbw->update( |
84 | 'oldimage', |
85 | array( |
86 | 'oi_archive_name = ' . $dbw->strreplace( 'oi_archive_name', |
87 | $dbw->addQuotes( $archiveName ), $dbw->addQuotes( $image->oi_name ) ) |
88 | ), |
89 | array( 'oi_archive_name' => $image->oi_archive_name ), |
90 | __METHOD__ |
91 | ); |
92 | |
93 | $count++; |
94 | } |
95 | file_put_contents( '/tmp/fixOldImagesLog.log', $log ); |
96 | print( "Total " . $count . " images altered\n" ); |
97 | } |
98 | } |
99 | |
100 | $maintClass = FixOldImage::class; |
101 | require_once( RUN_MAINTENANCE_IF_MAIN ); |