includes/stats.php
changeset 61 e9708657875a
parent 21 663fcf528726
child 73 0a74676a2f2f
equal deleted inserted replaced
60:71b50f8c8f85 61:e9708657875a
    22     if(!$page_id || !$namespace)
    22     if(!$page_id || !$namespace)
    23     {
    23     {
    24       $page_id = $paths->cpage['urlname_nons'];
    24       $page_id = $paths->cpage['urlname_nons'];
    25       $namespace = $paths->namespace;
    25       $namespace = $paths->namespace;
    26     }
    26     }
    27     if($namespace == 'Special' || $namespace == 'Admin') return false;
    27     if($namespace == 'Special' || $namespace == 'Admin') 
       
    28     {
       
    29       return false;
       
    30     }
    28     static $stats_done = false;
    31     static $stats_done = false;
    29     static $stats_data = Array();
    32     static $stats_data = Array();
    30     if(!$stats_done)
    33     if(!$stats_done)
    31     {
    34     {
    32       $q = $db->sql_query('INSERT INTO '.table_prefix.'hits (username,time,page_id,namespace) VALUES(\''.$db->escape($session->username).'\', '.time().', \''.$db->escape($page_id).'\', \''.$db->escape($namespace).'\')');
    35       $q = $db->sql_query('INSERT INTO '.table_prefix.'hits (username,time,page_id,namespace) VALUES(\''.$db->escape($session->username).'\', '.time().', \''.$db->escape($page_id).'\', \''.$db->escape($namespace).'\')');
    49  */
    52  */
    50 
    53 
    51 function stats_top_pages($num = 5)
    54 function stats_top_pages($num = 5)
    52 {
    55 {
    53   global $db, $session, $paths, $template, $plugins; // Common objects
    56   global $db, $session, $paths, $template, $plugins; // Common objects
    54   if(!is_int($num)) return false;
    57   if(!is_int($num)) 
    55   $q = $db->sql_query('SELECT page_id,namespace FROM '.table_prefix.'hits ORDER BY page_id ASC, namespace ASC;');
       
    56   if(!$q)
       
    57   {
    58   {
    58     echo $db->get_error();
       
    59     return false;
    59     return false;
    60   }
    60   }
    61   $counter = Array();
    61   
       
    62   $data = array();
       
    63   $q = $db->sql_query('SELECT COUNT(hit_id) AS num_hits, page_id, namespace FROM hits GROUP BY page_id, namespace ORDER BY num_hits DESC LIMIT ' . $num . ';');
       
    64   
    62   while ( $row = $db->fetchrow() )
    65   while ( $row = $db->fetchrow() )
    63   {
    66   {
    64     $kname = $paths->nslist[$row['namespace']] . $row['page_id'];
    67     if ( isset($paths->pages[ $paths->nslist[ $row['namespace'] ] . $row['page_id'] ]) )
    65     if(isset($counter[$kname])) $counter[$kname]++;
    68     {
    66     else $counter[$kname] = 1;
    69       $page_data =& $paths->pages[ $paths->nslist[ $row['namespace'] ] . $row['page_id'] ];
       
    70       $title = $page_data['name'];
       
    71       $page_id = $page_data['urlname'];
       
    72     }
       
    73     else if ( !isset($paths->nslist[$row['namespace']]) )
       
    74     {
       
    75       $title = $row['namespace'] . ':' . $row['page_id'];
       
    76       $page_id = sanitize_page_id($title);
       
    77     }
       
    78     else
       
    79     {
       
    80       $title = dirtify_page_id( $paths->nslist[$row['namespace']] . $row['page_id'] );
       
    81       $title = str_replace('_', ' ', $title);
       
    82       $page_id = sanitize_page_id($title);
       
    83     }
       
    84     $data[] = array(
       
    85         'page_urlname' => $page_id,
       
    86         'page_title' => $title,
       
    87         'num_hits' => $row['num_hits']
       
    88       );
    67   }
    89   }
    68   $db->free_result();
    90   
    69   // Pure magic! At least I don't have to do the work...
    91   return $data;
    70   arsort($counter);
       
    71   // Can't use array_slice here because key names are only preserved in PHP5
       
    72   $k = array_keys($counter);
       
    73   $final = Array();
       
    74   if(sizeof($counter) < $num || $num == -1) $num = sizeof($counter);
       
    75   for ( $i = 0; $i < $num; $i++ )
       
    76   {
       
    77     $final[$k[$i]] = $counter[$k[$i]];
       
    78   }
       
    79   unset($counter, $k, $row);
       
    80   return $final;
       
    81 }
    92 }
    82 
    93 
    83 ?>
    94 ?>