/** * 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::staticGet('url', $in_url); if (!empty($a)) { // this is a direct link to $a->url return $a->url; } else { $b = File_redirection::staticGet('url', $in_url); if (!empty($b)) { // this is a redirect to $b->file_id $a = File::staticGet('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; } }
/** * 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 * @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) { // let's see if we know this... $a = File::staticGet('url', $in_url); if (!empty($a)) { // this is a direct link to $a->url return $a->url; } else { $b = File_redirection::staticGet('url', $in_url); if (!empty($b)) { // this is a redirect to $b->file_id $a = File::staticGet('id', $b->file_id); return $a->url; } } $ret = File_redirection::lookupWhere($in_url); return $ret; }
/** * 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... try { $a = File::getByUrl($in_url); // this is a direct link to $a->url return $a->url; } catch (NoResultException $e) { try { $b = File_redirection::getByUrl($in_url); // this is a redirect to $b->file_id $a = File::getKV('id', $b->file_id); return $a->url; } catch (NoResultException $e) { // Oh well, let's keep going } } if ($discover) { $ret = File_redirection::lookupWhere($in_url); return $ret; } else { // No manual dereferencing; leave the unknown URL as is. return $in_url; } }
/** * 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 File_redirection */ static function where($in_url, $discover = true) { $redir = new File_redirection(); $redir->url = $in_url; $redir->urlhash = File::hashurl($redir->url); $redir->redirections = 0; try { $r = File_redirection::getByUrl($in_url); if ($r instanceof File_redirection) { return $r; } } catch (NoResultException $e) { try { $f = File::getByUrl($in_url); $redir->file_id = $f->id; $redir->file = $f; return $redir; } catch (NoResultException $e) { // Oh well, let's keep going } } if ($discover) { $redir_info = File_redirection::lookupWhere($in_url); if (is_string($redir_info)) { $redir_info = array('url' => $redir_info); } // Double check that we don't already have the resolved URL $r = self::where($redir_info['url'], false); if (!empty($r->file_id)) { return $r; } $redir->httpcode = $redir_info['code']; $redir->redirections = intval($redir_info['redirects']); $redir->redir_url = $redir_info['url']; $redir->file = new File(); $redir->file->url = $redir_info['url']; $redir->file->mimetype = $redir_info['type']; $redir->file->size = isset($redir_info['size']) ? $redir_info['size'] : null; $redir->file->date = isset($redir_info['time']) ? $redir_info['time'] : null; if (isset($redir_info['protected']) && !empty($redir_info['protected'])) { $redir->file->protected = true; } } return $redir; }
--dry-run look but don't touch END_OF_USERROLE_HELP; require_once INSTALLDIR . '/scripts/commandline.inc'; $dry = have_option('dry-run'); $f = new File(); $f->title = 'h'; $f->mimetype = 'h'; $f->size = 0; $f->protected = 0; $f->find(); echo "Found {$f->N} bad items:\n"; while ($f->fetch()) { echo "{$f->id} {$f->url}"; $data = File_redirection::lookupWhere($f->url); if ($dry) { if (is_array($data)) { echo " (unchanged)\n"; } else { echo " (unchanged, but embedding lookup failed)\n"; } } else { // NULL out the mime/title/size/protected fields $sql = sprintf("UPDATE file " . "SET mimetype=null,title=null,size=null,protected=null " . "WHERE id=%d", $f->id); $f->query($sql); $f->decache(); if (is_array($data)) { Event::handle('EndFileSaveNew', array($f, $data, $f->url)); echo " (ok)\n"; } else {