1<?php
2/**
3 * Plugin Skeleton: Displays "Hello World!"
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Myron Turner <turnermm02@shaw.ca>
7 */
8
9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once(DOKU_PLUGIN.'admin.php');
12
13/**
14 * All DokuWiki plugins to extend the admin function
15 * need to inherit from this class
16 */
17class admin_plugin_dwcommits extends DokuWiki_Admin_Plugin {
18    private $output = '';
19    private $submitted = false;
20    private $helper;
21    private $db;
22    private $current_page;
23
24    function __construct() {
25        $this->helper =& plugin_load('helper', 'dwcommits');
26        $this->db =  $this->helper->_getDB();
27        $this->helper->set_branches();
28        $this->helper->set_repros();
29//ini_set('display_errors',1);
30//ini_set('error_reporting',E_ALL);
31
32    }
33    /**
34     * return some info
35     */
36    function getInfo(){
37      return array(
38        'author' => 'Myron Turner',
39        'email'  => 'turnermm02@shaw.ca',
40        'date'   => '2015-10-01',
41        'name'   => 'admin plugin dwcommits',
42        'desc'   => 'demonstration dwcommits',
43        'url'    => 'http://www.dokuwiki.org/plugin:commits',
44      );
45    }
46
47    /**
48     * return sort order for position in admin menu
49     */
50    function getMenuSort() {
51      return 999;
52    }
53
54    /**
55     *  return a menu prompt for the admin menu
56     *  NOT REQUIRED - its better to place $lang['menu'] string in localised string file
57     *  only use this function when you need to vary the string returned
58     */
59
60    /**
61     * handle user request
62     */
63    function handle() {
64
65      if (!isset($_REQUEST['cmd'])) return;   // first time - nothing to do
66
67     $this->output = 'invalid';
68     $nov_11 = mktime(0,0,0,11,11,2010);
69
70    $dwc_Divs = array(
71        'init'=>"dcw_db_update",'update'=>"dcw_db_update",
72        'fetch'=>"dwc_git_advanced",'merge'=>"dwc_git_advanced",'commit'=>"dwc_git_advanced",'add'=>"dwc_git_advanced",
73        'status'=>"dcw_update_git", 'pull'=>"dcw_update_git",'remote_url'=>"dcw_update_git",
74        'branch'=>"dwc_repos_div",'repro'=>"dwc_repos_div",
75        'info'=>"dwc_info_div",
76        'query'=>"dwc_query_div",
77        'prune' =>"dwc_prune_div", 'restore'=>"dwc_prune_div",'prune_del'=>"dwc_prune_div"
78    );
79
80     if (!is_array($_REQUEST['cmd'])) return;
81
82      // verify valid values
83      switch (key($_REQUEST['cmd'])) {
84        case 'init' :
85           $start_timestamp = $this->get_timestamp($_REQUEST['d']);
86           if(!$start_timestamp) $start_timestamp = $nov_11;
87           $rows_done = $this->helper->recreate_table($start_timestamp);
88           $this->output = 'Initialized ' . $rows_done . ' rows';
89           break;
90        case 'fetch' :
91        case 'merge' :
92        case 'commit' :
93        case 'pull' :
94        case 'branch':
95        case 'remote_url':
96        case 'add':
97            $status = "";
98            $this->helper->update_commits(key($_REQUEST['cmd']));
99            $status = $this->helper->get_status_msg();
100            $this->output = $status;
101            break;
102
103        case 'update' :
104            $start_timestamp = $this->get_timestamp($_REQUEST['dup']);
105             if(!$start_timestamp){
106               $start_timestamp = $nov_11;
107               $this->output = 'date set to default';
108             }
109
110             $retv = $this->helper->populate($start_timestamp);
111             if(is_array($retv)) {
112               list($num,$recs) = $retv;
113               $this->output = "Records written to database: $num. Records in database: $recs.";
114             }
115            break;
116        case 'status' :
117            $status = "";
118            if(!$this->helper->get_status()) {
119              $status = $this->helper->get_status_msg();
120            }
121            else $status = $this->helper->get_status_msg();
122            $this->output = $status;
123            break;
124        case 'repro':
125           //path switched in helper constructor
126           $this->output = $this->getLang('repro_switched') . ':' . $_REQUEST['dwc__repro'];
127           break;
128        case 'query':
129            list($arr,$q) = $this->helper->select_all();
130            $this->output = "<b>$q</b><br />";
131            if($arr) {
132                if($_REQUEST['output_type'] == 'plain') {
133                 $this->output .= $this->helper->format_result_plain($arr);
134                }
135                else {
136                   $this->output .= $this->helper->format_result_table($arr);
137                }
138            }
139            else $this->output .= "no result";
140           break;
141        case 'set_remote_url':
142           $this->output = $this->helper->set_githubURL($_REQUEST['remote_url_name']);
143           break;
144        case 'prune':
145              $this->output = $this->helper->prune(false);
146            break;
147        case 'prune_del':
148              $this->output = $this->helper->prune(true);
149          break;
150        case 'restore':
151              $this->output = $this->helper->restore_backup();
152          break;
153
154      }
155
156    $this->current_page = $dwc_Divs[key($_REQUEST['cmd'])];
157
158  $this->submitted = "";
159  $this->submitted = $this->current_page . '<br />' . key($_REQUEST['cmd'])  . '<pre>' . print_r($_REQUEST,1) . '</pre>';
160    }
161
162    /**
163     * output appropriate html
164     */
165    function html() {
166
167      global $ID;
168      $date_str = $this->getConf('default_date');
169      if(isset($date_str)) {
170          list($month,$day,$year) = explode('-',$date_str);
171      }
172      else {
173          $month ='MM'; $day='DD'; $year='YYY';
174      }
175
176      /*  Navigation Bar  */
177      ptln('<DIV class="dwc_navbar">');
178      ptln('<table cellspacing="4">');
179      ptln('<tr><td><a href="javascript:dwc_toggle_div(\'dcw_db_update\'); void 0;">' . $this->getLang('sql_opts') . '</a>');
180      ptln('<td>&nbsp;&nbsp;<a href="javascript:dwc_toggle_div(\'dcw_update_git\'); void 0;">' . $this->getLang('git_opts') . '</a>');
181      ptln('<td>&nbsp;&nbsp;<a href="javascript:dwc_toggle_div(\'dwc_git_advanced\'); void 0;">' . $this->getLang('git_advanced_opts') . '</a>');
182      ptln('<td>&nbsp;&nbsp;<a href="javascript:dwc_toggle_div(\'dwc_repos_div\'); void 0;">' . $this->getLang('git_repos') . '</a>');
183      ptln('<td>&nbsp;&nbsp;<a href="javascript:dwc_toggle_div(\'dwc_query_div\'); void 0;">' . $this->getLang('git_query') . '</a>');
184      ptln('<td>&nbsp;&nbsp;<a href="javascript:dwc_toggle_div(\'dwc_prune_div\'); void 0;">' . $this->getLang('maintenance') . '</a>');
185
186      ptln('<td>&nbsp;&nbsp;<a href="javascript:dwc_toggle_info(\'dwc_info_div\'); void 0;">' . $this->getLang('git_info') . '</a>');
187      ptln('<td>&nbsp;&nbsp;<a href="javascript:dwc_close_all(); void 0;">' . $this->getLang('btn_close_all') . '</a>');
188      ptln('</table>');
189      ptln('</DIV>');
190      /*  Form  */
191      ptln('<form action="'.wl($ID).'" method="post">');
192
193      // output hidden values to ensure dokuwiki will return back to this plugin
194      ptln('  <input type="hidden" name="do"   value="admin" />');
195      ptln('  <input type="hidden" name="page" value="'.$this->getPluginName().'" />');
196      ptln('  <input type="hidden" name="dwc__branch" id="dwc__branch" value="'.  $this->helper->selected_branch() .'" />');
197      ptln('  <input type="hidden" name="dwc__repro" id="dwc__repro" value="'. $this->helper->selected_repro()   .'" />');
198
199     /* Initialize Sqlite Database */
200      ptln('<DIV id="dcw_db_update" class="dwc_box">');
201
202      ptln('<DIV CLASS="dwc_help_btn">');
203      ptln('<a href="javascript:dwc_help(\'updatecreate_sql_database\'); void 0;">');
204      ptln($this->getLang('git_info') .' </a>');
205      ptln('<a href="javascript:dwc_toggle_div(\'dcw_db_update\'); void 0;">' . $this->getLang('div_close') .' </a>' );
206
207      ptln('</DIV>');
208
209      ptln('<b>' . $this->getLang('header_init') .'</b><br />');
210      ptln($this->getLang('explain_init') . '<br />');
211      ptln($this->getLang('input_year').'&nbsp;&nbsp;(yyyy):  <input type="text" name="d[year]" size="4" value="'. $year .'" />&nbsp;&nbsp;');
212      ptln($this->getLang('input_month').'&nbsp;&nbsp;(mm):  <input type="text" name="d[month]" size="2" value="' .$month . '" />&nbsp;&nbsp;');
213      ptln($this->getLang('input_day').'&nbsp;&nbsp;(dd):  <input type="text" name="d[day]" size="2" value="' . $day .'" />&nbsp;&nbsp;');
214      ptln('&nbsp;&nbsp;<input type="submit" name="cmd[init]"  value="'.$this->getLang('btn_init').'" />');
215
216      /* Update Sqlite Database */
217      ptln('<br /><br /><b>' . $this->getLang('header_update')  .'</b><br />');
218      ptln($this->getLang('explain_update') . '<br />');
219      ptln($this->getLang('input_year').'&nbsp;&nbsp;(yyyy):  <input type="text" name="dup[year]" size="4" value="'. $year .'" />&nbsp;&nbsp;');
220      ptln($this->getLang('input_month').'&nbsp;&nbsp;(mm):  <input type="text" name="dup[month]" size="2" value="'. $month . '" />&nbsp;&nbsp;');
221      ptln($this->getLang('input_day').'&nbsp;&nbsp;(dd):  <input type="text" name="dup[day]" size="2" value="' . $day .'" />&nbsp;&nbsp;');
222      ptln('&nbsp;&nbsp;<input type="submit" name="cmd[update]"  value="'.$this->getLang('btn_update').'" />');
223
224      ptln('</DIV>');
225
226     /* Update git */
227      ptln('<DIV id="dcw_update_git" class="dwc_box">');
228      ptln('<DIV CLASS="dwc_help_btn">');
229      ptln('<a href="javascript:dwc_help(\'update_git\'); void 0;">');
230      ptln($this->getLang('git_info') .' </a>');
231      ptln('<a href="javascript:dwc_toggle_div(\'dcw_update_git\'); void 0;">' . $this->getLang('div_close') .' </a>' );
232      ptln('</DIV>');
233
234      ptln('<b>'. $this->getLang('header_git') . '</b>');
235      ptln('<br /><TABLE cellspacing="4">');
236      ptln('<tr><td colspan="5">' . $this->getLang('explain_git') . '</td>');
237
238      /* Check Git Status  */
239      ptln('<tr><td>&nbsp;&nbsp;<input type="submit" name="cmd[status]"  value="'.$this->getLang('btn_status').'" /></td>');
240      ptln('<td>' . $this->getLang('header_git_status') . '</td>');
241      ptln('<td>&nbsp;</td>');
242      ptln('<td>&nbsp;&nbsp;<input type="submit" name="cmd[pull]"  value="'.$this->getLang('btn_pull').'" /></td>');
243      ptln('<td>' . $this->getLang('header_git_pull') . '</td>');
244      ptln('</table>');
245
246     /* Get and Set Remote URL */
247      ptln('<b>' . $this->getLang('header_remote_url')  .'</b><br />');
248      ptln($this->getLang('explain_remote_url') . '<br />');
249      ptln('&nbsp;&nbsp;<input type="submit" name="cmd[remote_url]"  value="'.$this->getLang('btn_remote_url').'" />');
250      ptln('&nbsp;&nbsp;' . $this->getLang('remote_url_text')
251           . '&nbsp;<input type="text" name="remote_url_name" size="80"  value="'
252           . $this->helper->get_remote_url() .'" />');
253      ptln('&nbsp;&nbsp;<input type="submit" name="cmd[set_remote_url]"  value="'.$this->getLang('btn_set_remote').'" />');
254
255      ptln('</DIV>');
256
257     /* Advanced Git Options */
258      ptln('<DIV class="dwc_box" id="dwc_git_advanced">');
259      ptln('<DIV CLASS="dwc_help_btn">');
260      ptln('<a href="javascript:dwc_toggle_div(\'dwc_git_advanced\'); void 0;">' . $this->getLang('div_close') .' </a>' );
261      ptln('</DIV>');
262
263      ptln('<DIV class="dwc_advancedtop" id="dwc_advancedtop">');
264      ptln($this->getLang('header_additional'));
265      ptln('</DIV>');
266
267      ptln('<TABLE cellspacing="4" border="0">');
268
269      /* Fetch  */
270      ptln('<tr><td align="right"><input type="submit"  name="cmd[fetch]"  value="'.$this->getLang('btn_fetch').'" />');
271      ptln('<td>&nbsp;' . $this->getLang('header_git_fetch') . '</td>');
272      ptln('<td>&nbsp;&nbsp;</td>');
273
274      /* Merge  */
275      ptln('<td align="right"><input type="submit" name="cmd[merge]"  value="'. $this->getLang('btn_merge').'" />');
276      ptln('<td>&nbsp;' . $this->getLang('header_git_merge'));
277
278
279      ptln('<td>&nbsp;&nbsp;<td align="right"><input type="submit" name="cmd[add]"  value="'.$this->getLang('btn_add').'" />');
280      ptln('<td>&nbsp;' . $this->getLang('header_git_add'));
281      ptln('<td>&nbsp;&nbsp;</td>');
282
283     /* Add and Commit  */
284      ptln('<td>&nbsp;&nbsp;<td align="right"><input type="submit" name="cmd[commit]"  value="'.$this->getLang('btn_commit').'" />');
285      ptln('<td>&nbsp;' . $this->getLang('header_git_commit'));
286      ptln('<td>&nbsp;&nbsp;</td>');
287      ptln('</table>');
288      ptln('</DIV>');
289
290      ptln('<DIV class="dwc_box" id="dwc_repos_div">');
291      ptln('<DIV CLASS="dwc_help_btn">');
292      ptln('<a href="javascript:dwc_toggle_div(\'dwc_repos_div\'); void 0;">' . $this->getLang('div_close') .' </a>' );
293      ptln('</DIV>');
294
295      /* Branches and Repos*/
296      ptln('<TABLE cellspacing="4" border="0">');
297      ptln('<tr><td colspan="2" align="right">');
298
299         /* Current Sqlite DB Name */
300      ptln($this->getLang('current_db') . $this->helper->current_dbname());
301      ptln('<tr><td colspan="2">&nbsp;'); //Row spacer
302
303      /*Repos */
304      ptln('<tr><th align="center" colspan="2">' . $this->getLang('repo_names') . '&nbsp;&nbsp;&nbsp;</th>'  );
305      ptln('<tr><td align="left"><input type="submit" name="cmd[repro]"  value="'.$this->getLang('btn_repos').'" />');
306      ptln('<td>&nbsp;<Select onchange="dwc_repro(this)">');
307      $this->helper->get_repros();
308      ptln('</Select>');
309
310      ptln('<tr><td colspan="2">&nbsp;'); //Row spacer
311
312      /*Branches */
313      ptln('<tr><th align="center" colspan="2">' . $this->getLang('branch_names') . '&nbsp;&nbsp;&nbsp;</th>'  );
314      ptln('<tr><td align="left"><input type="submit" name="cmd[branch]"  value="'.$this->getLang('btn_branch').'" />');
315      ptln('<td>&nbsp;<Select onchange="dwc_branch(this)">');
316      $this->helper->get_branches();
317      ptln('</Select>');
318
319      ptln('</table>');
320
321      ptln('</DIV>');
322
323       /*Query */
324
325
326       ptln('<DIV class="dwc_box" id="dwc_query_div">');
327
328       ptln('<DIV CLASS="dwc_help_btn">');
329       ptln('<a href="javascript:dwc_help(\'query\'); void 0;">');
330       ptln($this->getLang('git_info') .' </a>');
331       ptln('<a href="javascript:dwc_toggle_div(\'dwc_query_div\'); void 0;">' . $this->getLang('div_close') .' </a>' );
332       ptln('</DIV>');
333
334       ptln('<div class="dwc_msgareatop">');
335       ptln($this->getLang('header_git_query'). '<br >');
336       ptln('</DIV>');
337       ptln('<TABLE CELLSPACING="4"  border="1" CLASS="dwc_dbq">');
338
339       ptln('<TR><TD ALIGN="RIGHT">' . $this->getLang('q_srch_term') . ' 1 <input type="text" value="" name="dwc_query[terms_1]"></TD>');
340       ptln('&nbsp;&nbsp;<TD>&nbsp;&nbsp;' . $this->getLang('q_srch_term') . ' 2 <input type="text" value="" name="dwc_query[terms_2]"></TD>');
341       ptln('<TD ALIGN="LEFT">&nbsp;&nbsp;' . $this->getLang('q_srch_type') . '&nbsp;&nbsp;OR <input type="RADIO" value="OR" CHECKED name="dwc_query[OP_1]">');
342       ptln('&nbsp;&nbsp;AND <input type="RADIO" value="AND" name="dwc_query[OP_1]"></TD>');
343
344       ptln('<TR><TD ALIGN="RIGHT">'. $this->getLang('q_author') .'&nbsp;&nbsp;<input type="text" value="" name="dwc_query[author]">');
345       ptln('<td COLSPAN="1">&nbsp;&nbsp;' . $this->getLang('q_srch_type') . '&nbsp;&nbsp;OR <input type="radio" value="OR" name="dwc_query[OP_2]">');
346       ptln('&nbsp;&nbsp;AND <input type="radio" value="AND" CHECKED name="dwc_query[OP_2]"></TD>');
347
348       ptln('<TD>&nbsp;&nbsp;'. $this->getLang('q_branch') . '&nbsp;&nbsp;<SELECT name="dwc_query[branch]">');
349       ptln($this->helper->get_branches());
350       ptln('<option value="any" selected>any</option>');
351       ptln('</SELECT>');
352
353       ptln('<TR><TD ALIGN="RIGHT">' .$this->getLang('q_start_date') .' <input type="text" value="" name="dwc_query[d1]"></TD>');
354       ptln('<TD>&nbsp;&nbsp;' . $this->getLang('q_end_date') .' <input type="text" value="" name="dwc_query[d2]"></TD>');
355       ptln('<TD>&nbsp;&nbsp;' . $this->getLang('q_date_fmt') . '&nbsp;&nbsp;MM-DD-YYYY</TD>');
356
357       ptln('<TR><TD>' .$this->getLang('q_output') . ':&nbsp;&nbsp;<input type ="radio" name="output_type" checked value="table">' . $this->getLang('q_table'));
358       ptln('&nbsp;&nbsp;<input type ="radio" name="output_type" value="plain">' . $this->getLang('q_plain'));
359       ptln('<TD COLSPAN="2" ALIGN="RIGHT">&nbsp;&nbsp;<input type="submit" value = "Submit query" name="cmd[query]">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TD>');
360
361       ptln('</TABLE>');
362       ptln('</DIV>');
363
364       ptln('<DIV class="dwc_box" id="dwc_prune_div">');
365       ptln('<DIV CLASS="dwc_help_btn">');
366       ptln('<a href="javascript:dwc_help(\'maintenance\'); void 0;">');
367       ptln($this->getLang('git_info') .' </a>');
368       ptln('<a href="javascript:dwc_toggle_div(\'dwc_prune_div\'); void 0;">' . $this->getLang('div_close') .' </a>' );
369       ptln('</DIV>');
370       ptln('<b>' .$this->getLang('explain_maint') . '</b><br /><br />');
371       echo  $this->helper->db_data();
372       ptln('<br /><input type="submit" value = "' . $this->getLang('prune') . '" name="cmd[prune]">');
373       ptln('&nbsp;&nbsp;&nbsp;<input type="submit" value = "' . $this->getLang('prune_del') . '" name="cmd[prune_del]">');
374       ptln('&nbsp;&nbsp;&nbsp;<input type="submit" value = "' . $this->getLang('prune_restore') . '" name="cmd[restore]">');
375       ptln('</DIV>');
376       ptln('</form>');
377
378
379     /* Info Div */
380       ptln('<DIV class="dwc_box" id="dwc_info_div">');
381       ptln('<DIV class="dwc_box" id="combined_info_div">');
382       ptln('Database Name: ' . $this->helper->current_dbname() . '<br>');
383       ptln('Current Repro: ' . $this->helper->selected_repro() . '<br />');
384       ptln('Branch: ' . $this->helper->selected_branch() . '<br>');
385       ptln('Remote Url: ' . $this->helper->get_remote_url() . '<br>');
386       ptln('</DIV>');
387       $help_file = $this->locale_xhtml('dwc_admin');
388       $help_file = preg_replace('/~~CLOSE~~/ms','<DIV class="closer"><a href="javascript:dwc_toggle_info(\'dwc_info_div\'); void 0;">' . $this->getLang('div_close') .'</a></DIV>',$help_file);
389       echo $help_file;
390       ptln('</DIV>');
391
392   /* Message Area */
393      ptln('<br /><div class="dwc_msgareatop" id="dwc_msgareatop">');
394      ptln($this->getLang('header_git_MsgArea'));
395
396      ptln('<DIV CLASS="dwc_help_btn">');
397      ptln('<a href="javascript:msg_area_bigger(); void 0;">');
398      ptln($this->getLang('btn_msg_big') .' </a>');
399      ptln('&nbsp;&nbsp;&nbsp;<a href="javascript:msg_area_smaller(); void 0;">');
400      ptln($this->getLang('btn_msg_small') .' </a>');
401      ptln('</DIV>');
402
403      ptln('</DIV>');
404
405      ptln('<DIV class="dwc_msgarea" id="dwc_msgarea">');
406      ptln('<p>'.$this->output.'</p>');
407      ptln('</DIV>');
408
409       if($this->submitted) {
410        //   ptln($this->submitted);
411       }
412     ptln('<script language="javascript">');
413     ptln('dwc_toggle_div("' . $this->current_page . '");');
414     ptln('</script>');
415    }
416
417   function get_timestamp($d) {
418      $dstr = implode(' ',$d);
419      if(preg_match('/[a-zA-Z]/',$dstr)){
420         msg('Date wasn\'t set:' . $dstr . ' Will be set to default', -1);
421         return false;
422      }
423      if((strlen($d['month']) < 2) || (strlen($d['year']) < 4)||(strlen($d['day']) < 2)  ) {
424                msg('Incorrect date format: ' . $dstr .' Will be set to default', -1);
425      }
426
427      if((strlen($d['month']) + strlen($d['year']) + strlen($d['day']) > 8 )  ) {
428                msg('Incorrect date format: ' . $dstr .' Will be set to default', -1);
429      }
430
431      return mktime (0,0,0, $d['month'], $d['day'], $d['year']);
432   }
433}
434
435