/[pdpsoft]/trunk/nl.nikhef.ndpf.tools/network-stats-mashup/includes/class.trunk.php
ViewVC logotype

Contents of /trunk/nl.nikhef.ndpf.tools/network-stats-mashup/includes/class.trunk.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1288 - (show annotations) (download) (as text)
Fri Dec 11 23:07:26 2009 UTC (12 years, 8 months ago) by aramv
File MIME type: text/x-php
File size: 5868 byte(s)
Minor UI fixes, since we don't have averaged graphs for the metatrunks
1 <?php
2 class Trunk {
3 public $trunk_page_url, // the url of the cricket page for this trunk
4 $metatrunk_page_url, // the url of a page where the constituent links of this trunk can be found
5 $name, // friendly name of the trunk
6 $constituents = array(), // an array of links that compose the current link
7 $daily_image_url, // the url to the daily graph image
8 $weekly_image_url, // the url to the weekly graph image
9 $random_token, // a random token, to ensure images are fresh
10 $is_constituent;
11
12 public function __construct($trunk_page_url, $metatrunk_page_url, $is_constituent = false){
13 if($this->is_relative_url($trunk_page_url)){
14 $this->trunk_page_url = $this->get_absolute_url() . $trunk_page_url;
15 } else {
16 $this->trunk_page_url = $trunk_page_url;
17 $this->get_cricket_images_from_url($trunk_page_url);
18 }
19
20 $this->metatrunk_page_url = $metatrunk_page_url;
21 $this->random_token = rand();
22 $this->is_constituent = $is_constituent;
23 if(!$this->is_constituent){
24 $this->find_constituents();
25 }
26
27 $this->name = $this->name_from_url($trunk_page_url);
28 }
29
30 public function to_html(){
31 $html_string = '<h2><a href=' . $this->trunk_page_url . '">' . $this->name . '</a></h2><p>';
32 if(!empty($this->constituents)){
33 $html_string .= '<div class="constituents_div"><ul>';
34 foreach($this->constituents as $index => $constituent){
35 $html_string .= '<li><a href="' . $constituent->trunk_page_url . '" alt="' . $constituent->name . '"><b>' . $constituent->name . '</b></li>';
36 }
37 $html_string .= '</ul></div>';
38 }
39 if($this->daily_image_url){
40 $html_string .= '<a href="' . $this->trunk_page_url . '" title="' . $this->name . '"><img alt="' . $this->name . '" src="' . $this->daily_image_url() . '"/></a><br/>';
41 }
42 if($this->weekly_image_url){
43 $html_string .= '<a href="' . $this->trunk_page_url . '" title="' . $this->name. '"><img alt="' . $this->name . '" src="' . $this->weekly_image_url() . '"/></a>';
44 }
45 $html_string .= '</p>';
46 return $html_string;
47 }
48
49 public function daily_image_url(){
50 return $this->daily_image_url . $this->random_token;
51 }
52
53 public function weekly_image_url(){
54 return $this->weekly_image_url . $this->random_token;
55 }
56
57 /* returns the two key components from the url, separated by <-> delimiter */
58 private function name_from_url($url){
59 $name = "";
60 $name_parts = split("%2F", CricketSpider::code_from_url($url));
61 if(@$name_parts[1]){
62 if($this->is_constituent){
63 $name = $name_parts[1];
64 } else {
65 $name = $name_parts[0] . " &harr; " . $name_parts[1];
66 }
67 } else {
68 $url_parts = split("/", $url);
69 $name = $url_parts[1];
70 }
71 return $name;
72 }
73
74 private function find_constituents(){
75 $constituent_urls = CricketSpider::get_urls_from_html_file($this->metatrunk_page_url);
76 foreach($constituent_urls[3] as $index => $url){
77 $this->constituents[] = new Trunk($constituent_urls[3][$index], $constituent_urls[2][$index], true);
78 }
79 }
80
81 private function is_relative_url($url){
82 return !stristr($url, "http://");
83 }
84
85 /* will set the daily_image_url and weekly_image_url if the provided url points to a remote page */
86 private function get_cricket_images_from_url($url){
87 $cricket_images_url_cache = CricketSpider::$cache_dir . '/' . md5($url);
88 if(CricketSpider::probe_cache($cricket_images_url_cache)){
89 $data = unserialize(file_get_contents($cricket_images_url_cache));
90 } else {
91 $local_urls = array();
92 $page = "";
93 /* apparently cricket checks the UA string and tries to display GIFs to wget, which don't work for some reason */
94 /* image links */
95 $page = shell_exec("wget -O- --user-agent=Firefox " . escapeshellarg($url));
96 preg_match_all('/<img src="(.*);rand=\d+".+/', $page, $local_urls);
97 /* $local_urls holds matched items, $local_urls[1] holds the matched part between ()s.
98 $local_urls[1][0] is the daily graph, $local_urls[1][1] is the weekly graph */
99 /* new_url is an array of:
100 url to page
101 url to graph image
102 name of the graph
103 */
104 $data = array("http://www.dutchgrid.nl/ndpf/cricket/" . $local_urls[1][0] . ";rand=", "http://www.dutchgrid.nl/ndpf/cricket/" . $local_urls[1][1] . ";rand=");
105 file_put_contents($cricket_images_url_cache, serialize($data));
106 }
107 $this->daily_image_url = $data[0];
108 $this->weekly_image_url = $data[1];
109 }
110
111 private function get_absolute_url(){
112 $full_url = 'http';
113 $script_name = '';
114 if(isset($_SERVER['REQUEST_URI'])) {
115 $script_name = $_SERVER['REQUEST_URI'];
116 } else {
117 $script_name = $_SERVER['PHP_SELF'];
118 if($_SERVER['QUERY_STRING']>' ') {
119 $script_name .= '?'.$_SERVER['QUERY_STRING'];
120 }
121 }
122
123 /* strip index.php from script name */
124
125 if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on') {
126 $full_url .= 's';
127 }
128 $full_url .= '://';
129 if($_SERVER['SERVER_PORT']!='80') {
130 $full_url .=
131 $_SERVER['HTTP_HOST'].':'.$_SERVER['SERVER_PORT'].$script_name;
132 } else {
133 $full_url .= $_SERVER['HTTP_HOST'].$script_name;
134 }
135
136 /* strip trailing slash */
137 return $full_url;
138 }
139
140
141 //function make_html_snippets($data, $token){
142 //$html_string = '<h2>Combined trunk</h2><p><ul id="dock-container">';
143 //foreach($data as $index => $trunk_data){
144 //$html_string .= '<li><a href="' . $trunk_data[0] . '" title ="' . $trunk_data[3] . '"><img alt="' . $trunk_data[3] . '" src="' . $trunk_data[1] . '" class="thumbnail"/></a></li>';
145 //}
146 //$html_string .= '</p></ul>';
147 //return $html_string;
148 //}
149
150 }
151 ?>
152

grid.support@nikhef.nl
ViewVC Help
Powered by ViewVC 1.1.28