<?php if (!defined('PmWiki')) exit();
# vim: set ts=4 sw=4 et:
##
##        File: Site.Reindex.php (must be renamed and placed in local dir)
##     Version: 2015-01-31
##      Status: alpha
##      Author: Peter Bowers
## Create Date: Jan 31, 2015
##   Copyright: 2015, Peter Bowers
##
## NOTE: For this to work it expects wiki.d/.reindex to be non-existent before
## starting the reindex. Then simply load the page with &reindex=1 in the url
## and wait through multiple loads (done automatically by redirect) until you 
## get the message that the reindexing process is complete.
##
## This program is free software; 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.
## http://www.gnu.org/copyleft/gpl.html
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##

$RecipeInfo['Reindex']['Version'] = '2015-01-31';

if ($_REQUEST['reindex']) {
    #global $PageIndexTime, $WikiDir, $FarmD;
    include_once("$FarmD/scripts/stdconfig.php");
    include_once("$FarmD/scripts/pagelist.php");

    SDV($ReindexFile, "$WorkDir/.reindex");
    #echo "DEBUG: Attempting to delete $ReindexFile<br />\n";
    #unlink($ReindexFile);

    set_time_limit(120);
    $PageIndexTime = 60;
    if (!file_exists($ReindexFile)) { // no .pageindex - start from scratch
        #echo "DEBUG: A<br />\n";
        $pagelist = $WikiDir->ls();
        sort($pagelist);
        file_put_contents($ReindexFile, implode("\n", $pagelist));
        fixperms($ReindexFile);
    } else { // we are assuming .pageindex has been created in order
        #echo "DEBUG: B<br />\n";
        $pagelist = explode("\n", file_get_contents($ReindexFile));
        $lastpage = '';
        $ifp = @fopen($PageIndexFile, 'r');
        if ($ifp) {
            while (!feof($ifp)) {
              $line = fgets($ifp, 4096);
              while (substr($line, -1, 1) != "\n" && !feof($ifp)) 
                $line .= fgets($ifp, 4096);
              $i = strpos($line, ':');
              if ($i === false) continue;
              $n = substr($line, 0, $i);
              if ($n > $lastpage) $lastpage = $n;
              else break;
            }
            fclose($ifp);
            for ($i = 0; $i < sizeof($pagelist); $i++)
                if ($pagelist[$i] >= $lastpage) break;
            if ($pagelist[$i] == $lastpage)
                $pagelist = array_slice($pagelist, $i+1);
        }
    }
    echo "DEBUG: count=".count($pagelist)."<br />\n";
    if (!count($pagelist)) {
        echo "Indexing complete. Deleting $ReindexFile<br />\n";
        if (file_exists($ReindexFile)) {
            fixperms($ReindexFile);
            unlink($ReindexFile); // for some reason this is giving err in windows
        }
    } else {
        PageIndexUpdate($pagelist);
        ob_flush(); flush();
        redirect($ScriptUrl.'?n='.$pagename.'&reindex=1');
    }
}