| 12 |
/** |
/** |
| 13 |
* The path of data files. This path can be modify trough drupal settings. |
* The path of data files. This path can be modify trough drupal settings. |
| 14 |
*/ |
*/ |
| 15 |
define('FILES_PATH', '/path/to/a/secure/path/'); |
define('FILES_PATH', './'); |
| 16 |
/** |
/** |
| 17 |
* The path to the visualization applet |
* The path to the visualization applet |
| 18 |
*/ |
*/ |
| 19 |
define('APPLET_PATH', '/modules/sna/applet/'); |
define('APPLET_PATH', 'sites/sna.drupaler.net/applet_tryout/'); |
| 20 |
/** |
/** |
| 21 |
* Set the dba_handler |
* Set the dba_handler |
| 22 |
*/ |
*/ |
| 371 |
} |
} |
| 372 |
} |
} |
| 373 |
|
|
| 374 |
|
/** |
| 375 |
|
* Create a graph from nodes-comments tables. |
| 376 |
|
* |
| 377 |
|
* @param array $edges The adjacentcy list of the graph |
| 378 |
|
* @return integer The number of interactions |
| 379 |
|
*/ |
| 380 |
|
function build_edges_from_nodes(&$edges) { |
| 381 |
|
$edges = array(); |
| 382 |
|
$node_replies_q = "SELECT users.uid as u2, users_1.uid as u1 |
| 383 |
|
FROM {node} node, {users} users, {comments} comments, {users} users_1 |
| 384 |
|
WHERE comments.nid = node.nid AND node.uid = users.uid AND users_1.uid = comments.uid |
| 385 |
|
AND users.name <> '' AND users_1.name <> '' |
| 386 |
|
AND comments.pid = 0"; |
| 387 |
|
|
| 388 |
|
$comment_replies_q = "SELECT users_1.uid as 'u2', users.uid as 'u1' |
| 389 |
|
FROM {users} users, {comments} comments, {comments} comments_1, {users} users_1 |
| 390 |
|
WHERE comments_1.cid = comments.pid |
| 391 |
|
AND users.uid = comments.uid |
| 392 |
|
AND users_1.uid = comments_1.uid |
| 393 |
|
AND users_1.name <> '' |
| 394 |
|
AND users.name <> ''"; |
| 395 |
|
|
| 396 |
|
if ((!$node_replies = db_query($node_replies_q)) || (!$comment_replies = db_query($comment_replies_q))) { |
| 397 |
|
die("Database problem\n"); |
| 398 |
|
} |
| 399 |
|
while ($line = db_fetch_array($comment_replies)) { |
| 400 |
|
if ($line["u1"] != $line["u2"]) { // Do not do hitches |
| 401 |
|
$edges[$line["u1"]][$line["u2"]]++; |
| 402 |
|
} |
| 403 |
|
} |
| 404 |
|
while ($line = db_fetch_array($node_replies)) { |
| 405 |
|
if ($line["u1"] != $line["u2"]) { |
| 406 |
|
$edges[$line["u1"]][$line["u2"]]++; |
| 407 |
|
} |
| 408 |
|
} |
| 409 |
|
return db_num_rows($node_replies) + db_num_rows($comment_replies); |
| 410 |
|
} |
| 411 |
|
|
| 412 |
|
/** |
| 413 |
|
* Create a graph from the buddylist module data |
| 414 |
|
* |
| 415 |
|
* @param array $edges The adjacentcy list of the graph |
| 416 |
|
* @return integer The numer of connections |
| 417 |
|
*/ |
| 418 |
|
function build_edges_from_buddy(&$edges) { |
| 419 |
|
$edges = array(); |
| 420 |
|
$buddy_q = "SELECT uid, buddy FROM {buddylist}"; |
| 421 |
|
if ((!$buddies = db_query($buddy_q))) { |
| 422 |
|
die("Database problem\n"); |
| 423 |
|
} |
| 424 |
|
while ($line = db_fetch_array($buddies)) { |
| 425 |
|
$edges[$line["uid"]][$line["buddy"]]++; |
| 426 |
|
} |
| 427 |
|
return db_num_rows($buddies); |
| 428 |
|
} |
| 429 |
|
|
| 430 |
|
/** |
| 431 |
|
* Create a graph from accesslog table. The connection is to view other's profile |
| 432 |
|
* |
| 433 |
|
* @param array $edges The adjacentcy list of the graph |
| 434 |
|
* @return integer The number of connections |
| 435 |
|
*/ |
| 436 |
|
function build_edges_from_stats(&$edges) { |
| 437 |
|
$edges = array(); |
| 438 |
|
$stats_q = "SELECT uid, path FROM {accesslog} WHERE path LIKE 'user/%'"; |
| 439 |
|
if ((!$stats = db_query($stats_q))) { |
| 440 |
|
die("Database problem\n"); |
| 441 |
|
} |
| 442 |
|
while ($line = db_fetch_array($stats)) { |
| 443 |
|
$dest = str_replace("user/", "", $line["path"]); |
| 444 |
|
if (is_numeric($dest) && $dest != $line["uid"]) { |
| 445 |
|
$edges[$line["uid"]][$dest]++; |
| 446 |
|
} |
| 447 |
|
} |
| 448 |
|
return db_num_rows($stats); |
| 449 |
|
} |
| 450 |
|
|
| 451 |
|
/** |
| 452 |
|
* Create a dot file from the graph to Graphviz |
| 453 |
|
* Graphviz is a graph visualization tool |
| 454 |
|
* |
| 455 |
|
* @param array $edges The adjacentcy list of the graph |
| 456 |
|
* @param $num_interactions Number of edges in the graph |
| 457 |
|
* @return boolean The success of writing out the file |
| 458 |
|
*/ |
| 459 |
|
function generate_graphviz_input($edges, $num_interactions) { |
| 460 |
|
$dot_graph = "digraph G {\n"; |
| 461 |
|
foreach ($edges as $u1 => $sub_arr) { |
| 462 |
|
if ($u1 === 0) { // Anonymous - don't count them! |
| 463 |
|
break; |
| 464 |
|
} |
| 465 |
|
foreach ($sub_arr as $u2 => $num) { |
| 466 |
|
if ($u2 === 0) { // Anonymous - don't count them! |
| 467 |
|
break; |
| 468 |
|
} |
| 469 |
|
$dot_graph .= "\t\"". get_real_name($u1). |
| 470 |
|
"\" -> \"". get_real_name($u2) . |
| 471 |
|
"\" [label=". round($edges[$u1][$u2], 2) ."];\n"; |
| 472 |
|
} |
| 473 |
|
} |
| 474 |
|
$dot_graph .= "}"; |
| 475 |
|
if (!$fp = fopen(DOT_PATH, "w")) { |
| 476 |
|
return FALSE; |
| 477 |
|
} |
| 478 |
|
// Write out the DOT file |
| 479 |
|
fwrite($fp, $dot_graph); |
| 480 |
|
fclose($fp); |
| 481 |
|
return TRUE; |
| 482 |
|
} |
| 483 |
|
|
| 484 |
|
/** |
| 485 |
|
* Create a net file from the graph to Pajek |
| 486 |
|
* Pajek is a graph analizer and visualization tool |
| 487 |
|
* http://vlado.fmf.uni-lj.si/pub/networks/pajek/ |
| 488 |
|
* |
| 489 |
|
* @param array $edges The adjacentcy list of the graph |
| 490 |
|
* @return boolean The success of writing out the file |
| 491 |
|
*/ |
| 492 |
|
function generate_pajek_input($edges) { |
| 493 |
|
// Count unique vertex |
| 494 |
|
$edg = "*Edges\n"; |
| 495 |
|
$vertex = get_all_vertices(); |
| 496 |
|
$num_vertex = count($vertex); |
| 497 |
|
for ($i = 0; $i < $num_vertex; $i++) { |
| 498 |
|
$vert .= ($i + 1) ." \"". get_real_name($vertex[$i]) ."\"\n"; |
| 499 |
|
// in NET files we have to index points in a strict order, uid is not suitable |
| 500 |
|
$real_id[$vertex[$i]] = $i + 1; |
| 501 |
|
} |
| 502 |
|
foreach ($edges as $vx_from => $next) { |
| 503 |
|
if ($vx_from === 0) { // Anonymous - don't count them! |
| 504 |
|
break; |
| 505 |
|
} |
| 506 |
|
foreach (array_keys($next) as $vx_to) { |
| 507 |
|
if ($vx_to === 0) { // Anonymous - don't count them! |
| 508 |
|
break; |
| 509 |
|
} |
| 510 |
|
$edg .= $real_id[$vx_from] ." ". $real_id[$vx_to] ." ". $edges[$vx_from][$vx_to] ."\n"; |
| 511 |
|
} |
| 512 |
|
} |
| 513 |
|
$net .= "*Vertices ". $num_vertex ."\n". $vert . $edg; |
| 514 |
|
if (!$fp = fopen(NET_PATH, "w")) { |
| 515 |
|
return FALSE; |
| 516 |
|
} |
| 517 |
|
fwrite($fp, $net); |
| 518 |
|
fclose($fp); |
| 519 |
|
return TRUE; |
| 520 |
|
} |
| 521 |
|
|
| 522 |
|
/** |
| 523 |
|
* Write out the includable graph data |
| 524 |
|
* |
| 525 |
|
* @param array $edges The adjacentcy list of the graph |
| 526 |
|
* @return boolean The success of writing out |
| 527 |
|
*/ |
| 528 |
|
function put_graph($edges) { |
| 529 |
|
if (!$file_s = fopen(DATA_PATH, "w")) { |
| 530 |
|
return FALSE; |
| 531 |
|
} |
| 532 |
|
if (!lock($file_s)) { |
| 533 |
|
return FALSE; |
| 534 |
|
} |
| 535 |
|
fwrite($file_s, '<?php $edges = ' . var_export($edges, TRUE) . ';?>'); |
| 536 |
|
fclose($file_s); |
| 537 |
|
return TRUE; |
| 538 |
|
} |
| 539 |
|
|
| 540 |
|
/** |
| 541 |
|
* Convert all the edges cost<->length |
| 542 |
|
* |
| 543 |
|
* @param array $edges The adjacentcy list of the graph |
| 544 |
|
* @return array $edges The adjacentcy list of the graph |
| 545 |
|
*/ |
| 546 |
|
function transform_edges($edges) { |
| 547 |
|
$min_max = get_min_and_max_strength($edges); |
| 548 |
|
$transformed_graph = array(); |
| 549 |
|
foreach (array_keys($edges) as $A) { |
| 550 |
|
foreach (array_keys($edges[$A]) as $B) { |
| 551 |
|
$transformed_graph[$A][$B] = get_edge_weight($edges, $A, $B, $min_max[0], $min_max[1]); |
| 552 |
|
} |
| 553 |
|
} |
| 554 |
|
return $transformed_graph; |
| 555 |
|
} |
| 556 |
|
|
| 557 |
?> |
?> |