Revision [19520]
This is an old revision of TRBCounter made by KrzysztofTrybowski on 2008-02-04 08:58:49.
Page Hit Counter
version 0.3
The idea
Let's count how many users read each page, so that we know what we should focus on!The details
This code is based on GmBowenCounter and is compatible with it in terms of database structure and counter action syntax. Therefore you should be fine to update from GmBowenCounter to this one. Also MostVisited add-on should be working fine. Since the time when original version 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. :)It has been tested with Wikka 1.1.7.0 but should work with 1.1.6.3.
The basics are more or less as in original version:
- you can add a counter to every page or turn it on globally for whole site;
- if you choose not to enable the counter globally, you must add an action called counter to the contents of a page to have its visits counted;
- the counter doesn't increment if you visit your own page;
- 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 to page owner, you can set the parameter show="owner";
- if you don't want to keep a count on a certain page, but you have enabled the counter globally, you can set the parameter show="disabled";
What's new in this version
since TRBCounter 0.2
- New global config directive page_hit_counter_ignore_users -- a comma-separated list of usernames that won't increment a counter
- Action counter has additional parameter ignore="name1, name2, name3" that overrides the above global setting
- New global config directive page_hit_counter_disable_pages -- a comma-separated list of pagenames that will have counter disabled (if it is enabled globally by show_page_hit_counter directive)
- Changed the way translation is made -- it’s now possibile to use number within a text, for example “So far this page has been visited by 35 people”.
- This is probably the last version compatible with the GmBowenCounter original release by GmBowen.
- Known issue: viewing older revision of a page doesn't increment a counter.
since TRBCounter 0.1
- Counter display moved to page's footer (where it really belongs).
- It's global now. You can add a config directive show_page_hit_counter to your wikka.config.php in order to enable/hide/disable the counter for all pages.
- The main code moved to a separate file counter.lib.php, that is called from footer.php.
- Action counter works as in 0.1 but it only changes the value of show_page_hit_counter directive.
- Action counter parameter show="disabled" added.
- Action nocounter added. An alias to show="off" setting.
- The text 'Total hits' is now translatable (in Wikka 1.1.7.0). Compatibility with Wikka 1.1.6.3 hopefuly retained (anybody test please!) ;)
- Page editing no longer adds +1 to a counter
- Known issue: viewing older revision of a page doesn't increment a counter.
since the original release by GmBowen
- code cleanup (just one SQL query, instead of two, less variables used)
- show="owner" -- will show the counter only to the page owner
- some checks so that counter is only incremented while reading a page (and not seeing page's history for example, which was the case previously)
- the output can now be formatted by CSS (previously done on tables)
- Known issue: page editing adds +1 to a counter
TODO
- keep a separate count of visits since new revision of a page (perhaps should be nice to be able to define that an edit is 'minor' and thus the count shouldn't reset then)
- there should be some sort of cookie check, so that multiple refreshes don't increment the counter ad infinitum
- make it compatible with my nofooter action (my own purposes) -- should count even if footer is not displayed
- it should be better fitted in Wikka file-structure; maybe should be written as a class?
- allow ignore parameter of counter action ADD or DELETE username from page_hit_counter_ignore_users list by writing ignore="+UserName1, -UserName2"
Instructions -- how to set up a counter in your Wikka-powered site
1. Update your database schema
The xx_pages table has to have the following field added (note that xx_ should be substituted with your prefix):`hits` int(50) NOT NULL default '0'
2. Create a file counter.lib.php
Create a file libs/counter.lib.php (feel free to choose a different location and update the file footer.php accordingly -- see point 6. below).<?php
// Let's grab the setting
$show = strtolower($this->GetConfigValue('show_page_hit_counter'));
if ($show == '') $show = 'disabled';
if ($show != 'disabled') {
$pagetag=$this->GetPageTag();
// Let's prepare a list of PageNames that should have counter disabled
$disable_pages = explode(',',strtr($this->GetConfigValue('page_hit_counter_disable_pages'), array(" " => "")));
// Let's delete possible empty entries
foreach ($disable_pages as $i => $v) if ($v == '') unset($disable_pages[$i]);
// Now, if current page is on that list -- disable counter
if (array_search($pagetag, $disable_pages)) $show = 'disabled';
}
// If the counter isn't off and we are allowed to read the page, proceed...
if (($show != 'disabled') && ($this->HasAccess('read')))
{
// 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'];
// Let's prepare a list of UserNames that shouldn't increment the counter.
$ignore_users = explode(',',strtr($this->GetConfigValue('page_hit_counter_ignore_users'),array(" " => "")));
// Let's remove incorrect names.
foreach ($ignore_users as $i => $v) if (!$this->IsWikiName($v)) unset($ignore_users[$i]);
// Let's add current user -- so that the list is ready
array_push($ignore_users, $this->GetPageOwner($tag));
// Counter is incremented if currently logged user in not in $ignore_users
// We check if handler == show to prevent counter from being incremented while viewing page's history.
// We only increment while viewing latest revision (older revisions are probably being viewed by editors).
if (($this->GetHandler() == 'show') && ($this->page['latest'] == 'Y') && (!array_search($this->GetUserName(), $ignore_users)))
{
$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());
}
// Should we display the counter?
if (($show !='off' && $show !='owner') or ($show =='owner' && $this->GetUserName() == $this->GetPageOwner($tag)))
{
// Yes, let's see if the translation is available
if (!defined('PAGE_HIT_COUNTER')) define('PAGE_HIT_COUNTER','Total hits: %s');
// Start output of counter
echo (":: <span class=\"counter\">"); echo sprintf(PAGE_HIT_COUNTER,"<span class=\"hits\">".$hit_count."</span>"); echo ("</span>");
}
}
?>
// Let's grab the setting
$show = strtolower($this->GetConfigValue('show_page_hit_counter'));
if ($show == '') $show = 'disabled';
if ($show != 'disabled') {
$pagetag=$this->GetPageTag();
// Let's prepare a list of PageNames that should have counter disabled
$disable_pages = explode(',',strtr($this->GetConfigValue('page_hit_counter_disable_pages'), array(" " => "")));
// Let's delete possible empty entries
foreach ($disable_pages as $i => $v) if ($v == '') unset($disable_pages[$i]);
// Now, if current page is on that list -- disable counter
if (array_search($pagetag, $disable_pages)) $show = 'disabled';
}
// If the counter isn't off and we are allowed to read the page, proceed...
if (($show != 'disabled') && ($this->HasAccess('read')))
{
// 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'];
// Let's prepare a list of UserNames that shouldn't increment the counter.
$ignore_users = explode(',',strtr($this->GetConfigValue('page_hit_counter_ignore_users'),array(" " => "")));
// Let's remove incorrect names.
foreach ($ignore_users as $i => $v) if (!$this->IsWikiName($v)) unset($ignore_users[$i]);
// Let's add current user -- so that the list is ready
array_push($ignore_users, $this->GetPageOwner($tag));
// Counter is incremented if currently logged user in not in $ignore_users
// We check if handler == show to prevent counter from being incremented while viewing page's history.
// We only increment while viewing latest revision (older revisions are probably being viewed by editors).
if (($this->GetHandler() == 'show') && ($this->page['latest'] == 'Y') && (!array_search($this->GetUserName(), $ignore_users)))
{
$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());
}
// Should we display the counter?
if (($show !='off' && $show !='owner') or ($show =='owner' && $this->GetUserName() == $this->GetPageOwner($tag)))
{
// Yes, let's see if the translation is available
if (!defined('PAGE_HIT_COUNTER')) define('PAGE_HIT_COUNTER','Total hits: %s');
// Start output of counter
echo (":: <span class=\"counter\">"); echo sprintf(PAGE_HIT_COUNTER,"<span class=\"hits\">".$hit_count."</span>"); echo ("</span>");
}
}
?>
3. 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
switch(strtolower($show)) {
case 'disabled':
case 'disable':
$this->SetConfigValue('show_page_hit_counter','disabled');
break;
case 'off':
case 'no':
$this->SetConfigValue('show_page_hit_counter','off');
break;
case 'owner':
$this->SetConfigValue('show_page_hit_counter','owner');
break;
default:
$this->SetConfigValue('show_page_hit_counter','on');
break;
}
if ($ignore != '') $this->SetConfigValue('page_hit_counter_ignore_users',$ignore);
?>
switch(strtolower($show)) {
case 'disabled':
case 'disable':
$this->SetConfigValue('show_page_hit_counter','disabled');
break;
case 'off':
case 'no':
$this->SetConfigValue('show_page_hit_counter','off');
break;
case 'owner':
$this->SetConfigValue('show_page_hit_counter','owner');
break;
default:
$this->SetConfigValue('show_page_hit_counter','on');
break;
}
if ($ignore != '') $this->SetConfigValue('page_hit_counter_ignore_users',$ignore);
?>
4. Create an action file nocounter.php in a proper place (optional)
If you wish to have the possibility to easily disable counter for a given page, while counter is globally enabled, create an action file nocounter.php.For an unstable Wikka 1.1.7.0 this will be actions/nocounter/counter.php.
For Wikka 1.1.6.3 this will be actions/nocounter.php.
<?php
$this->SetConfigValue('show_page_hit_counter','disabled');
?>
$this->SetConfigValue('show_page_hit_counter','disabled');
?>
5. Preventing the reset of the counter
If you only use the above code the counter will reset to zero every time you edit a page. The following changes fix that.5a. 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);
and change it to:
$this->SavePage($this->tag, $body, $note, $this->page['hits']);
It should be in line number 130 (1.1.7.0) or line number 133 (1.1.6.3).
5b. 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)
to
function SavePage($tag, $body, $note, $pagehits)
Just a few lines below note the line:
user = '".mysql_real_escape_string($username)."',
and add a following line after it:
hits = '".mysql_real_escape_string($pagehits)."',
6. Add the code to your footer.php
For an unstable Wikka 1.1.7.0 this will be templates/footer.php.For Wikka 1.1.6.3 this will be actions/footer.php.
Search for:
<?php echo $this->FormClose(); ?>
and above it add:
<?php require_once($_SERVER["DOCUMENT_ROOT"].'libs/counter.lib.php'); ?>
7. Add translation for the 'Total hits' text (Wikka >= 1.1.7.0 only) (optional)
Open lang/xx/xx.inc.php (substitute xx for a code of your language). Search for a line beginning with define('SEARCH_LABEL', and add the following line below it:...of course replacing Total hits with your translated text. The symbol %s will be replaced by a number.
8. Add a global counter settings (optional)
Open wikka.config.php and add the following lines:'show_page_hit_counter' => 'on',
This will globally set your counter on/off and display/hide it. Instead of 'on' you can use 'off' or 'owner'. The default is 'disabled'.
'page_hit_counter_ignore_users' => 'UserName1, UserName2',
This will set usernames that shouldn't increment the counter. It doesn't influence if counter is displayed. You can override this setting by using action's counter parameter ignore="".
'page_hit_counter_disable_pages' => 'PageIndex, RecentChanges, RecentlyCommented, MostVisited, UserSettings, TextSearch, TextSearchExpanded',
This will disable counter on the pages listed above. Customize your list as desired.
9. 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 */
Please test and give feedback. Regards, --KrzysztofTrybowski
CategoryUserContributions