All new accounts created on Gitlab now require administrator approval. If you invite any collaborators, please let Flux staff know so they can approve the accounts.

template_defs.php 39.2 KB
Newer Older
1 2 3 4 5 6 7
<?php
#
# EMULAB-COPYRIGHT
# Copyright (c) 2006 University of Utah and the Flux Group.
# All rights reserved.
#
#
8 9
# The template class is really just a wrapper around the DB data, plus
# some access routines and printing functions. 
10
#
11
class Template
12
{
13
    var	$template;
14
    
15 16 17 18 19 20 21
    function Template($guid, $vers) {
	$guid = addslashes($guid);
	$vers = addslashes($vers);
	
	$query_result =
	    DBQueryWarn("select * from experiment_templates ".
			"where guid='$guid' and vers='$vers'");
22

23 24 25 26 27
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->template = NULL;
	    return;
	}
	$this->template = mysql_fetch_array($query_result);
28 29
    }

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

35 36 37
    # Do class level lookup.
    function Lookup($guid, $vers) {
	$foo = new Template($guid, $vers);
38

39 40 41 42 43 44 45
	if ($foo->IsValid())
	    return $foo;
	return null;
    }
    # Do class level lookup for the root template.
    function LookupRoot($guid) {
	$foo = new Template($guid, 1); 
46

47 48 49
	if ($foo->IsValid())
	    return $foo;
	return null;
50 51 52
    }

    #
53 54 55 56 57
    # Refresh a template instance by reloading from the DB.
    #
    function Refresh() {
	if (! $this->IsValidTemplate())
	    return -1;
58

59 60
	$guid = $this->guid();
	$vers = $this->vers();
61
    
62 63 64 65 66 67 68 69 70
	$query_result =
	    DBQueryWarn("select * from experiment_templates ".
			"where guid='$guid' and vers='$vers'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->template = NULL;
	    return -1;
	}
	$this->template = mysql_fetch_array($query_result);
71 72 73
	return 0;
    }

74 75 76
    # accessors
    function guid() {
	return (is_null($this->template) ? -1 : $this->template['guid']);
77
    }
78 79
    function vers() {
	return (is_null($this->template) ? -1 : $this->template['vers']);
80
    }
81 82
    function pid() {
	return (is_null($this->template) ? -1 : $this->template['pid']);
83
    }
84 85
    function gid() {
	return (is_null($this->template) ? -1 : $this->template['gid']);
86
    }
87 88
    function eid() {
	return (is_null($this->template) ? -1 : $this->template['eid']);
89
    }
90 91
    function tid() {
	return (is_null($this->template) ? -1 : $this->template['tid']);
92
    }
93 94
    function uid() {
	return (is_null($this->template) ? -1 : $this->template['uid']);
95
    }
96 97
    function IsHidden() {
	return (is_null($this->template) ? -1 : $this->template['hidden']);
98
    }
99 100 101 102 103 104 105 106 107 108 109 110
    function created() {
	return (is_null($this->template) ? -1 : $this->template['created']);
    }
    function description() {
	return (is_null($this->template) ? -1 :
		$this->template['description']);
    }
    function parent_guid() {
	return (is_null($this->template) ? -1 :$this->template['parent_guid']);
    }
    function parent_vers() {
	return (is_null($this->template) ? -1 :$this->template['parent_vers']);
111
    }
112

113 114 115 116
    # The root template has no parent guid.
    function IsRoot() {
	if (is_null($this->template))
	    return -1;
117

118
	return is_null($this->template['parent_guid']);
119 120
    }

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    function AccessCheck($uid, $access_type) {
	global $TB_EXPT_READINFO;
	global $TB_EXPT_MODIFY;
	global $TB_EXPT_DESTROY;
	global $TB_EXPT_UPDATE;
	global $TB_EXPT_MIN;
	global $TB_EXPT_MAX;
	global $TBDB_TRUST_USER;
	global $TBDB_TRUST_LOCALROOT;
	global $TBDB_TRUST_GROUPROOT;
	global $TBDB_TRUST_PROJROOT;
	$mintrust;

	if ($access_type < $TB_EXPT_MIN ||
	    $access_type > $TB_EXPT_MAX) {
	    TBERROR("Invalid access type: $access_type!", 1);
	}
138

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        #
        # Admins do whatever they want!
        # 
	if (ISADMIN()) {
	    return 1;
	}
	$pid = $this->pid();
	$gid = $this->gid();

	if ($access_type == $TB_EXPT_READINFO) {
	    $mintrust = $TBDB_TRUST_USER;
	}
	else {
	    $mintrust = $TBDB_TRUST_LOCALROOT;
	}

        #
        # Either proper permission in the group, or group_root in the project.
        # This lets group_roots muck with other peoples experiments, including
        # those in groups they do not belong to.
        #
	return TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust) ||
	    TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_GROUPROOT);
162
    }
163

164
    #
165
    # Display a template in its own table.
166
    #
167 168 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
    function Show() {
	$guid        = $this->guid();
	$vers        = $this->vers();
	$pid         = $this->pid();
	$gid         = $this->gid();
	$uid         = $this->uid();
	$tid         = $this->tid();
	$created     = $this->created();
	$description = $this->description();

        #
        # We need the metadata guid/version for the TID and description since
        # they are mungable metadata.
        #
	$tid_metadata  = $this->LookupMetadataByName("TID");
	$desc_metadata = $this->LookupMetadataByName("description");
	
	if ($tid_metadata == NULL) {
	    TBERROR("Could not find Metadata 'TID' for $guid/$vers", 1);
	}
	if ($desc_metadata == NULL) {
	    TBERROR("Could not find Metadata 'description' for $guid/$vers",1);
	}
	$tid_guid  = $tid_metadata->guid();
	$tid_vers  = $tid_metadata->vers();
	$desc_guid = $desc_metadata->guid();
	$desc_vers = $desc_metadata->vers();
194
    
195 196 197 198 199 200 201 202
        #
        # Generate the table.
        #
	echo "<center>
               <h3>Details</h3>
              </center>\n";

	echo "<table align=center cellpadding=2 cellspacing=2 border=1>\n";
203
    
204
	ShowItem("GUID",
205
		 MakeLink("template",
206 207 208 209 210 211 212 213 214 215 216 217
			  "guid=$guid&version=$vers", "$guid/$vers"));
	
	ShowItem("ID",
		 MakeLink("metadata",
			  "action=modify&guid=$guid&version=$vers".
			  "&metadata_guid=$tid_guid&metadata_vers=$tid_vers",
			  $tid));
	
	ShowItem("Project", MakeLink("project", "pid=$pid", $pid));
	ShowItem("Group",   $gid);
	ShowItem("Creator", MakeLink("user", "target_uid=$uid", $uid));
	ShowItem("Created", $created);
218

219 220 221 222
	$onmouseover = MakeMouseOver($description);
	if (strlen($description) > 40) {
	    $description = substr($description, 0, 40) . " <b>... </b>";
	}
223 224 225
	ShowItem("Description", 
		 MakeLink("metadata",
			  "action=modify&guid=$guid&version=$vers".
226 227
			  "&metadata_guid=$desc_guid&metadata_vers=$desc_vers".
			  " $onmouseover",
228
			  $description));
229
    
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
	if (! $this->IsRoot()) {
	    $parent_guid = $this->parent_guid();
	    $parent_vers = $this->parent_vers();
	    
	    ShowItem("Parent Template",
		     MakeLink("template",
			      "guid=$parent_guid&version=$parent_vers",
			      "$parent_guid/$parent_vers"));
	}
	echo "</table>\n";
    }

    #
    # Display template parameters and default values in a table
    #
    function ShowParameters() {
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
250 251 252 253 254 255 256 257 258 259
	    DBQueryFatal("select p.name,p.value, ".
			 "       p.metadata_guid,p.metadata_vers, ".
			 "       m.value as description ".
			 "   from experiment_template_parameters as p ".
			 "left join experiment_template_metadata_items as m ".
			 "  on m.guid=p.metadata_guid and ".
			 "     m.vers=p.metadata_vers ".
			 "where p.parent_guid='$guid' and ".
			 "      p.parent_vers='$vers'");
    
260 261 262
	if (!$query_result ||
	    !mysql_num_rows($query_result))
	    return 0;
263 264

	echo "<center>
265
               <h3>Parameters</h3>
266 267 268 269 270 271
             </center> 
             <table align=center border=1 cellpadding=5 cellspacing=2>\n";

 	echo "<tr>
                <th>Name</th>
                <th>Default Value</th>
272
                <th>Description</th>
273 274 275
              </tr>\n";

	while ($row = mysql_fetch_array($query_result)) {
276 277 278 279 280 281
	    $name	   = $row['name'];
	    $value	   = $row['value'];
	    $metadata_guid = $row['metadata_guid'];
	    $metadata_vers = $row['metadata_vers'];
	    $description   = $row['description'];
	    
282 283 284 285 286 287
	    if (!isset($value)) {
		$value = "&nbsp";
	    }

	    echo "<tr>
                   <td>$name</td>
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
                   <td>$value</td>";

	    if (is_null($description)) {
		echo "<td>".
		     "<a href=template_metadata.php?action=add&".
		     "guid=$guid&version=$vers&type=parameter_description&".
		     "formfields[name]=${name}>Click to add</a></td>\n";
	    }
	    else {
		$onmouseover = MakeMouseOver($description);
		if (strlen($description) > 30) {
		    $description =
			substr($description, 0, 30) . " <b>... </b>";
		}
		echo "<td><a href='template_metadata.php?action=modify".
		     "&guid=$guid&version=$vers".
		     "&metadata_guid=$metadata_guid".
		     "&metadata_vers=$metadata_vers' $onmouseover>".
  		     "$description</a></td>\n";
	    }
	    echo "</tr>\n";
	    }
310 311 312
	echo "</table>\n";
	return 1;
    }
313
	
314 315 316 317 318 319 320 321 322
    #
    # Display template metadata and values in a table
    #
    function ShowMetadata() {
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
	    DBQueryFatal("select i.* from experiment_template_metadata as m ".
323 324 325
		     "left join experiment_template_metadata_items as i on ".
		     "     m.metadata_guid=i.guid and m.metadata_vers=i.vers ".
		     "where m.parent_guid='$guid' and ".
326 327
		     "      m.parent_vers='$vers' and ".
		     "      m.internal=0 and m.hidden=0");
328 329 330

	if (! mysql_num_rows($query_result))
	    return 0;
331 332

	echo "<center>
333
               <h3>Metadata</h3>
334 335 336 337 338
             </center> 
             <table align=center border=1 cellpadding=5 cellspacing=2>\n";

 	echo "<tr>
                <th>Edit</th>
339
                <th>Delete</th>
340 341 342 343 344 345 346 347 348
                <th>Name</th>
                <th>Value</th>
              </tr>\n";

	while ($row = mysql_fetch_array($query_result)) {
	    $name	   = $row['name'];
	    $value	   = $row['value'];
	    $metadata_guid = $row['guid'];
	    $metadata_vers = $row['vers'];
349 350 351
	    $onmouseover   = "";
	    
	    if (!isset($value) || $value == "") {
352 353
		$value = "&nbsp";
	    }
354 355 356 357 358 359
	    else {
		$onmouseover = MakeMouseOver($value);
		
		if (strlen($value) > 40) {
		    $value = substr($value, 0, 40) . "<b> ... </b></a>";
		}
360
	    }
361 362 363 364

	    echo "<tr>
   	           <td align=center>
                     <a href='template_metadata.php?action=modify".
365
		        "&guid=$guid&version=$vers".
366 367
		        "&metadata_guid=$metadata_guid".
		        "&metadata_vers=$metadata_vers'>
368
                     <img border=0 alt='modify' src='greenball.gif'></A></td>
369 370 371 372 373 374
   	           <td align=center>
                     <a href='template_metadata.php?action=delete".
		        "&guid=$guid&version=$vers".
		        "&metadata_guid=$metadata_guid".
		        "&metadata_vers=$metadata_vers'>
                     <img border=0 alt='delete' src='redball.gif'></A></td>
375
                   <td>$name</td>
376
                   <td $onmouseover>$value</td>
377 378 379 380 381 382
                  </tr>\n";
  	}
	echo "</table>\n";
	return 1;
    }

383 384 385 386 387 388
    #
    # Show the instance list for a template.
    #
    function ShowInstances() {
	$guid = $this->guid();
	$vers = $this->vers();
389 390

	$query_result =
391 392 393 394 395 396 397 398 399 400 401 402 403 404
	    DBQueryFatal("select e.*,count(r.node_id) as nodes, ".
			 "    round(minimum_nodes+.1,0) as min_nodes ".
			 "from experiment_template_instances as i ".
			 "left join experiments as e on e.idx=i.exptidx ".
			 "left join reserved as r on e.pid=r.pid and ".
			 "     e.eid=r.eid ".
			 "where e.pid is not null and ".
			 "      (i.parent_guid='$guid' and ".
			 "       i.parent_vers='$vers') ".
			 "group by e.pid,e.eid order by e.state,e.eid");

	if (! mysql_num_rows($query_result))
	    return;
	
405
	echo "<center>
406
               <h3>Template Instances</h3>
407 408 409 410 411 412
             </center> 
             <table align=center border=1 cellpadding=5 cellspacing=2>\n";

	echo "<tr>
               <th>EID</th>
               <th>State</th>
413
               <th align=center>Nodes</th>
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
              </tr>\n";

	$idlemark = "<b>*</b>";
	$stalemark = "<b>?</b>";
	
	while ($row = mysql_fetch_array($query_result)) {
	    $pid       = $row['pid'];
	    $eid       = $row['eid'];
	    $state     = $row['state'];
	    $nodes     = $row['nodes'];
	    $minnodes  = $row['min_nodes'];
	    $idlehours = TBGetExptIdleTime($pid, $eid);
	    $stale     = TBGetExptIdleStale($pid, $eid);
	    $ignore    = $row['idle_ignore'];
	    $name      = $row['expt_name'];
	    
	    if ($nodes == 0) {
		$nodes = "<font color=green>$minnodes</font>";
	    }
	    elseif ($row[swap_requests] > 0) {
		$nodes .= $idlemark;
	    }

	    $idlestr = $idlehours;
	    
	    if ($idlehours > 0) {
		if ($stale) {
		    $idlestr .= $stalemark;
		}
		if ($ignore) {
		    $idlestr = "($idlestr)";
		}
	    }
	    elseif ($idlehours == -1) {
		$idlestr = "&nbsp;";
	    }
	    
	    echo "<tr>
                   <td><A href='showexp.php3?pid=$pid&eid=$eid'>$eid</A></td>
  		   <td>$state</td>
                   <td align=center>$nodes</td>
                 </tr>\n";
	}
	echo "</table>\n";
    }

460 461 462 463 464 465
    #
    # Show the historical instance list for a template.
    #
    function ShowHistory($expand) {
	$guid = $this->guid();
	$vers = $this->vers();
466

467
	$query_result =
468 469 470 471 472 473
	    DBQueryFatal("select i.*,r.archive_tag ".
			 "  from experiment_template_instances as i ".
			 "left join experiment_stats as s on ".
			 "     s.exptidx=i.exptidx ".
			 "left join experiment_resources as r on ".
			 "     r.idx=s.rsrcidx ".
474 475 476 477 478 479 480
			 "where (i.parent_guid='$guid' and ".
			 "       i.parent_vers='$vers') ".
			 "order by i.start_time");

	if (! mysql_num_rows($query_result))
	    return 0;
	
481
	echo "<center>
Leigh B. Stoller's avatar
Leigh B. Stoller committed
482
               <h3>Template History (Swapins)</h3>
483 484 485 486
             </center> 
             <table align=center border=1 cellpadding=5 cellspacing=2>\n";

	echo "<tr>
487
               <th align=center>Expand</th>
488 489 490 491
               <th>EID</th>
               <th>UID</th>
               <th>Start Time</th>
               <th>Stop Time</th>
492
               <th align=center>Show</th>
Leigh B. Stoller's avatar
Leigh B. Stoller committed
493
               <th align=center>Archive</th>
494 495 496 497 498 499 500 501 502 503 504
              </tr>\n";

	$idlemark = "<b>*</b>";
	$stalemark = "<b>?</b>";
	
	while ($row = mysql_fetch_array($query_result)) {
	    $pid       = $row['pid'];
	    $eid       = $row['eid'];
	    $uid       = $row['uid'];
	    $start     = $row['start_time'];
	    $stop      = $row['stop_time'];
505
	    $exptidx   = $row['exptidx'];
506
	    $idx       = $row['idx'];
507
	    $tag       = $row['archive_tag'];
508 509 510 511

	    if (! isset($stop)) {
		$stop = "&nbsp";
	    }
512 513

	    $expandit = ((isset($expand) && $expand == $idx) ? 1 : 0);
514
	    
515
	    echo "<tr>\n";
516 517 518
	    echo " <td align=center>";
	    if ($expandit) {
		echo "<a href=template_history.php".
519
                         "?guid=$guid&version=$vers>".
520 521 522 523
		         "<img border=0 alt='c' src='/icons/down.png'></a>\n";
	    }
	    else {
 		echo "<a href=template_history.php".
524
                         "?guid=$guid&version=$vers&expand=$idx#$idx>".
525 526 527 528 529
		         "<img border=0 alt='e' src='/icons/right.png'></a>\n";
	    }
	    echo " </td>
                   <td>$eid</td>\n";
	    
530 531 532 533
	    echo " <td>$uid</td>
                   <td>$start</td>
                   <td>$stop</td>\n";

534 535
	    echo " <td align=center>".
 		    MakeLink("instance",
536
			     "guid=$guid&version=$vers&exptidx=$exptidx",
537 538 539
			     "<img border=0 alt='Show' src='greenball.gif'>");

	    echo " <td align=center>
540
                     <a href=archive_view.php3/$exptidx/history/$tag/".
541 542 543
		        "?exptidx=$exptidx>
                     <img border=0 alt='i' src='greenball.gif'></a></td>";
	    
544
	    echo " </td>
545
                 </tr>\n";
546 547

	    if ($expandit) {
548 549
		$instance = new TemplateInstance($exptidx);
		
550 551 552 553
		echo "<tr>\n";
		echo " <a NAME=$idx></a>\n";
		echo "<td>&nbsp</td>\n";
		echo " <td colspan=6>\n";
554
		$instance->ShowRunList(0);
555 556 557
		echo " </td>\n";
		echo "</tr>\n";
	    }
558 559 560
	}
	echo "</table>\n";
    }
561 562 563 564 565 566

    #
    # Dump the image map for a template to the output.
    #
    function ShowGraph() {
	$guid = $this->guid();
567
	$vers = $this->vers();
568 569 570 571 572 573 574 575 576 577 578

	$query_result =
	    DBQueryFatal("select imap from experiment_template_graphs ".
			 "where parent_guid='$guid'");

	if (!mysql_num_rows($query_result)) {
	    USERERROR("Experiment Template $guid is no longer in the DB!", 1);
	}
	$row  = mysql_fetch_array($query_result);
	$imap = $row['imap'];

579 580 581
	echo "<script type='text/javascript' ".
	            "src='js/wz_dragdrop.js'></script>";

582 583 584 585 586 587
	echo "<center>";
	echo "<div id=fee style='display: block; overflow: hidden; ".
	    "position: relative; z-index:1010; height: 400px; ".
	    "width: 700px; border: 2px solid black;'>\n";
	echo "<div id=\"D$guid\" style='position:relative;'>\n";

588 589 590 591 592
	echo "<div id='CurrentTemplate' style='display: block; opacity: 1; ".
	    "visibility: hidden; ".
	    "position: absolute; z-index:1020; left: 0px; top: 0px;".
	    "height: 0px; width: 0px; border: 2px solid red;'></div>\n";

593 594 595 596 597 598 599 600 601 602 603 604 605
	echo $imap;
	echo "<img id=\"G$guid\" border=0 usemap=\"#TemplateGraph\" ";
	echo "      src='template_graph.php?guid=$guid'>\n";
	echo "</div>\n";
	echo "</div>\n";

	echo "<script type='text/javascript'>
               <!--
                 SET_DHTML(\"D$guid\");
               //-->
              </script>\n";
    
	echo "</center>\n";
606 607 608 609 610 611 612
	echo "<script type='text/javascript' src='template_sup.js'></script>";
	echo "\n";

	echo "<script type='text/javascript'>
	       SetActiveTemplate(\"G$guid\", \"CurrentTemplate\", 
				 \"Tarea${vers}\");
              </script>\n";
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
    }

    #
    # Grab array of input files for a template, indexed by input_idx.
    #
    function InputFiles() {
	$guid = $this->guid();
	$vers = $this->vers();
	
	$input_list = array();

	$query_result =
	    DBQueryFatal("select * from experiment_template_inputs ".
			 "where parent_guid='$guid' and parent_vers='$vers'");

	while ($row = mysql_fetch_array($query_result)) {
	    $input_idx = $row['input_idx'];

	    $input_query =
		DBQueryFatal("select input ".
			     "  from experiment_template_input_data ".
			     "where idx='$input_idx'");

	    $input_row = mysql_fetch_array($input_query);
	    $input_list[] = $input_row['input'];
	}
	return $input_list;
    }

    #
    # Return number of parameters.
    #
    function ParameterCount() {
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
	    DBQueryFatal("select name,value ".
			 "   from experiment_template_parameters ".
			 "where parent_guid='$guid' and ".
			 "      parent_vers='$vers'");

	return mysql_num_rows($query_result);
    }

    #
    # Return number of metadata items
    #
    function MetadataCount() {
	$guid = $this->guid();
	$vers = $this->vers();

	$query_result =
	    DBQueryFatal("select internal ".
			 "  from experiment_template_metadata as m ".
			 "where m.parent_guid='$guid' and ".
669 670
			 "      m.parent_vers='$vers' and ".
			 "      m.internal=0 and m.hidden=0");
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741

	return mysql_num_rows($query_result);
    }

    #
    # Return number of instances
    #
    function InstanceCount() {
	$guid = $this->guid();
	$vers = $this->vers();

	$query_result =
	    DBQueryFatal("select * from experiment_template_instances as i ".
			 "where (i.parent_guid='$guid' and ".
			 "       i.parent_vers='$vers')");

	return mysql_num_rows($query_result);
    }

    #
    # Look for a metadata item in a template, by the guid/vers of the
    # metadata. Returns a class instance (see below).
    #
    function LookupMetadataByGUID($metadata_guid, $metadata_vers) {
	return TemplateMetadata::TemplateLookupByGUID($this,
						      $metadata_guid,
						      $metadata_vers);
    }
    # Ditto by name,
    function LookupMetadataByName($metadata_name) {
	return TemplateMetadata::TemplateLookupByName($this, $metadata_name);
    }

    # Grab the graph data.
    function GraphImage(&$image) {
	$guid = $this->guid();

	$query_result =
	    DBQueryWarn("select image from experiment_template_graphs ".
			"where parent_guid='$guid'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    return -1;
	}
	$row   = mysql_fetch_array($query_result);
	$image = $row['image'];
	return 0;
    }

    #
    # Return an array of the formal parameters for a template.
    #
    function FormalParameters(&$parameters) {
	$parameters = array();
	$guid = $this->guid();
	$vers = $this->vers();
	
	$query_result =
	    DBQueryFatal("select name,value ".
			 "   from experiment_template_parameters ".
			 "where parent_guid='$guid' and ".
			 "      parent_vers='$vers'");

	while ($row = mysql_fetch_array($query_result)) {
	    $name	= $row['name'];
	    $value	= $row['value'];

	    $parameters[$name] = $value;
	}
	return 0;
    }
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760

    #
    # Find next candidate for a template (modify) TID
    #
    function NextTID() {
	$tid  = $this->tid();
	$guid = $this->guid();

	$query_result =
	    DBQueryFatal("select MAX(vers) from experiment_templates ".
			 "where guid='$guid'");

	if (mysql_num_rows($query_result) == 0) {
	    return "T" . substr(md5(uniqid($foo, true)), 0, 10);
	}
	$row = mysql_fetch_array($query_result);
	$foo = $row[0] + 1;
	return "${tid}-V${foo}"; 
    }
761 762
}

763
#
764
# This is the class for a template instance (swapin).
765
#
766
class TemplateInstance
767
{
768 769
    var	$template;
    var $instance;
770

771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
    #
    # Instances are found by their experiment index. 
    #
    function TemplateInstance($exptidx) {
	$exptidx = addslashes($exptidx);

	$query_result =
	    DBQueryFatal("select * ".
			 "  from experiment_template_instances ".
			 "where exptidx='$exptidx'");
	
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->template = NULL;
	    $this->instance = NULL;
	    return;
	}
	$this->instance = mysql_fetch_array($query_result);
	$this->template = new Template($this->instance['parent_guid'],
				       $this->instance['parent_vers']);
    }
    
    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->instance);
    }

    # Do class level lookup.
    function LookupByExptidx($exptidx) {
	$foo = new TemplateInstance($exptidx);

	if ($foo->IsValid())
	    return $foo;
	return null;
    }

    # accessors
    function idx() {
	return (is_null($this->instance) ? -1 : $this->instance['idx']);
    }
    function exptidx() {
	return (is_null($this->instance) ? -1 : $this->instance['exptidx']);
    }
    function runidx() {
	return (is_null($this->instance) ? -1 : $this->instance['runidx']);
    }
    function pid() {
	return (is_null($this->instance) ? -1 : $this->instance['pid']);
    }
    function eid() {
	return (is_null($this->instance) ? -1 : $this->instance['eid']);
    }
    function uid() {
	return (is_null($this->instance) ? -1 : $this->instance['uid']);
    }
    function guid() {
	return (is_null($this->instance) ? -1 :
		$this->instance['parent_guid']);
    }
    function vers() {
	return (is_null($this->instance) ? -1 :
		$this->instance['parent_vers']);
    }
    function start_time() {
	return (is_null($this->instance) ? -1 :
		$this->instance['start_time']);
    }
    function stop_time() {
	return (is_null($this->instance) ? -1 :
		$this->instance['stop_time']);
    }
    function template() {
	return (is_null($this->instance) ? -1 : $this->template);
    }

    #
    # Show an instance.
    #
848
    function Show($detailed) {
849
	$exptidx = $this->exptidx();
850
	$runidx  = $this->runidx();
851 852 853 854 855 856
	$guid    = $this->guid();
	$vers    = $this->vers();
	$pid     = $this->pid();
	$uid     = $this->uid();
	$start   = $this->start_time();
	$stop    = $this->stop_time();
857 858 859 860 861 862 863 864 865
	$template= $this->template();
	$pcount  = $template->ParameterCount();

	if ($detailed && $pcount) {
	    echo "<table border=0 bgcolor=#000 color=#000 class=stealth ".
		 " cellpadding=0 cellspacing=0 align=center>\n";
            echo "<tr valign=top>";
	    echo "<td class=stealth align=center>\n";
	}
866
	echo "<center>
867
               <h3>Template Details</h3>
868 869 870 871 872 873
             </center>\n";

	echo "<table align=center cellpadding=2 cellspacing=2 border=1>\n";
    
	ShowItem("Template",
		 MakeLink("template",
874 875 876 877
			  "guid=$guid&version=$vers", "$guid/$vers"));
	ShowItem("ID",          $exptidx);
	ShowItem("Project",     MakeLink("project", "pid=$pid", $pid));
	ShowItem("Creator",     MakeLink("user", "target_uid=$uid", $uid));
878 879
	ShowItem("Started",     $start);
	ShowItem("Stopped",     (isset($stop) ? $stop : "&nbsp"));
880
	ShowItem("Current Run", (isset($runidx) ? $runidx : "&nbsp"));
881 882
	echo "</table>\n";

883 884 885 886 887 888 889 890
	if ($detailed && $pcount) {
	    echo "</td>";
	    echo "<td align=center class=stealth> &nbsp &nbsp &nbsp </td>\n";
	    echo "<td class=stealth align=center>\n";
	    $this->ShowBindings();
	    echo "</tr>";
	    echo "</table>\n";
	}
891

892 893
	if ($detailed) {
	    $this->ShowRunList(1);
894 895
	}
    }
896 897 898 899 900 901
    
    #
    # Display instance bindings in a table
    #
    function ShowBindings() {
	$instance_idx = $this->idx();
902

903 904 905
	$query_result =
	    DBQueryWarn("select * from experiment_template_instance_bindings ".
			"where instance_idx='$instance_idx'");
906

907 908 909 910
	if (!mysql_num_rows($query_result))
	    return 0;

	echo "<center>
911
               <h3>Instance Bindings</h3>
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
             </center> 
             <table align=center border=1 cellpadding=5 cellspacing=2>\n";

 	echo "<tr>
                <th>Name</th>
                <th>Value</th>
              </tr>\n";

	while ($row = mysql_fetch_array($query_result)) {
	    $name	= $row['name'];
	    $value	= $row['value'];
	    if (!isset($value)) {
		$value = "&nbsp";
	    }

	    echo "<tr>
                   <td>$name</td>
                   <td>$value</td>
                  </tr>\n";
  	}
	echo "</table>\n";
	return 1;
    }

    #
    # Show the run list for an instance.
    #
    function ShowRunList($withheader) {
	$exptidx = $this->exptidx();
	$guid    = $this->guid();
	$vers    = $this->vers();
	
	$query_result =
	    DBQueryFatal("select * from experiment_runs ".
			 "where exptidx='$exptidx'");
	
	if (! mysql_num_rows($query_result))
	    return 0;
	
951 952 953 954 955 956 957 958
	if ($withheader) {
	    echo "<center>
                    <h3>Instance History (Runs)</h3>
                  </center> \n";
	}
	echo "<table align=center border=1 cellpadding=5 cellspacing=2>\n";

	echo "<tr>
959
               <th align=center>Show</th>
960 961 962 963 964 965 966 967 968 969 970 971 972 973
               <th align=center>Archive</th>
               <th>RunID</th>
               <th>ID</th>
               <th>Start Time</th>
               <th>Stop Time</th>
               <th>Description</th>
              </tr>\n";

	while ($rrow = mysql_fetch_array($query_result)) {
	    $runidx    = $rrow['idx'];
	    $runid     = $rrow['runid'];
	    $start     = $rrow['start_time'];
	    $stop      = $rrow['stop_time'];
	    $exptidx   = $rrow['exptidx'];
974
	    $tag       = $rrow['archive_tag'];
975
	    $description = $rrow['description'];
976
	    $onmouseover = "";
977 978 979 980

	    if (! isset($stop)) {
		$stop = "&nbsp";
	    }
981 982 983 984 985 986 987 988 989 990 991 992 993

	    if (isset($description) && $description != "") {
		$onmouseover = MakeMouseOver($description);
		if (strlen($description) > 30) {
		    $description = substr($description, 0, 30) . " <b>...</b>";
		}
	    }
	    else {
		$description = "&nbsp ";
	    }

	    if (isset($tag) && $tag != "") {
		$archive_link =
994
		    "<a href=archive_view.php3".
995 996 997 998 999 1000
		    "/$exptidx/history/$tag/?exptidx=$exptidx>".
		    "<img border=0 alt='i' src='greenball.gif'></a>";
	    }
	    else {
		$archive_link = "&nbsp ";
	    }
1001 1002 1003 1004
	    
	    echo "<tr>\n";
	    echo " <td align=center>".
		    MakeLink("run",
1005
			     "guid=$guid&version=$vers".
1006 1007 1008
			     "&exptidx=$exptidx&runidx=$runidx",
			     "<img border=0 alt='i' src='greenball.gif'>");
	    echo " </td>
1009
                    <td align=center>$archive_link</td>
1010 1011 1012 1013
                    <td>$runid</td>
  		    <td>$runidx</td>
                    <td>$start</td>
                    <td>$stop</td>
1014
                    <td $onmouseover>$description</td>
1015 1016 1017 1018 1019
                   </tr>\n";
	}
	echo "</table>\n";
    }

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
    #
    # Check if a valid run.
    #
    function ValidRun($runidx) {
	$exptidx = $this->exptidx();
	$runidx  = addslashes($runidx);
	
	$query_result =
	    DBQueryFatal("select * from experiment_runs ".
			 "where exptidx='$exptidx' and idx='$runidx'");
1030

1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
	return mysql_num_rows($query_result);
    }

    #
    # Show details for an experiment run
    #
    function ShowRun($runidx) {
	$runidx  = addslashes($runidx);
	$exptidx = $this->exptidx();
	$guid    = $this->guid();
	$vers    = $this->vers();
	
	$query_result =
	    DBQueryFatal("select r.* from experiment_runs as r ".
			 "left join experiment_template_instances as i on ".
			 "     i.exptidx=r.exptidx ".
			 "where r.exptidx='$exptidx' and r.idx='$runidx'");

	if (!mysql_num_rows($query_result))
	    return;
	
	$row   = mysql_fetch_array($query_result);
	$start = $row['start_time'];
	$stop  = $row['stop_time'];
1055
	$description = $row['description'];
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067

	if (!isset($stop))
	    $stop = "&nbsp";
	
	echo "<center>
               <h3>Experiment Run</h3>
             </center>\n";

	echo "<table align=center cellpadding=2 cellspacing=2 border=1>\n";
    
	ShowItem("Template",
		 MakeLink("template",
1068
			  "guid=$guid&version=$vers", "$guid/$vers"));
1069 1070
	ShowItem("Experiment",
		 MakeLink("instance",
1071
			  "guid=$guid&version=$vers&exptidx$exptidx",
1072 1073 1074 1075 1076
			  "$exptidx"));
	ShowItem("ID",          $runidx);
	ShowItem("Started",     $start);
	ShowItem("Stopped",     $stop);

1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
	if (isset($description) && $description != "") {
	    echo "<tr>
                     <td align=center colspan=2>
                      Description
                     </td>
                  </tr>
                  <tr>
                     <td colspan=2 align=center class=left>
                         <textarea readonly
                            rows=5 cols=50>" .
		            ereg_replace("\r", "", $description) .
		         "</textarea>
                  </td>
              </tr>\n";
	}
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
	echo "</table>\n";

	$query_result =
	    DBQueryFatal("select * from experiment_run_bindings ".
			 "where exptidx='$exptidx' and runidx='$runidx'");

	if (mysql_num_rows($query_result)) {
	    echo "<center>
                   <h3>Experiment Run Bindings</h3>
                  </center> 
                  <table align=center border=1 cellpadding=5 cellspacing=2>\n";

	    echo "<tr>
                    <th>Name</th>
                    <th>Value</th>
                  </tr>\n";

	    while ($row = mysql_fetch_array($query_result)) {
		$name	= $row['name'];
		$value	= $row['value'];
		if (!isset($value)) {
		    $value = "&nbsp";
		}

		echo "<tr>
                       <td>$name</td>
                       <td>$value</td>
                      </tr>\n";
	    }
	    echo "</table>\n";
	}
    }

1125
    #
1126
    # Find next candidate for an experiment run. 
1127
    #
1128 1129 1130
    function NextRunID() {
	$exptidx = $this->exptidx();
	$eid     = $this->eid();
1131

1132 1133 1134
	$query_result =
	    DBQueryFatal("select MAX(idx) from experiment_runs ".
			 "where exptidx='$exptidx'");
1135

1136 1137 1138 1139 1140 1141
	if (mysql_num_rows($query_result) == 0) {
	    return 0;
	}
	$row = mysql_fetch_array($query_result);
	$foo = $row[0] + 1;
	return "${eid}-R${foo}"; 
1142 1143 1144
    }

    #
1145
    # Return an array of the bindings for a template instance.
1146
    #
1147 1148
    function Bindings(&$bindings) {
	$bindings = array();
1149

1150 1151 1152 1153 1154 1155 1156 1157 1158
	$guid = $this->guid();
	$vers = $this->vers();
	$instance_idx = $this->idx();
	
	$query_result =
	    DBQueryFatal("select * ".
			 "   from experiment_template_instance_bindings ".
			 "where parent_guid='$guid' and parent_vers='$vers' ".
			 "      and instance_idx='$instance_idx'");
1159

1160 1161 1162
	while ($row = mysql_fetch_array($query_result)) {
	    $name	= $row['name'];
	    $value	= $row['value'];
1163

1164 1165
	    $bindings[$name] = $value;
	}
1166 1167 1168 1169
	return 0;
    }
}

1170
#
1171
# This is the class for metadata.
1172
#
1173
class TemplateMetadata
1174
{
1175 1176
    var	$template;
    var $metadata;
1177

1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
    #
    # Instances are found by their experiment index. 
    #
    function TemplateMetadata($guid, $vers) {
	$guid = addslashes($guid);
	$vers = addslashes($vers);
	
	$query_result =
	    DBQueryFatal("select * from experiment_template_metadata_items ".
			 "where guid='$guid' and ".
			 "      vers='$vers'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->template = NULL;
	    $this->metadata = NULL;
	    return;
	}
	$this->metadata = mysql_fetch_array($query_result);
	$this->template = NULL;
1197
    }
1198 1199 1200 1201
    
    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->metadata);
1202 1203
    }

1204 1205 1206
    # Do class level lookup.
    function Lookup($guid, $vers) {
	$foo = new TemplateMetadata($guid, $vers);
1207

1208 1209 1210
	if ($foo->IsValid())
	    return $foo;
	return null;
1211
    }
1212 1213 1214 1215 1216
    function TemplateLookupByGUID($template, $guid, $vers) {
	$metadata_guid = addslashes($guid);
	$metadata_vers = addslashes($vers);
	$template_guid = $template->guid();
	$template_vers = $template->vers();
1217

1218 1219 1220 1221 1222 1223
	$query_result =
	    DBQueryFatal("select internal from experiment_template_metadata ".
			 "where parent_guid='$template_guid' and ".
			 "      parent_vers='$template_vers' and ".
			 "      metadata_guid='$metadata_guid' and ".
			 "      metadata_vers='$metadata_vers'");
1224

1225 1226
	if (! mysql_num_rows($query_result))
	    return null;
1227

1228
	$foo = new TemplateMetadata($guid, $vers);
1229

1230 1231
	if (! $foo->IsValid())
	    return null;
1232

1233 1234
	$foo->template = $template;
	return $foo;
1235 1236
    }

1237 1238 1239 1240
    function TemplateLookupByName($template, $name) {
	$metadata_name = addslashes($name);
	$template_guid = $template->guid();
	$template_vers = $template->vers();
1241

1242 1243 1244 1245 1246 1247 1248 1249
	$query_result =
	    DBQueryFatal("select i.guid,i.vers ".
		     "    from experiment_template_metadata as m ".
		     "left join experiment_template_metadata_items as i on ".
		     "     i.guid=m.metadata_guid and i.vers=m.metadata_vers ".
		     "where m.parent_guid='$template_guid' and ".
		     "      m.parent_vers='$template_vers' and ".
		     "      i.name='$metadata_name'");
1250

1251 1252 1253 1254 1255 1256 1257 1258
	if (! mysql_num_rows($query_result))
	    return null;
	
	$row = mysql_fetch_array($query_result);
	$metadata_guid = $row['guid'];
	$metadata_vers = $row['vers'];
	
	$foo = new TemplateMetadata($metadata_guid, $metadata_vers);
1259

1260 1261
	if (! $foo->IsValid())
	    return null;
1262

1263 1264
	$foo->template = $template;
	return $foo;
1265 1266
    }

1267 1268 1269
    # accessors
    function guid() {
	return (is_null($this->metadata) ? -1 : $this->metadata['guid']);
1270
    }
1271 1272
    function vers() {
	return (is_null($this->metadata) ? -1 : $this->metadata['vers']);
1273
    }
1274 1275
    function name() {
	return (is_null($this->metadata) ? -1 : $this->metadata['name']);
1276
    }
1277 1278
    function value() {
	return (is_null($this->metadata) ? -1 : $this->metadata['value']);
1279
    }
1280 1281 1282 1283
    function type() {
	return (is_null($this->metadata) ? -1 :
		$this->metadata['metadata_type']);
    }
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
    function parent_guid() {
	return (is_null($this->metadata) ? -1 :
		$this->metadata['parent_guid']);
    }
    function parent_vers() {
	return (is_null($this->metadata) ? -1 :
		$this->metadata['parent_vers']);
    }
    function template_guid() {
	return (is_null($this->metadata) ? -1 :
		$this->metadata['template_guid']);
    }
    function created() {
	return (is_null($this->metadata) ? -1 : $this->metadata['created']);
    }
    function uid() {
	return (is_null($this->metadata) ? -1 : $this->metadata['uid']);
1301 1302
    }

1303 1304 1305 1306 1307 1308 1309 1310 1311
    #
    # Display a metadata item in its own table.
    #
    function Show() {
	$metadata_guid  = $this->guid();
	$metadata_vers  = $this->vers();
	$created        = $this->created();
	$metadata_name  = $this->name();
	$metadata_value = $this->value();
1312
	$metadata_type  = $this->type();
1313

1314 1315 1316 1317
	echo "<table align=center cellpadding=2 cellspacing=2 border=1>\n";
	
	ShowItem("GUID",     "$metadata_guid/$metadata_vers");
	ShowItem("Name",     $metadata_name);
1318 1319 1320
	if (ISADMIN() && isset($metadata_type)) {
	    ShowItem("Type",     $metadata_type);
	}
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
	ShowItem("Created",  $created);

	if (! is_null($this->template)) {
	    $template_guid  = $template->guid();
	    $template_vers  = $template->vers();

	    ShowItem("Template",
		     MakeLink("template",
			      "guid=$template_guid&version=$template_vers",
			      "$template_guid/$template_vers"));
	}
1332
    
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
	if ($this->parent_guid()) {
	    $parent_guid = $this->parent_guid();
	    $parent_vers = $this->parent_vers();
	    
	    ShowItem("Parent Version",
		     MakeLink("metadata",
			      "action=show&guid=$parent_guid".
			      "&version=$parent_vers",
			      "$parent_guid/$parent_vers"));
	}
1343