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

Contents of /contributions/modules/sna/common.php

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


Revision 1.5 - (show annotations) (download) (as text)
Mon Sep 4 10:32:55 2006 UTC (3 years, 2 months ago) by aronnovak
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5, DRUPAL-4-7
Changes since 1.4: +4 -4 lines
File MIME type: text/x-php
Fix bad directory paths
1 <?php
2 // $Id: common.php,v 1.4 2006/09/04 10:13:07 aronnovak Exp $
3 /**
4 * Common things in SNA related files
5 *
6 * Store functions, constants that almost all file needs in SNA module
7 * @author Aron Novak <aaron@szentimre.hu
8 * @version 0.1
9 * @package sna
10 */
11
12 /**
13 * The path of data files. This path can be modify trough drupal settings.
14 */
15 define('FILES_PATH', '/path/to/a/secure/dir/');
16 /**
17 * The path to the visualization applet
18 */
19 define('APPLET_PATH', '/modules/sna/applet/');
20 /**
21 * Set the dba_handler
22 */
23 define('DBA_HANDLER', 'gdbm');
24
25 // Set some starting value
26 if (function_exists('variable_get')) {
27 $files_path = variable_get('sna_data_path', FILES_PATH);
28 $pic_size = variable_get('sna_pic_size', 300);
29 }
30 else {
31 $files_path = FILES_PATH;
32 $pic_size = 300;
33 }
34
35 /**
36 * Here store the whole graph. Should be inaccessible for www user
37 */
38 define('DATA_PATH', $files_path . 'ser.dat');
39 //define('DATA_PATH', './ser.dat');
40 /**
41 * The Graphviz file's path
42 */
43 define('DOT_PATH', $files_path . 'my.dot');
44 /**
45 * The Pajek file's path
46 */
47 define('NET_PATH', $files_path . 'my.net');
48 /**
49 * The minimal tree of shortest route - cache
50 */
51 define('CACHE_PATH', $files_path . 'min_trees');
52 /**
53 * It's a very bad idea to rewrite this to FALSE.
54 * If you do not have any dba_handler available in PHP
55 * and you want to try out this module it's a possible
56 * solution to turn this off.
57 */
58 define('SNA_CACHE_ENABLED', TRUE);
59
60 /**
61 * Possible options: nodes, buddy, stats
62 */
63 define('GRAPH_SOURCE', 'nodes');
64 /**
65 * At cron-time the script generate the top NUM_CACHE users minimal tree
66 */
67 define('NUM_CACHE', '10');
68 /**
69 * SVG picture width
70 */
71 define('PIC_WIDTH', $pic_size);
72 /**
73 * SVG picture height - same as width, we need 1:1 side proportion!
74 */
75 define('PIC_HEIGHT', PIC_WIDTH);
76 define('ROUTE_SHORTEST', 0);
77 define('ROUTE_MIN_STEP', 1);
78 /**
79 * Performace tester helper function. Start a timer and measure the current memory usage
80 *
81 * @return array Startup measured time and memory usage
82 */
83 function res_start() {
84 static $timer_index;
85 $mem_start = function_exists("memory_get_usage") ? memory_get_usage() : 0;
86 timer_start($timer_index);
87 return array(&$timer_index, $mem_start);
88 }
89
90 /**
91 * Performace tester helper function. Compute the diff between res_start's figures.
92 *
93 * @param array $at_start Startup measured time and memory usage
94 */
95 function res_stop($at_start) {
96 $res["time"] = timer_read($at_start[0]) / 1000 . " sec";
97 $res["mem"] = function_exists("memory_get_usage") ? (memory_get_usage() - $at_start[1]) / 1024 . " kB" : "NaN";
98 timer_stop($at_start[0]++);
99 return $res;
100 }
101
102 /**
103 * Lock the data file to avoid data corruption
104 *
105 * @param file_pointer $file_p
106 * @return boolean success or not
107 */
108 function lock($file_p) {
109 while (!flock($file_p, LOCK_EX)) {
110 $i++;
111 usleep(10);
112 if ($i > 10) {
113 return FALSE;
114 }
115 }
116 return TRUE;
117 }
118
119 /**
120 * Get the nickname of a user
121 *
122 * @param user_id $uid Drupal uid of the user
123 * @return nickname The uid's drupal nickname
124 */
125 function get_real_name($uid) {
126 $name_q = "SELECT name FROM {users} WHERE uid = %d";
127 $name = db_query($name_q, $uid);
128 $line = db_fetch_array($name);
129 return $line["name"];
130 }
131
132 /**
133 * Get from the database all the users' uid
134 *
135 * @return array All the users uid
136 */
137 function get_all_vertices() {
138 $vertices_q = "SELECT uid FROM {users} WHERE status = 1";
139 $vertices = db_query($vertices_q);
140 while ($line = db_fetch_array($vertices)) {
141 $users[] = $line['uid'];
142 }
143 return $users;
144 }
145
146 /**
147 * Get from the database all the users' uid and name to and assoc array
148 *
149 * @return array All the users uid and name
150 */
151 function get_all_vertices_for_forms() {
152 $vertices_q = "SELECT uid, name FROM {users} WHERE status = 1";
153 $vertices = db_query($vertices_q);
154 while ($line = db_fetch_array($vertices)) {
155 $users[$line['uid']] = $line['name'];
156 }
157 return $users;
158 }
159
160 /**
161 * Return the number of out-edges
162 *
163 * @param array $edges The adjacentcy list of the graph
164 * @param integer $vertex The vertex's id
165 * @return integer The number of out-edges
166 */
167 function vertex_degree($edges, $vertex) {
168 return count($edges[$vertex]);
169 }
170
171 /**
172 * Check if the minimal tree of <var>$vertex</var> is in cache or not
173 *
174 * @param integer $vertex The vertex's id
175 * @return boolean In cache or not
176 */
177 function is_in_cache($vertex) {
178 if (SNA_CACHE_ENABLED) {
179 if (!$db = dba_open(CACHE_PATH, "c", DBA_HANDLER)) {
180 die("Cannot open database\n");
181 }
182 $data = dba_fetch($vertex, $db);
183 dba_close($db);
184 return $data === FALSE ? FALSE : unserialize($data);
185 } else {
186 return FALSE;
187 }
188 }
189
190 /**
191 * Put the generated minimal tree to the cache
192 *
193 * @param integer $vertex The vertex's id
194 * @param array $data The minimal routes tree of $vertex
195 * @return boolean Success or not
196 */
197 function put_in_cache($vertex, $data) {
198 if (SNA_CACHE_ENABLED) {
199 if (!$db = dba_open(CACHE_PATH, "c", DBA_HANDLER)) {
200 return FALSE;
201 }
202
203 if (!dba_insert($vertex, serialize($data), $db)) {
204 dba_close($db);
205 return FALSE;
206 }
207 dba_close($db);
208 return TRUE;
209 }
210 else { // If the caching is off, we imitate that everything works fine.
211 return TRUE;
212 }
213 }
214
215 /**
216 * Find all the shortest routes from one vertex - Dijkstra algorithm
217 *
218 * @param array $edges The adjacentcy list of the graph
219 * @param integer $a The vertex's id
220 * @return array The minimal tree of routes
221 */
222 function a_to_any($edges, $a, $in_explore = false) {
223 if (!$in_explore) {
224 if ($data = is_in_cache($a)) {
225 return $data;
226 }
227 }
228
229 /* Set start values and load all the vertices in Q row */
230 $Q = array();
231 foreach ($edges as $key => $rel) {
232 $d[$key] = '-'; // Distance from $a
233 $p[$key] = '-';
234 $Q[] = $key;
235 foreach (array_keys($rel) as $child) {
236 $d[$child] = '-'; // Distance from $a
237 $p[$child] = '-';
238 $Q[] = $child;
239 }
240 }
241 /* The distance of start point is 0 */
242 $d[$a] = 0;
243 $Q[] = $a;
244 /* Delete duplicated elements */
245 $Q = array_unique($Q);
246
247 while (count($Q)) {
248 /* Search the minimal d in Q */
249 $u = reset($Q);
250 $min = $d[$u];
251 $min_key = key($Q);
252
253 foreach ($Q as $vk => $vertex) {
254 if (($min > $d[$vertex] || $min === '-') && is_numeric($d[$vertex])) {
255 $min = $d[$vertex];
256 $u = $vertex;
257 $min_key = $vk;
258 }
259 }
260 unset($Q[$min_key]);
261 /* Test the edges of $u vertex */
262 if (isset($edges[$u]) && $d[$u] !== '-') {
263 foreach (array_keys($edges[$u]) as $next) {
264 /* Test if get a shorter route - relaxation */
265 $w = $edges[$u][$next];
266 if ($d[$next] > $d[$u] + $w || $d[$next] === '-') {
267 $d[$next] = $d[$u] + $w;
268 $p[$next] = $u;
269 }
270 }
271 }
272 }
273 $out = array('dist' => $d, 'prev' => $p);
274 put_in_cache($a, $out);
275 return $out;
276 }
277
278 /**
279 * This is the graph's cost function
280 * In a weighted graph or digraph,
281 * each edge is associated with some value,
282 * variously called its cost, weight, length
283 *
284 * @param array $edges The adjacentcy list of the graph
285 * @param integer $a From verticle
286 * @param integer $b To verticle
287 * @return integer The weight of the edge or FALSE if the edge is not set
288 */
289 function get_edge_weight($edges, $a, $b, $min, $max) {
290 if (isset($edges[$a][$b])) {
291 /*
292 * The function is a line. This line contains two points
293 * x1 y1 x2 y2
294 * P1 (min, max_length) and P2 (max, min_length)
295 * min_length: 1 , max_length: 10
296 * The line's equation : y - y1 = (y2 - y1) / (x2 - x1) (x - x1)
297 * The result is a number between 1 and 10. 1 represents the
298 * strongest connection.
299 */
300 if ($max != $min) {
301 $y = (- 9 / ($max - $min) * ($edges[$a][$b] - $min)) + 10;
302 }
303 else { // Avoid division by zero!
304 $y = 1;
305 }
306 return $y;
307 }
308 else { // No edge from a to b
309 return FALSE;
310 }
311 }
312
313 /**
314 * Search the smallest and the biggest value in the <var>$edges</var> array that
315 * represents connection strength
316 *
317 * @param array $edges The adjacentcy list of the graph
318 * @return array The strength of the minimum and the maximum edge
319 */
320 function get_min_and_max_strength($edges) {
321 $degrees = array();
322 foreach ($edges as $neighbours) {
323 foreach ($neighbours as $degree) {
324 $degrees[] = $degree;
325 }
326 }
327 sort($degrees);
328 return array(reset($degrees), end($degrees));
329 }
330
331 /**
332 * Sort the edges by the number of out-edges
333 *
334 * @param array $edges The adjacentcy list of the graph
335 * @param integer $limit Return the <var>$limit</var> - top of the users
336 * @return array The sorted vertices list
337 */
338 function sort_by_popularity($edges, $limit = -1) {
339 $popularity = array();
340 foreach (array_keys($edges) as $vertex) {
341 $popularity[] = array(vertex_degree($edges, $vertex), $vertex);
342 }
343 usort($popularity, "compare");
344 return $limit === -1 ? $popularity : array_slice($popularity, 0, $limit);
345 }
346
347 /**
348 * Special compare function to sort multi-dimensional array easily
349 *
350 * @param array $a
351 * @param array $b
352 * @return boolean $b is greater than $a
353 */
354 function compare($a, $b) {
355 if ($a[0] < $b[0]) {
356 return TRUE;
357 }
358 else {
359 return FALSE;
360 }
361 }
362
363 /**
364 * Clear the dbm file that store the cached minimal routes
365 *
366 */
367 function clear_cache() {
368 if (SNA_CACHE_ENABLED) {
369 $db = dba_open(CACHE_PATH, "n", DBA_HANDLER);
370 dba_close($db);
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 ?>

  ViewVC Help
Powered by ViewVC 1.1.2