Display linked oembed resources as enclosures if they are of non-html mime types

This commit is contained in:
Craig Andrews 2009-08-26 15:40:51 -04:00
parent d9e8dabaf4
commit 6d60d74093
4 changed files with 63 additions and 28 deletions

View File

@ -195,17 +195,49 @@ class File extends Memcached_DataObject
return 'http://'.$server.$path.$filename; return 'http://'.$server.$path.$filename;
} }
function isEnclosure(){ function getEnclosure(){
$enclosure = (object) array();
$enclosure->title=$this->title;
$enclosure->url=$this->url;
$enclosure->title=$this->title;
$enclosure->date=$this->date;
$enclosure->modified=$this->modified;
$enclosure->size=$this->size;
$enclosure->mimetype=$this->mimetype;
if(isset($this->filename)){ if(isset($this->filename)){
return true; return $enclosure;
}else{
$notEnclosureMimeTypes = array('text/html','application/xhtml+xml');
$mimetype = strtolower($this->mimetype);
$semicolon = strpos($mimetype,';');
if($semicolon){
$mimetype = substr($mimetype,0,$semicolon);
}
if(in_array($mimetype,$notEnclosureMimeTypes)){
$ombed = File_oembed::staticGet('file_id',$this->id);
if($oembed){
$mimetype = strtolower($ombed->mimetype);
$semicolon = strpos($mimetype,';');
if($semicolon){
$mimetype = substr($mimetype,0,$semicolon);
}
if(in_array($mimetype,$notEnclosureMimeTypes)){
return false;
}else{
if($ombed->mimetype) $enclosure->mimetype=$ombed->mimetype;
if($ombed->url) $enclosure->url=$ombed->url;
if($ombed->title) $enclosure->title=$ombed->title;
if($ombed->modified) $enclosure->modified=$ombed->modified;
unset($ombed->size);
}
}else{
return $enclosure;
}
}else{
return $enclosure;
}
} }
$notEnclosureMimeTypes = array('text/html','application/xhtml+xml');
$mimetype = strtolower($this->mimetype);
$semicolon = strpos($mimetype,';');
if($semicolon){
$mimetype = substr($mimetype,0,$semicolon);
}
return(! in_array($mimetype,$notEnclosureMimeTypes));
} }
} }

View File

@ -1199,10 +1199,11 @@ class Notice extends Memcached_DataObject
$attachments = $this->attachments(); $attachments = $this->attachments();
if($attachments){ if($attachments){
foreach($attachments as $attachment){ foreach($attachments as $attachment){
if ($attachment->isEnclosure()) { $enclosure=$attachment->getEnclosure();
$attributes = array('rel'=>'enclosure','href'=>$attachment->url,'type'=>$attachment->mimetype,'length'=>$attachment->size); if ($enclosure) {
if($attachment->title){ $attributes = array('rel'=>'enclosure','href'=>$enclosure->url,'type'=>$enclosure->mimetype,'length'=>$enclosure->size);
$attributes['title']=$attachment->title; if($enclosure->title){
$attributes['title']=$enclosure->title;
} }
$xs->element('link', $attributes, null); $xs->element('link', $attributes, null);
} }

View File

@ -258,26 +258,27 @@ class Rss10Action extends Action
$attachments = $notice->attachments(); $attachments = $notice->attachments();
if($attachments){ if($attachments){
foreach($attachments as $attachment){ foreach($attachments as $attachment){
if ($attachment->isEnclosure()) { $enclosure=$attachment->getEnclosure();
if ($enclosure) {
// DO NOT move xmlns declaration to root element. Making it // DO NOT move xmlns declaration to root element. Making it
// the default namespace here improves compatibility with // the default namespace here improves compatibility with
// real-world feed readers. // real-world feed readers.
$attribs = array( $attribs = array(
'rdf:resource' => $attachment->url, 'rdf:resource' => $enclosure->url,
'url' => $attachment->url, 'url' => $enclosure->url,
'xmlns' => 'http://purl.oclc.org/net/rss_2.0/enc#' 'xmlns' => 'http://purl.oclc.org/net/rss_2.0/enc#'
); );
if ($attachment->title) { if ($enclosure->title) {
$attribs['dc:title'] = $attachment->title; $attribs['dc:title'] = $enclosure->title;
} }
if ($attachment->modified) { if ($enclosure->modified) {
$attribs['dc:date'] = common_date_w3dtf($attachment->modified); $attribs['dc:date'] = common_date_w3dtf($enclosure->modified);
} }
if ($attachment->size) { if ($enclosure->size) {
$attribs['length'] = $attachment->size; $attribs['length'] = $enclosure->size;
} }
if ($attachment->mimetype) { if ($enclosure->mimetype) {
$attribs['type'] = $attachment->mimetype; $attribs['type'] = $enclosure->mimetype;
} }
$this->element('enclosure', $attribs); $this->element('enclosure', $attribs);
} }

View File

@ -274,11 +274,12 @@ class TwitterapiAction extends Action
$enclosures = array(); $enclosures = array();
foreach ($attachments as $attachment) { foreach ($attachments as $attachment) {
if ($attachment->isEnclosure()) { $enclosure_o=$attachment->getEnclosure();
if ($enclosure_o) {
$enclosure = array(); $enclosure = array();
$enclosure['url'] = $attachment->url; $enclosure['url'] = $enclosure_o->url;
$enclosure['mimetype'] = $attachment->mimetype; $enclosure['mimetype'] = $enclosure_o->mimetype;
$enclosure['size'] = $attachment->size; $enclosure['size'] = $enclosure_o->size;
$enclosures[] = $enclosure; $enclosures[] = $enclosure;
} }
} }