diff admin/application/libraries/hgphp.php @ 8:6215bb22f3d3

Refactored config handler functions to new library.
author joshjcarrier
date Thu, 13 May 2010 17:43:10 -0700
parents 9790c4a95b14
children 97bc7635ce3f
line wrap: on
line diff
--- a/admin/application/libraries/hgphp.php	Wed May 12 23:51:08 2010 -0700
+++ b/admin/application/libraries/hgphp.php	Thu May 13 17:43:10 2010 -0700
@@ -1,10 +1,12 @@
 <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
 
 /**
- * CodeIgniter Mercurial Config Parser Class
+ * CodeIgniter Mercurial Repository Management class
  * 
  * Scans file system and Mercurial projects directory, allowing
- * web-based manipulation of the hgweb configuration files.
+ * web-based manipulation of the hgwebdir.config and hgrc configuration files.
+ * 
+ * Depends on the HgConf2Ini library.
  * 
  * @package        	CodeIgniter
  * @subpackage    	Libraries
@@ -26,7 +28,8 @@
 		
 		$this->_ci =& get_instance();
 		$this->_ci->lang->load('hgphp');
-        log_message('debug', 'Hg_ConfParser class Initialized');
+		$this->_ci->load->library('hgconf2ini');
+        log_message('debug', 'HgPHP class Initialized');
 	}
 	
 	function initialize($config = array())
@@ -34,7 +37,6 @@
 		foreach($config as $key=>$val)
 		{
 			$this->{'_'.$key} = $val;
-			//echo $key . "<br\>";
 		}
 	}
 	
@@ -52,7 +54,7 @@
 	function lsdir()
 	{
 		$realdir = $this->__realdirscan();
-		$hgwebdir_compat = $this->__hgwebconfscan();
+		$hgwebdir_compat = $this->_ci->hgconf2ini->getHgWebDirCollections();
 				
 		// FIXME test no directory cases
 		$allrepo = array_merge($realdir, $hgwebdir_compat);
@@ -79,16 +81,122 @@
 		return $hgrepos;
 	}
 	
-	function saveconf($collections)
+	/**
+	 * Create a Hg repository by:
+	 * - adding an entry in hgwebdir if not present
+	 * - creating a bare repository if not present
+	 */
+	function create_repository($new_repo_name)
 	{
-		$this->__hgwebconf_compat_persist($collections);
+		// FIXME constants required
+		$return_code = 0;
+		
+		if(!$this->can_create($new_repo_name))
+		{
+			// FIXME constants required
+			return -1;
+		}
+		
+		$lsdir = $this->lsdir();
+		$existingdir = array_keys($lsdir);
+		
+		// not registered in hgweb.config
+		if(!isset($lsdir[$new_repo_name]))
+		{
+			// create the repository
+			$create_status = $this->create_repository_dir($new_repo_name);
+			
+			if($create_status)
+			{
+				// edit the directory
+				$existingdir[$new_repo_name] = $new_repo_name;
+				$create_status = $this->_ci->hgconf2ini->setHgWebDirCollections($existingdir);
+				
+				// success message
+				//$this->template->inject_partial('user_msg', 'Repository "'. $new_repo_name .'" created successfully.');
+				return 0;
+			}
+			else
+			{
+				return -2;
+				//$this->template->inject_partial('user_err', 'Repository "'. $new_repo_name .'" could not be created; insufficient user or server privileges.');
+			}
+		}
+		// TODO repair missing directory?
+		else if($lsdir[$new_repo_name]['status'] == 2)
+		{
+			return -3;
+			//$this->template->inject_partial('user_msg', 'Repository "'. $new_repo_name .'" RESTORE.');
+		}
+		else
+		{
+			return -4;
+			//$this->template->inject_partial('user_err', 'Repository "'. $new_repo_name .'" could not be created; it already exists.');
+		}
+		
 	}
 	
-	/**
-	 * Public accessors - hgrc
-	 */
+	function update_repository()
+	{}
+	
+	function delete_repository($del_repo_name)
+	{
+		$return_code = 0;
+		
+		if(!$this->can_delete($del_repo_name))
+		{
+			// FIXME constants required
+			return -1;
+		}
+		
+		$lsdir = $this->lsdir();
+		$existingdir = array_keys($lsdir);
+		$tempexistingdir = array();
+		foreach($existingdir as $repo_name)
+		{
+			$tempexistingdir[$repo_name] = $repo_name;
+		}
+		$existingdir = $tempexistingdir;
+		
+		if(isset($lsdir[$del_repo_name]))
+		{
+			$del_status = true;
+			
+			// existing filesystem is not missing, thus needs to be deleted
+			if($lsdir[$del_repo_name]['status'] != 2)
+			{
+				$del_status = $this->delete_repository_dir($del_repo_name);
+			}
+			
+			// remove from hgweb.config
+			if($del_status)
+			{
+				// edit the directory
+				unset($existingdir[$del_repo_name]);
+
+				$this->_ci->hgconf2ini->setHgWebDirCollections($existingdir);
+
+				// success message
+				// FIXME constants required
+				return 0;
+//				$this->template->inject_partial('user_msg', 'Repository "'. $del_repo_name .'" deleted successfully.');
+			}
+			else
+			{
+				// FIXME constants required
+				return -2;
+//				$this->template->inject_partial('user_err', 'Repository "'. $del_repo_name .'" could not be deleted; insufficient user or server privileges.');
+			}
+		}
+		else
+		{
+			// FIXME constants required
+			return -3;
+//			$this->template->inject_partial('user_err', 'Repository "'. $del_repo_name .'" could not be deleted; it does not exist or is already deleted.');
+		}
+	}
+	
 	 
-	 /* TODO */
 	 
 	 /**
 	  * Public accessors - permissions
@@ -108,7 +216,7 @@
 	 * Public helpers - filesystem
 	 */
 	 
-	function create_repository($repository_name)
+	function create_repository_dir($repository_name)
 	{
 		if(!$this->can_create($repository_name))
 		{
@@ -116,14 +224,13 @@
 		}
 		
 		$cd = $this->_repositories_rel_dir;
-		mkdir($cd . $repository_name);
 		mkdir($cd . $repository_name . '/.hg/store/data/', 755, TRUE);
 		
 		// create hgrc
-		return $this->__hgrc_persist($repository_name);
+		return $this->_ci->hgconf2ini->touchHGRC($repository_name);
 	}
 	
-	function delete_repository($repository_name)
+	function delete_repository_dir($repository_name)
 	{
 		if(!$this->can_delete($repository_name))
 		{
@@ -165,123 +272,6 @@
 		return $verifiedrealdir;
 	}
 	
-	/**
-	 * Scans Mercurial's webconf.config configuration file, detecting
-	 * registered repositories.
-	 */
-	function __hgwebconfscan()
-	{
-		$hgwebconf_all = $this->__hgwebconf_compat_load();
-		
-		$hgwebconf_collections = array();
-		if(isset($hgwebconf_all['collections']))
-		{
-			foreach($hgwebconf_all['collections'] as $path => $name)
-			{
-				$hgwebconf_collections[$name] = $name;
-			}
-		}
-		
-		return $hgwebconf_collections;
-	}
-	
-	/**
-	 * Compatibility layer for Mercurial PHP config files,
-	 * which contain forward slashes '\' as key values.
-	 * 
-	 * @return contents of a php_ini array compatible representation
-	 */
-	function __hgwebconf_compat_load()
-	{
-		$hgwebconf_lock_path = $this->_lock_dir . 'hgweb.config.lock';
-		
-		// need to regenerate lock? (a php ini-parseable file)
-		// FIXME also compare timestamps, block other user access?
-		if(!is_readable($hgwebconf_lock_path))
-		{
-			// load the PHP to file
-			$hgwebconf = $this->_hgwebconf_path;
-			//$fh = fopen($hgwebconf, 'r');
-			$hgwebconf_str = file_get_contents($hgwebconf);//fread($fh, filesize($hgwebconf));
-			//fclose($fh);
-			
-			// replace all occurances of the forward slash '/'
-			$hgwebconf_str = str_replace('/', $this->_compatability_delimiter, $hgwebconf_str);
-			
-			// write temp compatible ini file
-			//$fh = fopen($hgwebconf_lock_path, 'w+');
-			//fwrite($fh, $hgwebconf_str);
-			//fclose($fh);
-			file_put_contents($hgwebconf_lock_path, $hgwebconf_str);
-		}
-		
-		// load the new compat ini
-		// FIXME minimize the number of times we need to do this
-		$hgwebconf_all = parse_ini_file($hgwebconf_lock_path, true);
-		
-		return $hgwebconf_all;
-	}
-	
-	/**
-	 * Takes a PHP-compatable php_ini array and converts it back into the original file.
-	 *  
-	 */
-	function __hgwebconf_compat_persist($hgwebconf_collections)
-	{
-		$hgwebconf_all = $this->__hgwebconf_compat_load();
-		
-		// replace the 'collections' section with the new one
-		$hgwebconf_all['collections'] = $hgwebconf_collections;
-		
-		$hgwebconf_new_ini = ';Generated by Mercurial Repository Manager';
-		// generate ini string
-		foreach($hgwebconf_all as $ini_section_name => $ini_section_items)
-		{
-			// section header
-			$hgwebconf_new_ini .= "\n[".$ini_section_name.']';
-			
-			if($ini_section_name == 'collections')
-			{
-				foreach($ini_section_items as $ini_item_name => $ini_item_value)
-				{
-					$hgwebconf_new_ini .= "\n" . $this->_repositories_abs_dir . $ini_item_value . ' = ' . $ini_item_value;
-				}
-			}
-			
-			else
-			{
-				foreach($ini_section_items as $ini_item_name => $ini_item_value)
-				{
-					$hgwebconf_new_ini .= "\n" . $ini_item_name . ' = ' . $ini_item_value;
-				}	
-			}
-			
-			
-		}
-		$hgwebconf_new_ini .= "\n;End generated file\n";
-		
-		// undo the compatibility to allow Mercurial
-		$hgwebconf_new_ini = str_replace($this->_compatability_delimiter, '/', $hgwebconf_new_ini);
-		
-		// FIXME double declared lock path..
-		$hgwebconf_lock_path = $this->_lock_dir . 'hgweb.config.lock';
-		$hgwebconf_tmp_path = $this->_lock_dir . 'hgweb.config.tmp';
-		
-		// persist to disk in a temporary file
-		file_put_contents($hgwebconf_tmp_path, $hgwebconf_new_ini);
-		
-		// TODO concurrency check
-		// overwrite existing hgweb.conf
-		rename($hgwebconf_tmp_path, $this->_hgwebconf_path);
-		
-		// cleanup 
-		unlink($hgwebconf_lock_path);
-		
-		return true;
-	}
-	
-	
-	
 	
 	
 	function getProjectParams()
@@ -289,20 +279,7 @@
 		return array('ui'=>array('config1'=>'value'));
 	}
 	
-	function setProjectParams($params)
-	{
-		
-	}
-	
-	function __hgrc_persist($project_name, $config = null)
-	{
-		$cd = $this->_repositories_rel_dir . $project_name . '/.hg/';
-		
-		// create file
-		touch($cd . 'hgrc');
-		
-		return true;
-	}
+
 	
 	
 	/**