1<?php
2/**
3  * @author     Myron Turner <turnermm02@shaw.ca>
4 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
5 */
6if(!defined('DOKU_INC')) die();
7if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
8if(!defined('QUICK_STATS')) define ('QUICK_STATS',DOKU_PLUGIN . 'quickstats/');
9require_once('GEOIP/ccArraysDat.php');
10class helper_plugin_quickstats extends Dokuwiki_Plugin {
11    private $isCached = false;
12    private $script_file;
13    private $cache;
14    private $cc_arrays;
15
16
17    function getMethods(){
18        $result = array();
19
20        $result[] = array(
21                 'name'   => 'is_inCache',
22                 'desc'   => 'is file cached',
23                 'params' => array(),
24                 'return' => array('result' => 'bool')
25                );
26
27        $result[] = array(
28                 'name'   => 'writeCache',
29                 'desc'   => 'write new cache item',
30                 'params' => array(),
31                 'return' => array('result' => 'bool')
32                );
33
34        $result[] = array(
35                 'name'   => 'checkWikiFile',
36                 'desc'   => 'does wiki file have quickstats syntax line',
37                 'params' => array(),
38                 'return' => array('result' => 'bool')
39                );
40        $result[] = array(
41                 'name'   => 'is_inConfList',
42                 'desc'   => 'is file in config options list',
43                 'params' => array(),
44                 'return' => array('result' => 'bool')
45                );
46	}
47
48    function __construct() {
49            $this->script_file = metaFN('quickstats:cache', '.ser');
50            $this->cache = unserialize(io_readFile($this->script_file,false));
51            if(!$this->cache) $this->cache = array();
52            $this->cc_arrays = new ccArraysDat();
53    }
54
55	function msg($text) {
56	    if(is_array($text)) {
57		   $text = '<pre>' . print_r($text,true) . '</pre>';
58		}
59		msg($text,2);
60	}
61
62    function get_cc_arrays() {
63        return $this->cc_arrays;
64    }
65    function is_inCache($id) {
66  //     msg('<pre>'  .  print_r($this->cache,true) .  '</pre>',2);
67         $md5 = md5($id);
68         if(isset($this->cache[$md5])) return true;
69         return false;
70    }
71
72	function pruneCache($confirms,$deletions) {
73	     $confirms = explode(',',$confirms);
74		 if($deletions) {
75		   $diff = array_intersect($confirms,array_keys($deletions));
76		 }
77		 else $diff = $confirms;
78		 //$this->msg($diff);
79	      foreach($diff as $del) {
80		      unset($this->cache[$del]);
81			  io_saveFile($this->script_file,serialize($this->cache));
82		  }
83		  return $this->cache;
84	}
85
86
87    function writeCache($id) {
88         if(!$this->is_inCache($id)) {
89            $this->cache[md5($id)] = $id;
90            io_saveFile($this->script_file,serialize($this->cache));
91             return true;
92         }
93         return false;
94    }
95
96    function is_inConfList($id) {
97         $sortable_ns = @$this->getConf('sortable_ns');
98        if(isset($sortable_ns) && $sortable_ns) {
99            $ns_choices = explode(',',$sortable_ns);
100            foreach($ns_choices as $ns) {
101              $ns = trim($ns);
102              if(preg_match("/$ns/",$id))  {
103                       return true;
104             }
105            }
106            return false;
107        }
108
109    }
110    function checkWikiFile() {
111         $file_name = wikiFN($ID);
112         $lines = file($file_name, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
113
114             foreach ($lines as $line) {
115                if(strpos($line,'~~QUICKSTATS') !== false) {
116                    return true;
117                }
118            }
119
120            return false;
121    }
122
123     function getCache() {
124         return  $this->cache;
125     }
126    function metaFilePath($directory=false) {
127       if($directory) {
128            return preg_replace('/quickstats.*$/','quickstats/',$this->script_file);
129       }
130        return $this->script_file;
131    }
132 }
133
134