/** * @return bool * @param string $s_input * @param string[] field names $a_keys * @desc Test whether a short URL is already taken by another page */ public function Test($s_input, $a_keys) { $short_url = new ShortUrl($s_input); $short_url->SetFormat($this->object_with_short_url->GetShortUrlFormat()); $short_url->SetParameterValuesFromObject($this->object_with_short_url); $manager = new ShortUrlManager($this->GetSiteSettings(), $this->GetDataConnection()); $taken = $manager->IsUrlTaken($short_url); unset($manager); return !$taken; }
/** * 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; }