FormatUser() Method

Ticket:221 (implemented)

This is the development page for the FormatUser() method, a small utility that allows rendering of user- and hostnames when they are retrieved from the database and printed.

This method renders usernames as links only when needed (when the corresponding userpage exists), avoiding the creation of dozens missing page links for users without userpage. It also makes other options configurable like truncating long hostnames or disabling link formatting.
 



The code


The following code should be added to libs/Wakka.class.php in the //USER section.


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



CategoryDevelopmentCore
Comments
Comment by WazoO
2006-06-09 01:38:17
This worked fine on a 1.1.6.1 install, but see that it also didn't make it into the 1.1.6.2-Alpha/Beta ... feedback offered to sway the decision for inclusion ...
Comment by NilsLindenberg
2006-06-09 04:00:22
It won't make it's way into the 1.1.6.2 release since the release list is closed! But I'll open up a ticket for (probable) inclusion in 1.1.6.3.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki