[MediaWiki-commits] [Gerrit] Convert recordUpload2() to using startAtomic/endAtomic - change (mediawiki/core)
jenkins-bot has submitted this change and it was merged. Change subject: Convert recordUpload2() to using startAtomic/endAtomic .. Convert recordUpload2() to using startAtomic/endAtomic * This avoids breaking transactions due to nesting * Also improve readability a bit in this area Change-Id: I81c41e83d14aa59930bfb99522ebcc25d8aa14f9 --- M includes/filerepo/file/LocalFile.php 1 file changed, 74 insertions(+), 75 deletions(-) Approvals: Krinkle: Looks good to me, approved jenkins-bot: Verified diff --git a/includes/filerepo/file/LocalFile.php b/includes/filerepo/file/LocalFile.php index 390b7fe..cd64f0d 100644 --- a/includes/filerepo/file/LocalFile.php +++ b/includes/filerepo/file/LocalFile.php @@ -316,7 +316,7 @@ /** * Purge the file object/metadata cache */ - function invalidateCache() { + public function invalidateCache() { $key = $this->getCacheKey(); if ( !$key ) { return; @@ -1209,21 +1209,15 @@ * @param null|User $user * @return bool */ - function recordUpload2( $oldver, $comment, $pageText, $props = false, $timestamp = false, - $user = null + function recordUpload2( + $oldver, $comment, $pageText, $props = false, $timestamp = false, $user = null ) { - if ( is_null( $user ) ) { global $wgUser; $user = $wgUser; } $dbw = $this->repo->getMasterDB(); - $dbw->begin( __METHOD__ ); - - if ( !$props ) { - $props = $this->repo->getFileProps( $this->getVirtualUrl() ); - } # Imports or such might force a certain timestamp; otherwise we generate # it and can fudge it slightly to keep (name,timestamp) unique on re-upload. @@ -1234,6 +1228,7 @@ $allowTimeKludge = false; } + $props = $props ?: $this->repo->getFileProps( $this->getVirtualUrl() ); $props['description'] = $comment; $props['user'] = $user->getId(); $props['user_text'] = $user->getName(); @@ -1243,12 +1238,11 @@ # Fail now if the file isn't there if ( !$this->fileExists ) { wfDebug( __METHOD__ . ": File " . $this->getRel() . " went missing!\n" ); - $dbw->rollback( __METHOD__ ); return false; } - $reupload = false; + $dbw->startAtomic( __METHOD__ ); # Test to see if the row exists using INSERT IGNORE # This avoids race conditions by locking the row until the commit, and also @@ -1273,13 +1267,18 @@ __METHOD__, 'IGNORE' ); - if ( $dbw->affectedRows() == 0 ) { + + $reupload = ( $dbw->affectedRows() == 0 ); + if ( $reupload ) { if ( $allowTimeKludge ) { # Use LOCK IN SHARE MODE to ignore any transaction snapshotting - $ltimestamp = $dbw->selectField( 'image', 'img_timestamp', + $ltimestamp = $dbw->selectField( + 'image', + 'img_timestamp', array( 'img_name' => $this->getName() ), __METHOD__, - array( 'LOCK IN SHARE MODE' ) ); + array( 'LOCK IN SHARE MODE' ) + ); $lUnixtime = $ltimestamp ? wfTimestamp( TS_UNIX, $ltimestamp ) : false; # Avoid a timestamp that is not newer than the last version # TODO: the image/oldimage tables should be like page/revision with an ID field @@ -1294,8 +1293,6 @@ # version of the file was broken. Allow registration of the new # version to continue anyway, because that's better than having # an image that's not fixable by user operations. - - $reupload = true; # Collision, this is an update of a file # Insert previous contents into oldimage $dbw->insertSelect( 'oldimage', 'image', @@ -1322,7 +1319,7 @@ # Update the current image row $dbw->update( 'image', - array( /* SET */ + array( 'img_size' => $this->size,
[MediaWiki-commits] [Gerrit] Convert recordUpload2() to using startAtomic/endAtomic - change (mediawiki/core)
Aaron Schulz has uploaded a new change for review. https://gerrit.wikimedia.org/r/250363 Change subject: Convert recordUpload2() to using startAtomic/endAtomic .. Convert recordUpload2() to using startAtomic/endAtomic * This avoids breaking transactions due to nesting * Also improve readability a bit in this area Change-Id: I81c41e83d14aa59930bfb99522ebcc25d8aa14f9 --- M includes/filerepo/file/LocalFile.php 1 file changed, 74 insertions(+), 75 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/63/250363/1 diff --git a/includes/filerepo/file/LocalFile.php b/includes/filerepo/file/LocalFile.php index 390b7fe..cd64f0d 100644 --- a/includes/filerepo/file/LocalFile.php +++ b/includes/filerepo/file/LocalFile.php @@ -316,7 +316,7 @@ /** * Purge the file object/metadata cache */ - function invalidateCache() { + public function invalidateCache() { $key = $this->getCacheKey(); if ( !$key ) { return; @@ -1209,21 +1209,15 @@ * @param null|User $user * @return bool */ - function recordUpload2( $oldver, $comment, $pageText, $props = false, $timestamp = false, - $user = null + function recordUpload2( + $oldver, $comment, $pageText, $props = false, $timestamp = false, $user = null ) { - if ( is_null( $user ) ) { global $wgUser; $user = $wgUser; } $dbw = $this->repo->getMasterDB(); - $dbw->begin( __METHOD__ ); - - if ( !$props ) { - $props = $this->repo->getFileProps( $this->getVirtualUrl() ); - } # Imports or such might force a certain timestamp; otherwise we generate # it and can fudge it slightly to keep (name,timestamp) unique on re-upload. @@ -1234,6 +1228,7 @@ $allowTimeKludge = false; } + $props = $props ?: $this->repo->getFileProps( $this->getVirtualUrl() ); $props['description'] = $comment; $props['user'] = $user->getId(); $props['user_text'] = $user->getName(); @@ -1243,12 +1238,11 @@ # Fail now if the file isn't there if ( !$this->fileExists ) { wfDebug( __METHOD__ . ": File " . $this->getRel() . " went missing!\n" ); - $dbw->rollback( __METHOD__ ); return false; } - $reupload = false; + $dbw->startAtomic( __METHOD__ ); # Test to see if the row exists using INSERT IGNORE # This avoids race conditions by locking the row until the commit, and also @@ -1273,13 +1267,18 @@ __METHOD__, 'IGNORE' ); - if ( $dbw->affectedRows() == 0 ) { + + $reupload = ( $dbw->affectedRows() == 0 ); + if ( $reupload ) { if ( $allowTimeKludge ) { # Use LOCK IN SHARE MODE to ignore any transaction snapshotting - $ltimestamp = $dbw->selectField( 'image', 'img_timestamp', + $ltimestamp = $dbw->selectField( + 'image', + 'img_timestamp', array( 'img_name' => $this->getName() ), __METHOD__, - array( 'LOCK IN SHARE MODE' ) ); + array( 'LOCK IN SHARE MODE' ) + ); $lUnixtime = $ltimestamp ? wfTimestamp( TS_UNIX, $ltimestamp ) : false; # Avoid a timestamp that is not newer than the last version # TODO: the image/oldimage tables should be like page/revision with an ID field @@ -1294,8 +1293,6 @@ # version of the file was broken. Allow registration of the new # version to continue anyway, because that's better than having # an image that's not fixable by user operations. - - $reupload = true; # Collision, this is an update of a file # Insert previous contents into oldimage $dbw->insertSelect( 'oldimage', 'image', @@ -1322,7 +1319,7 @@ # Update the current image row $dbw->update( 'image', - array( /* SET */ + array(