Index: trunk/phase3/maintenance/cleanupImages.php |
— | — | @@ -101,6 +101,14 @@ |
102 | 102 | return $this->repo->getRootDirectory() . '/' . $this->repo->getHashPath( $name ) . $name; |
103 | 103 | } |
104 | 104 | |
| 105 | + function imageExists( $name, $db ) { |
| 106 | + return $db->selectField( 'image', '1', array( 'img_name' => $name ), __METHOD__ ); |
| 107 | + } |
| 108 | + |
| 109 | + function pageExists( $name, $db ) { |
| 110 | + return $db->selectField( 'page', '1', array( 'page_namespace' => NS_FILE, 'page_title' => $name ), __METHOD__ ); |
| 111 | + } |
| 112 | + |
105 | 113 | private function pokeFile( $orig, $new ) { |
106 | 114 | $path = $this->filePath( $orig ); |
107 | 115 | if( !file_exists( $path ) ) { |
— | — | @@ -109,14 +117,24 @@ |
110 | 118 | } |
111 | 119 | |
112 | 120 | $db = wfGetDB( DB_MASTER ); |
| 121 | + |
| 122 | + /* |
| 123 | + * To prevent key collisions in the update() statements below, |
| 124 | + * if the target title exists in the image table, or if both the |
| 125 | + * original and target titles exist in the page table, append |
| 126 | + * increasing version numbers until the target title exists in |
| 127 | + * neither. (See also bug 16916.) |
| 128 | + */ |
113 | 129 | $version = 0; |
114 | 130 | $final = $new; |
| 131 | + $conflict = ( $this->imageExists( $final, $db ) || |
| 132 | + ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) ); |
115 | 133 | |
116 | | - while( $db->selectField( 'image', 'img_name', array( 'img_name' => $final ), __METHOD__ ) || |
117 | | - Title::makeTitle( NS_FILE, $final )->exists() ) { |
| 134 | + while( $conflict ) { |
118 | 135 | $this->output( "Rename conflicts with '$final'...\n" ); |
119 | 136 | $version++; |
120 | 137 | $final = $this->appendTitle( $new, "_$version" ); |
| 138 | + $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) ); |
121 | 139 | } |
122 | 140 | |
123 | 141 | $finalPath = $this->filePath( $final ); |