Avoid creating lots and lots of cat_id gaps
[lhc/web/wiklou.git] / includes / page / WikiPage.php
index 3f6a4db..3ec3e89 100644 (file)
@@ -3395,22 +3395,44 @@ class WikiPage implements Page, IDBAccessObject {
                                }
 
                                if ( count( $added ) ) {
-                                       $insertRows = array();
-                                       foreach ( $added as $cat ) {
-                                               $insertRows[] = array(
-                                                       'cat_title'   => $cat,
-                                                       'cat_pages'   => 1,
-                                                       'cat_subcats' => ( $ns == NS_CATEGORY ) ? 1 : 0,
-                                                       'cat_files'   => ( $ns == NS_FILE ) ? 1 : 0,
-                                               );
-                                       }
-                                       $dbw->upsert(
+                                       $existingAdded = $dbw->selectFieldValues(
                                                'category',
-                                               $insertRows,
-                                               array( 'cat_title' ),
-                                               $addFields,
-                                               $method
+                                               'cat_title',
+                                               array( 'cat_title' => $added ),
+                                               __METHOD__
                                        );
+
+                                       // For category rows that already exist, do a plain
+                                       // UPDATE instead of INSERT...ON DUPLICATE KEY UPDATE
+                                       // to avoid creating gaps in the cat_id sequence.
+                                       if ( count( $existingAdded ) ) {
+                                               $dbw->update(
+                                                       'category',
+                                                       $addFields,
+                                                       array( 'cat_title' => $existingAdded ),
+                                                       __METHOD__
+                                               );
+                                       }
+
+                                       $missingAdded = array_diff( $added, $existingAdded );
+                                       if ( count( $missingAdded ) ) {
+                                               $insertRows = array();
+                                               foreach ( $missingAdded as $cat ) {
+                                                       $insertRows[] = array(
+                                                               'cat_title'   => $cat,
+                                                               'cat_pages'   => 1,
+                                                               'cat_subcats' => ( $ns == NS_CATEGORY ) ? 1 : 0,
+                                                               'cat_files'   => ( $ns == NS_FILE ) ? 1 : 0,
+                                                       );
+                                               }
+                                               $dbw->upsert(
+                                                       'category',
+                                                       $insertRows,
+                                                       array( 'cat_title' ),
+                                                       $addFields,
+                                                       $method
+                                               );
+                                       }
                                }
 
                                if ( count( $deleted ) ) {