| 1 |
<?php
|
| 2 |
/* $Id: hg.php,v 1.15 2008/03/31 18:57:03 tjfulopp Exp $ */
|
| 3 |
|
| 4 |
|
| 5 |
drupalize();
|
| 6 |
function drupalize() {
|
| 7 |
while (!@stat('./includes/bootstrap.inc')) {
|
| 8 |
chdir('..');
|
| 9 |
}
|
| 10 |
require_once './includes/bootstrap.inc';
|
| 11 |
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
|
| 12 |
#drupal_cron_run();
|
| 13 |
}
|
| 14 |
# Crucial - to suppress Devel (if installed and enabled) output appearing in the generated XML!
|
| 15 |
$GLOBALS['devel_shutdown'] = FALSE;
|
| 16 |
# Get proper module path.
|
| 17 |
$hgmodulepath = url(drupal_get_path('module', 'graphs/hypergraph'), NULL, NULL, TRUE);
|
| 18 |
# url() ads i18n codes to the URL ... we need to remove them here...
|
| 19 |
if ($langcode <> '') {
|
| 20 |
$hgmodulepath = str_replace('/'. $langcode .'/', '/', $hgmodulepath);
|
| 21 |
}
|
| 22 |
# Non-clean URLs need removing ?q=
|
| 23 |
$hgmodulepath = str_replace("?q=", "", $hgmodulepath);
|
| 24 |
|
| 25 |
if ($_GET['dtd'] == 1) {
|
| 26 |
# Just supply the dtd.
|
| 27 |
#ob_start();
|
| 28 |
#echo 'http://quidne.vacilando.org' . $hgmodulepath . '/graphs/GraphXML.dtd';
|
| 29 |
#echo $hgmodulepath;
|
| 30 |
include $hgmodulepath .'GraphXML.dtd';
|
| 31 |
#include 'http://quidne.vacilando.org/sites/all/modules/hypergraph/graphs/GraphXML.dtd';
|
| 32 |
#$dtdfile = ob_get_clean();
|
| 33 |
#echo $dtdfile;
|
| 34 |
exit();
|
| 35 |
}
|
| 36 |
|
| 37 |
|
| 38 |
header("Content-type: text/xml");
|
| 39 |
header("Cache-Control: no-store, no-cache, must-revalidate");
|
| 40 |
header("Cache-Control: post-check=0, pre-check=0", false);
|
| 41 |
header("Pragma: no-cache");
|
| 42 |
|
| 43 |
# Cache expiration time.
|
| 44 |
$hgcachexpire = 3600;
|
| 45 |
#global $user;
|
| 46 |
$userId = $user->uid;
|
| 47 |
$hgmenucacheid = 'hg_menu_'. $userId .'_'. $_GET['par'];
|
| 48 |
/*
|
| 49 |
$begxml = '<?xml version="1.0"?><!DOCTYPE GraphXML SYSTEM "' . $hgmodulepath . 'GraphXML.dtd"><GraphXML><graph id="Quidne"><style><line tag="node" class="hpg0" colour="black"/><line tag="node" class="hpg1" colour="teal"/><line tag="node" class="hpg2" colour="red"/><line tag="node" class="hpg3" colour="blue"/><line tag="node" class="hpg4" colour="olive"/></style>';
|
| 50 |
*/
|
| 51 |
|
| 52 |
$begxml = '<?xml version="1.0"?><!DOCTYPE GraphXML SYSTEM "'. $hgmodulepath .'hg.php?dtd=1"><GraphXML><graph id="Quidne"><style><line tag="node" class="hpg0" colour="black"/><line tag="node" class="hpg1" colour="teal"/><line tag="node" class="hpg2" colour="red"/><line tag="node" class="hpg3" colour="blue"/><line tag="node" class="hpg4" colour="olive"/></style>';
|
| 53 |
$endxml = '</graph></GraphXML>';
|
| 54 |
|
| 55 |
/*
|
| 56 |
$fullxml = '<?xml version="1.0"?><!DOCTYPE GraphXML SYSTEM "http://quidne.vacilando.org/sites/all/modules/hypergraph/graphs/hg.php?dtd=1"><GraphXML><graph id="Quidne"><style><line tag="node" class="hpg0" colour="black"/><line tag="node" class="hpg1" colour="teal"/><line tag="node" class="hpg2" colour="red"/><line tag="node" class="hpg3" colour="blue"/><line tag="node" class="hpg4" colour="olive"/></style><node class="hpg1" name="1"><label>Navigation</label></node><node class="hpg4" name="326"><label>Gallery of galleries</label></node><node class="hpg3" name="125"><label>Brilliant Gallery</label></node><edge source="125" target="326" /><node class="hpg4" name="327"><label>Gallery of hypergraphs</label></node><node class="hpg3" name="322"><label>Hypergraph</label></node><edge source="322" target="327" /><node class="hpg2" name="320"><label>Drupal</label></node><edge source="320" target="125" /><edge source="320" target="322" /><node class="hpg2" name="283"><label>Vacilando.org</label></node><node class="hpg2" name="57"><label>Recent posts</label></node><node class="hpg2" name="304"><label>Stats</label></node><node class="hpg4" name="303"><label>Science</label></node><node class="hpg3" name="59"><label>Categories</label></node><edge source="59" target="303" /><node class="hpg3" name="58"><label>Sources</label></node><node class="hpg2" name="35"><label>Aggregator</label></node><edge source="35" target="59" /><edge source="35" target="58" /><node class="hpg2" name="42"><label>Contact</label></node><node class="hpg1" name="124"><label>Navigation</label></node><edge source="124" target="320" /><edge source="124" target="283" /><edge source="124" target="57" /><edge source="124" target="304" /><edge source="124" target="35" /><edge source="124" target="42" /><node class="hpg1" name="2"><label>Primary links</label></node><node class="hpg0" name="0"><label>root</label></node><edge source="0" target="1" /><edge source="0" target="124" /><edge source="0" target="2" /></graph></GraphXML>';
|
| 57 |
'<?xml version="1.0"?><!DOCTYPE GraphXML SYSTEM "http://quidne.vacilando.org/sites/all/modules/hypergraph/graphs/GraphXML.dtd"><GraphXML><graph id="Quidne"><style><line tag="node" class="hpg0" colour="black"/><line tag="node" class="hpg1" colour="teal"/><line tag="node" class="hpg2" colour="red"/><line tag="node" class="hpg3" colour="blue"/><line tag="node" class="hpg4" colour="olive"/></style><node class="hpg1" name="1"><label>Navigation</label></node><node class="hpg4" name="326"><label>Gallery of galleries</label><dataref><ref xlink:href="node/4" /></dataref></node><node class="hpg3" name="125"><label>Brilliant Gallery</label><dataref><ref xlink:href="node/3" /></dataref></node><edge source="125" target="326" /><node class="hpg4" name="327"><label>Gallery of hypergraphs</label><dataref><ref xlink:href="node/8" /></dataref></node><node class="hpg3" name="322"><label>Hypergraph</label><dataref><ref xlink:href="node/6" /></dataref></node><edge source="322" target="327" /><node class="hpg2" name="320"><label>Drupal</label><dataref><ref xlink:href="drupal" /></dataref></node><edge source="320" target="125" /><edge source="320" target="322" /><node class="hpg2" name="283"><label>Vacilando.org</label><dataref><ref xlink:href="http://www.vacilando.org" /></dataref></node><node class="hpg2" name="57"><label>Recent posts</label><dataref><ref xlink:href="tracker" /></dataref></node><node class="hpg2" name="304"><label>Stats</label><dataref><ref xlink:href="hof" /></dataref></node><node class="hpg4" name="303"><label>Science</label><dataref><ref xlink:href="aggregator/categories/1" /></dataref></node><node class="hpg3" name="59"><label>Categories</label><dataref><ref xlink:href="aggregator/categories" /></dataref></node><edge source="59" target="303" /><node class="hpg3" name="58"><label>Sources</label><dataref><ref xlink:href="aggregator/sources" /></dataref></node><node class="hpg2" name="35"><label>Aggregator</label><dataref><ref xlink:href="aggregator" /></dataref></node><edge source="35" target="59" /><edge source="35" target="58" /><node class="hpg2" name="42"><label>Contact</label><dataref><ref xlink:href="contact" /></dataref></node><node class="hpg1" name="124"><label>Navigation</label></node><edge source="124" target="320" /><edge source="124" target="283" /><edge source="124" target="57" /><edge source="124" target="304" /><edge source="124" target="35" /><edge source="124" target="42" /><node class="hpg1" name="2"><label>Primary links</label></node><node class="hpg0" name="0"><label>root</label></node><edge source="0" target="1" /><edge source="0" target="124" /><edge source="0" target="2" /></graph></GraphXML>
|
| 58 |
echo $fullxml; exit();
|
| 59 |
*/
|
| 60 |
|
| 61 |
|
| 62 |
#if ( $_SERVER['SERVER_ADDR'] == '64.13.192.90' ) {
|
| 63 |
if (variable_get('hypergraph_cache', 'd') == 'f') {
|
| 64 |
#echo '.....................' . $_SERVER['SERVER_ADDR'];
|
| 65 |
$temp = filecaching_wrapper_hgm();
|
| 66 |
}
|
| 67 |
else {
|
| 68 |
$temp = caching_wrapper_hgm();
|
| 69 |
}
|
| 70 |
|
| 71 |
#echo $hgmenucacheid;
|
| 72 |
/*
|
| 73 |
$tstart = time();
|
| 74 |
do {
|
| 75 |
sleep(1); # Give MySQL the time to write the data to the cache (if unfinished)
|
| 76 |
$r = db_query( "SELECT * FROM cache WHERE `cid` = '" . $hgmenucacheid . "'" ) or die(mysql_error());
|
| 77 |
$appletdata = '';
|
| 78 |
while( $h = db_fetch_object($r) ) {
|
| 79 |
$temp = $h->data;
|
| 80 |
$appletdata = unserialize( $temp );
|
| 81 |
}
|
| 82 |
} while ( ( ( time() - $tstart ) < 10 ) and $temp == '' ); # Search for the expected cache (there can be delay) but no longer than 60 seconds.
|
| 83 |
*/
|
| 84 |
|
| 85 |
if ($temp <> '') {
|
| 86 |
echo $temp;
|
| 87 |
}
|
| 88 |
else {
|
| 89 |
echo $begxml;
|
| 90 |
echo '<node name="1"><label>just</label></node>
|
| 91 |
<node name="2"><label>a</label></node>
|
| 92 |
<node name="3"><label>silly</label></node>
|
| 93 |
<node name="4"><label>demo</label></node>
|
| 94 |
<edge source="1" target="2" />
|
| 95 |
<edge source="1" target="3" />
|
| 96 |
<edge source="1" target="4" />
|
| 97 |
<edge source="4" target="3" />
|
| 98 |
<edge source="4" target="2" />';
|
| 99 |
echo $endxml;
|
| 100 |
}
|
| 101 |
# IMPORTANT - otherwise some process after Hypergraph adds strings and breaks the XML!
|
| 102 |
exit();
|
| 103 |
function filecaching_wrapper_hgm($reset = FALSE) {
|
| 104 |
global $hgcachexpire;
|
| 105 |
global $hgmenucacheid;
|
| 106 |
global $begxml;
|
| 107 |
global $endxml;
|
| 108 |
#echo $hgmenucacheid;
|
| 109 |
$cachedfile = file_directory_temp() .'/'. $hgmenucacheid;
|
| 110 |
$lastchanged = filectime($cachedfile);
|
| 111 |
if ($lastchanged === false or (time() - $lastchanged > ($hgcachexpire))) {
|
| 112 |
#echo '. 1.... ';
|
| 113 |
# Cache file does not exist or is too old.
|
| 114 |
$my_data = $begxml . hypergraph_get_menu(NULL) . $endxml;
|
| 115 |
# Now put $my_data to cache!
|
| 116 |
$fh = fopen($cachedfile, "w+");
|
| 117 |
fwrite($fh, $my_data);
|
| 118 |
fclose($fh);
|
| 119 |
}
|
| 120 |
else {
|
| 121 |
#echo '. 2.... ';
|
| 122 |
# Cache file exists.
|
| 123 |
$my_data = file_get_contents($cachedfile);
|
| 124 |
}
|
| 125 |
return $my_data;
|
| 126 |
}
|
| 127 |
|
| 128 |
function caching_wrapper_hgm($reset = FALSE) {
|
| 129 |
global $hgcachexpire;
|
| 130 |
global $hgmenucacheid;
|
| 131 |
global $begxml;
|
| 132 |
global $endxml;
|
| 133 |
#echo $hgmenucacheid;
|
| 134 |
static $my_data;
|
| 135 |
#echo '0.... ';
|
| 136 |
if (!isset($my_data) || $reset) {
|
| 137 |
if (!$reset && ($cache = cache_get($hgmenucacheid)) && !empty($cache->data)) {
|
| 138 |
$my_data = $cache->data;
|
| 139 |
#echo '1.... ';# . $my_data;
|
| 140 |
}
|
| 141 |
else {
|
| 142 |
// Do your expensive calculations here, and populate $my_data
|
| 143 |
// with the correct stuff..
|
| 144 |
$my_data = $begxml . hypergraph_get_menu(NULL) . $endxml;
|
| 145 |
cache_set($hgmenucacheid, 'cache', $my_data, time() + $hgcachexpire);
|
| 146 |
}
|
| 147 |
}
|
| 148 |
return $my_data;
|
| 149 |
}
|
| 150 |
|
| 151 |
function array_searchRecursive($needle, $haystack, $strict = false, $path = array()) {
|
| 152 |
# http://be2.php.net/manual/en/function.array-search.php
|
| 153 |
# Searches haystack for needle and returns an array of the key path if it is found in the (multidimensional) array, FALSE otherwise.
|
| 154 |
# mixed array_searchRecursive ( mixed needle, array haystack [, bool strict[, array path]] )
|
| 155 |
if (!is_array($haystack)) {
|
| 156 |
return false;
|
| 157 |
}
|
| 158 |
foreach ($haystack as $key => $val) {
|
| 159 |
if (is_array($val) && $subPath = array_searchRecursive($needle, $val, $strict, $path)) {
|
| 160 |
$path = array_merge($path, array($key), $subPath);
|
| 161 |
return $path;
|
| 162 |
}
|
| 163 |
elseif ((!$strict && $val == $needle) || ($strict && $val === $needle)) {
|
| 164 |
$path[] = $key;
|
| 165 |
return $path;
|
| 166 |
}
|
| 167 |
}
|
| 168 |
return false;
|
| 169 |
}
|
| 170 |
|
| 171 |
function findepth($needle, $haystack) {
|
| 172 |
$foundepth = 0;
|
| 173 |
$foundparent = NULL;
|
| 174 |
$searchparent = false;
|
| 175 |
do {
|
| 176 |
$searchparent = array_searchRecursive($needle, $haystack);
|
| 177 |
if ($searchparent !== false) {
|
| 178 |
$foundepth++;
|
| 179 |
unset($tempar[$searchparent]);
|
| 180 |
$needle = $searchparent[0];
|
| 181 |
}
|
| 182 |
#echo '... ' . $foundepth . "\n";
|
| 183 |
} while ($searchparent !== false);
|
| 184 |
return $foundepth;
|
| 185 |
}
|
| 186 |
|
| 187 |
function hypergraph_get_menu($beginfrom) {
|
| 188 |
ob_start();
|
| 189 |
#print_r( menu_get_menu() );
|
| 190 |
#print_r( $menu['visible'] ); # The returned structure contains much information that is useful only internally in the menu system. External modules are likely to need only the ['visible'] element of the returned array. All menu items that are accessible to the current user and not hidden will be present here, so modules and themes can use this structure to build their own representations of the menu.
|
| 191 |
$menu = menu_get_menu();
|
| 192 |
$temparx = $menu['visible'];
|
| 193 |
$tempar = NULL;
|
| 194 |
foreach ($temparx as $key => $value) {
|
| 195 |
foreach ($value['children'] as $key2 => $value2) {
|
| 196 |
$tempar[$key][$key2] = $value2;
|
| 197 |
}
|
| 198 |
}
|
| 199 |
#print_r( $tempar ) ;
|
| 200 |
foreach ($menu['visible'] as $key => $value) {
|
| 201 |
if ($value['title'] == '') {
|
| 202 |
$value['title'] = 'root';
|
| 203 |
}
|
| 204 |
echo '<node class="hpg'. findepth($key, $tempar) .'" name="'. $key .'"><label>'. $value['title'] .'</label>';
|
| 205 |
if ($value['path'] <> '') {
|
| 206 |
echo '<dataref><ref xlink:href="'. $value['path'] .'" /></dataref>';
|
| 207 |
}
|
| 208 |
# . "\n";
|
| 209 |
echo '</node>';
|
| 210 |
foreach ($value['children'] as $key2 => $value2) {
|
| 211 |
#\n';
|
| 212 |
echo '<edge source="'. $key .'" target="'. $value2 .'" />';
|
| 213 |
}
|
| 214 |
}
|
| 215 |
#print_r( menu_tree() );
|
| 216 |
#print_r( menu_secondary_links() );
|
| 217 |
$temp = ob_get_clean();
|
| 218 |
return $temp;
|
| 219 |
}
|
| 220 |
|
| 221 |
# Some people don't have PHP5 yet...
|
| 222 |
function microtime_float() {
|
| 223 |
list($usec, $sec) = explode(" ", microtime());
|
| 224 |
return ((float)$usec + (float)$sec);
|
| 225 |
}
|
| 226 |
|
| 227 |
|
| 228 |
|