experiment_defs.php 5.38 KB
Newer Older
Leigh Stoller's avatar
Leigh Stoller committed
1 2 3 4 5 6 7 8 9 10 11
<?php
#
# EMULAB-COPYRIGHT
# Copyright (c) 2006 University of Utah and the Flux Group.
# All rights reserved.
#
#
# This class is really just a wrapper around the DB data ...
#
class Experiment
{
12 13
    var	$experiment;
    var $group;
Leigh Stoller's avatar
Leigh Stoller committed
14

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    #
    # Constructor by lookup on unique index.
    #
    function Experiment($exptidx) {
	$safe_exptidx = addslashes($exptidx);

	$query_result =
	    DBQueryWarn("select * from experiments ".
			"where idx='$safe_exptidx'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->experiment = null;
	    return;
	}
	$this->experiment = mysql_fetch_array($query_result);

	# Load lazily;
	$this->group      = null;
    }

    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->experiment);
    }

40
    # Lookup by exptidx, but allow for lookup by pid,eid with variable args.
41
    function Lookup($exptidx) {
42 43
	$args = func_get_args();

44 45 46 47
	$foo = new Experiment($exptidx);

	if ($foo->IsValid())
	    return $foo;
48 49 50 51 52 53 54 55

        # Try lookup with pid,eid.
	if (count($args) == 2) {
	    $pid = array_shift($args);
	    $eid = array_shift($args);

	    $foo = Experiment::LookupByPidEid($pid, $eid);

56
	    if ($foo && $foo->IsValid())
57 58
		return $foo;
	}
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	return null;
    }

    # Backwards compatable lookup by pid,eid. Will eventually flush this.
    function LookupByPidEid($pid, $eid) {
	$safe_pid = addslashes($pid);
	$safe_eid = addslashes($eid);

	$query_result =
	    DBQueryWarn("select idx from experiments ".
			"where pid='$safe_pid' and eid='$safe_eid'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    return null;
	}
	$row = mysql_fetch_array($query_result);
	$idx = $row['idx'];

	return Experiment::Lookup($idx); 
    }
    
    #
    # Refresh an instance by reloading from the DB.
    #
    function Refresh() {
	if (! $this->IsValid())
	    return -1;

	$idx = $this->idx();

	$query_result =
	    DBQueryWarn("select * from experiments where idx='$idx'");
    
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->experiment = NULL;
	    return -1;
	}
	$this->experiment = mysql_fetch_array($query_result);
	$this->group      = null;
	return 0;
    }

    #
    # Load the group object for an experiment.
    #
    function Group() {
	$pid = $this->pid();
	$gid = $this->gid();

	if ($this->group)
	    return $group;

	$this->group = Group::LookupByPidGid($pid, $gid);
	if (! $this->group) {
	    TBERROR("Could not lookup group $pid/$gid!", 1);
	}
	return $this->group;
    }

118 119 120 121 122 123 124 125 126 127
    #
    # Get the creator for a project.
    #
    function GetCreator() {
	return User::Lookup($this->creator());
    }
    function GetSwapper() {
	return User::Lookup($this->swapper());
    }

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    # accessors
    function field($name) {
	return (is_null($this->experiment) ? -1 : $this->experiment[$name]);
    }
    function pid()	    { return $this->field('pid'); }
    function gid()	    { return $this->field('gid'); }
    function eid()	    { return $this->field('eid'); }
    function idx()	    { return $this->field('idx'); }
    function path()	    { return $this->field('path'); }
    function state()	    { return $this->field('state'); }
    function batchstate()   { return $this->field('batchstate'); }
    function batchmode()    { return $this->field('batchmode'); }
    function rsrcidx()      { return $this->stats('rsrcidx'); }
    function creator()      { return $this->field('expt_head_uid');}
    function canceled()     { return $this->field('canceled'); }
    function locked()       { return $this->field('expt_locked'); }
    function elabinelab()   { return $this->field('elab_in_elab');}
    function lockdown()     { return $this->field('lockdown'); }
    function created()      { return $this->field('expt_created'); }
    function swapper()      { return $this->field('expt_swap_uid');}
    function swappable()    { return $this->field('swappable');}
    function idleswap()     { return $this->field('idleswap');}
    function autoswap()     { return $this->field('autoswap');}
    function noswap_reason(){ return $this->field('noswap_reason');}
    function noidleswap_reason(){ return $this->field('noidleswap_reason');}
    function idleswap_timeout() { return $this->field('idleswap_timeout');}
    function autoswap_timeout() { return $this->field('autoswap_timeout');}
    function prerender_pid()    { return $this->field('prerender_pid');}
    function dpdb()		{ return $this->field('dpdb');}
    function dpdbname()         { return $this->field('dpdbname');}
    function dpdbpassword()     { return $this->field('dpdbpassword');}
    function instance_idx()     { return $this->field('instance_idx'); }

    #
    # Access Check. This is not code I want to duplicate, so hand off to
    # global routine until all code converted.
    #
165
    function AccessCheck ($user, $access_type) {
166 167
	$pid = $this->pid();
	$eid = $this->eid();
168
	$uid = $user->uid();
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	
	return TBExptAccessCheck($uid, $pid, $eid, $access_type);
    }

    #
    # Page header; spit back some html for the typical page header.
    #
    function PageHeader() {
	$pid = $this->pid();
	$eid = $this->eid();
	
	$html = "<font size=+2>Experiment <b>".
	    "<a href='showproject.php3?pid=$pid'>$pid</a>/".
	    "<a href='showexp.php3?pid=$pid&eid=$eid'>$eid</a>".
	    "</b></font>\n";

	return $html;
    }

    #
    # Return the Unix GID (numeric) for an experiments group.
    #
    function UnixGID() {
	$group = $this->Group();

	return $group->unix_gid();
    }
Leigh Stoller's avatar
Leigh Stoller committed
196
}