Revision [10271]

This is an old revision of InfoHandler made by DarTar on 2005-07-30 18:00:38.

 

Info Handler

See also: Note: The info handler is installed on this server as a beta feature.
 

This is the development page for the info handler.
 


The /info handler displays information and statistics about the current page.

Usage:


Append /info to any pagename in your browser.



Sample output:

Information about page DarTar




Authors:
User Edits Percentage
DarTar 46 86.8%
ppp15-148-59-62.dial… 5 9.4%
DarTar2 1 1.9%
132.229.91.74 1 1.9%
Statistics:
Statistics Count Percentage
Hits0 
Revisions530.5%
Comments80.4%
Backlinks941.7%
Referrers860.1%





Current version

(2007-07-30)

Latest avaliable version: 0.1

The code


Copy the following code as: handlers/page/info.php

  1. <div class="page">
  2. <?php
  3. /**
  4.  * Displays information on the current page.
  5.  *
  6.  * Usage: append /info to the URL of the page.
  7.  *
  8.  * @package         Handlers    
  9.  * @name                info
  10.  *
  11.  * @author          {@link http://wikka.jsnx.com/DarTar Dario Taraborelli} - first draft.
  12.  * @version         0.1
  13.  * @since               Wikka 1.1.X.X
  14.  *
  15.  * @todo                - bar styling in the CSS;
  16.  *                          - optimize queries and code;
  17.  */
  18.  
  19.  
  20. //Default constants
  21.  
  22.     // -------------------------------------
  23.     // set default values as constants
  24.     define('PERCENTAGE_BAR_WIDTH', '200'); # width of percentage bar in pixels
  25.     define('OPTION_LINK', '1'); # enable linking to userpages
  26.     define('OPTION_HOSTNAME_LENGTH', '20'); # max. length for hostnames
  27.    
  28.     // -------------------------------------
  29.     // User-interface: strings
  30.    
  31.     define('PAGE_TITLE','Information about page %s');
  32.     define('HEADING_AUTHORS','Authors:');
  33.     define('HEADING_STATS','Statistics:');
  34.     define('ERROR_PAGE_NOT_EXISTING','Sorry, this page does not exist.');
  35.     define('ERROR_NO_ACCESS','Sorry, You don\'t have access to this page.');
  36.     define('TABLE_AUTHORS_SUMMARY','List of authors of the current page ordered by number of edits');
  37.     define('TABLE_AUTHORS_HEADING_USER','User');
  38.     define('TABLE_AUTHORS_HEADING_EDITS','Edits');
  39.     define('TABLE_AUTHORS_HEADING_PERCENTAGE','Percentage');
  40.     define('TABLE_CELL_HITS_TITLE','Hits for %s (%d)');
  41.     define('TABLE_CELL_REVISIONS_TITLE','Display revisions for %s (%d)');
  42.     define('TABLE_CELL_COMMENTS_TITLE','Display comments for %s (%d)');
  43.     define('TABLE_CELL_BACKLINKS_TITLE','Display pages linking to %s (%d)');
  44.     define('TABLE_CELL_REFERRERS_TITLE','Display external sites linking to %s (%d)');
  45.     define('TABLE_STATS_SUMMARY','Statistics for current page');
  46.     define('TABLE_STATS_HEADING_STATISTICS','Statistics');
  47.     define('TABLE_STATS_HEADING_COUNT','Count');
  48.     define('TABLE_STATS_HEADING_PERCENTAGE','Percentage');
  49.        
  50. //initialize variables
  51.  
  52. $output = '';
  53. $tag = '';
  54. $box = '';
  55.  
  56. // print header
  57. echo $this->Format('==== '.sprintf(PAGE_TITLE,'[['.$this->tag.']]').' ====');
  58.  
  59. // 1. check source page existence
  60. if (!$this->page)
  61. {
  62.     // source page does not exist!
  63.     $box = ERROR_PAGE_NOT_EXISTING;
  64. }
  65. else
  66. {
  67.     $tag = $this->page['tag'];
  68.    
  69.     // 2. page exists - now check user's read-access to the source page
  70.     if (!$this->HasAccess('read'))
  71.     {
  72.         // user can't read source page!
  73.         $box = ERROR_NO_ACCESS;
  74.     } else
  75.     {
  76.         // page exists and user has read-access to the source - proceed
  77.  
  78.         // --- Authors ---
  79.        
  80.         $authors = $this->LoadAll('SELECT user, COUNT(*) AS edits FROM '.$this->config['table_prefix'].'pages WHERE  `tag`  =  "'.$tag.'" GROUP  BY user ORDER BY edits DESC');
  81.         $totaledits = $this->getCount('pages', '`tag`  =  "'.$tag.'"');
  82.         $output .= $this->Format('=='.HEADING_AUTHORS.'==');
  83.         if ($authors)
  84.         {
  85.             $output .= "<table summary=\"".TABLE_AUTHORS_SUMMARY."\">\n<thead>\n<tr>\n<th>".TABLE_AUTHORS_HEADING_USER."</th>\n<th>".TABLE_AUTHORS_HEADING_EDITS."</th>\n<th>".TABLE_AUTHORS_HEADING_PERCENTAGE."</th>\n</tr>\n</thead>\n<tbody>\n";
  86.             foreach($authors as $author)
  87.             {
  88.                 $percentage = $author['edits'] / $totaledits * 100;
  89.                 $width = (PERCENTAGE_BAR_WIDTH * $author['edits'] / $totaledits).'px';
  90.                 $bar = '<span style="display:block; background-color:red; width:'.$width.'">'.round($percentage,1).'%</span>';
  91.                 // use new FormatUser() method - added to wikka.php by DarTar 2005-07-30
  92.                 $output .= "<tr>\n<td>".$this->FormatUser($author['user'], OPTION_LINK, OPTION_HOSTNAME_LENGTH)."</td>\n<td class=\"number\">".$author['edits']."</td>\n<td>".$bar."</td>\n</tr>\n";
  93.             }
  94.             $output .= "</tbody></table>\n";
  95.         }
  96.  
  97.         // --- Statistics ---
  98.        
  99.         $whereTag       = "`tag` = '".$tag."'";
  100.         $wherePageTag   = "`page_tag` = '".$tag."'";
  101.         $whereToTag     = "`to_tag` = '".$tag."'";
  102.         $rv = $this->getCount('pages',$whereTag);
  103.         $cn = $this->getCount('comments',$wherePageTag);
  104.         $bn = $this->getCount('links',$whereToTag);
  105.         $rn = $this->getCount('referrers',$wherePageTag);
  106.  
  107.         $totalrv = $this->getCount('pages');
  108.         $totalcn = $this->getCount('comments');
  109.         $totalbn = $this->getCount('links');
  110.         $totalrn = $this->getCount('referrers');
  111.  
  112.         // @@ note: the link generation below is redundant and should be optimized
  113.  
  114.         // get page hits (forthcoming)
  115.         $hitspage = ($hn > 0) ? '<a href="'.$this->Href('hits',$page['tag'], '').'" title="'.sprintf(TABLE_CELL_HITS_TITLE, $page['tag'], $hn).'">'.$hn.'</a>' : '0';
  116.  
  117.         // get page revisions and create revision link if needed
  118.         $revpage = ($rv > 0) ? '<a href="'.$this->Href('revisions',$page['tag'], '').'" title="'.sprintf(TABLE_CELL_REVISIONS_TITLE, $page['tag'], $rv).'">'.$rv.'</a>' : '0';
  119.         $rvpercentage = $rv / $totalrv * 100;
  120.         $rvwidth = (PERCENTAGE_BAR_WIDTH * $rv / $totalrv).'px';
  121.         $rvbar = '<span style="display:block; background-color:red; width:'.$rvwidth.'">'.round($rvpercentage,1).'%</span>';
  122.  
  123.         // get page comments and create comments link if needed
  124.         $commentspage = ($cn > 0) ? '<a href="'.$this->Href('',$page['tag'], 'show_comments=1#comments').'" title="'.sprintf(TABLE_CELL_COMMENTS_TITLE, $page['tag'], $cn).'">'.$cn.'</a>' : '0';
  125.         $cnpercentage = $cn / $totalcn * 100;
  126.         $cnwidth = (PERCENTAGE_BAR_WIDTH * $cn / $totalcn).'px';
  127.         $cnbar = '<span style="display:block; background-color:red; width:'.$cnwidth.'">'.round($cnpercentage,1).'%</span>';
  128.  
  129.         // get page backlinks and create backlinks link
  130.         $backlinkpage = ($bn > 0) ? '<a href="'.$this->Href('backlinks',$page['tag'], '').'" title="'.sprintf(TABLE_CELL_BACKLINKS_TITLE, $page['tag'], $bn).'">'.$bn.'</a>' : '0';
  131.         $bnpercentage = $bn / $totalbn * 100;
  132.         $bnwidth = (PERCENTAGE_BAR_WIDTH * $bn / $totalbn).'px';
  133.         $bnbar = '<span style="display:block; background-color:red; width:'.$bnwidth.'">'.round($bnpercentage,1).'%</span>';
  134.  
  135.         // get page referrers and create referrer link
  136.         $refpage = ($rn > 0) ? '<a href="'.$this->Href('referrers',$page['tag'], '').'" title="'.sprintf(TABLE_CELL_REFERRERS_TITLE, $page['tag'], $rn).'">'.$rn.'</a>' : '0';
  137.         $rnpercentage = $rn / $totalrn * 100;
  138.         $rnwidth = (PERCENTAGE_BAR_WIDTH * $rn / $totalrn).'px';
  139.         $rnbar = '<span style="display:block; background-color:red; width:'.$rnwidth.'">'.round($rnpercentage,1).'%</span>';
  140.  
  141.         $output .= $this->Format('=='.HEADING_STATS.'==');
  142.  
  143.         $output .= "<table summary=\"".TABLE_STATS_SUMMARY."\">\n<thead>\n<tr>\n<th>".TABLE_STATS_HEADING_STATISTICS."</th>\n<th>".TABLE_STATS_HEADING_COUNT."</th>\n<th>".TABLE_STATS_HEADING_PERCENTAGE."</th>\n</tr>\n</thead>\n<tbody>\n".
  144.  
  145.         '<tr><td>Hits</td><td class="number">'.$hitspage.'</td><td>&nbsp;</td></tr>'."\n".
  146.         '<tr><td>Revisions</td><td class="number">'.$revpage.'</td><td>'.$rvbar.'</td></tr>'."\n".
  147.         '<tr><td>Comments</td><td class="number">'.$commentspage.'</td><td>'.$cnbar.'</td></tr>'."\n".
  148.         '<tr><td>Backlinks</td><td class="number">'.$backlinkpage.'</td><td>'.$bnbar.'</td></tr>'."\n".
  149.         '<tr><td>Referrers</td><td class="number">'.$refpage.'</td><td>'.$rnbar.'</td></tr>'."\n".
  150.         '</tbody></table>'."\n";
  151.        
  152.         // --- actions --- (forthcoming)
  153.  
  154.     }
  155. }
  156.  
  157. // display messages
  158. if (isset($box)) echo $this->Format(' --- '.$box.' --- --- ');
  159. // print form
  160. if (isset($output)) print $output;
  161. ?>
  162. </div>


The handler uses a FormatUser() method, that must be added to wikka.php, at the end of the user-related functions (right after function UserWantsComments()):

  1.     function UserWantsComments() { if (!$user = $this->GetUser()) return FALSE; return ($user["show_comments"] == "Y"); }
  2.     // 2005-07-30 added by DarTar
  3.      /**
  4.      * Formatter for usernames.
  5.      *
  6.      * Renders usernames as links only when needed, avoiding the creation of dozens
  7.      * missing page links for users without userpage. Makes other options configurable
  8.      * (like truncating long hostnames or disabling link formatting).
  9.      *
  10.      * @author      {@link http://wikka.jsnx.com/DarTar Dario Taraborelli}
  11.      * @version     0.1
  12.      *
  13.      * @param   string  $user   required: name of user or hostname retrieved from the DB;
  14.      * @param   string  $link   optional: enables/disables linking to userpage;
  15.      * @param   string  $maxhostlength  optional: max length for hostname, hostnames longer
  16.      *                              than this will be truncated with an ellipsis;
  17.      * @param   string  $ellipsis   optional: character to be use at the end of truncated hosts;
  18.      * @return string   $formatted_user: formatted username.
  19.      */
  20.     function FormatUser ($user, $link='1', $maxhostlength='10', $ellipsis='&#8230;')
  21.     {
  22.         if (strlen($user)>0)
  23.         {
  24.             // check if user is registered
  25.             if ($this->LoadUser($user))
  26.             {
  27.                 // check if userpage exists and if linking is enabled
  28.                 $formatted_user = ($this->ExistsPage($user) && ($link == 1))? $this->Link($user) : $user;
  29.             }
  30.             else
  31.             {
  32.                 // truncate long host names
  33.                 $user = (strlen($user) > $maxhostlength) ? '<span title="'.$user.'">'.substr($user, 0, $maxhostlength).$ellipsis.'</span>' : $user;
  34.                 $formatted_user = '<tt>'.$user.'</tt>';
  35.             }
  36.         }
  37.         else
  38.         {
  39.         // page has empty user field
  40.         $formatted_user = 'anonymous';
  41.         }
  42.         return $formatted_user;
  43.     }





CategoryDevelopmentHandlers
There are 7 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki