From: Ilmari Karonen Date: Mon, 31 Aug 2009 13:58:28 +0000 (+0000) Subject: redo r45388 (hopefully) more cleanly (see also bug 16916: Unnecessary title disambigu... X-Git-Tag: 1.31.0-rc.0~39991 X-Git-Url: http://git.cyclocoop.org/url?a=commitdiff_plain;h=03a180747da6f3a6f53cd0866c8366b5b23a4959;p=lhc%2Fweb%2Fwiklou.git redo r45388 (hopefully) more cleanly (see also bug 16916: Unnecessary title disambiguation by cleanupImages.php) --- diff --git a/maintenance/cleanupImages.php b/maintenance/cleanupImages.php index fceafea84a..f303e6814b 100644 --- a/maintenance/cleanupImages.php +++ b/maintenance/cleanupImages.php @@ -101,6 +101,14 @@ class ImageCleanup extends TableCleanup { return $this->repo->getRootDirectory() . '/' . $this->repo->getHashPath( $name ) . $name; } + function imageExists( $name, $db ) { + return $db->selectField( 'image', '1', array( 'img_name' => $name ), __METHOD__ ); + } + + function pageExists( $name, $db ) { + return $db->selectField( 'page', '1', array( 'page_namespace' => NS_FILE, 'page_title' => $name ), __METHOD__ ); + } + private function pokeFile( $orig, $new ) { $path = $this->filePath( $orig ); if( !file_exists( $path ) ) { @@ -109,14 +117,24 @@ class ImageCleanup extends TableCleanup { } $db = wfGetDB( DB_MASTER ); + + /* + * To prevent key collisions in the update() statements below, + * if the target title exists in the image table, or if both the + * original and target titles exist in the page table, append + * increasing version numbers until the target title exists in + * neither. (See also bug 16916.) + */ $version = 0; $final = $new; + $conflict = ( $this->imageExists( $final, $db ) || + ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) ); - while( $db->selectField( 'image', 'img_name', array( 'img_name' => $final ), __METHOD__ ) || - Title::makeTitle( NS_FILE, $final )->exists() ) { + while( $conflict ) { $this->output( "Rename conflicts with '$final'...\n" ); $version++; $final = $this->appendTitle( $new, "_$version" ); + $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) ); } $finalPath = $this->filePath( $final );