Beispiel #1
0
 public function getOutput()
 {
     //echo get_include_path ();
     require_once 'Image/GraphViz.php';
     $race = Dolumar_Races_Race::getFromId(Neuron_Core_Tools::getInput('_GET', 'race', 'int', 1));
     $default_settings = array('fontsize' => 8);
     $default_node_settings = $default_settings;
     $default_node_settings['shape'] = 'box';
     $tech_atts = $default_node_settings;
     $tech_atts['bgcolor'] = 'ff0000';
     $tech_atts['color'] = 'blue';
     $equip_atts = $default_node_settings;
     $equip_atts['bgcolor'] = 'ff0000';
     $equip_atts['color'] = 'red';
     $equip_atts['rankdir'] = 'tb';
     $equip_atts['constraint'] = false;
     $unit_atts = $default_node_settings;
     $unit_atts['bgcolor'] = 'ff0000';
     $unit_atts['color'] = 'green';
     $arrow_atts = $default_settings;
     $arrow_atts['arrowType'] = 'normal';
     $arrow_atts['fontsize'] = '6';
     $grayarrow_atts = $arrow_atts;
     $grayarrow_atts['color'] = 'gray';
     define('UNIT_PREFIX', '[U] ');
     define('TECHNOLOGY_PREFIX', '[T] ');
     define('BUILDING_PREFIX', '[B] ');
     define('EQUIPMENT_PREFIX', '[E] ');
     $show_equipment = Neuron_Core_Tools::getInput('_GET', 'equipment', 'int', 0) == 1;
     $show_technology = Neuron_Core_Tools::getInput('_GET', 'technology', 'int', 0) == 1;
     $show_units = Neuron_Core_Tools::getInput('_GET', 'units', 'int', 0) == 1;
     $gv = new Image_GraphViz(true, array('label' => $race->getName(), 'labelloc' => 't'));
     // All buildings
     $gv->addCluster("BUILDINGS", "Buildings");
     if ($show_technology) {
         $gv->addCluster("TECHNOLOGY", "Technology");
     }
     //$gv->addCluster ("EQUIPMENT", "Equipment");
     if ($show_equipment) {
         $gv->addCluster("weapon", "Weapons", array('rotate' => '90'));
         $gv->addCluster("armour", "Armour", array());
     }
     if ($show_units) {
         $gv->addCluster("UNITS", "Units");
     }
     $buildings = Dolumar_Buildings_Building::getBuildingObjects($race);
     foreach ($buildings as $building) {
         $building->setVillage(new Dolumar_Players_DummyVillage($race));
         //$building->setRace ($race);
         // Add building
         $gv->addNode(BUILDING_PREFIX . $building->getName(), $default_node_settings, "BUILDINGS");
         // Add building requirements
         foreach ($building->getRequiredBuildings() as $req) {
             $label = $req['amount'] . '+';
             $gv->addEdge(array(BUILDING_PREFIX . $req['building']->getName() => BUILDING_PREFIX . $building->getName()), array_merge($arrow_atts, array('label' => $label)));
         }
         // Technologies
         if ($show_technology) {
             foreach ($building->getTechnologies() as $tech) {
                 $label = 'Level ' . $tech->getMinLevel();
                 $gv->addNode(TECHNOLOGY_PREFIX . $tech->getName(), $tech_atts, "TECHNOLOGY");
                 $gv->addEdge(array(BUILDING_PREFIX . $building->getName() => TECHNOLOGY_PREFIX . $tech->getName()), array_merge($arrow_atts, array('label' => $label)));
                 // requirements for the technologies?
                 foreach ($tech->getRequiredTechnologies() as $req) {
                     $label = null;
                     $gv->addEdge(array(TECHNOLOGY_PREFIX . $req->getName() => TECHNOLOGY_PREFIX . $tech->getName()), array_merge($arrow_atts, array('label' => $label)));
                 }
             }
         }
         // Equipment
         if ($building instanceof Dolumar_Buildings_Crafting && $show_equipment) {
             foreach ($building->getEquipment() as $equip) {
                 //$gv->addNode (EQUIPMENT_PREFIX . $equip->getName (), $equip_atts, "EQUIPMENT");
                 $gv->addNode(EQUIPMENT_PREFIX . $equip->getName(), $equip_atts, $equip->getItemType());
                 // Arrow to this building
                 $label = $equip->getRequiredLevel() > 0 ? 'Level ' . $equip->getRequiredLevel() : null;
                 //$gv->addEdge (array (BUILDING_PREFIX . $building->getName () => EQUIPMENT_PREFIX . $equip->getName ()), array_merge ($arrow_atts, array ('label' => $label)));
                 $gv->addEdge(array(BUILDING_PREFIX . $building->getName() => EQUIPMENT_PREFIX . $equip->getName()), array_merge($grayarrow_atts, array('label' => $label, 'ltail' => 'EQUIPMENT')));
                 // Required technologies?
                 foreach ($equip->getRequiredTechnologies() as $tech) {
                     $label = null;
                     $gv->addEdge(array(TECHNOLOGY_PREFIX . $tech->getName() => EQUIPMENT_PREFIX . $equip->getName()), array_merge($grayarrow_atts, array('label' => $label)));
                 }
             }
         }
         // Units
         if ($building instanceof Dolumar_Buildings_Training && $show_units) {
             foreach ($building->getUnits() as $unit) {
                 // Units!
                 $gv->addNode(UNIT_PREFIX . $unit->getName(), $unit_atts, "UNITS");
                 // Arrow to this building
                 $label = null;
                 $gv->addEdge(array(BUILDING_PREFIX . $building->getName() => UNIT_PREFIX . $unit->getName()), array_merge($grayarrow_atts, array('label' => $label)));
                 // Required technologies?
                 foreach ($unit->getRequiredTechnologies() as $tech) {
                     $label = null;
                     $gv->addEdge(array(TECHNOLOGY_PREFIX . $tech->getName() => UNIT_PREFIX . $unit->getName()), array_merge($grayarrow_atts, array('label' => $label)));
                 }
             }
         }
     }
     // All equipment
     /*
     $gv->addCluster ("EQUIPMENT", "Equipment");
     $eqs = Dolumar_Players_Equipment::getAllEquipment();
     foreach ($eqs as $building)
     {
     	$gv->addNode ($building->getName (), $equip_atts, "EQUIPMENT");
     	
     	// Required technologies?
     	foreach ($building->getRequiredTechnologies () as $tech)
     	{
     		$label = null;
     		$gv->addEdge (array ($tech->getName () => $building->getName ()), array_merge ($arrow_atts, array ('label' => $label)));
     	}
     }
     */
     // All units
     $output = Neuron_Core_Tools::getInput('_GET', 'engine', 'varchar');
     switch ($output) {
         case "dot":
         case "neato":
             break;
         default:
             $output = "dot";
             break;
     }
     if (!$gv->image("png", $output)) {
         echo "Error... Is Graphviz installed?";
     }
 }
 /**
  * Runs the phpOpenTracker API call.
  *
  * @param  array $parameters
  * @return mixed
  * @access public
  */
 function run($parameters)
 {
     $parameters['document_color'] = isset($parameters['document_color']) ? $parameters['document_color'] : 'black';
     $parameters['exit_targets'] = isset($parameters['exit_targets']) ? $parameters['exit_targets'] : false;
     $parameters['exit_target_color'] = isset($parameters['exit_target_color']) ? $parameters['exit_target_color'] : 'red';
     $parameters['from'] = isset($parameters['from']) ? crc32($parameters['from']) : 0;
     $parameters['length'] = isset($parameters['length']) ? $parameters['length'] : false;
     $parameters['referers'] = isset($parameters['referers']) ? $parameters['referers'] : false;
     $parameters['referer_color'] = isset($parameters['referer_color']) ? $parameters['referer_color'] : 'green';
     $parameters['result_format'] = isset($parameters['result_format']) ? $parameters['result_format'] : 'graphviz';
     $parameters['subpaths'] = isset($parameters['subpaths']) ? $parameters['subpaths'] : false;
     $parameters['to'] = isset($parameters['to']) ? crc32($parameters['to']) : 0;
     if ($parameters['api_call'] == 'all_paths') {
         $parameters['subpaths'] = true;
     }
     $this->_paths = array();
     $this->_subpathStatistics = array();
     switch ($parameters['result_format']) {
         case 'graphviz':
         case 'graphviz_object':
             $result = new Image_GraphViz();
             if ($parameters['exit_targets']) {
                 $result->addCluster('exit_targets', 'Exit Targets');
             }
             if ($parameters['referers']) {
                 $result->addCluster('referers', 'Referers');
             }
             break;
         default:
             $result = array();
     }
     $accesslogID = 0;
     $visitor = -1;
     $this->db->query(sprintf("SELECT accesslog.accesslog_id AS accesslog_id,\r\n                accesslog.document_id  AS document_id,\r\n                accesslog.timestamp    AS timestamp,\r\n                documents.string       AS document,\r\n                documents.document_url AS document_url\r\n           FROM %s accesslog,\r\n                %s visitors,\r\n                %s documents\r\n          WHERE visitors.client_id    = '%d'\r\n            AND visitors.accesslog_id = accesslog.accesslog_id\r\n            AND accesslog.document_id = documents.data_id\r\n                %s\r\n                %s\r\n          ORDER BY accesslog.accesslog_id,\r\n                   accesslog.timestamp", $this->config['accesslog_table'], $this->config['visitors_table'], $this->config['documents_table'], $parameters['client_id'], $this->_constraint($parameters['constraints']), $this->_whereTimerange($parameters['start'], $parameters['end'], 'accesslog')));
     while ($row = $this->db->fetchRow()) {
         if (!isset($this->_documents[$row['document_id']])) {
             $this->_documents[$row['document_id']] = array($row['document'], $row['document_url'], 'document');
         }
         if ($accesslogID != $row['accesslog_id']) {
             $accesslogID = $row['accesslog_id'];
             $node = 0;
             $visitor++;
             unset($previousTimestamp);
             $accesslog_ids[$visitor] = $row['accesslog_id'];
         }
         $clickpaths[$visitor][$node]['document'] = $row['document_id'];
         $clickpaths[$visitor][$node]['time_spent'] = 1;
         if (isset($previousTimestamp)) {
             $clickpaths[$visitor][$node - 1]['time_spent'] = $row['timestamp'] - $previousTimestamp;
         }
         $previousTimestamp = $row['timestamp'];
         $node++;
     }
     if (!isset($clickpaths)) {
         return $result;
     }
     $numClickpaths = sizeof($clickpaths) - 1;
     if ($parameters['exit_targets']) {
         for ($i = 0; $i <= $numClickpaths; $i++) {
             $this->db->query(sprintf("SELECT accesslog.document_id    AS document_id,\r\n                    accesslog.exit_target_id AS exit_target_id,\r\n                    exit_targets.string      AS exit_target\r\n               FROM %s accesslog,\r\n                    %s exit_targets\r\n              WHERE accesslog.accesslog_id   = '%d'\r\n                AND accesslog.exit_target_id = exit_targets.data_id", $this->config['accesslog_table'], $this->config['exit_targets_table'], $accesslog_ids[$i]));
             while ($row = $this->db->fetchRow()) {
                 $this->_documents[$row['exit_target_id']] = array($row['exit_target'], 'http://' . $row['exit_target'], 'exit_target');
                 $visitor = sizeof($clickpaths);
                 $clickpaths[$visitor][0]['document'] = $row['document_id'];
                 $clickpaths[$visitor][0]['time_spent'] = 0;
                 $clickpaths[$visitor][1]['document'] = $row['exit_target_id'];
                 $clickpaths[$visitor][1]['time_spent'] = 0;
             }
         }
     }
     for ($i = 0; $i < sizeof($clickpaths) - 1; $i++) {
         $pathLength = sizeof($clickpaths[$i]);
         if ($parameters['referers'] && $i <= $numClickpaths) {
             $this->db->query(sprintf("SELECT referers.string     AS referer,\r\n                    visitors.referer_id AS referer_id\r\n               FROM %s visitors,\r\n                    %s referers\r\n              WHERE visitors.accesslog_id = '%d'\r\n                AND visitors.referer_id   = referers.data_id", $this->config['visitors_table'], $this->config['referers_table'], $accesslog_ids[$i]));
             if ($row = $this->db->fetchRow()) {
                 $this->_documents[$row['referer_id']] = array($row['referer'], 'http://' . $row['referer'], 'referer');
                 array_unshift($clickpaths[$i], array('document' => $row['referer_id'], 'time_spent' => 0));
             }
         }
         if (!$parameters['subpaths']) {
             $this->_processPath($clickpaths[$i], $parameters['from'], $parameters['to'], $parameters['length']);
         } else {
             for ($j = 2; $j <= $pathLength; $j++) {
                 $subpath = array_slice($clickpaths[$i], 0, $j);
                 $subpathLength = sizeof($subpath);
                 for ($k = 2; $k <= $subpathLength; $k++) {
                     $this->_processPath(array_slice($subpath, 0 - $k), $parameters['from'], $parameters['to'], $parameters['length']);
                 }
             }
         }
     }
     if (empty($this->_paths)) {
         return $result;
     }
     foreach ($this->_subpathStatistics as $fromNode => $toNodes) {
         foreach ($toNodes as $toNode => $data) {
             if ($parameters['api_call'] == 'all_paths') {
                 $fromColor = $parameters['document_color'];
                 $fromGroup = 'default';
                 $fromID = $this->_documents[$fromNode][0];
                 $fromRank = 'same';
                 $toColor = $parameters['document_color'];
                 $toGroup = 'default';
                 $toID = $this->_documents[$toNode][0];
                 $toRank = 'same';
                 $edgeColor = $parameters['document_color'];
                 if ($this->_documents[$toNode][2] == 'exit_target') {
                     $toColor = $parameters['exit_target_color'];
                     $toGroup = 'exit_targets';
                     $toID = 'exit_target_' . $toID;
                     $toRank = 'sink';
                     $edgeColor = $parameters['exit_target_color'];
                 }
                 if ($this->_documents[$fromNode][2] == 'referer') {
                     $fromColor = $parameters['referer_color'];
                     $fromGroup = 'referers';
                     $fromID = 'referer_' . $fromID;
                     $fromRank = 'source';
                     $edgeColor = $parameters['referer_color'];
                 }
                 $result->addNode($fromID, array('label' => $this->_documents[$fromNode][0], 'url' => $this->_documents[$fromNode][1], 'color' => $fromColor, 'rank' => $fromRank, 'shape' => 'box'), $fromGroup);
                 $result->addNode($toID, array('label' => $this->_documents[$toNode][0], 'url' => $this->_documents[$toNode][1], 'color' => $toColor, 'rank' => $toRank, 'shape' => 'box'), $toGroup);
                 $result->addEdge(array($fromID => $toID), array('color' => $edgeColor, 'label' => $data['count']));
             } else {
                 if (isset($data['count'])) {
                     $this->_subpathStatistics[$fromNode][$toNode]['time_spent'] = floor($data['time_spent'] / $data['count']);
                 }
             }
         }
     }
     if ($parameters['api_call'] != 'all_paths') {
         switch ($parameters['api_call']) {
             case 'shortest_paths':
                 $field = 'length';
                 $sort = SORT_ASC;
                 break;
             case 'longest_paths':
                 $field = 'length';
                 $sort = SORT_DESC;
                 break;
             case 'top_paths':
                 $field = 'count';
                 $sort = SORT_DESC;
                 break;
         }
         foreach ($this->_paths as $path) {
             $tmp[] = $path[$field];
         }
         array_multisort($tmp, $sort, $this->_paths);
         if ($parameters['limit'] && sizeof($this->_paths) > $parameters['limit']) {
             $this->_paths = array_slice($this->_paths, 0, $parameters['limit']);
         }
         $rank = 1;
         foreach ($this->_paths as $path => $statistics) {
             $documents = array();
             $documentURLs = array();
             $path = explode(':', $path);
             $pathLength = sizeof($path);
             $where = '';
             if ($parameters['subpaths']) {
                 $subpathStatistics = array();
                 for ($i = 0; $i < $pathLength - 1; $i++) {
                     $subpathStatistics[] = array('count' => intval($this->_subpathStatistics[$path[$i]][$path[$i + 1]]['count']), 'time_spent' => intval($this->_subpathStatistics[$path[$i]][$path[$i + 1]]['time_spent']));
                 }
             } else {
                 $subpathStatistics = false;
             }
             for ($i = 0; $i < $pathLength; $i++) {
                 $documents[$i] = $this->_documents[$path[$i]][0];
                 $documentURLs[$i] = $this->_documents[$path[$i]][1];
             }
             if ($parameters['result_format'] == 'array') {
                 $result[] = new phpOpenTracker_Clickpath($documents, $documentURLs, $subpathStatistics, intval($statistics['count']));
             } else {
                 $result->addCluster($rank, sprintf('%s. Taken by %s visitors.', $rank, $statistics['count']));
                 for ($i = 0; $i < $pathLength - 1; $i++) {
                     $fromID = $rank . '_' . $documents[$i];
                     $toID = $rank . '_' . $documents[$i + 1];
                     $result->addNode($fromID, array('label' => $documents[$i], 'url' => $documentURLs[$i], 'color' => $parameters['document_color'], 'shape' => 'box'), $rank);
                     $result->addNode($toID, array('label' => $documents[$i + 1], 'url' => $documentURLs[$i + 1], 'color' => $parameters['document_color'], 'shape' => 'box'), $rank);
                     $result->addEdge(array($fromID => $toID), array('color' => $parameters['document_color']));
                 }
             }
             $rank++;
         }
     }
     if ($parameters['result_format'] == 'graphviz') {
         return $result->parse();
     } else {
         return $result;
     }
 }
$unit_atts['color'] = 'green';
$arrow_atts = $default_settings;
$arrow_atts['arrowType'] = 'normal';
$arrow_atts['fontsize'] = '6';
$grayarrow_atts = $arrow_atts;
$grayarrow_atts['color'] = 'gray';
define('UNIT_PREFIX', '[U] ');
define('TECHNOLOGY_PREFIX', '[T] ');
define('BUILDING_PREFIX', '[B] ');
define('EQUIPMENT_PREFIX', '[E] ');
$show_equipment = Neuron_Core_Tools::getInput('_GET', 'equipment', 'int', 0) == 1;
$show_technology = Neuron_Core_Tools::getInput('_GET', 'technology', 'int', 0) == 1;
$show_units = Neuron_Core_Tools::getInput('_GET', 'units', 'int', 0) == 1;
$gv = new Image_GraphViz(true, array('label' => $race->getName(), 'labelloc' => 't'));
// All buildings
$gv->addCluster("BUILDINGS", "Buildings");
if ($show_technology) {
    $gv->addCluster("TECHNOLOGY", "Technology");
}
//$gv->addCluster ("EQUIPMENT", "Equipment");
if ($show_equipment) {
    $gv->addCluster("weapon", "Weapons", array('rotate' => '90'));
    $gv->addCluster("armour", "Armour", array());
}
if ($show_units) {
    $gv->addCluster("UNITS", "Units");
}
$buildings = Dolumar_Buildings_Building::getBuildingObjects($race);
foreach ($buildings as $building) {
    $building->setVillage(new Dolumar_Players_DummyVillage($race));
    //$building->setRace ($race);