/details/compare.php
PHP | 321 lines | 266 code | 40 blank | 15 comment | 71 complexity | a3835318b3674d25bf378ce899611a4d MD5 | raw file
1<?php 2require_once(dirname(dirname(__FILE__)).'/global.php'); 3require_once(dirname(dirname(__FILE__)).'/users/users.php'); 4 5$badinput = false; 6$urls = array(); 7$noinput = true; 8 9if (!array_key_exists('url', $_GET) || !is_array($_GET['url'])) { 10 $badinput = true; 11} else { 12 $noinput = false; 13 foreach ($_GET['url'] as $url) 14 { 15 if ($url == '') { 16 continue; 17 } 18 19 if (($url = filter_var($url, FILTER_VALIDATE_URL)) === false) { 20 $badinput = true; 21 break; 22 } 23 24 $urls[] = $url; 25 } 26} 27 28$urls = array_unique($urls); 29 30if (count($urls) < 2) { 31 $badinput = true; 32} 33 34$data = array(); 35 36// flags indicating if there is data for each of the rankers 37$counters = array( 38 'yslow' => 0, 39 'pagespeed' => 0, 40 'dynatrace' => 0 41); 42 43if (count($urls) > 0) { 44 // last event timestamp 45 $first = true; 46 $urllist = ''; 47 foreach ($urls as $url) { 48 if ($first) { 49 $first = false; 50 } 51 else { 52 $urllist .= ', '; 53 } 54 55 $urllist .= sprintf("'%s'", mysql_real_escape_string($url)); 56 } 57 58 $query = "SELECT urls.id, url, 59 y.timestamp as y_version, 60 p.timestamp as p_version, 61 d.timestamp as d_version 62 FROM urls 63 LEFT JOIN yslow2 y ON urls.yslow2_last_id = y.id 64 LEFT JOIN pagespeed p ON urls.pagespeed_last_id = p.id 65 LEFT JOIN dynatrace d ON urls.dynatrace_last_id = d.id 66 WHERE urls.url IN ($urllist)"; 67 $result = mysql_query($query); 68 69 if (!$result) { 70 error_log(mysql_error()); 71 } 72 73 // loading all data 74 while ($row = mysql_fetch_assoc($result)) { 75 $data[$row['url']] = array( 76 'yslow' => $row['y_version'], 77 'pagespeed' => $row['p_version'], 78 'dynatrace' => $row['d_version'] 79 ); 80 81 // if at least one value exists for ranker, enable it 82 if (!is_null($row['y_version'])) { 83 $counters['yslow'] += 1; 84 } 85 if (!is_null($row['p_version'])) { 86 $counters['pagespeed'] += 1; 87 } 88 if (!is_null($row['d_version'])) { 89 $counters['dynatrace'] += 1; 90 } 91 } 92 mysql_free_result($result); 93} 94 95// let's see data for which rankers is available and redirect accordingly 96$ranker = null; 97$TITLE = 'Compare rankings'; 98 99if (array_key_exists('ranker', $_GET)) { 100 if ($_GET['ranker'] == 'yslow') { 101 $ranker = 'yslow'; 102 $TITLE = 'Compare YSlow rankings'; 103 } else if ($_GET['ranker'] == 'pagespeed') { 104 $ranker = 'pagespeed'; 105 $TITLE = 'Compare Page Speed rankings'; 106 } else if ($_GET['ranker'] == 'dynatrace') { 107 $ranker = 'dynatrace'; 108 $TITLE = 'Compare dynaTrace rankings'; 109 } 110} 111 112error_log("ranker: $ranker"); 113 114// calculate query string 115$params = ''; 116$first = true; 117foreach ($urls as $url) { 118 if ($first) { 119 $first = false; 120 } 121 else { 122 $params.= '&'; 123 } 124 $params.='url[]='.urlencode($url); 125} 126 127// if ranker is not specified, but there is data for at least one ranker, redirect to that ranker 128if (is_null($ranker)) { 129 $default = null; 130 // in reverse order to override if data for ranker exists 131 if ($counters['dynatrace'] > 0) { $default = 'dynatrace'; } 132 if ($counters['pagespeed'] > 0) { $default = 'pagespeed'; } 133 if ($counters['yslow'] > 0) { $default = 'yslow'; } 134 135 // if there is data for the urls then use default ranker, otherwise display a form 136 if (!is_null($default)) { 137 header('Location: ?ranker='.$default.'&'.$params); 138 exit; 139 } 140} 141 142// if some URLs passed, add them to the title and load javascripts 143if (!$badinput) { 144 $TITLE .= ' for: '.implode(', ', $urls); 145 146 $SCRIPTS = array( 147 'http://yui.yahooapis.com/combo?2.8.1/build/yahoo/yahoo-min.js&2.8.1/build/event/event-min.js&2.8.1/build/yuiloader/yuiloader-min.js', 148 $showslow_base.'ajax/simile-ajax-api.js?bundle=true', 149 $showslow_base.'timeline/timeline-api.js?bundle=true', 150 $showslow_base.'timeplot/timeplot-api.js?bundle=true', 151 assetURL('details/compare.js') 152 ); 153} 154 155$SECTION = 'compare'; 156require_once(dirname(dirname(__FILE__)).'/header.php'); 157?> 158<style> 159.details { 160 cursor: help; 161} 162</style> 163 164<h1 style="margin-bottom: 0">Compare rankings</h1> 165<?php 166 167// only display menu if user picked the ranker specifically 168// (otherwise either there is no data or we don't reach this point and get redirected to default) 169if (!is_null($ranker)) { ?> 170 <div>Ranker: <b> 171 <?php 172 // let's see which menus should be displayed and which one is current 173 $menus = array(); 174 if ($ranker == 'yslow') { 175 $menus[] = '<span>YSlow</span>'; // current 176 } else { 177 if ($counters['yslow'] > 1) { // display link only if data for more then one URL is available 178 $menus[] = '<span><a href="?'.$params.'">YSlow</a></span>'; 179 } 180 } 181 182 if ($ranker == 'pagespeed') { 183 $menus[] = '<span>Page Speed</span>'; // current 184 } else { 185 if ($counters['pagespeed'] > 1) { // display link only if data for more then one URL is available 186 $menus[] = '<span><a href="?ranker=pagespeed&'.$params.'">Page Speed</a></span>'; 187 } 188 } 189 190 if ($ranker == 'dynatrace') { 191 $menus[] = '<span>dynaTrace</span>'; // current 192 } else { 193 if ($counters['dynatrace'] > 1) { // display link only if data for more then one URL is available 194 $menus[] = '<span><a href="?ranker=dynatrace&'.$params.'">dynaTrace</a></span>'; 195 } 196 } 197 198 echo implode(' | ', $menus); 199 ?> 200 </b></div> 201<?php } ?> 202 203<ul style="margin-top: 1em"> 204<?php foreach ($urls as $url) { ?> 205 <li> 206 <a href="./?url=<?php echo urlencode($url)?>"><?php echo htmlentities(substr($url, 0, 60))?><?php if (strlen($url) > 60) { ?>...<?php } ?></a><?php 207 208 if (is_null($data[$url][$ranker])) { 209 ?> (no <?php 210 if ($ranker == 'yslow') { echo 'YSlow '; } 211 if ($ranker == 'pagespeed') { echo 'Page Speed '; } 212 if ($ranker == 'dynatrace') { echo 'dynaTrace '; } 213 ?>data)<?php 214 } 215 ?></li> 216<?php } ?> 217</ul> 218<?php 219 220$enough_data = false; 221 222if (!is_null($ranker)) { 223 $colors = array( 224 '#FD4320', 225 '#3E25FA', 226 '#3EDF16', 227 '#EEB423', 228 '#F514B5' 229 ); 230 231 // now, let's calculate data to display for this ranker 232 $data_to_display = array(); 233 $colorindex = 0; 234 foreach ($data as $url => $versions) { 235 if (is_null($versions[$ranker])) { 236 continue; 237 } 238 239 $data_to_display[$url] = array( 240 'version' => urlencode($versions[$ranker]), 241 'color' => $colors[$colorindex] 242 ); 243 244 if ($colorindex == count($colors)) 245 { 246 $colorindex = 0; 247 } else { 248 $colorindex += 1; 249 } 250 } 251 252 if (count($data_to_display) >= 2) { 253 $enough_data = true; 254 255 // Graph 256 ?> 257 <script> 258 data = <?php echo json_encode($data_to_display)?>; 259 ranker = '<?php echo $ranker ?>'; 260 </script> 261 <?php if (!is_null($addThisProfile)) {?> 262 <!-- AddThis Button BEGIN --> 263 <div class="addthis_toolbox addthis_default_style" style="margin-right: 10px;"> 264 <a href="http://www.addthis.com/bookmark.php?v=250&username=<?php echo urlencode($addThisProfile)?>" class="addthis_button_compact">Share</a> 265 <span class="addthis_separator">|</span> 266 <a class="addthis_button_twitter"></a> 267 <a class="addthis_button_facebook"></a> 268 <a class="addthis_button_google"></a> 269 <a class="addthis_button_delicious"></a> 270 <a class="addthis_button_stumbleupon"></a> 271 <a class="addthis_button_reddit"></a> 272 <span class="addthis_separator">|</span> 273 <a class="addthis_button_favorites"></a> 274 <a class="addthis_button_print"></a> 275 <a class="addthis_button_email"></a> 276 </div> 277 <script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=<?php echo urlencode($addThisProfile)?>"></script> 278 <!-- AddThis Button END --> 279 <?php } ?> 280 281 <div id="my-timeplot" style="height: 250px; margin-top: 0.2em"></div> 282 <div style="fint-size: 0.2em"><?php 283 if ($ranker == 'yslow') { ?><b>YSlow</b> grades<?php } 284 if ($ranker == 'pagespeed') { ?><b>Page Speed</b> scores<?php } 285 if ($ranker == 'dynatrace') { ?><b>dynaTrace</b> ranks<?php } 286 ?> for: <?php 287 foreach ($urls as $url) { 288 if (!array_key_exists($url, $data_to_display)) { 289 continue; 290 } 291 ?><span style="font-weight: bold; color: <?php echo $data_to_display[$url]['color'] ?>"><?php echo $url ?></span> (0-100); 292 <?php 293 } 294 ?></div><?php 295 } 296} 297?> 298 299<form action="" method="GET"> 300<h3>Enter URL to compare:</h3> 301<?php if ($enough_data) { ?> 302<p>Enter up to 5 URLs in the form below:</p> 303<?php 304} else { 305 ?><p style="color: red; font-weight: bold">Not enought data to compare</p><?php 306} 307$inputs = 5; 308 309for ($i =0 ; $i < $inputs; $i++ ) { ?> 310 <input name="url[]" type="text" size="80" value="<?php 311 if ($i < count($urls)) { 312 echo htmlentities($urls[$i]); 313 } ?>"/><br/> 314<?php 315} 316?> 317<input type="hidden" name="ranker" value="<?php echo $ranker ?>"/> 318<input type="submit" value="compare"/> 319</form> 320<?php 321require_once(dirname(dirname(__FILE__)).'/footer.php');