Import: Try to stop revisions getting created with rev_page = 0
[lhc/web/wiklou.git] / includes / import / WikiRevision.php
index 9b8c74c..90b048a 100644 (file)
@@ -463,7 +463,7 @@ class WikiRevision {
                $page = WikiPage::factory( $this->title );
                $page->loadPageData( 'fromdbmaster' );
                if ( !$page->exists() ) {
-                       # must create the page...
+                       // must create the page...
                        $pageId = $page->insertOn( $dbw );
                        $created = true;
                        $oldcountable = null;
@@ -486,7 +486,17 @@ class WikiRevision {
                        }
                }
 
+               if ( !$pageId ) {
+                       // This seems to happen if two clients simultaneously try to import the
+                       // same page
+                       wfDebug( __METHOD__ . ': got invalid $pageId when importing revision of [[' .
+                               $this->title->getPrefixedText() . ']], timestamp ' . $this->timestamp . "\n" );
+                       return false;
+               }
+
                // Select previous version to make size diffs correct
+               // @todo This assumes that multiple revisions of the same page are imported
+               // in order from oldest to newest.
                $prevId = $dbw->selectField( 'revision', 'rev_id',
                        array(
                                'rev_page' => $pageId,
@@ -613,8 +623,9 @@ class WikiRevision {
                        wfDebug( __METHOD__ . ": Could not fetch remote file.\n" );
                        return false;
                }
+               $sha1File = ltrim( sha1_file( $source ), '0' );
                $sha1 = $this->getSha1();
-               if ( $sha1 && ( $sha1 !== sha1_file( $source ) ) ) {
+               if ( $sha1 && ( $sha1 !== $sha1File ) ) {
                        if ( $flags & File::DELETE_SOURCE ) {
                                # Broken file; delete it if it is a temporary file
                                unlink( $source );