1<?php
2/**
3 * Plugin Color: Sets new colors for text and background.
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Myron Turner <turnermm02@shaw.ca>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once(DOKU_PLUGIN.'syntax.php');
14if(!defined('DW_COMMITS')) define('DW_COMMITS',DOKU_INC.'lib/plugins/dwcommits/');
15
16/**
17 * All DokuWiki plugins to extend the parser/rendering mechanism
18 * need to inherit from this class
19 */
20class syntax_plugin_dwcommits extends DokuWiki_Syntax_Plugin {
21
22 private $helper;
23 private $db;
24 private $output;
25    function __construct() {
26        $this->helper =& plugin_load('helper', 'dwcommits');
27
28    }
29
30    /**
31     * return some info
32     */
33    function getInfo(){
34
35        return array(
36            'author' => 'Myron Turner',
37            'email'  => 'turnermm02@shaw.ca',
38            'date'   => '20011-02-19',
39            'name'   => 'dwcommits Syntax Plugin',
40            'desc'   => 'Output git database query',
41            'url'    =>  'http://www.dokuwiki.org/plugin:dwcommits',
42        );
43    }
44
45
46    function getType(){ return 'container'; }
47    function getAllowedTypes() { return array('formatting', 'substition', 'disabled'); }
48    function getSort(){ return 158; }
49    function getPType(){ return 'block';  }
50
51    function connectTo($mode) {
52      $this->Lexer->addEntryPattern('~~DWCOMMITS.*?',$mode,'plugin_dwcommits');
53      $this->Lexer->addSpecialPattern('{{dwcommits_INF}}',$mode,'plugin_dwcommits');
54
55    }
56    function postConnect() { $this->Lexer->addExitPattern('DWCOMMITS~~','plugin_dwcommits'); }
57
58
59    /**
60     * Handle the match
61     */
62    function handle($match, $state, $pos, Doku_Handler $handler){
63        switch ($state) {
64          case DOKU_LEXER_ENTER :
65                return array($state, "");
66
67          case DOKU_LEXER_UNMATCHED :
68                    if(!$this->parse_match($match) ) {
69                      return array($state, ' db parsing failed ');
70                    }
71                    return array($state, $match);
72          case DOKU_LEXER_EXIT :
73                    return array($state, '');
74
75         case DOKU_LEXER_SPECIAL :
76               $this->db_data();
77               return array($state,'');
78
79        }
80
81        return array();
82    }
83
84    /**
85     * Create output
86     */
87    function render($mode, Doku_Renderer $renderer, $data) {
88        if($mode == 'xhtml'){
89            $this->expire_cache();
90            list($state,$match) = $data;
91            switch ($state) {
92               case DOKU_LEXER_SPECIAL:
93                  $renderer->doc .= '<p><pre>' . $this->output . '</pre></p>'; break;
94                   break;
95
96              case DOKU_LEXER_ENTER :
97                $renderer->doc .= "<p>"; break;
98                break;
99
100              case DOKU_LEXER_UNMATCHED :
101               $renderer->doc .=  $this->output;
102              case DOKU_LEXER_EXIT :
103                $renderer->doc .= "</p>";
104                break;
105            }
106            return true;
107        }
108        return false;
109    }
110
111   function expire_cache() {
112        global $ID;
113        $data = array('cache' => 'expire');  // the metadata being added
114        $render = false;                     // no need to re-render metadata now
115        $persistent = false;                 // this change doesn't need to persist passed the next metadata render.
116        p_set_metadata($ID, $data, $render, $persistent);
117   }
118
119   function parse_match($match) {
120      $this->output = "";
121      $result = array();
122      $matches = explode("\n", trim($match));
123      foreach($matches as $entry)  {
124           list($field,$val) = explode(':',$entry);
125           $field = trim($field);
126           $val = trim($val);
127           if(preg_match('/^\s*#/',$field)) continue;
128           switch($field) {
129            	case 'DATABASE':
130                      /*
131                       This call to setup_syntax() will give,for instance:
132                       https://github.com/turnermm/fckgLite slot=url2
133                      */
134                      $remote_url = $this->helper->setup_syntax($val);
135                      $remote_url .= " db=" . $val;
136                       $val = "";
137            	        break;
138            	case 'TERM_1':
139                        $field = 'terms_1';
140
141            	        break;
142            	case 'AND_OR_TERM_2':
143                        $field = 'OP_1';
144                        if(!$val) $val = 'OR';
145
146            	        break;
147            	case 'TERM_2':
148                        $field = 'terms_2';
149
150            	        break;
151            	case 'AND_OR_AUTHOR':
152                        $field = 'OP_2';
153                        if(!$val) $val = 'AND';
154            	        break;
155            	case 'AUTHOR':
156                        $field = 'author';
157            	        break;
158            	case 'DATE_1':
159                        $field = 'd1';
160            	        break;
161            	case 'DATE_2':
162                        $field =  'd2';
163            	        break;
164            	case 'BRANCH':
165                         $field = 'branch';
166            	        break;
167           }
168                if($val) $result[$field]=$val;
169      }
170           $this->db =  $this->helper->_getDB();
171
172           list($arr,$q) = $this->helper->select_all($result);
173           if($arr) {
174               $this->output = "<b>Query: $q</b><br />";
175               $this->output .=  $this->helper->format_result_table($arr,$result);
176               return true;
177           }
178
179
180        return false;
181
182
183   }
184
185
186function db_data() {
187     $this->output = "";
188     $filename = DW_COMMITS . 'db/dbnames.ser';
189
190     $inf_str = file_get_contents ($filename);
191     $inf = unserialize($inf_str);
192
193    foreach($inf as $val=>$entry) {
194       if(preg_match('/dwcommits_(\d+)/',$entry, $matches)) {
195           $this->output .= "<b>Database File:</b> $entry\n";
196           if(($url = $this->dwc_element('url', $matches[1], $inf))!== false) {
197               $this->output .= "<b>Remote URL:</b> $url\n";
198           }
199
200           $git = $this->dwc_element('git', $matches[1], $inf);
201           if($git !== false) {
202             if(!file_exists($git)) {
203                $this->output .= "<b>Local Git Missing:</b>  $git\n";
204             }
205             else $this->output .= "<b>Local Git:</b> $git\n";
206
207           }
208          $this->output .= "\n";
209       }
210    }
211
212 }
213
214 function dwc_element($prefix, $suffix, $ar) {
215    $inx = $prefix . $suffix;
216    if(isset($ar[$inx])) {
217         return $ar[$inx];
218    }
219    return false;
220
221 }
222
223
224}
225?>
226