<?php
/**
  @author  Alexander 'E-Razor' Krause <alexander.krause@erazor-zone.de>
  @url     http://wiki.erazor-zone.de/wiki:projects:php:dokuwiki:plugins:darcs
  @date    2006.03.19
*/
      
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'admin.php');
 
/**
 * All DokuWiki plugins to extend the admin function
 * need to inherit from this class
 */
class admin_plugin_darcs extends DokuWiki_Admin_Plugin {
  var $log=array();
  /**
   * return some info
   */
  function getInfo(){
    return array(
      'author' => 'Alexander Krause',
      'email'  => 'alexander.krause@erazor-zone.de',
      'date'   => '2005-09-07',
      'name'   => 'darcs repository',
      'desc'   => 'Manipulate the DokuWiki devel',
      'url'    => 'https://www.dokuwiki.org/plugin:darcs',
    );
  }

  /**
   * return sort order for position in admin menu
   */
  function getMenuSort() {
    return 2;
  }

  /**
   * handle user request
   */
  function handle() {
  }

  /**
   * output appropriate html
   */
  function html() {
    global $conf;
    $this->setupLocale();

    $conf['htmlok']=1;
    require_once(realpath(dirname(__FILE__)).'/darcs.php');

    $my_repos=new Darcs_Repository();
    $my_repos->set_repository(DOKU_INC);

    $my_page.='====== '.$this->lang['menu']." ======\n";

    switch ($_REQUEST['darcs_do']) {
      case 'get_plugin':
        $my_page.=$this->wiki_getplugin_page (&$my_repos);
        break;
      case 'apply_all':
        $my_page.=$this->wiki_update_page (&$my_repos);
        break;
      case 'apply_plugins':
        $my_page.=$this->wiki_plugin_page (&$my_repos);
        break;
      case 'do_remove':
        if (is_array($_REQUEST['remove'])) {
          while (list($plugin_id, $plugin_name) = each($_REQUEST['remove'])) {
            exec('rm -r '.DOKU_INC.'lib/plugins/'.$plugin_name);
          }
        }
        $my_page.=$this->wiki_admin_page (&$my_repos);
        break;
      default:
        $my_page.=$this->wiki_admin_page (&$my_repos);
    }

    if ($conf['darcs']['debug']=='1') {
      $my_page.="\n\n=====".$this->lang['stdout']."=====\n";
      $my_page.="<file>".implode("\n",$this->log)."</file>";
    }
    echo $this->plugin_render($my_page, $format='xhtml') ;
  }

  function wiki_admin_page (&$my_repos) {
    global $ID;

    $my_page.="===== dokuwiki =====\n";
    $my_page.='^  '.$this->lang['head_new']."  ^^^^\n";
    $my_page.='^  '.$this->lang['name'].'  ^  '.$this->lang['date'].'  ^  '.$this->lang['author'].
              '  ^  '.$this->lang['description']."  ^\n";

    if ($my_repos->last_ec>=0) {
      $my_repos->get_patches();
      array_push($this->log,"--\n",'>cd '.$my_repos->repos_dir,'>cd '.$my_repos->last_cmd,
                  implode("\n",$my_repos->stdout));
      if ($my_repos->patch_count==0) {
        $my_page.='|  **'.$this->lang['error_u2d']."**  ||||\n";
      } else {
        while (list($patch_no, $patch) = each($my_repos->patch_list)) {
          $my_page.='| '.$patch['name'].'  | '.$patch['date'].'  | '.$patch['author'].
                    '| %%'.$patch['description']."%%  |\n";
        }

        $my_page.='|  <html><form action="'.wl($ID).'" method="get">'.
          '<input type="hidden" name="id" value="'.$ID.'" />'.
          '<input type="hidden" name="do" value="'.$_REQUEST['do'].'"/>'.
          '<input type="hidden" name="page" value="'.$_REQUEST['page'].'"/>'.
          '<input type="hidden" name="darcs_do" value="apply_all" />'.
          '<input type="submit" value="'.$this->lang['bt_update'].'" class="edit"/>'.
          $patch['name']."</form></html> ||||\n";
      }
    } else {
      if ($my_repos->last_ec==-1) {
         $my_page.='|  **'.$this->lang['error_dir']."**  ||||\n";
      } else if ($my_repos->last_ec==-2) {
         $my_page.='|  **'.$this->lang['error_darcs']."**  ||||\n";
      }
    }
 
    $my_page.="===== ".$this->lang['plugins']." =====\n";
    $my_page.='<html><form action="'.wl($ID).'" method="post">'.
          '<input type="hidden" name="id" value="'.$ID.'" />'.
          '<input type="hidden" name="do" value="'.$_REQUEST['do'].'"/>'.
          '<input type="hidden" name="page" value="'.$_REQUEST['page'].'"/>'.
          '<input type="hidden" name="darcs_do" value="apply_plugins" />'.
          $patch['name']."</html>\n";

    $my_page.='^  '.$this->lang['head_new']."  ^^^^\n";
    $my_page.='^  '.$this->lang['name'].'  ^  '.$this->lang['patches'].'  ^  '.
              $this->lang['remove'].'  ^  '.$this->lang['update']."  ^\n";

    $dir_hdl = opendir(realpath(dirname(__FILE__).'/../'));
    $plugin_count=0;
    $patch_count=0;
    while (false !== ($entry = readdir($dir_hdl))) {
      if (is_dir(realpath(dirname(__FILE__).'/../'.$entry.'/_darcs'))) {
        $my_repos->set_repository(realpath(dirname(__FILE__).'/../'.$entry.'/_darcs'),implode);
        $my_repos->get_patches();
        array_push($this->log,"--\n",'>cd '.$my_repos->repos_dir,'>cd '.$my_repos->last_cmd,
                    implode("\n",$my_repos->stdout));

        if ($my_repos->patch_count==0) {
          $up_state='disabled="disabled"';
        } else {
          $up_state='checked="checked"';
        }
        $my_page.='|  '.$entry.'  |  '.$my_repos->patch_count.
        '  |  <html><input type="checkbox" name="remove[]" value="'.$entry.'"/> </html>'.
        '  |  <html><input type="checkbox" name="update[]" value="'.$entry.'"'.$up_state.' /> </html>'.
        "  |\n";
        $plugin_count++;
        $patch_count+=$my_repos->patch_count;
      }
    }
    $disabled=($patch_count==0)?' disabled="disabled"':'';
    $my_page.= ($plugin_count==0) ?
                '|  **'.$this->lang['error_ndp']."**  ||||\n":
                '|  <html><input type="submit" name="remove_plugins" value="'.
                $this->lang['bt_remove_sel'].'" class="edit"/></html> \\\\ '.
                '   <html><input type="submit" name="update_plugins" value="'.
                $this->lang['bt_update_sel'].'"'.
                $disabled.
                ' class="edit"/></html> ||||'."\n <html></form></html>\n";

    $my_page.="\n\n\n";

    $my_page.="^ ".$this->lang['install_plugin']." ^\n";
        $my_page.='|  **darcs get** <html><form action="'.wl($ID).'" method="post">'.
          '<input type="hidden" name="id" value="'.$ID.'" />'.
          '<input type="hidden" name="do" value="'.$_REQUEST['do'].'"/>'.
          '<input type="hidden" name="page" value="'.$_REQUEST['page'].'"/>'.
          '<input type="hidden" name="darcs_do" value="get_plugin" />'.
          '<input name="plugin_url" value="http://" />'.
          '<input type="submit" value="'.$this->lang['get_plugin'].'" class="edit"/>'.
          $patch['name']."</form></html>  |\n";
    return $my_page;
  }

  function wiki_update_page (&$my_repos) {
    $my_repos->set_repository(DOKU_INC);

    if ($my_repos->last_ec>=0) {
      $my_repos->apply_patches();
      array_push($this->log,"--\n",'>cd '.$my_repos->repos_dir,'>cd '.$my_repos->last_cmd,
                  implode("\n",$my_repos->stdout));
      $this->wiki_admin_page (&$my_repos);
    } else {
      if ($my_repos->last_ec==-1) {
         $my_page='**'.$this->lang['error_dir']."**\n";
      } else if ($my_repos->last_ec<0) {
         $my_page='**'.$this->lang['error_darcs']."**\n";
      }
      $my_page.=$this->wiki_backbt ();
    }
    $my_page.=$this->wiki_admin_page (&$my_repos);

    return $my_page;
  }

  function wiki_getplugin_page (&$my_repos) {
    $my_repos->set_repository(DOKU_INC.'lib/plugins/');
    $plugin_name=basename($_REQUEST['plugin_url'],"/");
    if (is_dir(DOKU_INC.'lib/plugins/'.$plugin_name) ) {
      $my_page.='**'.$this->lang['error_pe']."**\n";
      $my_page.=$this->wiki_backbt();
    } else {
      if ($my_repos->last_ec>=0) {
        $my_repos->darcs_get($_REQUEST['plugin_url']);
        array_push($this->log,"--\n",'>cd '.$my_repos->repos_dir,'>cd '.$my_repos->last_cmd,
                    implode("\n",$my_repos->stdout));
        $my_page.=$this->wiki_admin_page(&$my_repos);
      } else {
        if ($my_repos->last_ec==-1) {
           $my_page.='**'.$this->lang['error_dir']."**\n";
        } else if ($my_repos->last_ec<0) {
           $my_page.='**'.$this->lang['error_darcs']."**\n";
        }
        $my_page.=$this->wiki_backbt();
      }
    }
    return $my_page;
  }

  function wiki_plugin_page (&$my_repos) {
    if (($_REQUEST['remove_plugins']!='') && (is_array($_REQUEST['remove'])))  {
      $my_page.="===== ".$this->lang['plugins']." =====\n";
      $my_page.='===== '.$this->lang['remove_warn']." =====\n".
        '<html><form action="'.wl($ID).'" method="get">'.
          '<input type="hidden" name="id" value="'.$ID.'" />'.
          '<input type="hidden" name="do" value="'.$_REQUEST['do'].'"/>'.
          '<input type="hidden" name="page" value="'.$_REQUEST['page'].'"/>'.
          '<input type="hidden" name="darcs_do" value="do_remove"/>'.
          '</html>'."\n";

      $my_page.='^  '.$this->lang['head_new']."  ^^^^\n";
      $my_page.='^  '.$this->lang['name'].'  ^  '.$this->lang['patches'].'  ^  '.
              $this->lang['remove'].'  ^  '.$this->lang['update']."  ^\n";

      while (list($plugin_id, $plugin_name) = each($_REQUEST['remove'])) {
        $my_page.='|  '.$plugin_name.'  |  ...  |  '.
              '<html><input type="checkbox" checked="checked"'.
              ' name="remove[]" value="'.$plugin_name.'"/></html>  |  ...  |'."\n";
      }

      $my_page.='|  <html><input type="submit" value="'.$this->lang['yes'].'" class="edit"/></form></html>';
      $my_page.=$this->wiki_backbt ($this->lang['no']).' ||||';
    } else {
      if (is_array($_REQUEST['update'])) {
        while (list($plugin_id, $plugin_name) = each($_REQUEST['update'])) {
          $my_repos->set_repository(realpath(dirname(__FILE__).'/../'.$plugin_name));
          $my_repos->apply_patches();
          array_push($this->log,"--\n",'>cd '.$my_repos->repos_dir,'>cd '.$my_repos->last_cmd,
                      implode("\n",$my_repos->stdout));
        }
      }
      $my_page.=$this->wiki_admin_page (&$my_repos);
    }
    return $my_page;
  }

  function wiki_backbt ($caption='') {
    global $ID;
    if ($caption=='') $caption=$this->lang['bt_back'];
    $retval='<html><form action="'.wl($ID).'" method="get">'.
          '<input type="hidden" name="id" value="'.$ID.'" />'.
          '<input type="hidden" name="do" value="'.$_REQUEST['do'].'"/>'.
          '<input type="hidden" name="page" value="'.$_REQUEST['page'].'"/>'.
          '<input type="submit" value="'.$caption.'" class="edit"/>'.
          '</form></html>';
    return $retval;
  }

}
