jenkins-bot has submitted this change and it was merged.

Change subject: Avoid exceptions on encodeTitle() INSERT races
......................................................................


Avoid exceptions on encodeTitle() INSERT races

Bug: T106849
Change-Id: I2a4adb3c0b1a77e0de39c731110289815e87c98a
---
M ShortUrl.utils.php
1 file changed, 36 insertions(+), 14 deletions(-)

Approvals:
  Gilles: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/ShortUrl.utils.php b/ShortUrl.utils.php
old mode 100644
new mode 100755
index 5045b77..19db0b9
--- a/ShortUrl.utils.php
+++ b/ShortUrl.utils.php
@@ -22,36 +22,57 @@
         * @param $title Title
         * @return mixed|string
         */
-       public static function encodeTitle( $title ) {
+       public static function encodeTitle( Title $title ) {
                global $wgMemc;
 
                $memcKey = wfMemcKey( 'shorturls', 'title', md5( 
$title->getPrefixedText() ) );
+
                $id = $wgMemc->get( $memcKey );
                if ( !$id ) {
-                       $dbr = wfGetDB( DB_SLAVE );
-                       $entry = $dbr->selectRow(
+                       $id = wfGetDB( DB_SLAVE )->selectField(
                                'shorturls',
-                               array( 'su_id' ),
+                               'su_id',
                                array(
                                        'su_namespace' => 
$title->getNamespace(),
                                        'su_title' => $title->getDBkey()
                                ),
                                __METHOD__
                        );
-                       if ( $entry !== false ) {
-                               $id = $entry->su_id;
-                       } else {
+
+                       // Automatically create an ID for this title if 
missing...
+                       if ( !$id ) {
                                $dbw = wfGetDB( DB_MASTER );
-                               $rowData = array(
-                                       'su_id' => $dbw->nextSequenceValue( 
'shorturls_id_seq' ),
-                                       'su_namespace' => 
$title->getNamespace(),
-                                       'su_title' => $title->getDBkey()
+                               $dbw->insert(
+                                       'shorturls',
+                                       array(
+                                               'su_id' => 
$dbw->nextSequenceValue( 'shorturls_id_seq' ),
+                                               'su_namespace' => 
$title->getNamespace(),
+                                               'su_title' => $title->getDBkey()
+                                       ),
+                                       __METHOD__,
+                                       array( 'IGNORE' )
                                );
-                               $dbw->insert( 'shorturls', $rowData, __METHOD__ 
);
-                               $id = $dbw->insertId();
+
+                               if ( $dbw->affectedRows() ) {
+                                       $id = $dbw->insertId();
+                               } else {
+                                       // Raced out; get the winning ID
+                                       $id = $dbw->selectField(
+                                               'shorturls',
+                                               'su_id',
+                                               array(
+                                                       'su_namespace' => 
$title->getNamespace(),
+                                                       'su_title' => 
$title->getDBkey()
+                                               ),
+                                               __METHOD__,
+                                               array( 'LOCK IN SHARE MODE' ) 
// ignore snapshot
+                                       );
+                               }
                        }
-                       $wgMemc->set( $memcKey, $id, 0 );
+
+                       $wgMemc->set( $memcKey, $id );
                }
+
                return base_convert( $id, 10, 36 );
        }
 
@@ -79,6 +100,7 @@
                        }
                        $wgMemc->set( $memcKey, $entry, 0 );
                }
+
                return Title::makeTitle( $entry->su_namespace, $entry->su_title 
);
        }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/226918
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I2a4adb3c0b1a77e0de39c731110289815e87c98a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ShortUrl
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <asch...@wikimedia.org>
Gerrit-Reviewer: Gilles <gdu...@wikimedia.org>
Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to