Revision [10290]

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

 

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 the URL of the page.


Sample output:

Page statistics for WikkaInternationalization




Revisions:
User Edits Percentage
JordaPolo 4 25%
JsnX 4 25%
AndreaRossato 2 12.5%
DarTar 2 12.5%
DotMG 1 6.25%
DreckFehler 1 6.25%
GregorLindner 1 6.25%
JavaWoman 1 6.25%
Statistics:
Statistics Count Percentage
Hits0 
Revisions160.16%
Comments90.44%
Backlinks50.09%
Referrers790.12%



Current version

(2007-07-30)

Latest available version: 0.1

Note: the rendering of the percentage bar for small values is not correct in IE since its width is always adjusted to accomodate the text width. A small change in the style or in the code might be needed for the style to be compatible with IE.

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