Using BBClone as a web counter for Plone
This How-To describes how to integrate the BBClone web counter in your Plone site.
Origin: https://plone.org/documentation/how-to/bbclonehowto
Goal
To be able to use BBClone, which is a fantastic web counter written in PHP, to record the access statistics of our Plone site.
Required packages
* PHParser/PHPGateway. This allows you to run PHP web scripts in your plone site.
* BBClone. The web counter.
* PHP URL Title Lookup. A PHP script needed to look up the title of web pages.
Installation
PHParser
Download the PHParser tarball file and extract it inside the Products directory of your Plone instance. Edit the file PHParser.py and add the path to the PHP CGI interpreter (in my case PHPath = "/usr/bin/php-cgi"). Restart Zope.
BBClone Web Counter
Download BBClone and extract it somewhere in your disk. It does not need to be in your Plone instance. In my case I have installed it into /webserver/plone/bbclone/. Make sure that the user which is running zope has read access to that directory and read/write access to the var/ directory and files within. We need to make some changes into the BBClone code because of the particular way that PHParser works. When it is asked to run a php file, it creates a temporary file in the disk (in /tmp/) and uses php to parse that file. BBClone uses then the filename (and title) of that file to record the access so the statistics become useless. We start changing that by patching the file lib/marker.php (you do not need to know anything about patches, just edit the file and remove the lines which start with '-' in the patch below and substitute by the ones which start with '+':
--- bbclone/lib/marker.php.orig 2005-04-04 22:10:01.000000000 +0100
+++ bbclone/lib/marker.php 2005-08-09 17:26:52.333886919 +0100
@@ -293,7 +293,8 @@
$filename = (!$PATH_TRANSLATED || ($PATH_TRANSLATED == $DOCUMENT_ROOT)) ? basename($SCRIPT_FILENAME) :
basename($PATH_TRANSLATED);
- $REQUEST_URI = $this->filter_uri($filename, $PATH_INFO , $PHP_SELF);
+ //$REQUEST_URI = $this->filter_uri($filename, $PATH_INFO , $PHP_SELF);
+ $REQUEST_URI = _BBC_PAGE_LOC;
$SERVER_ADDR = $this->get_srv_addr($SERVER_ADDR, $LOCAL_ADDR);
$HTTP_HOST = !$HTTP_HOST ? $SERVER_NAME : $HTTP_HOST;
$REMOTE_ADDR = (stristr(PHP_OS, "darwin") && $HTTP_PC_REMOTE_ADDR) ? $HTTP_PC_REMOTE_ADDR : $REMOTE_ADDR;
Download the PHP URL Title Lookup script and save it as url_title.php inside your bbclone/lib/ directory. Some changes need to be done to this script to adapt it to our needs. The patch is below:
--- bbclone/lib/url_title.php.orig 2005-08-09 17:16:30.497467296 +0100
+++ bbclone/lib/url_title.php 2005-08-09 17:16:38.519156722 +0100
@@ -80,8 +78,8 @@
$title = $text;
}
- $html = "<a href=\"$url\">$title</a>";
- return $html;
+ //$html = "<a href=\"$url\">$title</a>";
+ return $title;
}
/*
@@ -115,7 +113,7 @@
// Look for <title>(.*?)</title> in the text
function url_find_title($chunk)
{
- if (preg_match('/<title>(.*?)<\/title>/i', $chunk, $matches)) {
+ if (preg_match('/<title>(.*?)—.*<\/title>/i', $chunk, $matches)) {
// found start&end tag
$title = $matches[1];
}
You can download the patched file here: `url_title`_.
_`url_title`: https://plone.org/documentation/how-to/bbclonehowto/url_title.php/download
Adding pieces together
Now login in your Plone site as administrator and go to the ZMI. In the portal root add a PHPGateway object. The title and id are not important, just type bbclone. The root is the full path to bbclone, i.e., in my case /webserver/plone/bbclone. Once done that you should be able to go to your plone site and see the main page of bbclone in http://yourserver/bbclone/
Go back to the root of your Plone site and insert a PHParser object. Call it bbclone_activate and insert the following text on it:
<?php
// This is the page location
$pageloc=$ZOPE_VARS['URL1'];
// Avoid links containing 'bbclone'
if (preg_match('/bbclone/i', $pageloc)) {
return;
}
// This is to avoid non existant pages
if ($pageloc=="") {
return;
}
// BBClone full path directory in the disk
define("_BBCLONE_DIR", "/webserver/plone/bbclone/");
include_once(_BBCLONE_DIR."lib/url_title.php");
define("_BBC_PAGE_LOC", $pageloc);
define("_BBC_PAGE_NAME", url($pageloc . "?bbclone=1"));
define("COUNTER", _BBCLONE_DIR."mark_page.php");
if (is_readable(COUNTER)) include_once(COUNTER);
?>
The last piece is to add some code to all our pages calling the above PHP script. I have chosen to do it by adding a new portlet although other options are available (e.g, using another portlet, adding it to the footer, etc.). From the ZMI go to portal_skins/custom/ and add a page template. Call it portlet_bbclone and add this text to it:
<html xmlns:tal="http://xml.zope.org/namespaces/tal"
xmlns:metal="http://xml.zope.org/namespaces/metal"
i18n:domain="plone">
<body>
<div metal:define-macro="portlet"
tal:define="pageloc python: request.URL.split('/')[-1];
okToShowHere python: not pageloc in ('login_form', 'join_form', 'logout_form');
bbclone request/bbclone|nothing"
tal:condition="python: isAnon and okToShowHere and not bbclone">
<dl class="portlet" id="portlet-bbclone">
<dt class="portletHeader"></dt>
<div class="portletContent odd">
<span tal:replace="here/bbclone_activate">Info</span>
</div>
</dl>
</div>
</body>
</html>
This portlet will only show (and hence only the counter will be updated) if the user accesing the page is Anonymous and the page is neither of login_form logout_form or join_form.
The last piece is to go to the root of your site and add the portlet clicking on portal properties and adding here/portlet_bbclone/macros/portlet to either the left_slots or to the right_slots. Do not forget to read the documentation about BBClone and the different configuration parameters available.
This How-to applies to: Plone 2.1.x, Plone 2.0.x
Created by eddie0uk
Last modified 2005-10-18 05:06 PM