1<?php
2/**
3  @author  Alexander 'E-Razor' Krause <alexander.krause@erazor-zone.de>
4  @url     http://wiki.erazor-zone.de/wiki:projects:php:dokuwiki:plugins:darcs
5  @date    2006.03.19
6*/
7
8if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
9if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
10require_once(DOKU_PLUGIN.'admin.php');
11
12/**
13 * All DokuWiki plugins to extend the admin function
14 * need to inherit from this class
15 */
16class admin_plugin_darcs extends DokuWiki_Admin_Plugin {
17  var $log=array();
18  /**
19   * return some info
20   */
21  function getInfo(){
22    return array(
23      'author' => 'Alexander Krause',
24      'email'  => 'alexander.krause@erazor-zone.de',
25      'date'   => '2005-09-07',
26      'name'   => 'darcs repository',
27      'desc'   => 'Manipulate the DokuWiki devel',
28      'url'    => 'https://www.dokuwiki.org/plugin:darcs',
29    );
30  }
31
32  /**
33   * return sort order for position in admin menu
34   */
35  function getMenuSort() {
36    return 2;
37  }
38
39  /**
40   * handle user request
41   */
42  function handle() {
43  }
44
45  /**
46   * output appropriate html
47   */
48  function html() {
49    global $conf;
50    $this->setupLocale();
51
52    $conf['htmlok']=1;
53    require_once(realpath(dirname(__FILE__)).'/darcs.php');
54
55    $my_repos=new Darcs_Repository();
56    $my_repos->set_repository(DOKU_INC);
57
58    $my_page.='====== '.$this->lang['menu']." ======\n";
59
60    switch ($_REQUEST['darcs_do']) {
61      case 'get_plugin':
62        $my_page.=$this->wiki_getplugin_page (&$my_repos);
63        break;
64      case 'apply_all':
65        $my_page.=$this->wiki_update_page (&$my_repos);
66        break;
67      case 'apply_plugins':
68        $my_page.=$this->wiki_plugin_page (&$my_repos);
69        break;
70      case 'do_remove':
71        if (is_array($_REQUEST['remove'])) {
72          while (list($plugin_id, $plugin_name) = each($_REQUEST['remove'])) {
73            exec('rm -r '.DOKU_INC.'lib/plugins/'.$plugin_name);
74          }
75        }
76        $my_page.=$this->wiki_admin_page (&$my_repos);
77        break;
78      default:
79        $my_page.=$this->wiki_admin_page (&$my_repos);
80    }
81
82    if ($conf['darcs']['debug']=='1') {
83      $my_page.="\n\n=====".$this->lang['stdout']."=====\n";
84      $my_page.="<file>".implode("\n",$this->log)."</file>";
85    }
86    echo $this->plugin_render($my_page, $format='xhtml') ;
87  }
88
89  function wiki_admin_page (&$my_repos) {
90    global $ID;
91
92    $my_page.="===== dokuwiki =====\n";
93    $my_page.='^  '.$this->lang['head_new']."  ^^^^\n";
94    $my_page.='^  '.$this->lang['name'].'  ^  '.$this->lang['date'].'  ^  '.$this->lang['author'].
95              '  ^  '.$this->lang['description']."  ^\n";
96
97    if ($my_repos->last_ec>=0) {
98      $my_repos->get_patches();
99      array_push($this->log,"--\n",'>cd '.$my_repos->repos_dir,'>cd '.$my_repos->last_cmd,
100                  implode("\n",$my_repos->stdout));
101      if ($my_repos->patch_count==0) {
102        $my_page.='|  **'.$this->lang['error_u2d']."**  ||||\n";
103      } else {
104        while (list($patch_no, $patch) = each($my_repos->patch_list)) {
105          $my_page.='| '.$patch['name'].'  | '.$patch['date'].'  | '.$patch['author'].
106                    '| %%'.$patch['description']."%%  |\n";
107        }
108
109        $my_page.='|  <html><form action="'.wl($ID).'" method="get">'.
110          '<input type="hidden" name="id" value="'.$ID.'" />'.
111          '<input type="hidden" name="do" value="'.$_REQUEST['do'].'"/>'.
112          '<input type="hidden" name="page" value="'.$_REQUEST['page'].'"/>'.
113          '<input type="hidden" name="darcs_do" value="apply_all" />'.
114          '<input type="submit" value="'.$this->lang['bt_update'].'" class="edit"/>'.
115          $patch['name']."</form></html> ||||\n";
116      }
117    } else {
118      if ($my_repos->last_ec==-1) {
119         $my_page.='|  **'.$this->lang['error_dir']."**  ||||\n";
120      } else if ($my_repos->last_ec==-2) {
121         $my_page.='|  **'.$this->lang['error_darcs']."**  ||||\n";
122      }
123    }
124
125    $my_page.="===== ".$this->lang['plugins']." =====\n";
126    $my_page.='<html><form action="'.wl($ID).'" method="post">'.
127          '<input type="hidden" name="id" value="'.$ID.'" />'.
128          '<input type="hidden" name="do" value="'.$_REQUEST['do'].'"/>'.
129          '<input type="hidden" name="page" value="'.$_REQUEST['page'].'"/>'.
130          '<input type="hidden" name="darcs_do" value="apply_plugins" />'.
131          $patch['name']."</html>\n";
132
133    $my_page.='^  '.$this->lang['head_new']."  ^^^^\n";
134    $my_page.='^  '.$this->lang['name'].'  ^  '.$this->lang['patches'].'  ^  '.
135              $this->lang['remove'].'  ^  '.$this->lang['update']."  ^\n";
136
137    $dir_hdl = opendir(realpath(dirname(__FILE__).'/../'));
138    $plugin_count=0;
139    $patch_count=0;
140    while (false !== ($entry = readdir($dir_hdl))) {
141      if (is_dir(realpath(dirname(__FILE__).'/../'.$entry.'/_darcs'))) {
142        $my_repos->set_repository(realpath(dirname(__FILE__).'/../'.$entry.'/_darcs'),implode);
143        $my_repos->get_patches();
144        array_push($this->log,"--\n",'>cd '.$my_repos->repos_dir,'>cd '.$my_repos->last_cmd,
145                    implode("\n",$my_repos->stdout));
146
147        if ($my_repos->patch_count==0) {
148          $up_state='disabled="disabled"';
149        } else {
150          $up_state='checked="checked"';
151        }
152        $my_page.='|  '.$entry.'  |  '.$my_repos->patch_count.
153        '  |  <html><input type="checkbox" name="remove[]" value="'.$entry.'"/> </html>'.
154        '  |  <html><input type="checkbox" name="update[]" value="'.$entry.'"'.$up_state.' /> </html>'.
155        "  |\n";
156        $plugin_count++;
157        $patch_count+=$my_repos->patch_count;
158      }
159    }
160    $disabled=($patch_count==0)?' disabled="disabled"':'';
161    $my_page.= ($plugin_count==0) ?
162                '|  **'.$this->lang['error_ndp']."**  ||||\n":
163                '|  <html><input type="submit" name="remove_plugins" value="'.
164                $this->lang['bt_remove_sel'].'" class="edit"/></html> \\\\ '.
165                '   <html><input type="submit" name="update_plugins" value="'.
166                $this->lang['bt_update_sel'].'"'.
167                $disabled.
168                ' class="edit"/></html> ||||'."\n <html></form></html>\n";
169
170    $my_page.="\n\n\n";
171
172    $my_page.="^ ".$this->lang['install_plugin']." ^\n";
173        $my_page.='|  **darcs get** <html><form action="'.wl($ID).'" method="post">'.
174          '<input type="hidden" name="id" value="'.$ID.'" />'.
175          '<input type="hidden" name="do" value="'.$_REQUEST['do'].'"/>'.
176          '<input type="hidden" name="page" value="'.$_REQUEST['page'].'"/>'.
177          '<input type="hidden" name="darcs_do" value="get_plugin" />'.
178          '<input name="plugin_url" value="http://" />'.
179          '<input type="submit" value="'.$this->lang['get_plugin'].'" class="edit"/>'.
180          $patch['name']."</form></html>  |\n";
181    return $my_page;
182  }
183
184  function wiki_update_page (&$my_repos) {
185    $my_repos->set_repository(DOKU_INC);
186
187    if ($my_repos->last_ec>=0) {
188      $my_repos->apply_patches();
189      array_push($this->log,"--\n",'>cd '.$my_repos->repos_dir,'>cd '.$my_repos->last_cmd,
190                  implode("\n",$my_repos->stdout));
191      $this->wiki_admin_page (&$my_repos);
192    } else {
193      if ($my_repos->last_ec==-1) {
194         $my_page='**'.$this->lang['error_dir']."**\n";
195      } else if ($my_repos->last_ec<0) {
196         $my_page='**'.$this->lang['error_darcs']."**\n";
197      }
198      $my_page.=$this->wiki_backbt ();
199    }
200    $my_page.=$this->wiki_admin_page (&$my_repos);
201
202    return $my_page;
203  }
204
205  function wiki_getplugin_page (&$my_repos) {
206    $my_repos->set_repository(DOKU_INC.'lib/plugins/');
207    $plugin_name=basename($_REQUEST['plugin_url'],"/");
208    if (is_dir(DOKU_INC.'lib/plugins/'.$plugin_name) ) {
209      $my_page.='**'.$this->lang['error_pe']."**\n";
210      $my_page.=$this->wiki_backbt();
211    } else {
212      if ($my_repos->last_ec>=0) {
213        $my_repos->darcs_get($_REQUEST['plugin_url']);
214        array_push($this->log,"--\n",'>cd '.$my_repos->repos_dir,'>cd '.$my_repos->last_cmd,
215                    implode("\n",$my_repos->stdout));
216        $my_page.=$this->wiki_admin_page(&$my_repos);
217      } else {
218        if ($my_repos->last_ec==-1) {
219           $my_page.='**'.$this->lang['error_dir']."**\n";
220        } else if ($my_repos->last_ec<0) {
221           $my_page.='**'.$this->lang['error_darcs']."**\n";
222        }
223        $my_page.=$this->wiki_backbt();
224      }
225    }
226    return $my_page;
227  }
228
229  function wiki_plugin_page (&$my_repos) {
230    if (($_REQUEST['remove_plugins']!='') && (is_array($_REQUEST['remove'])))  {
231      $my_page.="===== ".$this->lang['plugins']." =====\n";
232      $my_page.='===== '.$this->lang['remove_warn']." =====\n".
233        '<html><form action="'.wl($ID).'" method="get">'.
234          '<input type="hidden" name="id" value="'.$ID.'" />'.
235          '<input type="hidden" name="do" value="'.$_REQUEST['do'].'"/>'.
236          '<input type="hidden" name="page" value="'.$_REQUEST['page'].'"/>'.
237          '<input type="hidden" name="darcs_do" value="do_remove"/>'.
238          '</html>'."\n";
239
240      $my_page.='^  '.$this->lang['head_new']."  ^^^^\n";
241      $my_page.='^  '.$this->lang['name'].'  ^  '.$this->lang['patches'].'  ^  '.
242              $this->lang['remove'].'  ^  '.$this->lang['update']."  ^\n";
243
244      while (list($plugin_id, $plugin_name) = each($_REQUEST['remove'])) {
245        $my_page.='|  '.$plugin_name.'  |  ...  |  '.
246              '<html><input type="checkbox" checked="checked"'.
247              ' name="remove[]" value="'.$plugin_name.'"/></html>  |  ...  |'."\n";
248      }
249
250      $my_page.='|  <html><input type="submit" value="'.$this->lang['yes'].'" class="edit"/></form></html>';
251      $my_page.=$this->wiki_backbt ($this->lang['no']).' ||||';
252    } else {
253      if (is_array($_REQUEST['update'])) {
254        while (list($plugin_id, $plugin_name) = each($_REQUEST['update'])) {
255          $my_repos->set_repository(realpath(dirname(__FILE__).'/../'.$plugin_name));
256          $my_repos->apply_patches();
257          array_push($this->log,"--\n",'>cd '.$my_repos->repos_dir,'>cd '.$my_repos->last_cmd,
258                      implode("\n",$my_repos->stdout));
259        }
260      }
261      $my_page.=$this->wiki_admin_page (&$my_repos);
262    }
263    return $my_page;
264  }
265
266  function wiki_backbt ($caption='') {
267    global $ID;
268    if ($caption=='') $caption=$this->lang['bt_back'];
269    $retval='<html><form action="'.wl($ID).'" method="get">'.
270          '<input type="hidden" name="id" value="'.$ID.'" />'.
271          '<input type="hidden" name="do" value="'.$_REQUEST['do'].'"/>'.
272          '<input type="hidden" name="page" value="'.$_REQUEST['page'].'"/>'.
273          '<input type="submit" value="'.$caption.'" class="edit"/>'.
274          '</form></html>';
275    return $retval;
276  }
277
278}
279