+ /**
+ * Check if this URL is a redirect and return redir info.
+ * If a File record is present for this URL, it is not considered a redirect.
+ * If a File_redirection record is present for this URL, the recorded target is returned.
+ *
+ * If no File or File_redirect record is present, the URL is hit and any
+ * redirects are followed, up to 10 levels or until a protected URL is
+ * reached.
+ *
+ * @param string $in_url
+ * @param boolean $discover true to attempt dereferencing the redirect if we don't know it already
+ * @return mixed one of:
+ * string - target URL, if this is a direct link or a known redirect
+ * array - redirect info if this is an *unknown* redirect:
+ * associative array with the following elements:
+ * code: HTTP status code
+ * redirects: count of redirects followed
+ * url: URL string of final target
+ * type (optional): MIME type from Content-Type header
+ * size (optional): byte size from Content-Length header
+ * time (optional): timestamp from Last-Modified header
+ */
+ public function where($in_url, $discover=true) {
+ // let's see if we know this...
+ $a = File::getKV('url', $in_url);
+
+ if (!empty($a)) {
+ // this is a direct link to $a->url
+ return $a->url;
+ } else {
+ $b = File_redirection::getKV('url', $in_url);
+ if (!empty($b)) {
+ // this is a redirect to $b->file_id
+ $a = File::getKV('id', $b->file_id);
+ return $a->url;
+ }
+ }
+
+ if ($discover) {
+ $ret = File_redirection::lookupWhere($in_url);
+ return $ret;
+ } else {
+ // No manual dereferencing; leave the unknown URL as is.
+ return $in_url;
+ }
+ }
+
+ /**
+ * Shorten a URL with the current user's configured shortening
+ * options, if applicable.
+ *
+ * If it cannot be shortened or the "short" URL is longer than the
+ * original, the original is returned.
+ *
+ * If the referenced item has not been seen before, embedding data
+ * may be saved.
+ *
+ * @param string $long_url
+ * @param User $user whose shortening options to use; defaults to the current web session user
+ * @return string
+ */
+ function makeShort($long_url, $user=null)
+ {
+ $canon = File_redirection::_canonUrl($long_url);
+
+ $short_url = File_redirection::_userMakeShort($canon, $user);
+
+ // Did we get one? Is it shorter?
+
+ if (!empty($short_url)) {
+ return $short_url;
+ } else {
+ return $long_url;
+ }