see also: TRBMostVisited

Most Visited Pages


This script assumes that you have setup properly the counter.php by GmBowen
find out how to setup it up by visiting GmBowenCounter

See the notes in the script to figure out the configuration and customisation options ...

You'll need to create /actions/mostvisited.php with the following contents
<?php
    /*  
        @filename:      mostvisited.php
        @author:        George Petsagourakis
        @email:         petsagouris@hotmail.com
        @date:          24 Dec 2004
        @license:       GPL

            @description:   this file assumes that you are running
                            the counter.php by GmBowen on your Wakka
                            // config vars //
                                $hitlist_limit : how many pages are going to be displayed in the list.
                                $table_alignment : accepts the values that the "align" attr of a <table> accepts.
                                $show_ranking : show the numbers on the side of the list ( starting from 1 ).
                                $show_owner : show the page"s owner along with the page title.
                                $show_current_version_hits : show the hits that the page received in its current version.

        @usage:         insert {{mostvisited}} any where in a wakka page.
    */

    // Configuration variables ...
    $hitlist_limit = 10;
    $table_alignment = "center";
    $show_ranking = true;
    $show_owner = true;
    $show_current_version_hits = true;

    // mysql query ...
    $hitlistQuery = $this->Query( "SELECT id,hits,tag,owner,latest FROM ".$this->config['table_prefix']."pages; " );
   
    // initialising variables ...
    $hitlist = array();
    $i = 0;

    // assign the resultset of the msyql query to $hitlist in a custom way ...
    while( $row = mysql_fetch_array($hitlistQuery) )
    {
        if (!$hitlist[$row['tag']]){
            $hitlist[$row['tag']] = array(  "owner" => $row['owner'],
                                        "hits" => $row['hits']
                                        );
            if ($row['latest'] == "Y") $hitlist[$row['tag']]['latest'] = $row['hits']+0;
        }
        else {
            $hitlist[$row['tag']]['hits'] += $row['hits'];
            if ( ($row['latest'] == "Y") && (!$hitlist[$row['latest']]) ) $hitlist[$row['tag']]['latest'] = $row['hits'];
        }
    }
   
    // sorting the array ...
    function hitlistsort( $a, $b ) {
        if ($a['hits'] == $b['hits']) return 0;
        return ($a['hits'] > $b['hits']) ? -1 : 1;
    }
    uasort($hitlist, "hitlistsort");
   
    // creating the output ...
    $str = "<table align=\"".$table_alignment."\">\n\t<tr>\n\t";
    $str .= "\t\t<th align=\"center\" colspan=\"4\">Most Visited Pages</th>\n";
    $str .= "\t</tr>\n";
    $str .= "\t<tr>\n";
    if ($show_ranking) $str .= "\t\t<th align=\"center\">Rank</th>\n";
    $str .= "\t\t<th align=\"center\">PageName</th>\n";
    if ($show_current_version_hits) $str .= "\t\t<th align=\"center\">Cur.Version Hits</th>\n";
    $str .= "\t\t<th align=\"center\">Total Hits</th>\n";
    $str .= "\t</tr>\n";
    // creating the listing ...
    foreach($hitlist as $pag => $arr){
       
        if ( ($i <= $hitlist_limit) && ( $arr['hits'] !== 0) )
        {
            if ($show_owner)    $arr['owner'] = ($arr['owner'] == "") ? "( not owned )" : "( ".$arr['owner']." )";
            $i++;
           
            $str .= "\t<tr>\n";
            if ($show_ranking)
                $str .= "\t\t<td align=\"right\">".$i.".&nbsp;</td>\n";
           
            $str .= "\t\t<td bgcolor=\"#999999\">".$this->Format($pag)."&nbsp;";
           
            if ($show_owner)
                $str .= "<small>".$this->Format($arr['owner'])."</small>";
           
            $str .="</td>\n";
            if ($show_current_version_hits)
                $str .= "\t\t<td align=\"center\">".$arr['latest']."</td>\n";
           
            $str .= "\t\t<td align=\"center\">".$arr['hits']."</td>\n";
            $str .= "\t</tr>\n";
        }
        else break;
   
    }
    $str .= "</table>";
    // displaying the output ...
    print $this->ReturnSafeHTML($str);
?>


Comments and suggestions as well as modifications are more than welcome ...



OK another way around

1° create a function in Wikka.php just before function LoadRecentlyChanged()
Actually its a copy of the function function LoadRecentlyChanged() that will make an SQL sorted on hits.

order by hits orders by hits for without totalling the hits per page, this could be improved, i should dig in SQL and make a Sum or Total field

php

	function LoadMostVisited()
	{
		if ($pages = $this->LoadAll("select * from ".$this->config["table_prefix"]."pages where latest = 'Y' order by hits desc"))
		{
			foreach ($pages as $page)
			{
				$this->CachePage($page);
			}
			return $pages;
		}
	}



2° Create a copy of recentchanged.xml.ph in directory /handlers/page/ and rename the copy as mostvisited.xml.php
and change one line if ($pages = $this->LoadMostVisited()) instead of recentchanged() function

<?php
header("Content-type: text/xml");

$xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
$xml .= '<?xml-stylesheet href="' . $this->GetConfigValue("base_url") .'/css/xml.css" type="text/css"?' .">\n";
$xml .= "<rss version=\"0.92\">\n";
$xml .= "<channel>\n";
$xml .= "<title>".$this->GetConfigValue("wakka_name")." - ".$this->tag."</title>\n";
$xml .= "<link>".$this->GetConfigValue("base_url")."</link>\n";
$xml .= "<description>Most visited pages of  ".$this->GetConfigValue("wakka_name")."</description>\n";
$xml .= "<language>en-us</language>\n";

if ($pages = $this->LoadMostVisited())
{
	$max = $this->GetConfigValue("xml_recent_changes");

	$c = 0;
	foreach ($pages as $page)
	{
		$c++;
		if (($c <= $max) || !$max)
		{
			$xml .= "<item>\n";
			$xml .= "<title>".$page["tag"]."</title>\n";
			$xml .= "<link>".$this->Href("show", $page["tag"], "time=".urlencode($page["time"]))."</link>\n";
			$xml .= "\t<description>".$page["time"]." by ".$page["user"].($page["note"] ? " - ".$page["note"] : "")."</description>\n";
			//$xml .= "\t<guid>".$page["id"]."</guid>";
			$xml .= "\t<pubDate>".date("r",strtotime($page["time"]))."</pubDate>\n";
			$xml .= "</item>\n";
		}
	}
}
else
{
	$xml .= "<item>\n";
	$xml .= "<title>Error</title>\n";
	$xml .= "<link>".$this->Href("show")."</link>\n";
	$xml .= "<description>You're not allowed to access this information.</description>\n";
	$xml .= "</item>\n";
}

$xml .= "</channel>\n";
$xml .= "</rss>\n";

print($xml);

?> 


3° Now you are ready to create XML output of 'mostvisited' pages.
and with the rss function you can display all the most popular pages sorted with Bowens hit counter.php
.

{{rss url="http://www.avk.be/Wikka/HomePage/mostvisited.xml" cachetime="60"}}


Should be equivalent to the function Mostvisited, but i find the solution a bit cleaner.

4° To make every page counted by the counter.php

Add this one line to footer.php

			  include("actions/counter.php");


This activates the counter for each page without having to add the Unknown action ""counter"" action on each page.



CategoryUserContributions
Comments
Comment by GmBowen
2004-12-15 22:18:29
Wow, neat.

As the code currently is it shows the sum of all hits on current and past pages....totalled (whereas the latest page already has that if you put in all of the code I provided). I think it might have to do with how this line....
if ($row['latest'] == 'Y') $hitlist[$row['tag']]["latest"] = $row['hits']+0;
is working (or not working as the case may be).

I fixed it by changing this line...
$hitlistQuery = $this->Query( 'SELECT id,hits,tag,owner,latest FROM '.$this->config["table_prefix"].'pages; ' );

to this line....
$hitlistQuery = $this->Query( 'SELECT id,hits,tag,owner,latest FROM '.$this->config["table_prefix"].'pages WHERE latest = "Y" ');

(which admittedly is inelegant cuz it solves the problem one way, but doesn't fix the problem in the right spot....but I couldn't figure why that spot was broken).....

Good job George. You should make sure you add a profile of yourself to CategoryUsers (I mean if you're going to contribute stuff, we'd like to know who you are and where you are etc.....we're nosy that way). Cheers,
Comment by GeorgePetsagourakis
2004-12-16 14:01:01
The appearance of the CurrentVersionHits is depending on the $show_current_version_hits configuration variable (look at the top of of the list .. )

$show_current_version_hits = false; // doesnt show the column ...
$show_current_version_hits = true; // shows the column ... ;)

the where latest='Y' thingie is taken care of in the code as you might have possibly seen .. it was more convinient to get everything on the array and handle stuff from there to me .. ;)
Comment by GmBowen
2004-12-16 15:27:23
ooops, yup. You're a more sophisticated programmer than I obviously....embarrassed I missed that (and, I don't know the emoticon symbols for embarassed.....sigh).
Comment by GmBowen
2005-01-03 16:42:52
Um, this new version completely "breaks" my wiki page (WSOD)....thought you'd like to know. Seems to be because I'm using an older version of wikka....if I replace the last line with print $str; it works fine. (My wikka version doesn't have that function).
Comment by GeorgePetsagourakis
2005-01-03 23:04:18
thx for the input ...
I agree with you .. and so does php ;) trying to use a non existant function doesnt get anywhere;)
your solution is definately the way to go ..

btw why dont you upgrade ? :)
Comment by GmBowen
2005-01-04 00:06:25
I'm waiting for the next release. I've modified many of the wikka files and upgrading will take a long time, so I've been waiting for a major-ish release. I'll be upgrading soon....
Comment by GeorgePetsagourakis
2005-01-04 13:53:50
Yes I hope that the Wikka-God gives its database some 'hits' column ;)
Comment by dD5E0616A.access.telenet.be
2005-08-29 22:21:49
Suggestion: If you want to create a random function
then change the hitlistsort with rand
// random
function hitlistsort( $a, $b ) {
if ($a['hits'] == $b['hits']) return rand(-1,1);
return ($a['hits'] > $b['hits']) ? rand(-1,1) :rand(-1,1);
}
uasort($hitlist, "hitlistsort");
Comment by KrzysztofTrybowski
2008-02-03 20:27:30
It's wrong to include counter.php in footer.php -- there's no read access check in footer.php, so the counter will get updated even if we get a denial of access. See my version of the counter (TRBCounter) that should do the trick better in this case.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki