Source

phpHgAdmin / admin / application / libraries / hg_ini.php

The default branch has multiple heads

Full commit
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * CodeIgniter Mercurial Config Parser Class
 * 
 * Overcomes general PHP ini parsing limitation due to invalid characters in hgweb.conf 'Collection' keys when
 * generating key-value pairs, and handles concurrency. Also handles per-repository HGRC configuration.
 * 
 * @package        	CodeIgniter
 * @subpackage    	Libraries
 * @category    	Libraries
 * @author 			Josh Carrier
 * @link			blog.joshjcarrier.com
 * 
 */
class Hg_Ini
{
	private $_ci;
	
	private $_ofl_lock;
	private $_default_webdir;
	private $_webdir_arr;
	
	function __construct()
	{
		$this->_ci = &get_instance();
		$this->_ci->load->library('optimisticflock');
		
		$this->_default_webdir = $this->_ci->config->item('default_profile');
		$this->_webdir_arr = $this->_ci->config->item('profile');
	}
	
	function register_OFL(&$ofl_lock)
	{
		$this->_ofl_lock = &$ofl_lock;
	}
	
	function unregister_OFL()
	{
		$dummy = '';
		$this->_ofl_lock = &$dummy;
	}
	
	function registerRepo($r_name, $webdir = null)
	{
		$ini = $this->loadHgWeb($webdir);
		
		// use default registration
		$install_config = $this->getInstallConfig($webdir);
		$repo_path = $install_config['default_repo_dir'] . $r_name;
		
		$ini->set($r_name, $repo_path, 'paths');
		return $this->saveHgWeb($ini, $webdir);
	}
	
	function unregisterRepo($r_key, $webdir = null)
	{
		$ini = $this->loadHgWeb($webdir);
		$ini->unsetKey($r_key, 'paths');
		return $this->saveHgWeb($ini, $webdir);
	}
	
	function getRepoList($webdir = null)
	{
		$ini = $this->loadHgWeb($webdir);
		if(is_object($ini))
		{
			return $ini->get('paths');
		}
		return FALSE; 
	}
	
	function loadHgWeb($webdir = null)
	{
		$install_config = $this->getInstallConfig($webdir);
		$ini_path = $install_config['ini'];

		// concurrency-safe get contents
		$hgwebconf_err_code = '';
		$hgwebconf_all = $this->_ci->optimisticflock->ofl_parse_ini_file($ini_path, true, $this->_ofl_lock, $hgwebconf_err_code);
		if($hgwebconf_err_code == HGPHP_OK)
		{
			return $hgwebconf_all;
		}
		return $hgwebconf_err_code;
	}
	
	function saveHgWeb($hgrc_data, $hgweb = null)
	{
		// load the PHP to file
		$install_config = $this->getInstallConfig($hgweb);
		$ini_path = $install_config['ini'];
		
		$hgrc_new_ini = ';Generated by Hg-PHP Mercurial Repository Manager v'.HGPHP_VERSION;
		
		if(is_object($hgrc_data))
		{
			$hgrc_new_ini .= $hgrc_data->toString();
			$hgrc_new_ini .= "\n;End generated HGRC\n";
			
			// concurrency-safe persist to disk
			return $this->_ci->optimisticflock->ofl_file_put_contents($ini_path, $hgrc_new_ini, $this->_ofl_lock, true);
		}
		else
		{
			return HGPHP_ERR_FS_PREEXISTS;
		}
	}
	
	function getHgRCPath($r_name, $hgweb = null)
	{
		$hgweb_ini = $this->loadHgWeb($hgweb);
		$repo_dir = $hgweb_ini->get($r_name, 'paths');
		if(empty($repo_dir))
		{
			$install_config = $this->getInstallConfig($hgweb);
			return $install_config['default_repo_dir'] . $r_name;
		}
		return $repo_dir;
	}
	
	function loadHgRC($r_name, $hgweb = null)
	{
		// load the PHP to file
		
		$hgwebconf = $this->getHgRCPath($r_name, $hgweb).'/.hg/hgrc';
		
		// concurrency-safe get contents
		$hgwebconf_err_code = '';
		$hgwebconf_all = $this->_ci->optimisticflock->ofl_parse_ini_file($hgwebconf, true, $this->_ofl_lock, $hgwebconf_err_code);
		if($hgwebconf_err_code == HGPHP_OK)
		{
				return $hgwebconf_all;
		}
		return $hgwebconf_err_code;
	}
	
	/**
	 * touchHGRC
	 * Generates a new HGRC file in a given repository directory, with default values
	 * @param r_name name of the repo folder which will have an HGRC file added to
	 * @return status code
	 */
	function touchHgRC($r_name, $hgweb = null)
	{
		$hgrc_path = $this->getHgRCPath($r_name, $hgweb) . '/.hg/hgrc';
		
		// create file

		$touch_status = $this->_ci->optimisticflock->ofl_touch($hgrc_path, $this->_ofl_lock);
		if($touch_status == HGPHP_OK)
		{
			$blank_hgrc = $this->__blank_hgrc($r_name, $hgweb);
			// update touch status for setHGRC return code
			$touch_status = $this->saveHgRC($r_name, $blank_hgrc, $hgweb);
		}
		return $touch_status;
	}
	
	/**
	 * unlinkHGRC
	 * @param r_name name of repo to delete
	 * @return status code
	 * @unused
	 */
	function unlinkHgRC($r_name, $hgweb = null)
	{
		$hgrc_path = $this->getHgRCPath($r_name, $hgweb) . '/.hg/hgrc';
		
		// design decision - force deletion of repository always
		return $this->_ci->optimisticflock->ofl_unlink($hgrc_path, $this->_ofl_lock, TRUE);
	}
	
/**
	 * __hgrc_compat_persist (private method)
	 * Replaces the current HGRC file with one generated represented by a multi-dimensional array
	 * @param r_name name of the repository containing the target HGRC file
	 * @param hgrc_data multi-array representation of the new HGRC file
	 * @return status code
	 */
	function saveHgRC($r_name, $hgrc_data, $profile = null)
	{
		$dummy = '';
		$ofl_lock = $this->_ofl_lock;
		$this->_ofl_lock = &$dummy;
		$hgrc_path = $this->getHgRCPath($r_name, $profile) . '/.hg/hgrc';
		$this->_ofl_lock = &$ofl_lock;
		
		$hgrc_new_ini = ';Generated by Hg-PHP Mercurial Repository Manager v'.HGPHP_VERSION;
		
		if(is_object($hgrc_data))
		{
			$hgrc_new_ini .= $hgrc_data->toString();
			$hgrc_new_ini .= "\n;End generated HGRC\n";
			
			// concurrency-safe persist to disk
			return $this->_ci->optimisticflock->ofl_file_put_contents($hgrc_path, $hgrc_new_ini, $this->_ofl_lock, false);
		}
		else
		{
			return HGPHP_ERR_FS_PREEXISTS;
		}
	}

	/**
	 * touchHGRC
	 * Generates a new HGRC representation as an array
	 * @param r_name name of the repository which will own the hgrc file
	 * @return hgrc reprsented as array
	 */
	function __blank_hgrc($r_name, $profile = null)
	{
		$profile_arr = $this->getInstallConfig($profile);
		// automatic variable management
		$hgserve_url = $profile_arr['hgserve_url'];
		$blank_hgrc = $profile_arr['default_hgrc'];
		
		foreach($blank_hgrc as $c_name => $collection)
		{
			foreach($collection as $i_name => $i_value)
			{
				$blank_hgrc[$c_name][$i_name] = sprintf($i_value, $hgserve_url, $r_name);
			}
		}
		return new ini64($blank_hgrc);
	}
	
	function getInstallConfig($profile = null)
	{
		if(empty($profile))
		{
			$profile = $this->_default_webdir;
		}
		
		return $this->_webdir_arr[$profile];
	}
}