From e56a2e2a301f0a1b638e6631b279350bcc3c0722 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Sun, 10 Oct 2004 22:56:23 +0000 Subject: [PATCH] When deleting images / old image revisions, don't fail out if the file doesn't exist or the oi_archive_name field is empty. Treat it gracefully and continue deleting the database records. Bug 484: image deletion causes an internal error --- includes/ImagePage.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/includes/ImagePage.php b/includes/ImagePage.php index f8b4c51d6e..3c558b6436 100644 --- a/includes/ImagePage.php +++ b/includes/ImagePage.php @@ -219,8 +219,13 @@ class ImagePage extends Article { } $dest = wfImageDir( $image ); $archive = wfImageDir( $image ); - if ( ! @unlink( "{$dest}/{$image}" ) ) { - $wgOut->fileDeleteError( "{$dest}/{$image}" ); + + # Delete the image file if it exists; due to sync problems + # or manual trimming sometimes the file will be missing. + $targetFile = "{$dest}/{$image}"; + if( file_exists( $targetFile ) && ! @unlink( $targetFile ) ) { + # If the deletion operation actually failed, bug out: + $wgOut->fileDeleteError( $targetFile ); return; } $dbw->delete( 'image', array( 'img_name' => $image ) ); @@ -280,7 +285,17 @@ class ImagePage extends Article { $name = substr( $oldimage, 15 ); $archive = wfImageArchiveDir( $name ); - if ( ! @unlink( "{$archive}/{$oldimage}" ) ) { + + # Delete the image if it exists. Sometimes the file will be missing + # due to manual intervention or weird sync problems; treat that + # condition gracefully and continue to delete the database entry. + # Also some records may end up with an empty oi_archive_name field + # if the original file was missing when a new upload was made; + # don't try to delete the directory then! + # + $targetFile = "{$archive}/{$oldimage}"; + if( $oldimage != '' && file_exists( $targetFile ) && !@unlink( $targetFile ) ) { + # If we actually have a file and can't delete it, throw an error. $wgOut->fileDeleteError( "{$archive}/{$oldimage}" ); } else { # Log the deletion -- 2.20.1