includes/stats.php
changeset 1227 bdac73ed481e
parent 1081 745200a9cc2a
equal deleted inserted replaced
1226:de56132c008d 1227:bdac73ed481e
    14  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
    14  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
    15  */
    15  */
    16 
    16 
    17 function doStats( $page_id = false, $namespace = false )
    17 function doStats( $page_id = false, $namespace = false )
    18 {
    18 {
    19   global $db, $session, $paths, $template, $plugins; // Common objects
    19 	global $db, $session, $paths, $template, $plugins; // Common objects
    20   if(getConfig('log_hits') == '1')
    20 	if(getConfig('log_hits') == '1')
    21   {
    21 	{
    22     if(!$page_id || !$namespace)
    22 		if(!$page_id || !$namespace)
    23     {
    23 		{
    24       $page_id = $paths->page_id;
    24 			$page_id = $paths->page_id;
    25       $namespace = $paths->namespace;
    25 			$namespace = $paths->namespace;
    26     }
    26 		}
    27     if($namespace == 'Special' || $namespace == 'Admin') 
    27 		if($namespace == 'Special' || $namespace == 'Admin') 
    28     {
    28 		{
    29       return false;
    29 			return false;
    30     }
    30 		}
    31     static $stats_done = false;
    31 		static $stats_done = false;
    32     static $stats_data = Array();
    32 		static $stats_data = Array();
    33     if(!$stats_done)
    33 		if(!$stats_done)
    34     {
    34 		{
    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).'\')');
    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).'\')');
    36       if(!$q)
    36 			if(!$q)
    37       {
    37 			{
    38         echo $db->get_error();
    38 				echo $db->get_error();
    39         return false;
    39 				return false;
    40       }
    40 			}
    41       $db->free_result();
    41 			$db->free_result();
    42       $stats_done = true;
    42 			$stats_done = true;
    43       return true;
    43 			return true;
    44     }
    44 		}
    45   }
    45 	}
    46 }
    46 }
    47 
    47 
    48 /**
    48 /**
    49  * Fetch a list of the most-viewed pages
    49  * Fetch a list of the most-viewed pages
    50  * @param int the number of pages to return, send -1 to get all pages (suicide for large sites)
    50  * @param int the number of pages to return, send -1 to get all pages (suicide for large sites)
    51  * @return array key names are a string set to the page ID/namespace, and values are an int with the number of hits
    51  * @return array key names are a string set to the page ID/namespace, and values are an int with the number of hits
    52  */
    52  */
    53 
    53 
    54 function stats_top_pages($num = 5)
    54 function stats_top_pages($num = 5)
    55 {
    55 {
    56   global $db, $session, $paths, $template, $plugins; // Common objects
    56 	global $db, $session, $paths, $template, $plugins; // Common objects
    57   if(!is_int($num)) 
    57 	if(!is_int($num)) 
    58   {
    58 	{
    59     return false;
    59 		return false;
    60   }
    60 	}
    61   
    61 	
    62   $data = array();
    62 	$data = array();
    63   $q = $db->sql_query('SELECT COUNT(hit_id) AS num_hits, page_id, namespace FROM '.table_prefix.'hits GROUP BY page_id, namespace ORDER BY num_hits DESC LIMIT ' . $num . ';');
    63 	$q = $db->sql_query('SELECT COUNT(hit_id) AS num_hits, page_id, namespace FROM '.table_prefix.'hits GROUP BY page_id, namespace ORDER BY num_hits DESC LIMIT ' . $num . ';');
    64   
    64 	
    65   while ( $row = $db->fetchrow($q) )
    65 	while ( $row = $db->fetchrow($q) )
    66   {
    66 	{
    67     $title = get_page_title_ns($row['page_id'], $row['namespace']);
    67 		$title = get_page_title_ns($row['page_id'], $row['namespace']);
    68     $data[] = array(
    68 		$data[] = array(
    69         'page_urlname' => $paths->get_pathskey($row['page_id'], $row['namespace']),
    69 				'page_urlname' => $paths->get_pathskey($row['page_id'], $row['namespace']),
    70         'page_title' => $title,
    70 				'page_title' => $title,
    71         'num_hits' => $row['num_hits']
    71 				'num_hits' => $row['num_hits']
    72       );
    72 			);
    73   }
    73 	}
    74   
    74 	
    75   return $data;
    75 	return $data;
    76 }
    76 }
    77 
    77 
    78 ?>
    78 ?>