<?php
-# See diff.doc
+/**
+ * See diff.doc
+ * @package MediaWiki
+ */
+/**
+ * @todo document
+ * @package MediaWiki
+ */
class DifferenceEngine {
/* private */ var $mOldid, $mNewid;
- /* private */ var $mOldtitle, $mNewtitle;
+ /* private */ var $mOldtitle, $mNewtitle, $mPagetitle;
/* private */ var $mOldtext, $mNewtext;
/* private */ var $mOldUser, $mNewUser;
/* private */ var $mOldComment, $mNewComment;
function DifferenceEngine( $old, $new, $rcid = 0 )
{
- $this->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, $wgUseRCPatrol;
- $fname = "DifferenceEngine::showDiffPage";
+ $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;
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();
$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 = " <strong>[" . $sk->makeKnownLinkObj( $wgTitle, wfMsg( "rollbacklink" ),
- "action=rollback&from=" . urlencode($this->mNewUser) ) . "]</strong>";
+ $rollback = ' <strong>[' . $sk->makeKnownLinkObj( $wgTitle, wfMsg( 'rollbacklink' ),
+ 'action=rollback&from=' . urlencode($this->mNewUser) ) . ']</strong>';
} else {
- $rollback = "";
+ $rollback = '';
}
- if ( $wgUseRCPatrol &&
- ($this->mRcidMarkPatrolled != 0 && $wgUser->getID() != 0 &&
- ( $wgUser->isSysop() || !$wgOnlySysopsCanPatrol ) ) )
+ 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 = "<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 );
- $wgOut->addHTML( "<hr /><h2>{$this->mNewtitle}</h2>\n" );
+ $wgOut->addHTML( "<hr /><h2>{$this->mPagetitle}</h2>\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 = "<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 );
{
global $wgOut, $wgUseExternalDiffEngine;
- $otext = str_replace( "\r\n", "\n", htmlspecialchars( $otext ) );
- $ntext = str_replace( "\r\n", "\n", htmlspecialchars( $ntext ) );
-
+ $wgOut->addHTML( "
+ <table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>
+ <tr>
+ <td colspan='2' width='50%' align='center' class='diff-otitle'>{$otitle}</td>
+ <td colspan='2' width='50%' align='center' class='diff-ntitle'>{$ntitle}</td>
+ </tr>
+ " );
- $wgOut->addHTML( "<table border='0' width='98%'
-cellpadding='0' cellspacing='4px' class='diff'><tr>
-<td colspan='2' width='50%' align='center' class='diff-otitle'>
-{$otitle}</td>
-<td colspan='2' width='50%' align='center' class='diff-ntitle'>
-{$ntitle}</td>
-</tr>\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 );
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 = "<a href='$newLink'>{$this->mPagetitle}</a>";
$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;
}
'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;
}
$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 = "<a href='$newLink'>{$this->mPagetitle}</a>";
$this->mNewUser = $s->old_user_text;
$this->mNewComment = $s->old_comment;
}
'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 = "<a href='$oldLink'>" . wfMsg( 'revisionasof', $t ) . '</a>';
$this->mOldUser = $s->old_user_text;
$this->mOldComment = $s->old_comment;
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() {
}
}
+/**
+ * @todo document
+ * @package MediaWiki
+ */
class _DiffOp_Copy extends _DiffOp {
var $type = 'copy';
}
}
+/**
+ * @todo document
+ * @package MediaWiki
+ */
class _DiffOp_Delete extends _DiffOp {
var $type = 'delete';
}
}
+/**
+ * @todo document
+ * @package MediaWiki
+ */
class _DiffOp_Add extends _DiffOp {
var $type = 'add';
}
}
+/**
+ * @todo document
+ * @package MediaWiki
+ */
class _DiffOp_Change extends _DiffOp {
var $type = 'change';
* are my own.
*
* @author Geoffrey T. Dairiki
+ * @package MediaWiki
* @access private
*/
class _DiffEngine
/**
* Class representing a 'diff' between two sequences of strings.
+ * @todo document
+ * @package MediaWiki
*/
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.
* 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
{
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();
}
}
function _added($lines) {
- $this->_lines($lines, ">");
+ $this->_lines($lines, '>');
}
function _deleted($lines) {
- $this->_lines($lines, "<");
+ $this->_lines($lines, '<');
}
function _changed($orig, $closing) {
define('NBSP', ' '); // iso-8859-x non-breaking space.
+/**
+ * @todo document
+ * @package MediaWiki
+ */
class _HWLDF_WordAccumulator {
function _HWLDF_WordAccumulator () {
$this->_lines = array();
function _flushGroup ($new_tag) {
if ($this->_group !== '') {
- if ($this->_tag == 'mark')
- $this->_line .= '<span class="diffchange">'.$this->_group.'</span>';
- else
- $this->_line .= $this->_group;
- }
+ if ($this->_tag == 'mark')
+ $this->_line .= '<span class="diffchange">' .
+ htmlspecialchars ( $this->_group ) . '</span>';
+ else
+ $this->_line .= htmlspecialchars ( $this->_group );
+ }
$this->_group = '';
$this->_tag = $new_tag;
}
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 = '';
}
if ($word == '')
continue;
if ($word[0] == "\n") {
- $this->_group .= NBSP;
$this->_flushLine($tag);
$word = substr($word, 1);
}
}
}
+/**
+ * @todo document
+ * @package MediaWiki
+ */
class WordLevelDiff extends MappedDiff
{
function WordLevelDiff ($orig_lines, $closing_lines) {
}
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)) {
/**
* Wikipedia Table style diff formatter.
- *
+ * @todo document
+ * @package MediaWiki
*/
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 = '<tr><td colspan="2" align="left"><strong>'.$l1."</strong></td>\n" .
'<td colspan="2" align="left"><strong>'.$l2."</strong></td></tr>\n";
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 '<td>+</td><td class="diff-addedline">' .
- $line.'</td>';
+ return "<td>+</td><td class='diff-addedline'>{$line}</td>";
}
+ # HTML-escape parameter before calling this
function deletedLine( $line ) {
- return '<td>-</td><td class="diff-deletedline">' .
- $line.'</td>';
+ return "<td>-</td><td class='diff-deletedline'>{$line}</td>";
}
- function emptyLine() {
- return '<td colspan="2"> </td>';
+ # HTML-escape parameter before calling this
+ function contextLine( $line ) {
+ return "<td> </td><td class='diff-context'>{$line}</td>";
}
- function contextLine( $line ) {
- return '<td> </td><td class="diff-context">'.$line.'</td>';
+ function emptyLine() {
+ return '<td colspan="2"> </td>';
}
- function _added($lines) {
+ function _added( $lines ) {
global $wgOut;
foreach ($lines as $line) {
$wgOut->addHTML( '<tr>' . $this->emptyLine() .
- $this->addedLine( $line ) . "</tr>\n" );
+ $this->addedLine( htmlspecialchars ( $line ) ) . "</tr>\n" );
}
}
function _deleted($lines) {
global $wgOut;
foreach ($lines as $line) {
- $wgOut->addHTML( '<tr>' . $this->deletedLine( $line ) .
+ $wgOut->addHTML( '<tr>' . $this->deletedLine( htmlspecialchars ( $line ) ) .
$this->emptyLine() . "</tr>\n" );
}
}
function _context( $lines ) {
global $wgOut;
foreach ($lines as $line) {
- $wgOut->addHTML( '<tr>' . $this->contextLine( $line ) .
- $this->contextLine( $line ) . "</tr>\n" );
+ $wgOut->addHTML( '<tr>' .
+ $this->contextLine( htmlspecialchars ( $line ) ) .
+ $this->contextLine( htmlspecialchars ( $line ) ) . "</tr>\n" );
}
}
$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( '<tr>' . $this->deletedLine( $line ) .
- $this->addedLine( $aline ) . "</tr>\n" );
+ $this->addedLine( $aline ) . "</tr>\n" );
+ }
+ foreach ($add as $line) { # If any leftovers
+ $wgOut->addHTML( '<tr>' . $this->emptyLine() .
+ $this->addedLine( $line ) . "</tr>\n" );
}
- $this->_added( $add ); # If any leftovers
}
}