/**
  * Generate a short URL for an object if none exists
  *
  * @param IHasShortUrl $object
  * @param bool $b_regenerate
  * @return ShortUrl
  */
 public function EnsureShortUrl(IHasShortUrl $object, $b_regenerate = false)
 {
     if (!$object->GetShortUrl() or $b_regenerate) {
         $i_preference = 0;
         $current_url = $object->GetShortUrl();
         $object->SetShortUrl('');
         do {
             $i_preference++;
             $object->SetShortUrl($object->SuggestShortUrl($i_preference));
             if ($b_regenerate and $object->GetShortUrl() == $current_url) {
                 return;
             }
             # if regenerating, it's ok to keep the current URL if it's still appropriate
         } while ($this->IsUrlTaken($object->GetShortUrl()));
     }
     # Now add the new version, but if this is a new item there's a problem.
     # This method is run before adding the new item to the database, so that we
     # can write the item's short URL to the database at the same time as the
     # rest of the item. But that means the item doesn't have an id yet, and
     # generally we want to use the id. So, return the ShortUrl object so that it
     # can be updated after the item has been inserted into the database.
     $short_url = new ShortUrl($object->GetShortUrl());
     $short_url->SetFormat($object->GetShortUrlFormat());
     # Use instance, not static, method so object has a chance to customise format
     $short_url->SetParameterValuesFromObject($object);
     return $short_url;
 }