<?php if (!defined('PmWiki')) exit();
/*
  Copyright 2004-2017 Patrick R. Michaud (pmichaud@pobox.com)
  This file is expirediff.php; you can redistribute it and/or modify
  it under the terms of the GNU General Public License version 2 
  as published by the Free Software Foundation.
  
  Changes 2005, by Karl Loncarek (Klonk) www.loncarek.de
  Changes 2015, by Petko Yotov www.pmwiki.org/petko
  
  See documentation for this recipe at:
    http://www.pmwiki.org/wiki/Cookbook/ExpireDiff 

  Script maintained by Petko Yotov www.pmwiki.org/petko
*/

$RecipeInfo['ExpireDiff']['Version'] = '20180109';

# add "?action=expirediff"
SDV($HandleActions['expirediff'],'HandleExpireDiff');
SDV($HandleAuth['expirediff'], 'edit');

if(IsEnabled($EnableNoHistoryDirective, 0))
  Markup('nohistory', 'directives', '/\\(:nohistory:\\)/i', 'DropHistory');

function DropHistory() {
  global $DiffKeepDays, $DiffKeepNum;
  $DiffKeepDays = $DiffKeepNum = -1;
  return '';
}

function HandleExpireDiff($pagename, $auth) {
  global $WikiDir, $Now;
  Lock(2);
  $page = RetrieveAuthPage($pagename, $auth);
  if (!$page) { Abort("?cannot get $pagename"); }
  $keepdays = intval(@$_REQUEST['keepdays']);
  $keepgmt = $Now - $keepdays * 86400;
  $keys = array_keys($page);
  foreach($keys as $k) 
    if (preg_match("/^\\w+:(\\d+)/", $k, $match)) 
      if ($match[1] < $keepgmt) unset($page[$k]);
  $WikiDir->delete($pagename);
  WritePage($pagename, $page);
  Redirect($pagename);
  exit;
}


## -- For expiring all pages at once; first parameter is the desired group,
##                                    second parameter tells whether a backup should be done,
##                                    third parameter tells which DIFFs should be kept.
function ExpireDiffAll($group='',$backup=1,$keepdays=0) {
  global $WikiDir,$Now;
  $pagelist = $WikiDir->ls();
  $pagelist = array_unique($pagelist);
  sort($pagelist);
  foreach($pagelist as $k=>$p)
      if (!(($group==substr($p,0,strpos($p,"."))) or ($group==''))) {
        unset($pagelist[$k]);
      } 
  $pagecount = count($pagelist);

  echo "
    <html>
    <head>
    <title>Expire pages (remove DIFFs)</title>
    </head>
    <body>
    <h2>Expire existing pages (remove DIFFs)</h2>
    <p>I'm now removing the DIFFs from the files (pages) you have stored in your wiki.
    When this is finished you can get rid of the <tt>ExpireDiff(...);</tt> line in your 
    local/config.php</p>";

  if ($pagelist) {
    foreach($pagelist as $p) {
      echo "<li>Expiring $p</li>\n";
      $page = ReadPage($p);
      $keepgmt = $Now - $keepdays * 86400;
      $keys = array_keys($page);
      foreach($keys as $k) 
        if (preg_match("/^\\w+:(\\d+)/", $k, $match)) 
          if ($match[1] < $keepgmt) unset($page[$k]);
      if ($backup==1) {
        $WikiDir->delete($p);
      };
      WritePage($p,$page);
    }
  }
  echo "<p>Removed DIFFs from ", $pagecount, " pages.</p>\n";
  echo "<p>Now you can get rid of the <tt>ExpireDiff(...);</tt> line in your 
    local/config.php</p>
    </body></html>\n";
  exit(0);
}