ItSpiderman has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/350821 )

Change subject: Fix for archive images + maintenance scripts
......................................................................

Fix for archive images + maintenance scripts

When moving files withing one NS (renaming) archive images would get
broken, because name with NS prefix would be looked for on FS

Maintence scripts:
Database table oldimage might be corrupted, oi_archive_names might not
have NS prefix eventhough they should.
fixOldImages.php fixes this, and also fixes wrong files (with NS in name)
on the FS

checkFiles.php checks if every file in DB has its counterpart on FS

removeDuplicateNS.php fixes oi_archive_name if it contains NS prefix
multiple times

These bugs were identified on EWP, ERM: #4275

Change-Id: I10eb3234a46bf1bebc28f4d518f6f6fbe8dd977a
---
M includes/filerepo/file/NSLocalFile.php
A maintenance/checkFiles.php
M maintenance/fixOldImage.php
A maintenance/removeDuplicateNS.php
4 files changed, 161 insertions(+), 13 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/NSFileRepo 
refs/changes/21/350821/1

diff --git a/includes/filerepo/file/NSLocalFile.php 
b/includes/filerepo/file/NSLocalFile.php
index c5a9dcf..1fe0254 100644
--- a/includes/filerepo/file/NSLocalFile.php
+++ b/includes/filerepo/file/NSLocalFile.php
@@ -396,6 +396,11 @@
                                wfDebug( "Old file name doesn't match: 
'$oldName' \n" );
                                continue;
                        }
+/* This is the part that changed from LocalFileMoveBatch */
+                       #When file is moved within a namespace we do not want it
+                       #looking to NS:Name format in FS
+                       $strippedOldName = $this->file->getFileNameStripped( 
$oldName );
+/* End of changes */
 
                        $this->oldCount++;
 
@@ -405,7 +410,7 @@
                        }
 /* This is the part that changed from LocalFile */
                        $this->olds[] = array(
-                               "{$archiveBase}/{$this->oldHash}{$oldName}",
+                               
"{$archiveBase}/{$this->oldHash}{$strippedOldName}",
                                
"{$archiveBase}/{$this->newHash}{$timestamp}!{$newName}"
                        );
 /* End of changes */
diff --git a/maintenance/checkFiles.php b/maintenance/checkFiles.php
new file mode 100644
index 0000000..7d44947
--- /dev/null
+++ b/maintenance/checkFiles.php
@@ -0,0 +1,73 @@
+<?php
+
+require_once( dirname(dirname(dirname(dirname(__DIR__)))) . 
'/maintenance/Maintenance.php' );
+
+/**
+ * This script checks if all files in DB have their
+ * counterpart on FileSystem
+ */
+class CheckFiles extends Maintenance {
+
+       function __construct() {
+               parent::__construct();
+       }
+
+       function execute() {
+               $dbr = wfGetDB( DB_SLAVE );
+               print( "Using DB: " . $dbr->getDBName() ) . PHP_EOL;
+
+               $aImgNames = array();
+               $res = $dbr->select('image',
+                       array( 'img_name' ),
+                       array(),
+                       __METHOD__
+               );
+
+               foreach( $res as $row ) {
+                       $sName = preg_replace( '/_/', ' ', $row->img_name );
+                        $oTitle = Title::makeTitle( NS_FILE, $sName );
+
+                        if( !$oTitle->exists() ) {
+                                print ( "Title for " . $sName . " does not 
exist" . PHP_EOL );
+                                continue;
+                        }
+
+                        if( $oTitle->getNamespace() !== NS_FILE ) {
+                                print ( "Title for " . $sName . " is not in 
NS_FILE" . PHP_EOL );
+                                continue;
+                        }
+
+                        $oFile = wfFindFile( $sName );
+                        if( !$oFile || !$oFile->exists() ) {
+                                print( "File " . $sName . " does not exist!" . 
PHP_EOL );
+                        }
+                        $sFileLocalPath = $oFile->getLocalRefPath();
+                       if( !$sFileLocalPath || !file_exists( $sFileLocalPath ) 
) {
+                               print( "Image " . $sName . " not found!" . 
PHP_EOL );
+                       }
+               }
+
+               $res = $dbr->select('oldimage',
+                       array( 'oi_name', 'oi_archive_name' ),
+                       array(),
+                       __METHOD__
+               );
+
+               foreach( $res as $row ) {
+                       $oTitle = Title::makeTitle( NS_FILE, $row->oi_name );
+                       $repo = RepoGroup::singleton()->getRepo( 'local' );
+                       $strippedName = NSLocalFile::getFilenameStripped( 
$row->oi_archive_name );
+                       $file = OldLocalFile::newFromArchiveName( $oTitle, 
$repo, $strippedName );
+                       if( !$file->getLocalRefPath() || !file_exists( 
$file->getLocalRefPath() ) ) {
+                               $file = OldLocalFile::newFromArchiveName( 
$oTitle, $repo, $row->oi_archive_name );
+                               print( "Archive file: " . $row->oi_archive_name 
. " not found" . PHP_EOL );
+                               if( $file->getLocalRefPath() && file_exists( 
$file->getLocalRefPath() ) ) {
+                                       print( "\t...but wrong version of this 
file exists: " . $file->getLocalRefPath() . PHP_EOL );
+                               }
+                       }
+               }
+       }
+}
+
+$maintClass = 'CheckFiles';
+require_once( RUN_MAINTENANCE_IF_MAIN );
diff --git a/maintenance/fixOldImage.php b/maintenance/fixOldImage.php
index d8293ee..987ebb6 100644
--- a/maintenance/fixOldImage.php
+++ b/maintenance/fixOldImage.php
@@ -7,14 +7,10 @@
 
        function __construct() {
                parent::__construct();
-               $this->addOption( 'db', 'DB to run script on', true, true );
        }
 
        function execute() {
-               $this->mDBName = $this->getOption( 'db' );
-
                $dbw = wfGetDB( DB_MASTER );
-               $dbw->selectDB( $this->mDBName );
                print( "USING DB: " . $dbw->getDBName() . "\n" );
                $images = $dbw->select( 'oldimage',
                        array( 'oi_name', 'oi_archive_name' ),
@@ -41,17 +37,39 @@
                                $archiveName = $archiveBits[1];
                        }
 
+                       $title = Title::makeTitle( NS_FILE, $image->oi_name );
+                       if( !$title || !$title->exists() ) {
+                               print( "Title wrong " . $image->oi_name . 
PHP_EOL );
+                               continue;
+                       }
+
+                       $repo = RepoGroup::singleton()->getRepo( 'local' );
+                       $strippedName = NSLocalFile::getFilenameStripped( 
$image->oi_archive_name );
+
+                       $file = OldLocalFile::newFromArchiveName( $title, 
$repo, $strippedName );
+                       if( !$file || !$file->exists() ) {
+                               $file = OldLocalFile::newFromArchiveName( 
$title, $repo, $image->oi_archive_name );
+                               if( $file && $file->exists() && file_exists( 
$file->getLocalRefPath() ) ) {
+                                       $path = $file->getLocalRefPath();
+                                       print( "Found wrong file on FS: " . 
$path . "..." );
+                                       $baseDir = dirname( $path );
+                                       $renamed = rename( $path, $baseDir . 
'/' . $strippedName );
+                                       if( $renamed ) {
+                                               print("renamed" . PHP_EOL );
+                                               $log .= "Renamed " . $path . " 
to " . $baseDir . '/' . $strippedName . "\n";
+                                       } else {
+                                               print("failed to rename" . 
PHP_EOL );
+                                               $log .= "Failed to rename " . 
$path . " to " . $baseDir . '/' . $strippedName . "\n";
+                                       }
+
+                               }
+                       }
+
                        if( $nameNS === $archiveNS && $nameName == $archiveName 
) {
                                continue;
                        }
 
                        if( $nameNS != $archiveNS && $nameName != $archiveName 
) {
-                               continue;
-                       }
-                       $title = Title::newFromText( $image->oi_name, NS_FILE );
-                       $repo = RepoGroup::singleton()->getRepo( 'local' );
-                       $file = OldLocalFile::newFromArchiveName( $title, 
$repo, $image->oi_archive_name );
-                       if( !$file->exists() ) {
                                continue;
                        }
 
@@ -63,13 +81,13 @@
                                        'oi_archive_name = ' . 
$dbw->strreplace( 'oi_archive_name',
                                                $dbw->addQuotes( $archiveName 
), $dbw->addQuotes( $image->oi_name ) )
                                ),
-                               array( 'oi_name' => $image->oi_name ),
+                               array( 'oi_archive_name' => 
$image->oi_archive_name ),
                                __METHOD__
                        );
 
                        $count++;
                }
-               #file_put_contents( '/tmp/fixOldImagesLog.log', $log );
+               file_put_contents( '/tmp/fixOldImagesLog.log', $log );
                print( "Total " . $count . " images altered\n" );
        }
 }
diff --git a/maintenance/removeDuplicateNS.php 
b/maintenance/removeDuplicateNS.php
new file mode 100644
index 0000000..7a197ff
--- /dev/null
+++ b/maintenance/removeDuplicateNS.php
@@ -0,0 +1,52 @@
+<?php
+
+require_once( dirname(dirname(dirname(dirname(__DIR__)))) . 
'/maintenance/Maintenance.php' );
+
+/**
+ * If in table oldimage oi_archive_name contains
+ * multiple NS prefixes, this script fixes it
+ */
+class RemoveDuplicateNS extends Maintenance {
+
+       function __construct() {
+               parent::__construct();
+       }
+
+       function execute() {
+               $dbw = wfGetDB( DB_MASTER );
+               print( "USING DB: " . $dbw->getDBName() . "\n" );
+               $images = $dbw->select( 'oldimage',
+                       array( 'oi_name', 'oi_archive_name' ),
+                       array(),
+                       __METHOD__
+               );
+
+               foreach( $images as $image ) {
+                       $archiveTS = explode( '!', $image->oi_archive_name )[0];
+                       $archiveName = explode( '!', $image->oi_archive_name 
)[1];
+                       $archiveBits = explode( ':', $archiveName );
+                       if( count($archiveBits) > 2 ) {
+                               $archiveNS = $archiveBits[0];
+                               $archiveFName = 
$archiveBits[count($archiveBits)-1];
+                               unset($archiveBits[count($archiveBits)-1] );
+                               $archiveFill = implode( ':', $archiveBits );
+                               print( "Removing: " . $archiveFill . PHP_EOL );
+                               $dbw->update(
+                                       'oldimage',
+                                       array(
+                                               'oi_archive_name = ' . 
$dbw->strreplace( 'oi_archive_name',
+                                                       $dbw->addQuotes( 
$archiveFill ), $dbw->addQuotes( $archiveNS ) )
+                                       ),
+                                       array( 'oi_name' => $image->oi_name ),
+                                       __METHOD__
+                               );
+                       } else {
+                               continue;
+                       }
+               }
+
+       }
+}
+
+$maintClass = 'RemoveDuplicateNS';
+require_once( RUN_MAINTENANCE_IF_MAIN );

-- 
To view, visit https://gerrit.wikimedia.org/r/350821
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I10eb3234a46bf1bebc28f4d518f6f6fbe8dd977a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/NSFileRepo
Gerrit-Branch: master
Gerrit-Owner: ItSpiderman <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to