Unbreak maintenance/deleteDefaultMessages.php for PostgreSQL
authorMarcin Cieślak <saper@users.mediawiki.org>
Thu, 15 Mar 2012 01:52:38 +0000 (01:52 +0000)
committerMarcin Cieślak <saper@users.mediawiki.org>
Thu, 15 Mar 2012 01:52:38 +0000 (01:52 +0000)
deleteDefaultMessages.php was failing during upgrade
from MediaWiki 1.7.3 with a databaser error.

A stub user:

   $user = User::newFromName( 'MediaWiki default' );

has user ID 0, so that $user->isAnon() is true.

Unfortunately, ManualLogEntry::publish() from r96441
tries to insert $user->getName() ("MediaWiki default")
into rc_ip.

PostgreSQL won't allow this, because rc_ip is of
Postgres-specific CIDR type.

Traceback:

Checking existence of old default messages...
...deleting old default messages (this may take a long time!)...A database query syntax error has occurred.
The last attempted database query was:
"INSERT INTO "recentchanges" (rc_timestamp,rc_cur_time,rc_namespace,rc_title,rc_type,rc_minor,rc_user,rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid,rc_bot,rc_moved_to_ns,rc_moved_to_title,rc_ip,rc_patrolled,rc_new,rc_old_len,rc_new_len,rc_deleted,rc_logid,rc_log_type,rc_log_action,rc_params,rc_id) VALUES ('2012-03-14 21:51:05 GMT','2012-03-14 21:51:05 GMT','8','1movedto2','3','0','0','MediaWiki default','No longer required','0','0',1,'0','','MediaWiki default','1','0',NULL,NULL,'0','1','delete','delete','a:0:{}','1')"
from within function "RecentChange::save".
MySQL returned error "1: ERROR:  invalid input syntax for type cidr: "MediaWiki default"
LINE 1: ...ki default','No longer required','0','0',1,'0','','MediaWiki...
                                                             ^"
Backtrace:
#0 /usr/home/saper/public_html/pg/w/includes/db/DatabasePostgres.php(332): DatabaseBase->reportQueryError('ERROR:  invalid...', 1, 'INSERT INTO "re...', 'RecentChange::s...', '')
#1 /usr/home/saper/public_html/pg/w/includes/db/Database.php(904): DatabasePostgres->reportQueryError('ERROR:  invalid...', 1, 'INSERT INTO "re...', 'RecentChange::s...', '')
#2 /usr/home/saper/public_html/pg/w/includes/db/DatabasePostgres.php(604): DatabaseBase->query('INSERT INTO "re...', 'RecentChange::s...', '')
#3 /usr/home/saper/public_html/pg/w/includes/RecentChange.php(199): DatabasePostgres->insert('recentchanges', Array, 'RecentChange::s...')
#4 /usr/home/saper/public_html/pg/w/includes/logging/LogEntry.php(479): RecentChange->save('pleasedontudp')
#5 /usr/home/saper/public_html/pg/w/includes/WikiPage.php(2042): ManualLogEntry->publish('1')
#6 /usr/home/saper/public_html/pg/w/includes/WikiPage.php(1937): WikiPage->doDeleteArticleReal('No longer requi...', false, 0, false, '', Object(User))
#7 /usr/home/saper/public_html/pg/w/maintenance/deleteDefaultMessages.php(73): WikiPage->doDeleteArticle('No longer requi...', false, 0, false, '', Object(User))
#8 /usr/home/saper/public_html/pg/w/maintenance/update.php(128): DeleteDefaultMessages->execute()
#9 /usr/home/saper/public_html/pg/w/maintenance/doMaintenance.php(105): UpdateMediaWiki->execute()
#10 /usr/home/saper/public_html/pg/w/maintenance/update.php(151): require_once('/usr/home/saper...')
#11 {main}

includes/RecentChange.php
includes/logging/LogEntry.php
maintenance/deleteDefaultMessages.php

index 9fcdbcd..444e40f 100644 (file)
@@ -382,12 +382,6 @@ class RecentChange {
         */
        public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
                $lastTimestamp, $bot, $ip='', $oldSize=0, $newSize=0, $newId=0, $patrol=0 ) {
-               global $wgRequest;
-               if( !$ip ) {
-                       $ip = $wgRequest->getIP();
-                       if( !$ip ) $ip = '';
-               }
-
                $rc = new RecentChange;
                $rc->mAttribs = array(
                        'rc_timestamp'  => $timestamp,
@@ -405,7 +399,7 @@ class RecentChange {
                        'rc_bot'        => $bot ? 1 : 0,
                        'rc_moved_to_ns' => 0,
                        'rc_moved_to_title' => '',
-                       'rc_ip'         => $ip,
+                       'rc_ip'         => self::checkIPAddress( $ip ),
                        'rc_patrolled'  => intval($patrol),
                        'rc_new'        => 0,  # obsolete
                        'rc_old_len'    => $oldSize,
@@ -446,14 +440,6 @@ class RecentChange {
         */
        public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
                $ip='', $size=0, $newId=0, $patrol=0 ) {
-               global $wgRequest;
-               if( !$ip ) {
-                       $ip = $wgRequest->getIP();
-                       if( !$ip ) {
-                               $ip = '';
-                       }
-               }
-
                $rc = new RecentChange;
                $rc->mAttribs = array(
                        'rc_timestamp'      => $timestamp,
@@ -471,7 +457,7 @@ class RecentChange {
                        'rc_bot'            => $bot ? 1 : 0,
                        'rc_moved_to_ns'    => 0,
                        'rc_moved_to_title' => '',
-                       'rc_ip'             => $ip,
+                       'rc_ip'             => self::checkIPAddress( $ip ),
                        'rc_patrolled'      => intval($patrol),
                        'rc_new'            => 1, # obsolete
                        'rc_old_len'        => 0,
@@ -540,12 +526,6 @@ class RecentChange {
        public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
                $type, $action, $target, $logComment, $params, $newId=0, $actionCommentIRC='' ) {
                global $wgRequest;
-               if( !$ip ) {
-                       $ip = $wgRequest->getIP();
-                       if( !$ip ) {
-                               $ip = '';
-                       }
-               }
 
                $rc = new RecentChange;
                $rc->mAttribs = array(
@@ -564,7 +544,7 @@ class RecentChange {
                        'rc_bot'        => $user->isAllowed( 'bot' ) ? $wgRequest->getBool( 'bot', true ) : 0,
                        'rc_moved_to_ns' => 0,
                        'rc_moved_to_title' => '',
-                       'rc_ip'         => $ip,
+                       'rc_ip'         => self::checkIPAddress( $ip ),
                        'rc_patrolled'  => 1,
                        'rc_new'        => 0, # obsolete
                        'rc_old_len'    => null,
@@ -575,6 +555,8 @@ class RecentChange {
                        'rc_log_action' => $action,
                        'rc_params'     => $params
                );
+               wfDebug(__METHOD__ . ": " . var_export($rc->mAttribs, TRUE) . "\n");
+
                $rc->mExtra =  array(
                        'prefixedDBkey' => $title->getPrefixedDBkey(),
                        'lastTimestamp' => 0,
@@ -769,4 +751,18 @@ class RecentChange {
                }
                return ChangesList::showCharacterDifference( $old, $new );
        }
+
+       public static function checkIPAddress( $ip ) {
+               global $wgRequest;
+               if ( $ip ) {
+                       if ( !IP::isIPAddress( $ip ) ) {
+                               throw new MWException( "Attempt to write \"" . $ip . "\" as an IP address into recent changes" );
+                       }
+               } else {
+                       $ip = $wgRequest->getIP();
+                       if( !$ip ) 
+                               $ip = '';
+               }
+               return $ip;
+       }
 }
index 5a01c52..0be0740 100644 (file)
@@ -458,12 +458,22 @@ class ManualLogEntry extends LogEntryBase {
 
                $logpage = SpecialPage::getTitleFor( 'Log', $this->getType() );
                $user = $this->getPerformer();
+               $ip = "";
+               if ( $user->isAnon() ) {
+                       /*
+                        * "MediaWiki default" and friends may have
+                        * no IP address in their name
+                        */
+                       if ( IP::isIPAddress( $user->getName() ) ) {
+                               $ip = $user->getName();
+                       }
+               }
                $rc = RecentChange::newLogEntry(
                        $this->getTimestamp(),
                        $logpage,
                        $user,
                        $formatter->getPlainActionText(),
-                       $user->isAnon() ? $user->getName() : '',
+                       $ip,
                        $this->getType(),
                        $this->getSubtype(),
                        $this->getTarget(),
index 73947e9..989f7bd 100644 (file)
@@ -74,7 +74,7 @@ class DeleteDefaultMessages extends Maintenance {
                        $dbw->commit( __METHOD__ );
                }
 
-               $this->output( 'done!', 'msg' );
+               $this->output( 'done!\n', 'msg' );
        }
 }