X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FDifferenceEngine.php;h=52bf654d7795b086c249ef8449704f4d61f68017;hb=8d704a794292929a72a2060458be604b545ffbfb;hp=c2c388467f14059c08fe9680ce69f5bcbfecf782;hpb=075396a961140c3ae7baf27238f6a7a0d79757cc;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/DifferenceEngine.php b/includes/DifferenceEngine.php index c2c388467f..52bf654d77 100644 --- a/includes/DifferenceEngine.php +++ b/includes/DifferenceEngine.php @@ -1,9 +1,16 @@ mOldid = $old; - $this->mNewid = $new; + 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" ); + + } 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); + } $this->mRcidMarkPatrolled = intval($rcid); # force it to be an integer } function showDiffPage() { - global $wgUser, $wgTitle, $wgOut, $wgLang, $wgOnlySysopsCanPatrol; - $fname = "DifferenceEngine::showDiffPage"; + global $wgUser, $wgTitle, $wgOut, $wgLang, $wgOnlySysopsCanPatrol, $wgUseRCPatrol; + $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 ); + $mtext = wfMsg( 'missingarticle', $t ); $wgOut->setArticleFlag( false ); if ( ! $this->loadText() ) { - $wgOut->setPagetitle( wfMsg( "errorpagetitle" ) ); + $wgOut->setPagetitle( wfMsg( 'errorpagetitle' ) ); $wgOut->addHTML( $mtext ); wfProfileOut( $fname ); return; @@ -41,10 +89,10 @@ class DifferenceEngine { if( $oldTitle == $newTitle ) { $wgOut->setPageTitle( $newTitle ); } else { - $wgOut->setPageTitle( $oldTitle . ", " . $newTitle ); + $wgOut->setPageTitle( $oldTitle . ', ' . $newTitle ); } - $wgOut->setSubtitle( wfMsg( "difference" ) ); - $wgOut->setRobotpolicy( "noindex,follow" ); + $wgOut->setSubtitle( wfMsg( 'difference' ) ); + $wgOut->setRobotpolicy( 'noindex,follow' ); if ( !( $this->mOldPage->userCanRead() && $this->mNewPage->userCanRead() ) ) { $wgOut->loginToUse(); @@ -55,42 +103,112 @@ class DifferenceEngine { $sk = $wgUser->getSkin(); $talk = $wgLang->getNsText( NS_TALK ); - $contribs = wfMsg( "contribslink" ); + $contribs = wfMsg( 'contribslink' ); $this->mOldComment = $sk->formatComment($this->mOldComment); $this->mNewComment = $sk->formatComment($this->mNewComment); - $oldUserLink = $sk->makeLinkObj( Title::makeTitle( NS_USER, $this->mOldUser ), $this->mOldUser ); - $newUserLink = $sk->makeLinkObj( Title::makeTitle( NS_USER, $this->mNewUser ), $this->mNewUser ); - $oldUTLink = $sk->makeLinkObj( Title::makeTitle( NS_USER_TALK, $this->mOldUser ), $talk ); - $newUTLink = $sk->makeLinkObj( Title::makeTitle( NS_USER_TALK, $this->mNewUser ), $talk ); - $oldContribs = $sk->makeKnownLinkObj( Title::makeTitle( NS_SPECIAL, "Contributions" ), $contribs, - "target=" . urlencode($this->mOldUser) ); - $newContribs = $sk->makeKnownLinkObj( Title::makeTitle( NS_SPECIAL, "Contributions" ), $contribs, - "target=" . urlencode($this->mNewUser) ); + $oldUserLink = $sk->makeLinkObj( Title::makeTitleSafe( NS_USER, $this->mOldUser ), $this->mOldUser ); + $newUserLink = $sk->makeLinkObj( Title::makeTitleSafe( NS_USER, $this->mNewUser ), $this->mNewUser ); + $oldUTLink = $sk->makeLinkObj( Title::makeTitleSafe( NS_USER_TALK, $this->mOldUser ), $talk ); + $newUTLink = $sk->makeLinkObj( Title::makeTitleSafe( NS_USER_TALK, $this->mNewUser ), $talk ); + $oldContribs = $sk->makeKnownLinkObj( Title::makeTitle( NS_SPECIAL, 'Contributions' ), $contribs, + 'target=' . urlencode($this->mOldUser) ); + $newContribs = $sk->makeKnownLinkObj( Title::makeTitle( NS_SPECIAL, 'Contributions' ), $contribs, + 'target=' . urlencode($this->mNewUser) ); if ( !$this->mNewid && $wgUser->isSysop() ) { - $rollback = "   [" . $sk->makeKnownLinkObj( $wgTitle, wfMsg( "rollbacklink" ), - "action=rollback&from=" . urlencode($this->mNewUser) ) . "]"; + $rollback = '   [' . $sk->makeKnownLinkObj( $wgTitle, wfMsg( 'rollbacklink' ), + 'action=rollback&from=' . urlencode($this->mNewUser) ) . ']'; } else { - $rollback = ""; + $rollback = ''; } - if ( $this->mRcidMarkPatrolled != 0 && $wgUser->getID() != 0 && + if ( $wgUseRCPatrol && $this->mRcidMarkPatrolled != 0 && $wgUser->getID() != 0 && ( $wgUser->isSysop() || !$wgOnlySysopsCanPatrol ) ) { - $patrol = " [" . $sk->makeKnownLinkObj( $wgTitle, wfMsg( 'markaspatrolleddiff' ), - "action=markpatrolled&rcid={$this->mRcidMarkPatrolled}" ) . "]"; + $patrol = ' [' . $sk->makeKnownLinkObj( $wgTitle, wfMsg( 'markaspatrolleddiff' ), + "action=markpatrolled&rcid={$this->mRcidMarkPatrolled}" ) . ']'; + } else { + $patrol = ''; + } + + $prevlink = $sk->makeKnownLinkObj( $wgTitle, wfMsg( 'previousdiff' ), 'diff=prev&oldid='.$this->mOldid ); + if ( $this->mNewid == 0 ) { + $nextlink = ''; } else { - $patrol = ""; + $nextlink = $sk->makeKnownLinkObj( $wgTitle, wfMsg( 'nextdiff' ), 'diff=next&oldid='.$this->mNewid ); } $oldHeader = "{$this->mOldtitle}
$oldUserLink " . - "($oldUTLink | $oldContribs)
" . $this->mOldComment; + "($oldUTLink | $oldContribs)
" . $this->mOldComment . + '
' . $prevlink; $newHeader = "{$this->mNewtitle}
$newUserLink " . - "($newUTLink | $newContribs) $rollback
" . $this->mNewComment . $patrol; + "($newUTLink | $newContribs) $rollback
" . $this->mNewComment . + '
' . $nextlink . $patrol; DifferenceEngine::showDiff( $this->mOldtext, $this->mNewtext, $oldHeader, $newHeader ); - $wgOut->addHTML( "

{$this->mNewtitle}

\n" ); + $wgOut->addHTML( "

{$this->mPagetitle}

\n" ); + $wgOut->addWikiText( $this->mNewtext ); + + 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 = "
{$this->mOldtitle}
$userLink " . + "($uTLink | $contribs)
" . $this->mOldComment . + '
' . $nextlink. "
\n"; + + $wgOut->addHTML( $header ); + + $wgOut->setSubtitle( wfMsg( 'difference' ) ); + $wgOut->setRobotpolicy( 'noindex,follow' ); + + + # Show current revision + # + $wgOut->addHTML( "

{$this->mPagetitle}

\n" ); $wgOut->addWikiText( $this->mNewtext ); wfProfileOut( $fname ); @@ -100,23 +218,24 @@ class DifferenceEngine { { global $wgOut, $wgUseExternalDiffEngine; - $otext = str_replace( "\r\n", "\n", htmlspecialchars( $otext ) ); - $ntext = str_replace( "\r\n", "\n", htmlspecialchars( $ntext ) ); - + $wgOut->addHTML( " + + + + + + " ); - $wgOut->addHTML( "
{$otitle}{$ntitle}
- - -\n" ); if ( $wgUseExternalDiffEngine ) { - dl("php_wikidiff.so"); + # For historical reasons, external diff engine expects + # input text to be HTML-escaped already + $otext = str_replace( "\r\n", "\n", htmlspecialchars ( $otext ) ); + $ntext = str_replace( "\r\n", "\n", htmlspecialchars ( $ntext ) ); + dl('php_wikidiff.so'); $wgOut->addHTML( wikidiff_do_diff( $otext, $ntext, 2) ); } else { - $ota = explode( "\n", $otext); - $nta = explode( "\n", $ntext); + $ota = explode( "\n", str_replace( "\r\n", "\n", $otext ) ); + $nta = explode( "\n", str_replace( "\r\n", "\n", $ntext ) ); $diffs = new Diff( $ota, $nta ); $formatter = new TableDiffFormatter(); $formatter->format( $diffs ); @@ -132,17 +251,20 @@ cellpadding='0' cellspacing='4px' class='diff'> function loadText() { global $wgTitle, $wgOut, $wgLang; - $fname = "DifferenceEngine::loadText"; + $fname = 'DifferenceEngine::loadText'; $dbr =& wfGetDB( DB_SLAVE ); if ( 0 == $this->mNewid || 0 == $this->mOldid ) { $wgOut->setArticleFlag( true ); - $this->mNewtitle = wfMsg( "currentrev" ); + $newLink = $wgTitle->getLocalUrl(); + $this->mPagetitle = wfMsg( 'currentrev' ); + $this->mNewtitle = "{$this->mPagetitle}"; $id = $wgTitle->getArticleID(); $s = $dbr->getArray( 'cur', array( 'cur_text', 'cur_user_text', 'cur_comment' ), array( 'cur_id' => $id ), $fname ); if ( $s === false ) { + wfDebug( "Unable to load cur_id $id\n" ); return false; } @@ -155,6 +277,7 @@ cellpadding='0' cellspacing='4px' class='diff'> 'old_flags','old_user_text','old_comment' ), array( 'old_id' => $this->mNewid ), $fname ); if ( $s === false ) { + wfDebug( "Unable to load old_id {$this->mNewid}\n" ); return false; } @@ -162,7 +285,9 @@ cellpadding='0' cellspacing='4px' class='diff'> $t = $wgLang->timeanddate( $s->old_timestamp, true ); $this->mNewPage = Title::MakeTitle( $s->old_namespace, $s->old_title ); - $this->mNewtitle = wfMsg( "revisionasof", $t ); + $newLink = $wgTitle->getLocalUrl ('oldid=' . $this->mNewid); + $this->mPagetitle = wfMsg( 'revisionasof', $t ); + $this->mNewtitle = "{$this->mPagetitle}"; $this->mNewUser = $s->old_user_text; $this->mNewComment = $s->old_comment; } @@ -174,21 +299,27 @@ cellpadding='0' cellspacing='4px' class='diff'> 'old_title' => $this->mNewPage->getDBkey() ), $fname, array( 'ORDER BY' => 'inverse_timestamp', 'USE INDEX' => 'name_title_timestamp' ) ); + if ( $s === false ) { + wfDebug( 'Unable to load ' . $this->mNewPage->getPrefixedDBkey . " from old\n" ); + return false; + } } else { $s = $dbr->getArray( 'old', array( 'old_namespace','old_title','old_timestamp','old_text','old_flags','old_user_text','old_comment'), array( 'old_id' => $this->mOldid ), $fname ); - } - if ( $s === false ) { - return false; + if ( $s === false ) { + wfDebug( "Unable to load old_id {$this->mOldid}\n" ); + return false; + } } $this->mOldPage = Title::MakeTitle( $s->old_namespace, $s->old_title ); $this->mOldtext = Article::getRevisionText( $s ); $t = $wgLang->timeanddate( $s->old_timestamp, true ); - $this->mOldtitle = wfMsg( "revisionasof", $t ); + $oldLink = $this->mOldPage->getLocalUrl ('oldid=' . $this->mOldid); + $this->mOldtitle = "" . wfMsg( 'revisionasof', $t ) . ''; $this->mOldUser = $s->old_user_text; $this->mOldComment = $s->old_comment; @@ -204,13 +335,17 @@ cellpadding='0' cellspacing='4px' class='diff'> define('USE_ASSERTS', function_exists('assert')); +/** + * @todo document + * @package MediaWiki + */ class _DiffOp { var $type; var $orig; var $closing; function reverse() { - trigger_error("pure virtual", E_USER_ERROR); + trigger_error('pure virtual', E_USER_ERROR); } function norig() { @@ -222,6 +357,10 @@ class _DiffOp { } } +/** + * @todo document + * @package MediaWiki + */ class _DiffOp_Copy extends _DiffOp { var $type = 'copy'; @@ -237,6 +376,10 @@ class _DiffOp_Copy extends _DiffOp { } } +/** + * @todo document + * @package MediaWiki + */ class _DiffOp_Delete extends _DiffOp { var $type = 'delete'; @@ -250,6 +393,10 @@ class _DiffOp_Delete extends _DiffOp { } } +/** + * @todo document + * @package MediaWiki + */ class _DiffOp_Add extends _DiffOp { var $type = 'add'; @@ -263,6 +410,10 @@ class _DiffOp_Add extends _DiffOp { } } +/** + * @todo document + * @package MediaWiki + */ class _DiffOp_Change extends _DiffOp { var $type = 'change'; @@ -295,6 +446,7 @@ class _DiffOp_Change extends _DiffOp { * are my own. * * @author Geoffrey T. Dairiki + * @package MediaWiki * @access private */ class _DiffEngine @@ -680,6 +832,8 @@ class _DiffEngine /** * Class representing a 'diff' between two sequences of strings. + * @todo document + * @package MediaWiki */ class Diff { @@ -809,15 +963,16 @@ class Diff } $lcs = $this->lcs(); - trigger_error("Diff okay: LCS = $lcs", E_USER_NOTICE); + trigger_error('Diff okay: LCS = '.$lcs, E_USER_NOTICE); } } /** * FIXME: bad name. + * @todo document + * @package MediaWiki */ -class MappedDiff -extends Diff +class MappedDiff extends Diff { /** * Constructor. @@ -873,6 +1028,8 @@ extends Diff * This class formats the diff in classic diff format. * It is intended that this class be customized via inheritance, * to obtain fancier outputs. + * @todo document + * @package MediaWiki */ class DiffFormatter { @@ -966,7 +1123,7 @@ class DiffFormatter elseif ($edit->type == 'change') $this->_changed($edit->orig, $edit->closing); else - trigger_error("Unknown edit type", E_USER_ERROR); + trigger_error('Unknown edit type', E_USER_ERROR); } $this->_end_block(); } @@ -1007,10 +1164,10 @@ class DiffFormatter } function _added($lines) { - $this->_lines($lines, ">"); + $this->_lines($lines, '>'); } function _deleted($lines) { - $this->_lines($lines, "<"); + $this->_lines($lines, '<'); } function _changed($orig, $closing) { @@ -1028,6 +1185,10 @@ class DiffFormatter define('NBSP', ' '); // iso-8859-x non-breaking space. +/** + * @todo document + * @package MediaWiki + */ class _HWLDF_WordAccumulator { function _HWLDF_WordAccumulator () { $this->_lines = array(); @@ -1038,11 +1199,12 @@ class _HWLDF_WordAccumulator { function _flushGroup ($new_tag) { if ($this->_group !== '') { - if ($this->_tag == 'mark') - $this->_line .= ''.$this->_group.''; - else - $this->_line .= $this->_group; - } + if ($this->_tag == 'mark') + $this->_line .= '' . + htmlspecialchars ( $this->_group ) . ''; + else + $this->_line .= htmlspecialchars ( $this->_group ); + } $this->_group = ''; $this->_tag = $new_tag; } @@ -1050,7 +1212,10 @@ class _HWLDF_WordAccumulator { function _flushLine ($new_tag) { $this->_flushGroup($new_tag); if ($this->_line != '') - $this->_lines[] = $this->_line; + array_push ( $this->_lines, $this->_line ); + else + # make empty lines visible by inserting an NBSP + array_push ( $this->_lines, NBSP ); $this->_line = ''; } @@ -1063,7 +1228,6 @@ class _HWLDF_WordAccumulator { if ($word == '') continue; if ($word[0] == "\n") { - $this->_group .= NBSP; $this->_flushLine($tag); $word = substr($word, 1); } @@ -1078,6 +1242,10 @@ class _HWLDF_WordAccumulator { } } +/** + * @todo document + * @package MediaWiki + */ class WordLevelDiff extends MappedDiff { function WordLevelDiff ($orig_lines, $closing_lines) { @@ -1090,8 +1258,6 @@ class WordLevelDiff extends MappedDiff } function _split($lines) { - // FIXME: fix POSIX char class. -# if (!preg_match_all('/ ( [^\S\n]+ | [[:alnum:]]+ | . ) (?: (?!< \n) [^\S\n])? /xs', if (!preg_match_all('/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs', implode("\n", $lines), $m)) { @@ -1127,7 +1293,8 @@ class WordLevelDiff extends MappedDiff /** * Wikipedia Table style diff formatter. - * + * @todo document + * @package MediaWiki */ class TableDiffFormatter extends DiffFormatter { @@ -1137,8 +1304,8 @@ class TableDiffFormatter extends DiffFormatter } function _block_header( $xbeg, $xlen, $ybeg, $ylen ) { - $l1 = wfMsg( "lineno", $xbeg ); - $l2 = wfMsg( "lineno", $ybeg ); + $l1 = wfMsg( 'lineno', $xbeg ); + $l2 = wfMsg( 'lineno', $ybeg ); $r = '\n" . '\n"; @@ -1153,39 +1320,40 @@ class TableDiffFormatter extends DiffFormatter function _end_block() { } - function _lines( $lines, $prefix=' ', $color="white" ) { + function _lines( $lines, $prefix=' ', $color='white' ) { } + # HTML-escape parameter before calling this function addedLine( $line ) { - return ''; + return ""; } + # HTML-escape parameter before calling this function deletedLine( $line ) { - return ''; + return ""; } - function emptyLine() { - return ''; + # HTML-escape parameter before calling this + function contextLine( $line ) { + return ""; } - function contextLine( $line ) { - return ''; + function emptyLine() { + return ''; } - function _added($lines) { + function _added( $lines ) { global $wgOut; foreach ($lines as $line) { $wgOut->addHTML( '' . $this->emptyLine() . - $this->addedLine( $line ) . "\n" ); + $this->addedLine( htmlspecialchars ( $line ) ) . "\n" ); } } function _deleted($lines) { global $wgOut; foreach ($lines as $line) { - $wgOut->addHTML( '' . $this->deletedLine( $line ) . + $wgOut->addHTML( '' . $this->deletedLine( htmlspecialchars ( $line ) ) . $this->emptyLine() . "\n" ); } } @@ -1193,8 +1361,9 @@ class TableDiffFormatter extends DiffFormatter function _context( $lines ) { global $wgOut; foreach ($lines as $line) { - $wgOut->addHTML( '' . $this->contextLine( $line ) . - $this->contextLine( $line ) . "\n" ); + $wgOut->addHTML( '' . + $this->contextLine( htmlspecialchars ( $line ) ) . + $this->contextLine( htmlspecialchars ( $line ) ) . "\n" ); } } @@ -1204,12 +1373,18 @@ class TableDiffFormatter extends DiffFormatter $del = $diff->orig(); $add = $diff->closing(); + # Notice that WordLevelDiff returns HTML-escaped output. + # Hence, we will be calling addedLine/deletedLine without HTML-escaping. + while ( $line = array_shift( $del ) ) { $aline = array_shift( $add ); $wgOut->addHTML( '' . $this->deletedLine( $line ) . - $this->addedLine( $aline ) . "\n" ); + $this->addedLine( $aline ) . "\n" ); + } + foreach ($add as $line) { # If any leftovers + $wgOut->addHTML( '' . $this->emptyLine() . + $this->addedLine( $line ) . "\n" ); } - $this->_added( $add ); # If any leftovers } }
-{$otitle} -{$ntitle}
'.$l1."'.$l2."
+' . - $line.'+{$line}-' . - $line.'-{$line}  {$line} '.$line.'