(bug 7688) When viewing diff, section anchors in autosummary jump to section on curre...
[lhc/web/wiklou.git] / includes / DifferenceEngine.php
index 3a58392..9adcda5 100644 (file)
@@ -5,12 +5,6 @@
  * @subpackage DifferenceEngine
  */
 
-/** */
-require_once( 'Revision.php' );
-
-define( 'MAX_DIFF_LINE', 10000 );
-define( 'MAX_DIFF_XREF_LENGTH', 10000 );
-
 /**
  * @todo document
  * @public
@@ -21,24 +15,14 @@ class DifferenceEngine {
        /**#@+
         * @private
         */
-       private
-               $mNewid,
-               $mNewPage,
-               $mNewRev,
-               $mNewtext,
-               $mNewtitle,
-
-               $mOldid,
-               $mOldPage,
-               $mOldRev,
-               $mOldtext,
-               $mOldtitle,
-
-               $mPagetitle,
-               $mRcidMarkPatrolled,
-               $mRevisionsLoaded = false, // Have the revisions been loaded
-               $mTextLoaded = 0, // How many text blobs have been loaded, 0, 1 or 2?
-               $mTitle;
+       var $mOldid, $mNewid, $mTitle;
+       var $mOldtitle, $mNewtitle, $mPagetitle;
+       var $mOldtext, $mNewtext;
+       var $mOldPage, $mNewPage;
+       var $mRcidMarkPatrolled;
+       var $mOldRev, $mNewRev;
+       var $mRevisionsLoaded = false; // Have the revisions been loaded
+       var $mTextLoaded = 0; // How many text blobs have been loaded, 0, 1 or 2?
        /**#@-*/
 
        /**
@@ -124,6 +108,9 @@ CONTROL;
                        wfProfileOut( $fname );
                        return;
                }
+               
+               wfRunHooks( 'DiffViewHeader', array( $this, $this->mOldRev, $this->mNewRev ) );
+
                if ( $this->mNewRev->isCurrent() ) {
                        $wgOut->setArticleFlag( true );
                }
@@ -147,7 +134,7 @@ CONTROL;
                        $wgOut->setPageTitle( $oldTitle . ', ' . $newTitle );
                }
                $wgOut->setSubtitle( wfMsg( 'difference' ) );
-               $wgOut->setRobotpolicy( 'noindex,follow' );
+               $wgOut->setRobotpolicy( 'noindex,nofollow' );
 
                if ( !( $this->mOldPage->userCanRead() && $this->mNewPage->userCanRead() ) ) {
                        $wgOut->loginToUse();
@@ -161,11 +148,7 @@ CONTROL;
                $contribs = wfMsg( 'contribslink' );
 
                if ( $this->mNewRev->isCurrent() && $wgUser->isAllowed('rollback') ) {
-                       $username = $this->mNewRev->getUserText();
-                       $rollback = '&nbsp;&nbsp;&nbsp;<strong>[' . $sk->makeKnownLinkObj( $this->mTitle, wfMsg( 'rollbacklink' ),
-                               'action=rollback&from=' . urlencode( $username ) .
-                               '&token=' . urlencode( $wgUser->editToken( array( $this->mTitle->getPrefixedText(), $username ) ) ) ) .
-                               ']</strong>';
+                       $rollback = '&nbsp;&nbsp;&nbsp;' . $sk->generateRollback( $this->mNewRev );
                } else {
                        $rollback = '';
                }
@@ -178,26 +161,39 @@ CONTROL;
                $prevlink = $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'previousdiff' ),
                        'diff=prev&oldid='.$this->mOldid, '', '', 'id="differences-prevlink"' );
                if ( $this->mNewRev->isCurrent() ) {
-                       $nextlink = '';
+                       $nextlink = '&nbsp;';
                } else {
                        $nextlink = $sk->makeKnownLinkObj( $this->mTitle, wfMsgHtml( 'nextdiff' ),
                                'diff=next&oldid='.$this->mNewid, '', '', 'id="differences-nextlink"' );
                }
 
+               $oldminor = '';
+               $newminor = '';
+
+               if ($this->mOldRev->mMinorEdit == 1) {
+                       $oldminor = wfElement( 'span', array( 'class' => 'minor' ),
+                               wfMsg( 'minoreditletter') ) . ' ';
+               }
+
+                if ($this->mNewRev->mMinorEdit == 1) {
+                        $newminor = wfElement( 'span', array( 'class' => 'minor' ),
+                               wfMsg( 'minoreditletter') ) . ' ';
+                }
+
                $oldHeader = "<strong>{$this->mOldtitle}</strong><br />" .
                        $sk->revUserTools( $this->mOldRev ) . "<br />" .
-                       $sk->revComment( $this->mOldRev ) . "<br />" .
+                       $oldminor . $sk->revComment( $this->mOldRev ) . "<br />" .
                        $prevlink;
                $newHeader = "<strong>{$this->mNewtitle}</strong><br />" .
                        $sk->revUserTools( $this->mNewRev ) . " $rollback<br />" .
-                       $sk->revComment( $this->mNewRev ) . "<br />" .
+                       $newminor . $sk->revComment( $this->mNewRev, true ) . "<br />" .
                        $nextlink . $patrol;
 
                $this->showDiff( $oldHeader, $newHeader );
                $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\n" );
 
                if( !$this->mNewRev->isCurrent() ) {
-                       $oldEditSectionSetting = $wgOut->mParserOptions->setEditSection( false );
+                       $oldEditSectionSetting = $wgOut->parserOptions()->setEditSection( false );
                }
 
                $this->loadNewText();
@@ -207,7 +203,7 @@ CONTROL;
                $wgOut->addSecondaryWikiText( $this->mNewtext );
 
                if( !$this->mNewRev->isCurrent() ) {
-                       $wgOut->mParserOptions->setEditSection( $oldEditSectionSetting );
+                       $wgOut->parserOptions()->setEditSection( $oldEditSectionSetting );
                }
 
                wfProfileOut( $fname );
@@ -310,7 +306,7 @@ CONTROL;
         * Returns false on error
         */
        function getDiffBody() {
-               global $wgMemc, $wgDBname;
+               global $wgMemc;
                $fname = 'DifferenceEngine::getDiffBody';
                wfProfileIn( $fname );
                
@@ -318,7 +314,7 @@ CONTROL;
                $key = false;
                if ( $this->mOldid && $this->mNewid ) {
                        // Try cache
-                       $key = "$wgDBname:diff:oldid:{$this->mOldid}:newid:{$this->mNewid}";
+                       $key = wfMemcKey( 'diff', 'oldid', $this->mOldid, 'newid', $this->mNewid );
                        $difftext = $wgMemc->get( $key );
                        if ( $difftext ) {
                                wfIncrStats( 'diff_cache_hit' );
@@ -420,8 +416,8 @@ CONTROL;
                # Native PHP diff
                $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
                $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
-               $diffs =& new Diff( $ota, $nta );
-               $formatter =& new TableDiffFormatter();
+               $diffs = new Diff( $ota, $nta );
+               $formatter = new TableDiffFormatter();
                return $wgContLang->unsegmentForDiff( $formatter->format( $diffs ) );
        }
                
@@ -436,7 +432,7 @@ CONTROL;
 
        function localiseLineNumbersCb( $matches ) {
                global $wgLang;
-               return wfMsg( 'lineno', $wgLang->formatNum( $matches[1] ) );
+               return wfMsgExt( 'lineno', array('parseinline'), $wgLang->formatNum( $matches[1] ) );
        }
 
        /**
@@ -495,17 +491,23 @@ CONTROL;
                }
 
                // Set assorted variables
+               $timestamp = $wgLang->timeanddate( $this->mNewRev->getTimestamp(), true );
+               $this->mNewPage = $this->mNewRev->getTitle();
                if( $this->mNewRev->isCurrent() ) {
-                       $this->mPagetitle = htmlspecialchars( wfMsg( 'currentrev' ) );
-                       $this->mNewPage = $this->mTitle;
                        $newLink = $this->mNewPage->escapeLocalUrl();
-                       $this->mNewtitle = "<a href='$newLink'>{$this->mPagetitle}</a>";
+                       $this->mPagetitle = htmlspecialchars( wfMsg( 'currentrev' ) );
+                       $newEdit = $this->mNewPage->escapeLocalUrl( 'action=edit' );
+                       
+                       $this->mNewtitle = "<strong><a href='$newLink'>{$this->mPagetitle}</a> ($timestamp)</strong>"
+                               . " (<a href='$newEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</a>)";
+
                } else {
-                       $this->mNewPage = $this->mNewRev->getTitle();
-                       $newLink = $this->mNewPage->escapeLocalUrl ('oldid=' . $this->mNewid );
-                       $t = $wgLang->timeanddate( $this->mNewRev->getTimestamp(), true );
-                       $this->mPagetitle = htmlspecialchars( wfMsg( 'revisionasof', $t ) );
-                       $this->mNewtitle = "<a href='$newLink'>{$this->mPagetitle}</a>";
+                       $newLink = $this->mNewPage->escapeLocalUrl( 'oldid=' . $this->mNewid );
+                       $newEdit = $this->mNewPage->escapeLocalUrl( 'action=edit&oldid=' . $this->mNewid );
+                       $this->mPagetitle = htmlspecialchars( wfMsg( 'revisionasof', $timestamp ) );
+                       
+                       $this->mNewtitle = "<strong><a href='$newLink'>{$this->mPagetitle}</a></strong>"
+                               . " (<a href='$newEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</a>)";
                }
 
                // Load the old revision object
@@ -533,7 +535,9 @@ CONTROL;
 
                        $t = $wgLang->timeanddate( $this->mOldRev->getTimestamp(), true );
                        $oldLink = $this->mOldPage->escapeLocalUrl( 'oldid=' . $this->mOldid );
-                       $this->mOldtitle = "<a href='$oldLink'>" . htmlspecialchars( wfMsg( 'revisionasof', $t ) ) . '</a>';
+                       $oldEdit = $this->mOldPage->escapeLocalUrl( 'action=edit&oldid=' . $this->mOldid );
+                       $this->mOldtitle = "<strong><a href='$oldLink'>" . htmlspecialchars( wfMsg( 'revisionasof', $t ) )
+                               . "</a></strong> (<a href='$oldEdit'>" . htmlspecialchars( wfMsg( 'editold' ) ) . "</a>)";
                }
 
                return true;
@@ -603,10 +607,9 @@ define('USE_ASSERTS', function_exists('assert'));
  * @subpackage DifferenceEngine
  */
 class _DiffOp {
-       private
-               $closing,
-               $orig,
-               $type;
+       var $type;
+       var $orig;
+       var $closing;
 
        function reverse() {
                trigger_error('pure virtual', E_USER_ERROR);
@@ -628,8 +631,7 @@ class _DiffOp {
  * @subpackage DifferenceEngine
  */
 class _DiffOp_Copy extends _DiffOp {
-       public
-               $type = 'copy';
+       var $type = 'copy';
 
        function _DiffOp_Copy ($orig, $closing = false) {
                if (!is_array($closing))
@@ -650,7 +652,7 @@ class _DiffOp_Copy extends _DiffOp {
  * @subpackage DifferenceEngine
  */
 class _DiffOp_Delete extends _DiffOp {
-       public $type = 'delete';
+       var $type = 'delete';
 
        function _DiffOp_Delete ($lines) {
                $this->orig = $lines;
@@ -669,7 +671,7 @@ class _DiffOp_Delete extends _DiffOp {
  * @subpackage DifferenceEngine
  */
 class _DiffOp_Add extends _DiffOp {
-       public $type = 'add';
+       var $type = 'add';
 
        function _DiffOp_Add ($lines) {
                $this->closing = $lines;
@@ -688,8 +690,7 @@ class _DiffOp_Add extends _DiffOp {
  * @subpackage DifferenceEngine
  */
 class _DiffOp_Change extends _DiffOp {
-       private
-               $type = 'change';
+       var $type = 'change';
 
        function _DiffOp_Change ($orig, $closing) {
                $this->orig = $orig;
@@ -728,6 +729,8 @@ class _DiffOp_Change extends _DiffOp {
  */
 class _DiffEngine
 {
+       const MAX_XREF_LENGTH =  10000;
+
        function diff ($from_lines, $to_lines) {
                $fname = '_DiffEngine::diff';
                wfProfileIn( $fname );
@@ -825,7 +828,7 @@ class _DiffEngine
         * Returns the whole line if it's small enough, or the MD5 hash otherwise
         */
        function _line_hash( $line ) {
-               if ( strlen( $line ) > MAX_DIFF_XREF_LENGTH ) {
+               if ( strlen( $line ) > self::MAX_XREF_LENGTH ) {
                        return md5( $line );
                } else {
                        return $line;
@@ -1148,7 +1151,7 @@ class _DiffEngine
  */
 class Diff
 {
-       public $edits;
+       var $edits;
 
        /**
         * Constructor.
@@ -1360,7 +1363,7 @@ class DiffFormatter
         * This should be left at zero for this class, but subclasses
         * may want to set this to other values.
         */
-       public $leading_context_lines = 0;
+       var $leading_context_lines = 0;
 
        /**
         * Number of trailing context "lines" to preserve.
@@ -1368,7 +1371,7 @@ class DiffFormatter
         * This should be left at zero for this class, but subclasses
         * may want to set this to other values.
         */
-       public $trailing_context_lines = 0;
+       var $trailing_context_lines = 0;
 
        /**
         * Format a diff.
@@ -1580,6 +1583,8 @@ class _HWLDF_WordAccumulator {
  */
 class WordLevelDiff extends MappedDiff
 {
+       const MAX_LINE_LENGTH = 10000;
+
        function WordLevelDiff ($orig_lines, $closing_lines) {
                $fname = 'WordLevelDiff::WordLevelDiff';
                wfProfileIn( $fname );
@@ -1608,7 +1613,7 @@ class WordLevelDiff extends MappedDiff
                                $words[] = "\n";
                                $stripped[] = "\n";
                        }
-                       if ( strlen( $line ) > MAX_DIFF_LINE ) {
+                       if ( strlen( $line ) > self::MAX_LINE_LENGTH ) {
                                $words[] = $line;
                                $stripped[] = $line;
                        } else {