+ var $fileFactoryKey = array( 'LocalFile', 'newFromKey' );
+ var $oldFileFactory = array( 'OldLocalFile', 'newFromTitle' );
+ var $oldFileFactoryKey = array( 'OldLocalFile', 'newFromKey' );
+ var $fileFromRowFactory = array( 'LocalFile', 'newFromRow' );
+ var $oldFileFromRowFactory = array( 'OldLocalFile', 'newFromRow' );
+
+ /**
+ * @throws MWException
+ * @param $row
+ * @return File
+ */
+ function newFileFromRow( $row ) {
+ if ( isset( $row->img_name ) ) {
+ return call_user_func( $this->fileFromRowFactory, $row, $this );
+ } elseif ( isset( $row->oi_name ) ) {
+ return call_user_func( $this->oldFileFromRowFactory, $row, $this );
+ } else {
+ throw new MWException( __METHOD__.': invalid row' );
+ }
+ }
+
+ /**
+ * @param $title
+ * @param $archiveName
+ * @return OldLocalFile
+ */
+ function newFromArchiveName( $title, $archiveName ) {
+ return OldLocalFile::newFromArchiveName( $title, $this, $archiveName );
+ }
+
+ /**
+ * Delete files in the deleted directory if they are not referenced in the
+ * filearchive table. This needs to be done in the repo because it needs to
+ * interleave database locks with file operations, which is potentially a
+ * remote operation.
+ *
+ * @param $storageKeys array
+ *
+ * @return FileRepoStatus
+ */
+ function cleanupDeletedBatch( $storageKeys ) {
+ $root = $this->getZonePath( 'deleted' );
+ $dbw = $this->getMasterDB();
+ $status = $this->newGood();
+ $storageKeys = array_unique( $storageKeys );
+ foreach ( $storageKeys as $key ) {
+ $hashPath = $this->getDeletedHashPath( $key );
+ $path = "$root/$hashPath$key";
+ $dbw->begin();
+ // Check for usage in deleted/hidden files and pre-emptively
+ // lock the key to avoid any future use until we are finished.
+ $deleted = $this->deletedFileHasKey( $key, 'lock' );
+ $hidden = $this->hiddenFileHasKey( $key, 'lock' );
+ if ( !$deleted && !$hidden ) { // not in use now
+ wfDebug( __METHOD__ . ": deleting $key\n" );
+ wfSuppressWarnings();
+ $unlink = unlink( $path );
+ wfRestoreWarnings();
+ if ( !$unlink ) {
+ $status->error( 'undelete-cleanup-error', $path );
+ $status->failCount++;
+ }
+ } else {
+ wfDebug( __METHOD__ . ": $key still in use\n" );
+ $status->successCount++;
+ }
+ $dbw->commit();
+ }
+ return $status;
+ }
+
+ /**
+ * Check if a deleted (filearchive) file has this sha1 key
+ * @param $key String File storage key (base-36 sha1 key with file extension)
+ * @param $lock String|null Use "lock" to lock the row via FOR UPDATE
+ * @return bool File with this key is in use
+ */
+ protected function deletedFileHasKey( $key, $lock = null ) {
+ $options = ( $lock === 'lock' ) ? array( 'FOR UPDATE' ) : array();
+
+ $dbw = $this->getMasterDB();
+ return (bool)$dbw->selectField( 'filearchive', '1',
+ array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
+ __METHOD__, $options
+ );
+ }
+
+ /**
+ * Check if a hidden (revision delete) file has this sha1 key
+ * @param $key String File storage key (base-36 sha1 key with file extension)
+ * @param $lock String|null Use "lock" to lock the row via FOR UPDATE
+ * @return bool File with this key is in use
+ */
+ protected function hiddenFileHasKey( $key, $lock = null ) {
+ $options = ( $lock === 'lock' ) ? array( 'FOR UPDATE' ) : array();
+
+ $sha1 = self::getHashFromKey( $key );
+ $ext = File::normalizeExtension( substr( $key, strcspn( $key, '.' ) + 1 ) );
+
+ $dbw = $this->getMasterDB();
+ return (bool)$dbw->selectField( 'oldimage', '1',
+ array( 'oi_sha1' => $sha1,
+ 'oi_archive_name ' . $dbw->buildLike( $dbw->anyString(), ".$ext" ),
+ $dbw->bitAnd( 'oi_deleted', File::DELETED_FILE ) => File::DELETED_FILE ),
+ __METHOD__, array( 'FOR UPDATE' )
+ );
+ }
+
+ /**
+ * Gets the SHA1 hash from a storage key
+ *
+ * @param string $key
+ * @return string
+ */
+ public static function getHashFromKey( $key ) {
+ return strtok( $key, '.' );
+ }
+
+ /**
+ * Checks if there is a redirect named as $title
+ *
+ * @param $title Title of file
+ * @return bool
+ */
+ function checkRedirect( Title $title ) {
+ global $wgMemc;
+
+ $title = File::normalizeTitle( $title, 'exception' );