Revision [10279]

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

 

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