Fix handling of oldest revision with diff-links from the user contributions.
authorJens Frank <jeluf@users.mediawiki.org>
Sun, 29 Aug 2004 18:13:23 +0000 (18:13 +0000)
committerJens Frank <jeluf@users.mediawiki.org>
Sun, 29 Aug 2004 18:13:23 +0000 (18:13 +0000)
ENH#228 Have next/previous links in diffs.

includes/DifferenceEngine.php
languages/Language.php

index 5241c4b..3bc8269 100644 (file)
@@ -14,14 +14,35 @@ class DifferenceEngine {
        {
                global $wgTitle;
                if ( 'prev' == $new ) {
+                       # Show diff between revision $old and the previous one.
+                       # Get previous one from DB.
+                       #
                        $this->mNewid = intval($old);
                        $dbr =& wfGetDB( DB_SLAVE );
                        $this->mOldid = $dbr->selectField( 'old', 'old_id',  
                                "old_title='" . $wgTitle->getDBkey() . "'" .
                                ' AND old_namespace=' . $wgTitle->getNamespace() .
-                               " AND old_id<{$this->mNewid} order by old_id desc" );
+                               " AND old_id<{$this->mNewid} ORDER BY old_id DESC" );
+
+               } elseif ( 'next' == $new ) {
+
+                       # Show diff between revision $old and the previous one.
+                       # Get previous one from DB.
+                       #
+                       $this->mOldid = intval($old);
+                       $dbr =& wfGetDB( DB_SLAVE );
+                       $this->mNewid = $dbr->selectField( 'old', 'old_id',  
+                               "old_title='" . $wgTitle->getDBkey() . "'" .
+                               ' AND old_namespace=' . $wgTitle->getNamespace() .
+                               " AND old_id>{$this->mOldid} ORDER BY old_id " );
+                       if ( false === $this->mNewid ) {
+                               # if no result, NewId points to the newest old revision. The only newer
+                               # revision is cur, which is "0".
+                               $this->mNewid = 0;
+                       }
 
                } else {
+
                        $this->mOldid = intval($old);
                        $this->mNewid = intval($new);
                }
@@ -34,6 +55,15 @@ class DifferenceEngine {
                $fname = 'DifferenceEngine::showDiffPage';
                wfProfileIn( $fname );
 
+               # mOldid is false if the difference engine is called with a "vague" query for
+               # a diff between a version V and its previous version V' AND the version V
+               # is the first version of that article. In that case, V' does not exist.
+               if ( $this->mOldid === false ) {
+                       $this->showFirstRevision();
+                       wfProfileOut( $fname );
+                       return;
+               }
+
                $t = $wgTitle->getPrefixedText() . " (Diff: {$this->mOldid}, " .
                  "{$this->mNewid})";
                $mtext = wfMsg( 'missingarticle', $t );
@@ -94,10 +124,19 @@ class DifferenceEngine {
                        $patrol = '';
                }
 
+               $prevlink = $sk->makeKnownLinkObj( $wgTitle, wfMsg( 'previousdiff' ), 'diff=prev&oldid='.$this->mOldid );
+               if ( $this->mNewid == 0 ) {
+                       $nextlink = '';
+               } else {
+                       $nextlink = $sk->makeKnownLinkObj( $wgTitle, wfMsg( 'nextdiff' ), 'diff=next&oldid='.$this->mNewid );
+               }
+
                $oldHeader = "<strong>{$this->mOldtitle}</strong><br />$oldUserLink " .
-                       "($oldUTLink | $oldContribs)<br />" . $this->mOldComment;
+                       "($oldUTLink | $oldContribs)<br />" . $this->mOldComment .
+                       '<br />' . $prevlink;
                $newHeader = "<strong>{$this->mNewtitle}</strong><br />$newUserLink " .
-                       "($newUTLink | $newContribs) $rollback<br />" . $this->mNewComment . $patrol;
+                       "($newUTLink | $newContribs) $rollback<br />" . $this->mNewComment . 
+                       '<br />' . $nextlink . $patrol;
 
                DifferenceEngine::showDiff( $this->mOldtext, $this->mNewtext,
                  $oldHeader, $newHeader );
@@ -107,6 +146,67 @@ class DifferenceEngine {
                wfProfileOut( $fname );
        }
 
+       # Show the first revision of an article. Uses normal diff headers in contrast to normal
+       # "old revision" display style.
+       #
+       function showFirstRevision()
+       {
+               global $wgOut, $wgTitle, $wgUser, $wgLang;
+
+               $fname = 'DifferenceEngine::showFirstRevision';
+               wfProfileIn( $fname );
+
+
+               $this->mOldid = $this->mNewid; # hack to make loadText() work.
+
+               # Get article text from the DB
+               #
+               if ( ! $this->loadText() ) {
+                       $t = $wgTitle->getPrefixedText() . " (Diff: {$this->mOldid}, " .
+                         "{$this->mNewid})";
+                       $mtext = wfMsg( 'missingarticle', $t );
+                       $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) );
+                       $wgOut->addHTML( $mtext );
+                       wfProfileOut( $fname );
+                       return;
+               }
+
+               # Check if user is allowed to look at this page. If not, bail out.
+               #
+               if ( !( $this->mOldPage->userCanRead() ) ) {
+                       $wgOut->loginToUse();
+                       $wgOut->output();
+                       wfProfileOut( $fname );
+                       exit;
+               }
+
+               # Prepare the header box
+               #
+               $sk = $wgUser->getSkin();
+
+               $uTLink = $sk->makeLinkObj( Title::makeTitleSafe( NS_USER_TALK, $this->mOldUser ),  $wgLang->getNsText( NS_TALK ) );
+               $userLink = $sk->makeLinkObj( Title::makeTitleSafe( NS_USER, $this->mOldUser ), $this->mOldUser );
+               $contribs = $sk->makeKnownLinkObj( Title::makeTitle( NS_SPECIAL, 'Contributions' ), wfMsg( 'contribslink' ),
+                       'target=' . urlencode($this->mOldUser) );
+               $nextlink = $sk->makeKnownLinkObj( $wgTitle, wfMsg( 'nextdiff' ), 'diff=next&oldid='.$this->mNewid );
+               $header = "<div class=\"firstrevisionheader\" style=\"text-align: center\"><strong>{$this->mOldtitle}</strong><br />$userLink " .
+                       "($uTLink | $contribs)<br />" . $this->mOldComment .
+                       '<br />' . $nextlink. "</div>\n";
+       
+               $wgOut->addHTML( $header );
+
+               $wgOut->setSubtitle( wfMsg( 'difference' ) );
+               $wgOut->setRobotpolicy( 'noindex,follow' );
+
+
+               # Show current revision
+               #
+               $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
+               $wgOut->addWikiText( $this->mNewtext );
+
+               wfProfileOut( $fname );
+       }
+
        function showDiff( $otext, $ntext, $otitle, $ntitle )
        {
                global $wgOut, $wgUseExternalDiffEngine;
index 5c0c3ff..79eb150 100644 (file)
@@ -1525,6 +1525,10 @@ ta[\'ca-nstab-category\'] = new Array(\'c\',\'View the category page\');
 # image deletion
 'deletedrevision' => 'Deleted old revision $1.',
 
+# browsing diffs
+'previousdiff' => '&larr; Go to previous diff',
+'nextdiff' => 'Go to next diff &rarr;',
+
 
 );