* Allow for things that call addEntry() to pass in a DB object to make the action...
authorAaron Schulz <aaron@users.mediawiki.org>
Tue, 15 Apr 2008 23:34:45 +0000 (23:34 +0000)
committerAaron Schulz <aaron@users.mediawiki.org>
Tue, 15 Apr 2008 23:34:45 +0000 (23:34 +0000)
* Improve logging transactions for block/delete

includes/Article.php
includes/Block.php
includes/LogPage.php
includes/SpecialBlockip.php

index 473d19a..95979f1 100644 (file)
@@ -1862,9 +1862,6 @@ class Article {
 
                                # Update the protection log
                                $log = new LogPage( 'protect' );
-
-
-
                                if( $protect ) {
                                        $log->addEntry( $modified ? 'modify' : 'protect', $this->mTitle, trim( $reason . " [$updated]$cascade_description$expiry_description" ) );
                                } else {
@@ -2274,6 +2271,7 @@ class Article {
                        $bitfield = 'rev_deleted';
                }
 
+               $dbw->begin();
                // For now, shunt the revision data into the archive table.
                // Text is *not* removed from the text table; bulk storage
                // is left intact to avoid breaking block-compression or
@@ -2320,6 +2318,11 @@ class Article {
 
                # Now that it's safely backed up, delete it
                $dbw->delete( 'page', array( 'page_id' => $id ), __METHOD__);
+               $ok = ( $dbw->affectedRows() > 0 ); // getArticleId() uses slave, could be laggy
+               if( !$ok ) {
+                       $dbw->rollback();
+                       return false;
+               }
 
                # If using cascading deletes, we can skip some explicit deletes
                if ( !$dbw->cascadingDeletes() ) {
@@ -2357,7 +2360,13 @@ class Article {
                # Log the deletion, if the page was suppressed, log it at Oversight instead
                $logtype = $suppress ? 'suppress' : 'delete';
                $log = new LogPage( $logtype );
-               $log->addEntry( 'delete', $this->mTitle, $reason );
+               # Make sure logging got through
+               $ok = $log->addEntry( 'delete', $this->mTitle, $reason, array(), $dbw );
+               if( !$ok ) {
+                       $dbw->rollback();
+                       return false;
+               }
+               $dbw->commit();
 
                return true;
        }
index f740d35..9a79389 100644 (file)
@@ -358,12 +358,13 @@ class Block
 
        /**
        * Insert a block into the block table.
-       *@return Whether or not the insertion was successful.
+       * @param Database $dbw, optional
+       * @return Whether or not the insertion was successful.
        */
-       function insert()
+       function insert( $dbw = NULL)
        {
                wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
-               $dbw = wfGetDB( DB_MASTER );
+               $dbw = $dbw ? $dbw : wfGetDB( DB_MASTER );
 
                # Unset ipb_anon_only for user blocks, makes no sense
                if ( $this->mUser ) {
@@ -410,7 +411,6 @@ class Block
                        ), 'Block::insert', array( 'IGNORE' )
                );
                $affected = $dbw->affectedRows();
-               $dbw->commit();
 
                if ($affected)
                        $this->doRetroactiveAutoblock();
index 041d05c..e86aadf 100644 (file)
@@ -51,13 +51,13 @@ class LogPage {
                $this->updateRecentChanges = $rc;
        }
 
-       function saveContent() {
+       function saveContent( $dbw ) {
                if( wfReadOnly() ) return false;
 
                global $wgUser, $wgLogRestrictions;
                $fname = 'LogPage::saveContent';
 
-               $dbw = wfGetDB( DB_MASTER );
+               $dbw = $dbw ? $dbw : wfGetDB( DB_MASTER );
                $uid = $wgUser->getID();
                $log_id = $dbw->nextSequenceValue( 'log_log_id_seq' );
 
@@ -237,8 +237,9 @@ class LogPage {
         * @param object &$target A title object.
         * @param string $comment Description associated
         * @param array $params Parameters passed later to wfMsg.* functions
+        * @param Database $dbw, optional
         */
-       function addEntry( $action, $target, $comment, $params = array() ) {
+       function addEntry( $action, $target, $comment, $params = array(), $dbw = NULL ) {
                if ( !is_array( $params ) ) {
                        $params = array( $params );
                }
@@ -250,7 +251,7 @@ class LogPage {
 
                $this->actionText = LogPage::actionText( $this->type, $action, $target, NULL, $params );
 
-               return $this->saveContent();
+               return $this->saveContent( $dbw );
        }
 
        /**
index 61a2910..048a9a4 100644 (file)
@@ -349,14 +349,14 @@ class IPBlockForm {
                        $this->BlockCreateAccount, $this->BlockEnableAutoblock, $this->BlockHideName,
                        $this->BlockEmail);
 
-               if (wfRunHooks('BlockIp', array(&$block, &$wgUser))) {
-
-                       if ( !$block->insert() ) {
+               if ( wfRunHooks('BlockIp', array(&$block, &$wgUser)) ) {
+                       $dbw = wfGetDB( DB_MASTER );
+                       $dbw->begin();
+                       if ( !$block->insert( $dbw ) ) {
+                               $dbw->rollback(); // this could be commit as well; zero rows either way
                                return array('ipb_already_blocked', htmlspecialchars($this->BlockAddress));
                        }
 
-                       wfRunHooks('BlockIpComplete', array($block, $wgUser));
-
                        # Prepare log parameters
                        $logParams = array();
                        $logParams[] = $expirestr;
@@ -365,14 +365,20 @@ class IPBlockForm {
                        # Make log entry, if the name is hidden, put it in the oversight log
                        $log_type = ($this->BlockHideName) ? 'suppress' : 'block';
                        $log = new LogPage( $log_type );
-                       $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
+                       $ok = $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ),
                          $reasonstr, $logParams );
-
+                       # Make sure logging got through
+                       if( !$ok ) {
+                               $dbw->rollback();
+                               return array('databaseerror');
+                       }
+                       $dbw->commit();
+                       wfRunHooks('BlockIpComplete', array($block, $wgUser));
                        # Report to the user
                        return array();
-               }
-               else
+               } else {
                        return array('hookaborted');
+               }
        }
 
        /**