includes/stats.php
changeset 1 fe660c52c48f
child 16 64e0d3d4cf14
equal deleted inserted replaced
0:902822492a68 1:fe660c52c48f
       
     1 <?php
       
     2 /*
       
     3  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
       
     4  * Version 1.0 (Banshee)
       
     5  * Copyright (C) 2006-2007 Dan Fuhry
       
     6  * stats.php - handles statistics for pages (disablable in the admin CP)
       
     7  *
       
     8  *   ***** UNFINISHED ***** 
       
     9  *
       
    10  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
       
    11  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
       
    12  *
       
    13  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    14  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
       
    15  */
       
    16 
       
    17 function doStats( $page_id = false, $namespace = false )
       
    18 {
       
    19   global $db, $session, $paths, $template, $plugins; // Common objects
       
    20   if(getConfig('log_hits') == '1')
       
    21   {
       
    22     if(!$page_id || !$namespace)
       
    23     {
       
    24       $page_id = $paths->cpage['urlname_nons'];
       
    25       $namespace = $paths->namespace;
       
    26     }
       
    27     if($namespace == 'Special' || $namespace == 'Admin') return false;
       
    28     static $stats_done = false;
       
    29     static $stats_data = Array();
       
    30     if(!$stats_done)
       
    31     {
       
    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).'\')');
       
    33       if(!$q)
       
    34       {
       
    35         echo $db->get_error();
       
    36         return false;
       
    37       }
       
    38       $db->free_result();
       
    39       $stats_done = true;
       
    40       return true;
       
    41     }
       
    42   }
       
    43 }
       
    44 
       
    45 /**
       
    46  * Fetch a list of the most-viewed pages
       
    47  * @param int the number of pages to return, send -1 to get all pages (suicide for large sites)
       
    48  * @return array key names are a string set to the page ID/namespace, and values are an int with the number of hits
       
    49  */
       
    50 
       
    51 function stats_top_pages($num = 5)
       
    52 {
       
    53   global $db, $session, $paths, $template, $plugins; // Common objects
       
    54   if(!is_int($num)) return false;
       
    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     echo $db->get_error();
       
    59     return false;
       
    60   }
       
    61   $counter = Array();
       
    62   while ( $row = $db->fetchrow() )
       
    63   {
       
    64     $kname = $paths->nslist[$row['namespace']] . $row['page_id'];
       
    65     if(isset($counter[$kname])) $counter[$kname]++;
       
    66     else $counter[$kname] = 1;
       
    67   }
       
    68   $db->free_result();
       
    69   // Pure magic! At least I don't have to do the work...
       
    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 }
       
    82 
       
    83 ?>