PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/details/compare.php

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