");
SDV($DiffDelFmt['c'],"
        
\$[Changed line \$DiffLines from:]
        ");
SDV($DiffAddFmt['d'],"
        
\$[Added line \$DiffLines:]
        ");
SDV($DiffAddFmt['c'],"
        $[to:]
        ");
SDV($DiffEndDelAddFmt,"
");
SDV($DiffEndFmt,"
");
SDV($DiffRestoreFmt,"
      
");
SDV($HandleActions['diff'], 'HandleDiff');
SDV($HandleAuth['diff'], 'read');
SDV($ActionTitleFmt['diff'], '| $[History]');
SDV($HTMLStylesFmt['diff'], "
  .diffbox { width:570px; border-left:1px #999999 solid; margin-top:1.33em; }
  .diffauthor { font-weight:bold; }
  .diffchangesum { font-weight:bold; }
  .difftime { font-family:verdana,sans-serif; font-size:66%;
    background-color:#dddddd; }
  .difftype { clear:both; font-family:verdana,sans-serif;
    font-size:66%; font-weight:bold; }
  .diffadd { border-left:5px #99ff99 solid; padding-left:5px; }
  .diffdel { border-left:5px #ffff99 solid; padding-left:5px; }
  .diffrestore { clear:both; font-family:verdana,sans-serif;
    font-size:66%; margin:1.5em 0px; }
  .diffmarkup { font-family:monospace; } ");
function PrintDiff($pagename) {
  global $DiffShow,$DiffStartFmt,$TimeFmt,$DiffDelFmt,$DiffAddFmt,
    $DiffEndDelAddFmt,$DiffEndFmt,$DiffRestoreFmt,$FmtV, $LinkFunctions,
    $DiffCountPerPage;
  SDV($DiffCountPerPage, 0);// 0 means unlimited
  $diffstart = intval(@$_REQUEST['diffstart']);
  $page = ReadPage($pagename);
  if (!$page) return;
  krsort($page); reset($page);
  $lf = $LinkFunctions;
  $LinkFunctions['http:'] = 'LinkSuppress';
  $LinkFunctions['https:'] = 'LinkSuppress';
  $j=0;// counter for diffstart
  $diffmore=0; // are there more revisions?
  foreach($page as $k=>$v) {
    if (!preg_match("/^diff:(\d+):(\d+):?([^:]*)/",$k,$match)) continue;
    if($j++<$diffstart) continue;
    if($diffstart >=0 && $DiffCountPerPage != 0
       && $j > $diffstart+$DiffCountPerPage){$diffmore=1; break;}
    $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]; }
        if ($match[4]=='a' || $match[4]=='c') {
          $txt = str_replace('line',$lines,$DiffDelFmt[$match[4]]);
          $FmtV['$DiffLines'] = $count;
          echo FmtPageName($txt,$pagename);
          if ($DiffShow['source']=='y')
            echo "
",
              str_replace("\n","
",htmlspecialchars(join("\n",$in))),
              "
";
          else echo MarkupToHTML($pagename,
            preg_replace('/\\(:.*?:\\)/e',"Keep(PSS('$0'))",join("\n",$in)));
        }
        if ($match[4]=='d' || $match[4]=='c') {
          $txt = str_replace('line',$lines,$DiffAddFmt[$match[4]]);
          $FmtV['$DiffLines'] = $count;
          echo FmtPageName($txt,$pagename);
          if ($DiffShow['source']=='y')
            echo "
",
              str_replace("\n","
",htmlspecialchars(join("\n",$out))),
              "
";
          else echo MarkupToHTML($pagename,
            preg_replace('/\\(:.*?:\\)/e',"Keep(PSS('$0'))",join("\n",$out)));
        }
        echo FmtPageName($DiffEndDelAddFmt,$pagename);
      }
      $in=array(); $out=array(); $dtype=$d;
    }
    echo FmtPageName($DiffEndFmt,$pagename);
    echo FmtPageName($DiffRestoreFmt,$pagename);
  }
  $diffprevnext = "";
  if($diffstart>0) $diffprevnext .= "[[$pagename?action=diff&source={$DiffShow['source']}&minor={$DiffShow['minor']}&diffstart=".
  max(0, $diffstart-$DiffCountPerPage)."|←$[Newer changes] ]] ";
  if($diffstart>0 || $diffmore>0) $diffprevnext .= "[[$pagename?action=diff&source={$DiffShow['source']}&minor={$DiffShow['minor']}&diffstart=-1 | $[Show all] ]] ";
  if($diffmore>0) $diffprevnext .= "[[$pagename?action=diff&source={$DiffShow['source']}&minor={$DiffShow['minor']}&diffstart=".
  max(0, $j-1)." | $[Older changes]→]] ";
  if($DiffCountPerPage>0 && $diffprevnext) echo MarkupToHTML($pagename, "\n$diffprevnext\n");
  $LinkFunctions = $lf;
}
function HandleDiff($pagename, $auth='read') {
  global $HandleDiffFmt, $PageStartFmt, $PageDiffFmt, $PageEndFmt;
  $page = RetrieveAuthPage($pagename, $auth, true);
  if (!$page) { Abort("?cannot diff $pagename"); }
  PCache($pagename, $page);
  SDV($HandleDiffFmt,array(&$PageStartFmt,
    &$PageDiffFmt,"
", 'function:PrintDiff', '
',
    &$PageEndFmt));
  PrintFmt($pagename,$HandleDiffFmt);
}