Revision [19515]
This is an old revision of TRBCounter made by KrzysztofTrybowski on 2008-02-03 20:28:27.
Page Hit Counter
This is an updated version of GmBowenCounter. It has been tested with Wikka 1.1.7.0. Since the time when original description was made, many things in Wikka code changed, hence I made this description so that those who don't feel strong enough in PHP can still benefit from a counter. :)The basics are more or less as in original version:
- the counter doesn't increment if it's your own page, only for the pages of others;
- to have page's visits counted, you must add counter action to the contents;
- if you want it to keep a count, but without showing the number (so people don't have the urge to "up" the number on you), you can set the parameter show="off" or show="no";
- if you want to keep the count hidden from readers but visible for page owner, you can set the parameter show="owner";
1. Update your database schema
The _pages table has to have the following field added:`hits` int(50) NOT NULL default '0'
2. Create an action file counter.php in a proper place
For an unstable Wikka 1.1.7.0 this will be actions/counter/counter.php.For Wikka 1.1.6.3 this will be actions/counter.php.
<?php
$pagetag=$this->GetPageTag();
// Get hit count
$result = mysql_query( "SELECT hits FROM ".$this->config["table_prefix"]."pages WHERE tag='$pagetag' AND latest='Y'");
$row = mysql_fetch_array($result);
$hit_count = $row['hits'];
// Count is incremented if not your own page
// NOTE: we are checking if handler == show: this way we prevent counter from being updated while viewing page's history.
if (($this->GetHandler() == 'show') && ($this->GetUserName() != $this->GetPageOwner($tag)))
{
// Start Update Hit
$hit_count+=1;
mysql_query("UPDATE `".$this->config["table_prefix"]."pages` SET `hits` = `hits` + 1 WHERE tag='$pagetag' AND latest='Y'") or die("Unable to process query: " . mysql_error());
}
// Parameter 'show' being checked for
$show = strtolower($show);
if (($show !='off' && $show !='no' && $show !='owner') or ($show =='owner' && $this->GetUserName() == $this->GetPageOwner($tag)))
{
// Start output of counter
echo ("<span class=\"counter\">Total Hits: <span class=\"hits\">".$hit_count."</span></span>");
}
?>
$pagetag=$this->GetPageTag();
// Get hit count
$result = mysql_query( "SELECT hits FROM ".$this->config["table_prefix"]."pages WHERE tag='$pagetag' AND latest='Y'");
$row = mysql_fetch_array($result);
$hit_count = $row['hits'];
// Count is incremented if not your own page
// NOTE: we are checking if handler == show: this way we prevent counter from being updated while viewing page's history.
if (($this->GetHandler() == 'show') && ($this->GetUserName() != $this->GetPageOwner($tag)))
{
// Start Update Hit
$hit_count+=1;
mysql_query("UPDATE `".$this->config["table_prefix"]."pages` SET `hits` = `hits` + 1 WHERE tag='$pagetag' AND latest='Y'") or die("Unable to process query: " . mysql_error());
}
// Parameter 'show' being checked for
$show = strtolower($show);
if (($show !='off' && $show !='no' && $show !='owner') or ($show =='owner' && $this->GetUserName() == $this->GetPageOwner($tag)))
{
// Start output of counter
echo ("<span class=\"counter\">Total Hits: <span class=\"hits\">".$hit_count."</span></span>");
}
?>
3. Preventing the reset of the counter
If you only use the above code the counter will re-set to zero every time you edit a page. You have to do the following changes as well If you don't want that to happen.3a. Editing edit.php handler
In an unstable Wikka 1.1.7.0 open a file handlers/edit/edit.php. In Wikka 1.1.6.3 open a file handlers/page/edit.php. Search for a line:$this->SavePage($this->tag, $body, $note);
$this->SavePage($this->tag, $body, $note, $this->page['hits']);
3b. Editing the Wakka class
Open a file libs/Wakka.class.php and search for function SavePage. In my unstable Wikka 1.1.7.0 it is around line 1867. In official 1.1.6.3 it is in line 608.Change
function SavePage($tag, $body, $note)
function SavePage($tag, $body, $note, $pagehits)
Just a few lines below note the line:
user = '".mysql_real_escape_string($username)."',
hits = '".mysql_real_escape_string($pagehits)."',
4. Edit your wikka.css file (optional)
You may change the way a counter is displayed on page, by adding lines to css/wikka.css, for example like this:.counter {font-size: smaller;} /* influences whole counter text */
.counter .hits {font-weight: bolder;} /* influences just the number */
.counter .hits {font-weight: bolder;} /* influences just the number */
So, that's it. It works for me. And it has a few drawbacks:
- the text is not translatable (an issue for Wikka 1.1.7.0 that supports language packs)
- you need to add an action to every page that should be counted (it's not global)
- editing a page adds +1 to a counter
- there should be some sort of cookie check, so that multiple refreshes don't increment the counter ad infinitum
- not implemented: GmBowen's proposal of a parameter ignore="name1, name2, name3" so that certain users wouldn't increment a counter
- it should be better fitted in Wikka file-structure
I intend to deal with these problems later.
CategoryUserContributions