<?php if (!defined('PmWiki')) exit();
/*
	This script defines routines for displaying page revisions inline style.

	Modified from Anno's version 2008-01-17 by Eemeli Aro
*/

$RecipeInfo['InlineDiff']['Version'] = 'EA-2009-06-09';

SDV($DiffShow['source'],(@$_REQUEST['source']=='y')?'y':'n');
if ($DiffShow['source']!='y') return;

SDV($DiffShow['minor'],(@$_REQUEST['minor']!='n')?'y':'n');
SDV($DiffMinorFmt, ($DiffShow['minor']=='y') ?
  "<a href='{\$PageUrl}?action=diff&amp;source=y&amp;minor=n'>$[Hide minor edits]</a>" :
  "<a href='{\$PageUrl}?action=diff&amp;source=y&amp;minor=y'>$[Show minor edits]</a>" );
SDV($DiffSourceFmt, "<a href='{\$PageUrl}?action=diff&amp;source=n&amp;minor=".$DiffShow['minor']."'>$[Show changes to output]</a>");
SDV($PageDiffFmt,"<h2 class='wikiaction'>$[{\$FullName} History]</h2>
  <p>$DiffMinorFmt - $DiffSourceFmt</p>
  ");

SDV($DiffAddFmt['c'],"
        <div class='difftype'>$[Changed line \$DiffLines:]</div>
        <div class='diffadd'>");

SDV($HandleDiffFmt, array(
	&$PageStartFmt,
	&$PageDiffFmt,"<div id='wikidiff'>", 'function:PrintDiffInline', '</div>',
	&$PageEndFmt ));

SDV($HTMLStylesFmt['diffinline'], "
	.diffmarkup del { background:#fcc; }
	.diffmarkup ins { background:#cfc; }");


function PrintDiffInline($pagename) {
  global $DiffShow,$DiffStartFmt,$TimeFmt,$DiffDelFmt,$DiffAddFmt,
    $DiffEndDelAddFmt,$DiffEndFmt,$DiffRestoreFmt,$FmtV, $LinkFunctions, $FarmD;
  $page = ReadPage($pagename);
  if (!$page) return;
  krsort($page); reset($page);
  $lf = $LinkFunctions;
  $LinkFunctions['http:'] = 'LinkSuppress';
  $LinkFunctions['https:'] = 'LinkSuppress';
  foreach($page as $k=>$v) {
    if (!preg_match("/^diff:(\d+):(\d+):?([^:]*)/",$k,$match)) continue;
    $diffclass = $match[3];
    if ($diffclass=='minor' && $DiffShow['minor']!='y') continue;
    $diffgmt = $match[1]; $FmtV['$DiffTime'] = strftime($TimeFmt,$diffgmt);
    $diffauthor = @$page["author:$diffgmt"];
    if (!$diffauthor) @$diffauthor=$page["host:$diffgmt"];
    if (!$diffauthor) $diffauthor="unknown";
    $FmtV['$DiffChangeSum'] = htmlspecialchars(@$page["csum:$diffgmt"]);
    $FmtV['$DiffHost'] = @$page["host:$diffgmt"];
    $FmtV['$DiffAuthor'] = $diffauthor;
    $FmtV['$DiffId'] = $k;
    echo FmtPageName($DiffStartFmt,$pagename);
    $difflines = explode("\n",$v."\n");
    $in=array(); $out=array(); $dtype='';
    foreach($difflines as $d) {
      if ($d>'') {
        if ($d[0]=='-' || $d[0]=='\\') continue;
        if ($d[0]=='<') { $out[]=substr($d,2); continue; }
        if ($d[0]=='>') { $in[]=substr($d,2); continue; }
      }
      if (preg_match("/^(\\d+)(,(\\d+))?([adc])(\\d+)(,(\\d+))?/",
          $dtype,$match)) {
        if (@$match[7]>'') {
          $lines='lines';
          $count=$match[1].'-'.($match[1]+$match[7]-$match[5]);
        } elseif ($match[3]>'') {
          $lines='lines'; $count=$match[1].'-'.$match[3];
        } else { $lines='line'; $count=$match[1]; }
        $FmtV['$DiffLines'] = $count;
        switch($match[4]) {
          case 'a':
            $txt = str_replace('line',$lines,$DiffDelFmt['a']);
            echo FmtPageName($txt,$pagename),
              "<div class='diffmarkup'><del>",
              str_replace("\n","<br />",htmlspecialchars(join("\n",$in))),
              "</del></div>";
            break;
          case 'd':
            $txt = str_replace('line',$lines,$DiffAddFmt['d']);
            echo FmtPageName($txt,$pagename),
              "<div class='diffmarkup'><ins>",
              str_replace("\n","<br />",htmlspecialchars(join("\n",$out))),
              "</ins></div>";
            break;
          case 'c':
            $txt = str_replace('line',$lines,$DiffAddFmt['c']);
            echo FmtPageName($txt,$pagename);
            include_once("$FarmD/cookbook/pagerevinline/Text/Diff.php");
            include_once("$FarmD/cookbook/pagerevinline/Text/Diff/Renderer/inline.php");
            $diff = &new Text_Diff($in, $out);
            $renderer = &new Text_Diff_Renderer_inline();
            echo "<div class='diffmarkup'>",
              str_replace("\n","<br />",$renderer->render($diff)),
              "</div>";
            break;
        }
        echo FmtPageName($DiffEndDelAddFmt,$pagename);
      }
      $in=array(); $out=array(); $dtype=$d;
    }
    echo FmtPageName($DiffEndFmt,$pagename);
    echo FmtPageName($DiffRestoreFmt,$pagename);
  }
  $LinkFunctions = $lf;
}