Revision history for TRBCounter


Revision [23334]

Last edited on 2016-05-20 07:38:47 by KrzysztofTrybowski [Replaces old-style internal links with new pipe-split links.]
Additions:
This version is compatible with the original GmBowenCounter in terms of **counter** action syntax. However it's not so with database structure. Namely, the meaning of //hits// field in **_pages** table is changed. It used to be "total hits for that page", while now it is "hits for that page since last edit". Hence, to get total hits in this version it's necessary to sum up //hits// values of all revisions of a page. The advantage is that now we could see how many hits did each revision have (this is planned for [[TRBMostVisited | my version of MostVisited action]]) Also this new approach doesn’t require altering of Wakka class. If you need a version that counts the old way, get TRBCounter version 0.3 (which is unfortunately not compatible with Wikka prior to 1.1.7.0).
~-Different way of counting - makes it incompatible with original GmBowenCounter, but allows for counting not only total hits, but also current version hits (since last edit). Surprisingly this new, different way of counting is what [[MostVisited | original MostVisited]] action expects (it turns out that [[GmBowenCounter]] and [[MostVisited]] are NOT compatible). See also [[TRBMostVisited | my version of MostVisited]].
~-This is probably the last version compatible with the [[GmBowenCounter | original release by GmBowen]].
Deletions:
This version is compatible with the original GmBowenCounter in terms of **counter** action syntax. However it's not so with database structure. Namely, the meaning of //hits// field in **_pages** table is changed. It used to be "total hits for that page", while now it is "hits for that page since last edit". Hence, to get total hits in this version it's necessary to sum up //hits// values of all revisions of a page. The advantage is that now we could see how many hits did each revision have (this is planned for [[TRBMostVisited my version of MostVisited action]]) Also this new approach doesn’t require altering of Wakka class. If you need a version that counts the old way, get TRBCounter version 0.3 (which is unfortunately not compatible with Wikka prior to 1.1.7.0).
~-Different way of counting - makes it incompatible with original GmBowenCounter, but allows for counting not only total hits, but also current version hits (since last edit). Surprisingly this new, different way of counting is what [[MostVisited original MostVisited]] action expects (it turns out that [[GmBowenCounter]] and [[MostVisited]] are NOT compatible). See also [[TRBMostVisited my version of MostVisited]].
~-This is probably the last version compatible with the [[GmBowenCounter original release by GmBowen]].


Revision [19598]

Edited on 2008-02-23 12:48:33 by KrzysztofTrybowski [small description improvement]
Additions:
~-While incrementing the count also date and time of last visit are stored. To enable this feature add //page_hit_counter_last_visited_enable// in your Wikka.config.php and set it to ‘true’. It will be used in TRBMostVisited action.
Deletions:
~-While incrementing the count also date and time of last visit are stored. To enable this feature add //page_hit_counter_last_visited_enable// in your Wikka.config.php and set it to ‘true’.


Revision [19597]

Edited on 2008-02-23 12:42:24 by KrzysztofTrybowski [version 0.6]
Additions:
==version 0.6==
This is a page hit counter, that counts how many people visit each of the pages at your Wikka-powered site. Installation requires adding some files and involves some small changes in two or three others as well as slight addictions to database structure.
This code is based on [[GmBowenCounter]]. Since the time when that original version was made, many things in Wikka code changed, hence I made this version so that those who don't feel strong enough in PHP can still benefit from a counter. This version also adds a lot of functionality.
~~-on – count and display the number;
~~-owner – count but display the number only to the page owner;
~~-off – count but don’t display the number;
~~-disabled – don’t count (turns the counter off completely).
~-While incrementing the count also date and time of last visit are stored. To enable this feature add //page_hit_counter_last_visited_enable// in your Wikka.config.php and set it to ‘true’.
This version is compatible with the original GmBowenCounter in terms of **counter** action syntax. However it's not so with database structure. Namely, the meaning of //hits// field in **_pages** table is changed. It used to be "total hits for that page", while now it is "hits for that page since last edit". Hence, to get total hits in this version it's necessary to sum up //hits// values of all revisions of a page. The advantage is that now we could see how many hits did each revision have (this is planned for [[TRBMostVisited my version of MostVisited action]]) Also this new approach doesn’t require altering of Wakka class. If you need a version that counts the old way, get TRBCounter version 0.3 (which is unfortunately not compatible with Wikka prior to 1.1.7.0).
This has been tested with Wikka 1.1.7.0 and 1.1.6.4RC1.
==0.6 (since TRBCounter 0.5)==
~-Stores date and time of last visit (only if counter is incremented, so all restrictions apply).
~-Slightly different database structure (now we use //unsigned// integer type, since negative values would be a nonsense anyway).
~-Fixed compatibility with Wikka 1.1.6.x (tested on 1.1.6.4) - was broken since version 0.2. Thanks for EmeraldIsland for pointing this out. From now on, my code is tested on Wikka 1.1.7.0 (trunk) and Wikka 1.1.6.4.
~-A cookie-based protection against counter incrementing through reloading. Set cookie lasts 6 hours, so every re-visit within this amount of time will not be counted.
~-New global config directive //page_hit_counter_ignore_users// -- a comma-separated list of usernames that won’t increment a counter
~-Changed the way translation is made -- it’s now possible to use number within a text, for example “So far this page has been visited by **35** people”.
~-The text 'Total hits' is now translatable (in Wikka 1.1.7.0). Compatibility with Wikka 1.1.6.3 hopefully retained.
The **//xx//_pages** table has to have the following fields added (note that xx_ should be substituted with your prefix):
%%(sql)ALTER TABLE `xx_pages` ADD `hits` INT UNSIGNED DEFAULT '0' NOT NULL;%%
%%(sql)ALTER TABLE `xx_pages` ADD `last_visited` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00';%%
If you don’t intend to use “last visited” functionality, you may omit the second one.
$ignore_users = explode(',',strtr($this->GetConfigValue('page_hit_counter_ignore_users'),array(" " => "", "'" => "")));
// Should we update the last_visited value?
if ($this->GetConfigValue('page_hit_counter_last_visited_enable'))
{
$this->Query("UPDATE `".$this->config["table_prefix"]."pages` SET `last_visited` = NOW() WHERE tag='$pagetag' AND latest='Y'") or die("Unable to process query: " . mysql_error());
}
For Wikka 1.1.6.x this will be **actions/counter.php**.
For Wikka 1.1.6.x this will be **actions/nocounter.php**.
**This section is only for people upgrading from [[GmBowenCounter]] or [[TRBCounter]] version prior to 0.4.**
%%(php)'page_hit_counter_last_visited_enable' => 'true',%%
This will turn on recording of a date and time of last visit.
Deletions:
==version 0.5==
This is a page hit counter, that counts how many people visit each of the pages at your Wikka-powered site. Installation requires adding two or three files and involves some small changes in two or three others.
This code is based on [[GmBowenCounter]]. Since the time when that 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. This version also adds a lot of functionality.
~~-on - count and display the number;
~~-owner - count but display the number only to the page owner;
~~-off - count but don't display the number;
~~-disabled - don't count (turns the counter off completely).
This version is compatible with the original GmBowenCounter in terms of **counter** action syntax. However it's not so with database structure. Namely, the meaning of //hits// field in **_pages** table is changed. It used to be "total hits for that page", while now it is "hits for that page since last edit". Hence, to get total hits in this version it's necessary to sum up //hits// values of all revisions of a page. The advantage is that now we could see how many hits did each revision have (this is planned for [[TRBMostVisited my version of MostVisited action]]. If you need a version that counts the old way, get TRBCounter version 0.3.
This has been tested with Wikka 1.1.7.0 but should work with 1.1.6.3.
~-Compatibility with Wikka 1.1.6.x (tested on 1.1.6.4). Thanks for EmeraldIsland for pointing this out. From now on, my code is tested on Wikka 1.1.7.0 (trunk) and Wikka 1.1.6.4.
~-A cookie-based protection against counter incrementing through reloading. Set cookie lasts 6 hours, so every visit within this amount of time will not be counted.
~-New global config directive //page_hit_counter_ignore_users// -- a comma-separated list of usernames that won't increment a counter
~-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”.
~-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!) ;)
~-Store date of last visit, so that we can create ""RecentlyVisited"" or add such column to MostVisited
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'%%
$ignore_users = explode(',',strtr($this->GetConfigValue('page_hit_counter_ignore_users'),array(" " => "")));
For Wikka 1.1.6.3 this will be **actions/counter.php**.
For Wikka 1.1.6.3 this will be **actions/nocounter.php**.
**This section is only for people upgrading from [[GmBowenCounter]] or [[TRBCounter]] versions prior to 0.4.**


Revision [19577]

Edited on 2008-02-22 09:36:48 by KrzysztofTrybowski [footer.php location changed in 1.1.6.4]
Additions:
For an unstable Wikka 1.1.7.0 and Wikka 1.1.6.4 this will be **templates/footer.php**.
Deletions:
For an unstable Wikka 1.1.7.0 this will be **templates/footer.php**.


Revision [19576]

Edited on 2008-02-22 09:21:48 by KrzysztofTrybowski [version 0.5]
Additions:
==version 0.5==
==0.5 (since TRBCounter 0.4)==
~-Compatibility with Wikka 1.1.6.x (tested on 1.1.6.4). Thanks for EmeraldIsland for pointing this out. From now on, my code is tested on Wikka 1.1.7.0 (trunk) and Wikka 1.1.6.4.
// For compatibility with Wikka 1.1.6.x
if ($this->method != '') $this_handler = $this->method; else $this_handler = $this->handler;
if (method_exists($this,'GetCookie')) $this_cookie = $this->GetCookie("TRBCounter".$pagetag); else $this_cookie = $this->GetWikkaCookie("TRBCounter".$pagetag);
if ((($this_handler == 'show') && ($this->page['latest'] == 'Y') && (!array_search($this->GetUserName(), $ignore_users))) && !$this_cookie)
// USING SetPersistentCookie FOR COMPATIBILITY WITH 1.1.6.x
$this->SetPersistentCookie("TRBCounter".$pagetag,"1",6);
Deletions:
==version 0.4==
if ((($this->GetHandler() == 'show') && ($this->page['latest'] == 'Y') && (!array_search($this->GetUserName(), $ignore_users))) && !$this->GetWikkaCookie("TRBCounter".$pagetag))
$this->SetWikkaCookie("TRBCounter".$pagetag,"1",6);


Revision [19526]

Edited on 2008-02-06 15:15:50 by KrzysztofTrybowski [version 0.4]
Additions:
>>//see also:// [[TRBMostVisited]]>>=====Page Hit Counter=====
==version 0.4==
This is a page hit counter, that counts how many people visit each of the pages at your Wikka-powered site. Installation requires adding two or three files and involves some small changes in two or three others.
This code is based on [[GmBowenCounter]]. Since the time when that 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. This version also adds a lot of functionality.
===How it works===
~-You can turn the counter on either globally (for the whole site) or just for some pages (using **counter** action).
~-If you choose to turn the counter on globally, just edit your Wikka.config.php and set //show_page_hit_counter// to either //on//, //owner// or //off//. The default setting is //disabled//.
~-The meaning of the setting mentioned above is:
~~-on - count and display the number;
~~-owner - count but display the number only to the page owner;
~~-off - count but don't display the number;
~~-disabled - don't count (turns the counter off completely).
~-If you choose to turn the counter on globally, you can still have it disabled on chosen pages (I keep it disabled for: ""PageIndex, RecentChanges, RecentlyCommented, MostVisited, UserSettings, CategoryCategory, TextSearch, TextSearchExpanded"" pages). Put //page_hit_counter_disable_pages// in your Wikka.config.php file and set it to comma-separated list of pages.
~-You can also make a list of users that shouldn't increment a counter (like editors for example). Put //page_hit_counter_ignore_users// in your Wikka.config.php file and set it to comma-separated list of usernames.
~-You can override the global, site-wide setting of a counter for any page individually (turn it on/off; hide it or show; set different list of ignored users). Use **counter** action with parameters: **show=on|owner|off|disable** and **ignore="""UserName1, UserName2, ..."""**.
~-Also if you choose NOT to turn the counter on globally, but want to have counter enabled for a certain page, you need to use **counter** action for that.
~-The counter doesn't increment if you visit your own page.
~-The counter remembers users that visited the page within last six hours, and doesn't increment within that time if they re-visit.
===Compatibility===
This version is compatible with the original GmBowenCounter in terms of **counter** action syntax. However it's not so with database structure. Namely, the meaning of //hits// field in **_pages** table is changed. It used to be "total hits for that page", while now it is "hits for that page since last edit". Hence, to get total hits in this version it's necessary to sum up //hits// values of all revisions of a page. The advantage is that now we could see how many hits did each revision have (this is planned for [[TRBMostVisited my version of MostVisited action]]. If you need a version that counts the old way, get TRBCounter version 0.3.
The original [[MostVisited]] by George Petsagourakis works fine with this code, since it seems it already expects data the way TRBCounter 0.4 (and newer) counts it.
This has been tested with Wikka 1.1.7.0 but should work with 1.1.6.3.
==0.4 (since TRBCounter 0.3)==
~-A cookie-based protection against counter incrementing through reloading. Set cookie lasts 6 hours, so every visit within this amount of time will not be counted.
~-Different way of counting - makes it incompatible with original GmBowenCounter, but allows for counting not only total hits, but also current version hits (since last edit). Surprisingly this new, different way of counting is what [[MostVisited original MostVisited]] action expects (it turns out that [[GmBowenCounter]] and [[MostVisited]] are NOT compatible). See also [[TRBMostVisited my version of MostVisited]].
~-Watch out for update instructions -- you'll have to revert changes in Wakka.class.php and edit.php.
==0.3 (since TRBCounter 0.2)==
~-Known issue: viewing older revision of a page doesn't increment a counter. Probably it will stay like this, so I won't mention it again.
==0.2 (since TRBCounter 0.1)==
==0.1 (since the original release by GmBowen)==
~-Store date of last visit, so that we can create ""RecentlyVisited"" or add such column to MostVisited
$showcounter = strtolower($this->GetConfigValue('show_page_hit_counter'));
if ($showcounter == '') $showcounter = 'disabled';
if ($showcounter != 'disabled') {
// If this page is blacklisted, disable counter
if (array_search($pagetag, $disable_pages)) $showcounter = 'disabled';
if (($showcounter != 'disabled') && ($this->HasAccess('read')))
$result = $this->Query( "SELECT SUM(hits) AS hits FROM ".$this->config["table_prefix"]."pages WHERE tag='$pagetag'");
// Let's add current user.
// We only increment if the cookie is not set.
if ((($this->GetHandler() == 'show') && ($this->page['latest'] == 'Y') && (!array_search($this->GetUserName(), $ignore_users))) && !$this->GetWikkaCookie("TRBCounter".$pagetag))
$this->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());
// Creating a cookie to make counter reload-proof (6 hours)
$this->SetWikkaCookie("TRBCounter".$pagetag,"1",6);
if (($showcounter !='off' && $showcounter !='owner') or ($showcounter =='owner' && $this->GetUserName() == $this->GetPageOwner($tag)))
If you keep your counter enabled globally, and wish to have the possibility to easily disable it, create an action file **nocounter.php**.
Writing ""{{nocounter}}"" is exactly the same as ""{{""counter show="disable"""}}"" just shorter.
===5. Reverting changes in Wakka class and edit handler===
**This section is only for people upgrading from [[GmBowenCounter]] or [[TRBCounter]] versions prior to 0.4.**
In previous versions of this add-on you were advised to make some changes to your edit handler and Wakka class. Now you need to revert them to originals.
==5a. Reverting edit.php handler==
==5b. Reverting the Wakka class==
Just a few lines below find the line:
and delete it.
Deletions:
=====Page Hit Counter=====
==version 0.3==
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";
==since TRBCounter 0.2==
==since TRBCounter 0.1==
==since the original release by GmBowen==
~-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
$show = strtolower($this->GetConfigValue('show_page_hit_counter'));
if ($show == '') $show = 'disabled';
if ($show != 'disabled') {
// Now, if current page is on that list -- disable counter
if (array_search($pagetag, $disable_pages)) $show = 'disabled';
if (($show != 'disabled') && ($this->HasAccess('read')))
$result = mysql_query( "SELECT hits FROM ".$this->config["table_prefix"]."pages WHERE tag='$pagetag' AND latest='Y'");
// Let's add current user -- so that the list is ready
if (($this->GetHandler() == 'show') && ($this->page['latest'] == 'Y') && (!array_search($this->GetUserName(), $ignore_users)))
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());
if (($show !='off' && $show !='owner') or ($show =='owner' && $this->GetUserName() == $this->GetPageOwner($tag)))
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**.
===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==
==5b. Editing the Wakka class==
Just a few lines below note the line:
%%(php)user = '".mysql_real_escape_string($username)."',%%
and add a following line after it:


Revision [19523]

Edited on 2008-02-05 10:42:33 by KrzysztofTrybowski [small typo in point 4.]
Additions:
For an unstable Wikka 1.1.7.0 this will be **actions/nocounter/nocounter.php**.
Deletions:
For an unstable Wikka 1.1.7.0 this will be **actions/nocounter/counter.php**.


Revision [19521]

Edited on 2008-02-04 09:05:45 by KrzysztofTrybowski [small description fix]
Additions:
~-Action **counter** has additional parameter //ignore="name1, name2, name3"// that overrides the above global setting
~-allow //ignore// parameter of **counter** action ADD or DELETE username from //page_hit_counter_ignore_users// list by writing ignore="+""UserName1"", -""UserName2"""
Deletions:
~-Action **counter** has additional parameter ignore="name1, name2, name3" that overrides the above global setting
~-allow //ignore// parameter of **counter** action ADD or DELETE username from //page_hit_counter_ignore_users// list by writing ignore="+UserName1, -UserName2"


Revision [19520]

Edited on 2008-02-04 08:58:49 by KrzysztofTrybowski [version 0.3]
Additions:
==version 0.3==
==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.
~-Page editing no longer adds +1 to a counter
~-Known issue: viewing older revision of a page doesn't increment a counter.
~-Known issue: page editing adds +1 to a counter
~-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)
~-allow //ignore// parameter of **counter** action ADD or DELETE username from //page_hit_counter_ignore_users// list by writing ignore="+UserName1, -UserName2"
if ($show != 'disabled') {
// 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';
// 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)))
if (!defined('PAGE_HIT_COUNTER')) define('PAGE_HIT_COUNTER','Total hits: %s');
echo (":: <span class=\"counter\">"); echo sprintf(PAGE_HIT_COUNTER,"<span class=\"hits\">".$hit_count."</span>"); echo ("</span>");
if ($ignore != '') $this->SetConfigValue('page_hit_counter_ignore_users',$ignore);
%%(php)define('PAGE_HIT_COUNTER', 'Total hits: %s');%%
...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:
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'.
%%(php)'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=""//.
%%(php)'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.
Deletions:
==version 0.2==
~-Known issue: editing a page adds +1 to a counter
~-keep a separate count of visits since new version 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)
~-GmBowen's proposal of a parameter ignore="name1, name2, name3" so that certain users wouldn't increment a counter
~-Ignored pages global setting, so that certain pages won't be counted by default (good if you use MostVisited add-on and don't want 'technical' pages, like textsearch, listed)
~-Ignored users global, so that certain users don't increment the counter
// Let's default to 'disabled'
// 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.
// we only want to count latest versions of the page being displayed, since older versions are probably being viewed by
// users working on them. Hence, no point counting them. This also fixes a problem of a counter being incremented upon page editing.
if (($this->GetHandler() == 'show') && ($this->page['latest'] == 'Y') && ($this->GetUserName() != $this->GetPageOwner($tag)))
if (!defined('PAGE_HIT_COUNTER')) define('PAGE_HIT_COUNTER','Total hits:');
echo (":: <span class=\"counter\">"); echo PAGE_HIT_COUNTER; echo (" <span class=\"hits\">".$hit_count."</span></span>");
}?>%%
%%(php)define('PAGE_HIT_COUNTER', 'Total hits:');%%
...of course replacing //Total hits// with your translated text.
===8. Add a global counter setting (optional)===
Open **wikka.config.php** and add a line:
Instead of 'on' you can use 'off' or 'owner'. The default is 'disabled'.


Revision [19519]

Edited on 2008-02-03 22:17:01 by KrzysztofTrybowski [TODO item added]
Additions:
~-keep a separate count of visits since new version 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)


Revision [19517]

Edited on 2008-02-03 21:52:07 by KrzysztofTrybowski [TODO item added]
Additions:
%%(php)$this->SavePage($this->tag, $body, $note);%%
%%(php)$this->SavePage($this->tag, $body, $note, $this->page['hits']);%%
%%(php)function SavePage($tag, $body, $note)%%
%%(php)function SavePage($tag, $body, $note, $pagehits)%%
%%(php)user = '".mysql_real_escape_string($username)."',%%
%%(php)hits = '".mysql_real_escape_string($pagehits)."',%%
%%(css).counter {font-size: smaller;} /* influences whole counter text */
Deletions:
%%(php)$this->SavePage($this->tag, $body, $note);%%
%%(php)$this->SavePage($this->tag, $body, $note, $this->page['hits']);%%
%%(php)function SavePage($tag, $body, $note)%%
%%(php)function SavePage($tag, $body, $note, $pagehits)%%
%%(php)user = '".mysql_real_escape_string($username)."',%%
%%(php)hits = '".mysql_real_escape_string($pagehits)."',%%
%%(css).counter {font-size: smaller;} /* influences whole counter text */


Revision [19516]

Edited on 2008-02-03 21:46:02 by KrzysztofTrybowski [version 0.2]
Additions:
==version 0.2==
====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.
~- 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 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.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!) ;)
==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: editing a page adds +1 to a counter
==TODO==
~-GmBowen's proposal of a parameter ignore="name1, name2, name3" so that certain users wouldn't increment a counter
~-Ignored pages global setting, so that certain pages won't be counted by default (good if you use MostVisited add-on and don't want 'technical' pages, like textsearch, listed)
~-Ignored users global, so that certain users don't increment the counter
~-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?
====Instructions -- how to set up a counter in your Wikka-powered site====
The **//xx//_pages** table has to have the following field added (note that xx_ should be substituted with your prefix):
===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)<?php
// Let's grab the setting
$show = strtolower($this->GetConfigValue('show_page_hit_counter'));
// Let's default to 'disabled'
if ($show == '') $show = 'disabled';

// If the counter isn't off and we are allowed to read the page, proceed...
if (($show != 'disabled') && ($this->HasAccess('read')))
$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.
// we only want to count latest versions of the page being displayed, since older versions are probably being viewed by
// users working on them. Hence, no point counting them. This also fixes a problem of a counter being incremented upon page editing.
if (($this->GetHandler() == 'show') && ($this->page['latest'] == 'Y') && ($this->GetUserName() != $this->GetPageOwner($tag)))
{
$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:');
// Start output of counter
echo (":: <span class=\"counter\">"); echo PAGE_HIT_COUNTER; echo (" <span class=\"hits\">".$hit_count."</span></span>");
}
}?>%%
===3. Create an action file counter.php in a proper place===
%%(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;
?>%%
===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)<?php
$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==
==5b. Editing the Wakka class==
===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)<?php echo $this->FormClose(); ?>%%
and **above it** add:
%%(php)<?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:
%%(php)define('PAGE_HIT_COUNTER', 'Total hits:');%%
...of course replacing //Total hits// with your translated text.
===8. Add a global counter setting (optional)===
Open **wikka.config.php** and add a line:
%%(php)'show_page_hit_counter' => 'on',%%
Instead of 'on' you can use 'off' or 'owner'. The default is 'disabled'.
===9. Edit your wikka.css file (optional)===
Please test and give feedback. Regards, --KrzysztofTrybowski
Deletions:
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 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 to keep the count hidden from readers but visible for page owner, you can set the parameter show="owner";
The **_pages** table has to have the following field added:
===2. Create an action file counter.php in a proper place===
%%(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>");
?>
%%
===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==
==3b. Editing the Wakka class==
==4. Edit your wikka.css file (optional)==
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
~-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.


Revision [19515]

Edited on 2008-02-03 20:28:27 by KrzysztofTrybowski [TODO updated]
Additions:
~-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


Revision [19514]

Edited on 2008-02-03 18:16:04 by KrzysztofTrybowski [TODO updated]
Additions:
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.
Deletions:
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.


Revision [19513]

Edited on 2008-02-03 18:14:26 by KrzysztofTrybowski [TODO updated]
Additions:
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.
Deletions:
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.


Revision [19511]

Edited on 2008-02-03 17:35:29 by KrzysztofTrybowski [The counter now isn't updated when viewing page's history.]
Additions:
// 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)))
%%(php)hits = '".mysql_real_escape_string($pagehits)."',%%
~-editing a page adds +1 to a counter
Deletions:
if ($this->GetUserName() != $this->GetPageOwner($tag))
%%(php)hits = '".mysql_real_escape_string($pagehits-2)."',%%
Note the $pagehits-2 in the above line -- it seems that during edit, the page is read twice. Therefore I had to work it around. This may have however some other implications that are unknown to me right now.


Revision [19510]

Edited on 2008-02-03 17:24:44 by KrzysztofTrybowski [Fixed a bug preventing from show='owner' to work.]
Additions:
if (($show !='off' && $show !='no' && $show !='owner') or ($show =='owner' && $this->GetUserName() == $this->GetPageOwner($tag)))
Deletions:
if (($show !='off' && $show !='no') or ($show =='owner' && $this->GetUserName() == $this->GetPageOwner($tag)))


Revision [19509]

The oldest known version of this page was created on 2008-02-03 17:18:29 by KrzysztofTrybowski [Fixed a bug preventing from show='owner' to work.]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki