I fixed the statistics!!! YAY!!
authorDan
Tue, 10 Jul 2007 12:31:12 -0400
changeset 61 e9708657875a
parent 60 71b50f8c8f85
child 62 9dc4fded30e6
I fixed the statistics!!! YAY!!
includes/pageprocess.php
includes/stats.php
index.php
plugins/SpecialAdmin.php
--- a/includes/pageprocess.php	Tue Jul 10 11:59:02 2007 -0400
+++ b/includes/pageprocess.php	Tue Jul 10 12:31:12 2007 -0400
@@ -130,10 +130,11 @@
   }
   
   /**
-   * The main method to send the page content. Also responsible for checking permissions.
+   * The main method to send the page content. Also responsible for checking permissions and calling the statistics counter.
+   * @param bool If true, the stat counter is called. Defaults to false.
    */
   
-  function send()
+  function send( $do_stats = false )
   {
     global $db, $session, $paths, $template, $plugins; // Common objects
     if ( !$this->perms->get_permissions('read') )
@@ -160,6 +161,10 @@
         }
       }
     }
+    if ( $this->page_exists && $this->namespace != 'Special' && $this->namespace != 'Admin' && $do_stats )
+    {
+      doStats($this->page_id, $this->namespace);
+    }
     if ( $this->namespace == 'Special' || $this->namespace == 'Admin' )
     {
       if ( !$this->page_exists )
@@ -226,8 +231,17 @@
       
       if ( empty($ob) )
       {
+        die('ob is empty');
         $this->err_page_not_existent();
       }
+      else
+      {
+        // Something sent content, so we'll assume the page exist...ed at least according to the plugin
+        if ( $this->namespace != 'Special' && $this->namespace != 'Admin' && $do_stats )
+        {
+          doStats($this->page_id, $this->namespace);
+        }
+      }
     }
     else // (disabled for compatibility reasons) if ( in_array($this->namespace, array('Article', 'User', 'Project', 'Help', 'File', 'Category')) && $this->page_exists )
     {
--- a/includes/stats.php	Tue Jul 10 11:59:02 2007 -0400
+++ b/includes/stats.php	Tue Jul 10 12:31:12 2007 -0400
@@ -24,7 +24,10 @@
       $page_id = $paths->cpage['urlname_nons'];
       $namespace = $paths->namespace;
     }
-    if($namespace == 'Special' || $namespace == 'Admin') return false;
+    if($namespace == 'Special' || $namespace == 'Admin') 
+    {
+      return false;
+    }
     static $stats_done = false;
     static $stats_data = Array();
     if(!$stats_done)
@@ -51,33 +54,41 @@
 function stats_top_pages($num = 5)
 {
   global $db, $session, $paths, $template, $plugins; // Common objects
-  if(!is_int($num)) return false;
-  $q = $db->sql_query('SELECT page_id,namespace FROM '.table_prefix.'hits ORDER BY page_id ASC, namespace ASC;');
-  if(!$q)
+  if(!is_int($num)) 
   {
-    echo $db->get_error();
     return false;
   }
-  $counter = Array();
+  
+  $data = array();
+  $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 . ';');
+  
   while ( $row = $db->fetchrow() )
   {
-    $kname = $paths->nslist[$row['namespace']] . $row['page_id'];
-    if(isset($counter[$kname])) $counter[$kname]++;
-    else $counter[$kname] = 1;
+    if ( isset($paths->pages[ $paths->nslist[ $row['namespace'] ] . $row['page_id'] ]) )
+    {
+      $page_data =& $paths->pages[ $paths->nslist[ $row['namespace'] ] . $row['page_id'] ];
+      $title = $page_data['name'];
+      $page_id = $page_data['urlname'];
+    }
+    else if ( !isset($paths->nslist[$row['namespace']]) )
+    {
+      $title = $row['namespace'] . ':' . $row['page_id'];
+      $page_id = sanitize_page_id($title);
+    }
+    else
+    {
+      $title = dirtify_page_id( $paths->nslist[$row['namespace']] . $row['page_id'] );
+      $title = str_replace('_', ' ', $title);
+      $page_id = sanitize_page_id($title);
+    }
+    $data[] = array(
+        'page_urlname' => $page_id,
+        'page_title' => $title,
+        'num_hits' => $row['num_hits']
+      );
   }
-  $db->free_result();
-  // Pure magic! At least I don't have to do the work...
-  arsort($counter);
-  // Can't use array_slice here because key names are only preserved in PHP5
-  $k = array_keys($counter);
-  $final = Array();
-  if(sizeof($counter) < $num || $num == -1) $num = sizeof($counter);
-  for ( $i = 0; $i < $num; $i++ )
-  {
-    $final[$k[$i]] = $counter[$k[$i]];
-  }
-  unset($counter, $k, $row);
-  return $final;
+  
+  return $data;
 }
 
 ?>
--- a/index.php	Tue Jul 10 11:59:02 2007 -0400
+++ b/index.php	Tue Jul 10 12:31:12 2007 -0400
@@ -56,7 +56,7 @@
       $page->send_headers = true;
       $pagepass = ( isset($_REQUEST['pagepass']) ) ? sha1($_REQUEST['pagepass']) : '';
       $page->password = $pagepass;
-      $page->send();
+      $page->send(true);
       break;
     case 'comments':
       $template->header();
--- a/plugins/SpecialAdmin.php	Tue Jul 10 11:59:02 2007 -0400
+++ b/plugins/SpecialAdmin.php	Tue Jul 10 12:31:12 2007 -0400
@@ -85,18 +85,16 @@
   if(getConfig('log_hits') == '1')
   {
     $stats = stats_top_pages(10);
+    //die('<pre>'.print_r($stats,true).'</pre>');
     $c = 0;
     $cls = 'row2';
     echo '<h3>Most requested pages</h3><div class="tblholder"><table style="width: 100%;" border="0" cellspacing="1" cellpadding="4"><tr><th>Page</th><th>Hits</th></tr>';
-    foreach($stats as $page => $count)
+    foreach($stats as $data)
     {
-      if(isset($paths->pages[$page]))
-      {
-        echo '<tr>';
-        $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
-        echo '<td class="'.$cls.'"><a href="'.makeUrl($page).'">'.$paths->pages[$page]['name'].'</a></td><td style="text-align: center;" class="'.$cls.'">'.$count.'</td>';
-        echo '</tr>';
-      }
+      echo '<tr>';
+      $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
+      echo '<td class="'.$cls.'"><a href="'.makeUrl($data['page_urlname']).'">'.$data['page_title'].'</a></td><td style="text-align: center;" class="'.$cls.'">'.$data['num_hits'].'</td>';
+      echo '</tr>';
     }
     echo '</table></div>';
   }