/[drupal]/contributions/modules/sna/explore.php
ViewVC logotype

Diff of /contributions/modules/sna/explore.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.4, Wed Aug 23 20:34:36 2006 UTC revision 1.5, Mon Sep 4 10:13:07 2006 UTC
# Line 13  Line 13 
13   */   */
14  require_once './includes/bootstrap.inc';  require_once './includes/bootstrap.inc';
15  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
16  require_once 'modules/sna/common.php';  require_once 'common.php';
   
 /**  
  * Create a graph from nodes-comments tables.  
  *  
  * @param array $edges The adjacentcy list of the graph  
  * @return integer The number of interactions  
  */  
 function build_edges_from_nodes(&$edges) {  
   $edges = array();  
   $node_replies_q = "SELECT users.uid as u2, users_1.uid as u1  
                      FROM {node} node, {users} users, {comments} comments, {users} users_1  
                      WHERE comments.nid = node.nid AND node.uid = users.uid  AND users_1.uid = comments.uid  
                      AND users.name <> '' AND users_1.name <> ''  
                      AND comments.pid = 0";  
   
   $comment_replies_q = "SELECT users_1.uid as 'u2', users.uid as 'u1'  
                         FROM {users} users, {comments} comments, {comments} comments_1, {users} users_1  
                         WHERE comments_1.cid = comments.pid  
                         AND users.uid = comments.uid  
                         AND users_1.uid = comments_1.uid  
                         AND users_1.name <> ''  
                         AND users.name <> ''";  
   
   if ((!$node_replies = db_query($node_replies_q)) || (!$comment_replies = db_query($comment_replies_q))) {  
     die("Database problem\n");  
   }  
   while ($line = db_fetch_array($comment_replies)) {  
     if ($line["u1"] != $line["u2"]) { // Do not do hitches  
       $edges[$line["u1"]][$line["u2"]]++;  
     }  
   }  
   while ($line = db_fetch_array($node_replies)) {  
     if ($line["u1"] != $line["u2"]) {  
       $edges[$line["u1"]][$line["u2"]]++;  
     }  
   }  
   return db_num_rows($node_replies) + db_num_rows($comment_replies);  
 }  
   
 /**  
  * Create a graph from the buddylist module data  
  *  
  * @param array $edges The adjacentcy list of the graph  
  * @return integer The numer of connections  
  */  
 function build_edges_from_buddy(&$edges) {  
   $edges = array();  
   $buddy_q = "SELECT uid, buddy FROM {buddylist}";  
   if ((!$buddies = db_query($buddy_q))) {  
     die("Database problem\n");  
   }  
   while ($line = db_fetch_array($buddies)) {  
     $edges[$line["uid"]][$line["buddy"]]++;  
   }  
   return db_num_rows($buddies);  
 }  
   
 /**  
  * Create a graph from accesslog table. The connection is to view other's profile  
  *  
  * @param array $edges The adjacentcy list of the graph  
  * @return integer The number of connections  
  */  
 function build_edges_from_stats(&$edges) {  
   $edges = array();  
   $stats_q = "SELECT uid, path FROM {accesslog} WHERE path LIKE 'user/%'";  
   if ((!$stats = db_query($stats_q))) {  
     die("Database problem\n");  
   }  
   while ($line = db_fetch_array($stats)) {  
     $dest = str_replace("user/", "", $line["path"]);  
     if (is_numeric($dest) && $dest != $line["uid"]) {  
       $edges[$line["uid"]][$dest]++;  
     }  
   }  
   return db_num_rows($stats);  
 }  
   
 /**  
  * Create a dot file from the graph to Graphviz  
  * Graphviz is a graph visualization tool  
  *  
  * @param array $edges The adjacentcy list of the graph  
  * @param $num_interactions Number of edges in the graph  
  * @return boolean The success of writing out the file  
  */  
 function generate_graphviz_input($edges, $num_interactions) {  
   $dot_graph = "digraph G {\n";  
   foreach ($edges as $u1 => $sub_arr) {  
     if ($u1 === 0) {    // Anonymous - don't count them!  
       break;  
     }  
     foreach ($sub_arr as $u2 => $num) {  
       if ($u2 === 0) {  // Anonymous - don't count them!  
         break;  
       }  
       $dot_graph .= "\t\"". get_real_name($u1).  
                     "\" -> \"". get_real_name($u2) .  
                     "\" [label=". round($edges[$u1][$u2], 2) ."];\n";  
     }  
   }  
   $dot_graph .= "}";  
   if (!$fp = fopen(DOT_PATH, "w")) {  
     return FALSE;  
   }  
   // Write out the DOT file  
   fwrite($fp, $dot_graph);  
   fclose($fp);  
   return TRUE;  
 }  
   
 /**  
  * Create a net file from the graph to Pajek  
  * Pajek is a graph analizer and visualization tool  
  * http://vlado.fmf.uni-lj.si/pub/networks/pajek/  
  *  
  * @param array $edges The adjacentcy list of the graph  
  * @return boolean The success of writing out the file  
  */  
 function generate_pajek_input($edges) {  
   // Count unique vertex  
   $edg = "*Edges\n";  
   $vertex = get_all_vertices();  
   $num_vertex = count($vertex);  
   for ($i = 0; $i < $num_vertex; $i++) {  
     $vert .= ($i + 1) ." \"". get_real_name($vertex[$i]) ."\"\n";  
     // in NET files we have to index points in a strict order, uid is not suitable  
     $real_id[$vertex[$i]] = $i + 1;  
   }  
   foreach ($edges as $vx_from => $next) {  
     if ($vx_from === 0) {    // Anonymous - don't count them!  
       break;  
     }  
     foreach (array_keys($next) as $vx_to) {  
       if ($vx_to === 0) {    // Anonymous - don't count them!  
         break;  
       }  
       $edg .= $real_id[$vx_from] ." ". $real_id[$vx_to] ." ". $edges[$vx_from][$vx_to] ."\n";  
     }  
   }  
   $net .= "*Vertices ". $num_vertex ."\n". $vert . $edg;  
   if (!$fp = fopen(NET_PATH, "w")) {  
     return FALSE;  
   }  
   fwrite($fp, $net);  
   fclose($fp);  
   return TRUE;  
 }  
   
 /**  
  * Write out the includable graph data  
  *  
  * @param array $edges The adjacentcy list of the graph  
  * @return boolean The success of writing out  
  */  
 function put_graph($edges) {  
   if (!$file_s = fopen(DATA_PATH, "w")) {  
     return FALSE;  
   }  
   if (!lock($file_s)) {  
     return FALSE;  
   }  
   fwrite($file_s, '<?php $edges = ' . var_export($edges, TRUE) . ';?>');  
   fclose($file_s);  
   return TRUE;  
 }  
   
 /**  
  * Convert all the edges cost<->length  
  *  
  * @param array $edges The adjacentcy list of the graph  
  * @return array $edges The adjacentcy list of the graph  
  */  
 function transform_edges($edges) {  
   $min_max = get_min_and_max_strength($edges);  
   $transformed_graph = array();  
   foreach (array_keys($edges) as $A) {  
     foreach (array_keys($edges[$A]) as $B) {  
       $transformed_graph[$A][$B] = get_edge_weight($edges, $A, $B, $min_max[0], $min_max[1]);  
     }  
   }  
   return $transformed_graph;  
 }  
17    
18  $at_start = res_start();  $at_start = res_start();
19  if (!function_exists('dba_open') && SNA_CACHE_ENABLED) {  if (!function_exists('dba_open') && SNA_CACHE_ENABLED) {
# Line 220  $limit = $most_popular_size < $size_cach Line 37  $limit = $most_popular_size < $size_cach
37  $at_start = res_start();  $at_start = res_start();
38  for ($i = 0; $i < $limit; $i++) {  for ($i = 0; $i < $limit; $i++) {
39    a_to_any($edges, $most_popular[$i][1], TRUE);    a_to_any($edges, $most_popular[$i][1], TRUE);
   print $most_popular[$i][1] . " cached\n";  
40  }  }
41  generate_graphviz_input($edges, $num_interactions);  generate_graphviz_input($edges, $num_interactions);
42  generate_pajek_input($edges);  generate_pajek_input($edges);
 print_r(res_stop($at_start));  
43    
44  ?>  ?>

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

  ViewVC Help
Powered by ViewVC 1.1.2