<?php
/**
 * DokuWiki Plugin outdated (Helper Component)
 *
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
 * @author  Andreas Böhler <dev@aboehler.at>
 */

// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();

class helper_plugin_outdated extends DokuWiki_Plugin {

    /**
     * checks if an id is within one of the namespaces in $namespace_list
     * taken from the publish plugin
     *
     * @param string $namespace_list
     * @param string $id
     *
     * @return bool
     */
    function in_namespace($namespace_list, $id) {
        // PHP apparantly does not have closures -
        // so we will parse $valid ourselves. Wasteful.
        $namespace_list = preg_split('/\s+/', $namespace_list);
        //if(count($valid) == 0) { return true; }//whole wiki matches
        if((count($namespace_list)==1) and ($namespace_list[0]=="")) { return true; }//whole wiki matches
        $id = trim($id, ':');
        $id = explode(':', $id);

        // Check against all possible namespaces
        foreach($namespace_list as $namespace) {
            $namespace = explode(':', $namespace);
            $current_ns_depth = 0;
            $total_ns_depth = count($namespace);
            $matching = true;

            // Check each element, untill all elements of $v satisfied
            while($current_ns_depth < $total_ns_depth) {
                if(
                    !isset($id[$current_ns_depth]) ||
                    !isset($namespace[$current_ns_depth]) ||
                    $namespace[$current_ns_depth] != $id[$current_ns_depth]
                ) {
                    // not a match
                    $matching = false;
                    break;
                }
                $current_ns_depth += 1;
            }
            if($matching) { return true; } // a match
        }
        return false;
    }

    function getLatestRevision() {
        global $INFO;
        return $INFO['meta']['date']['modified'];
    }

    function isActive($id = null) {
        if ($id == null) {
            global $ID;
            $id = $ID;
        }
        if (!$this->in_namespace($this->getConf('outdated_namespaces'), $id)) {
            return false;
        }

        return true;
    }

    function getOutdatedMessageForCurrentPage() {
        global $INFO;
        if(isset($INFO['meta']['plugin_outdated'])) {
            return $INFO['meta']['plugin_outdated']['message'];
        } else {
            return $this->getConf('outdated_message');
        }
    }

    function getTimedeltaForCurrentPage() {
        global $INFO;
        if(isset($INFO['meta']['plugin_outdated'])) {
            return $INFO['meta']['plugin_outdated']['timedelta'];
        } else {
            return $this->getConf('outdated_timedelta');
        }
    }

    function isCurrentRevisionOutdated() {
        $ts = $this->getLatestRevision();
        $now = time();
        $delta = $this->getTimedeltaForCurrentPage();
        if($now - $ts > $delta) {
            return true;
        }
        return false;
    }


}

