stats_referers_to_array.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /***************************************************************************\
  3. * SPIP, Systeme de publication pour l'internet *
  4. * *
  5. * Copyright (c) 2001-2014 *
  6. * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
  7. * *
  8. * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
  9. * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
  10. \***************************************************************************/
  11. if (!defined('_ECRIRE_INC_VERSION')) return;
  12. include_spip('inc/statistiques');
  13. // moyenne glissante sur 30 jours
  14. define('MOYENNE_GLISSANTE_JOUR', 30);
  15. // moyenne glissante sur 12 mois
  16. define('MOYENNE_GLISSANTE_MOIS', 12);
  17. include_spip('inc/referenceurs');
  18. function inc_stats_referers_to_array_dist($limit, $jour, $id_article, $options = array()) {
  19. $visites = 'visites';
  20. $table = "spip_referers";
  21. $where = array();
  22. $serveur = '';
  23. if (in_array($jour,array('jour','veille'))){
  24. $visites .= "_$jour";
  25. $where[] = "$visites>0";
  26. }
  27. //$res = $referenceurs (0, "SUM(visites_$jour)", 'spip_referers', "visites_$jour>0", "referer", $limit);
  28. if ($id_article) {
  29. $table = "spip_referers_articles";
  30. $where[] = "id_article=".intval($id_article);
  31. }
  32. $where = implode(" AND ",$where);
  33. $limit = $limit?"0,".intval($limit):'';
  34. $result = sql_select("referer_md5, referer, $visites AS vis", $table, $where, '', "maj DESC", $limit,'',$serveur);
  35. $referers = array();
  36. $trivisites = array(); // pour le tri
  37. while ($row = sql_fetch($result,$serveur)) {
  38. $referer = interdire_scripts($row['referer']);
  39. $buff = stats_show_keywords($referer, $referer);
  40. if ($buff["host"]) {
  41. $refhost = $buff["hostname"];
  42. $visites = $row['vis'];
  43. $host = $buff["scheme"]."://".$buff["host"];
  44. $referers[$refhost]['referer_md5'] = $row['referer_md5'];
  45. if (!isset($referers[$refhost]['liens'][$referer]))
  46. $referers[$refhost]['liens'][$referer] = 0;
  47. if (!isset($referers[$refhost]['hosts'][$host]))
  48. $referers[$refhost]['hosts'][$host] = 0;
  49. if (!isset($referers[$refhost]['visites']))
  50. $referers[$refhost]['visites'] = 0;
  51. if (!isset($referers[$refhost]['visites_racine']))
  52. $referers[$refhost]['visites_racine'] = 0;
  53. if (!isset($referers[$refhost]['referers']))
  54. $referers[$refhost]['referers'] = array();
  55. $referers[$refhost]['hosts'][$host]++;
  56. $referers[$refhost]['liens'][$referer]++ ;
  57. $referers[$refhost]['visites'] += $visites;
  58. $trivisites[$refhost] = $referers[$refhost]['visites'];
  59. $tmp = "";
  60. $set = array(
  61. 'referer'=>$referer,
  62. 'visites'=>$visites,
  63. 'referes'=>$id_article?'':referes($row['referer_md5'])
  64. );
  65. if (isset($buff["keywords"])
  66. AND $c = $buff["keywords"]) {
  67. if (!isset($referers[$refhost]['keywords'][$c])) {
  68. $referers[$refhost]['keywords'][$c] = true;
  69. $set['keywords'] = $c;
  70. }
  71. }
  72. else {
  73. $tmp = $buff["path"];
  74. if ($buff["query"])
  75. $tmp .= "?".$buff['query'];
  76. if (strlen($tmp))
  77. $set['path'] = "/$tmp";
  78. }
  79. if (isset($set['path']) OR isset($set['keywords']))
  80. $referers[$refhost]['referers'][] = $set;
  81. else
  82. $referers[$refhost]['visites_racine'] += $visites;
  83. }
  84. }
  85. // trier les liens pour trouver le principal
  86. foreach($referers as $k=>$r){
  87. arsort($referers[$k]['liens']);
  88. $referers[$k]['liens'] = array_keys($referers[$k]['liens']);
  89. arsort($referers[$k]['hosts']);
  90. $referers[$k]['hosts'] = array_keys($referers[$k]['hosts']);
  91. $referers[$k]['url'] = reset($referers[$k]['hosts']);
  92. }
  93. if (count($trivisites)){
  94. array_multisort($trivisites, SORT_DESC,$referers);
  95. }
  96. return $referers;
  97. }
  98. ?>