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
There are 9 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki