We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

Commit 1776c90c authored by mattl's avatar mattl

Moved oEmbed stuff out to a plugin (Oembed).

parent b0c4e8fc
......@@ -592,23 +592,6 @@ desclimit: maximum number of characters to allow in group descriptions.
addtag: Whether to add a tag for the group nickname for every group post
(pre-1.0.x behaviour). Defaults to false.
oembed
--------
oEmbed endpoint for multimedia attachments (links in posts). Will also
work as 'oohembed' for backwards compatibility.
endpoint: oohembed endpoint using http://oohembed.com/ software. Defaults to
'http://oohembed.com/oohembed/'.
order: Array of methods to check for OEmbed data. Methods include 'built-in'
(use a built-in function to simulate oEmbed for some sites),
'well-known' (use well-known public oEmbed endpoints),
'discovery' (discover using <link> headers in HTML), 'service' (use
a third-party service, like oohembed or embed.ly. Default is
array('built-in', 'well-known', 'service', 'discovery'). Note that very
few sites implement oEmbed; 'discovery' is going to fail 99% of the
time.
search
------
......
......@@ -96,28 +96,6 @@ class AttachmentAction extends Action
return $a->title();
}
function extraHead()
{
$this->element('link',array('rel'=>'alternate',
'type'=>'application/json+oembed',
'href'=>common_local_url(
'oembed',
array(),
array('format'=>'json', 'url'=>
common_local_url('attachment',
array('attachment' => $this->attachment->id)))),
'title'=>'oEmbed'),null);
$this->element('link',array('rel'=>'alternate',
'type'=>'text/xml+oembed',
'href'=>common_local_url(
'oembed',
array(),
array('format'=>'xml','url'=>
common_local_url('attachment',
array('attachment' => $this->attachment->id)))),
'title'=>'oEmbed'),null);
}
/**
* Handle input
*
......
......@@ -282,21 +282,6 @@ class ShownoticeAction extends Action
'content' => $id->toString()));
}
$this->element('link',array('rel'=>'alternate',
'type'=>'application/json+oembed',
'href'=>common_local_url(
'oembed',
array(),
array('format'=>'json','url'=>$this->notice->getUrl())),
'title'=>'oEmbed'),null);
$this->element('link',array('rel'=>'alternate',
'type'=>'text/xml+oembed',
'href'=>common_local_url(
'oembed',
array(),
array('format'=>'xml','url'=>$this->notice->getUrl())),
'title'=>'oEmbed'),null);
// Extras to aid in sharing notices to Facebook
$avatarUrl = $this->profile->avatarUrl(AVATAR_PROFILE_SIZE);
$this->element('meta', array('property' => 'og:image',
......
......@@ -75,62 +75,26 @@ class File extends Managed_DataObject
// I don't know why we have to keep doing this but I'm adding this last check to avoid
// uniqueness bugs.
$x = File::getKV('url', $given_url);
$file = File::getKV('url', $given_url);
if (!$x instanceof File) {
$x = new File;
$x->url = $given_url;
if (!empty($redir_data['protected'])) $x->protected = $redir_data['protected'];
if (!empty($redir_data['title'])) $x->title = $redir_data['title'];
if (!empty($redir_data['type'])) $x->mimetype = $redir_data['type'];
if (!empty($redir_data['size'])) $x->size = intval($redir_data['size']);
if (isset($redir_data['time']) && $redir_data['time'] > 0) $x->date = intval($redir_data['time']);
$file_id = $x->insert();
if (!$file instanceof File) {
$file = new File;
$file->url = $given_url;
if (!empty($redir_data['protected'])) $file->protected = $redir_data['protected'];
if (!empty($redir_data['title'])) $file->title = $redir_data['title'];
if (!empty($redir_data['type'])) $file->mimetype = $redir_data['type'];
if (!empty($redir_data['size'])) $file->size = intval($redir_data['size']);
if (isset($redir_data['time']) && $redir_data['time'] > 0) $file->date = intval($redir_data['time']);
$file_id = $file->insert();
}
$x->saveOembed($redir_data, $given_url);
return $x;
}
/**
* Save embedding information for this file, if applicable.
*
* Normally this won't need to be called manually, as File::saveNew()
* takes care of it.
*
* @param array $redir_data lookup data eg from File_redirection::where()
* @param string $given_url
* @return boolean success
*/
public function saveOembed(array $redir_data, $given_url)
{
if (isset($redir_data['type'])
&& (('text/html' === substr($redir_data['type'], 0, 9)
|| 'application/xhtml+xml' === substr($redir_data['type'], 0, 21)))) {
try {
$oembed_data = File_oembed::_getOembed($given_url);
} catch (Exception $e) {
return false;
}
if ($oembed_data === false) {
return false;
}
$fo = File_oembed::getKV('file_id', $this->id);
if ($fo instanceof File_oembed) {
common_log(LOG_WARNING, "Strangely, a File_oembed object exists for new file $file_id", __FILE__);
} else {
File_oembed::saveNew($oembed_data, $this->id);
return true;
}
}
return false;
Event::handle('EndFileSaveNew', array($file, $redir_data, $given_url));
return $file;
}
/**
* Go look at a URL and possibly save data about it if it's new:
* - follow redirect chains and store them in file_redirection
* - look up oEmbed data and save it in file_oembed
* - if a thumbnail is available, save it in file_thumbnail
* - save file record with basic info
* - optionally save a file_to_post record
......@@ -384,7 +348,7 @@ class File extends Managed_DataObject
$enclosure->size=$this->size;
$enclosure->mimetype=$this->mimetype;
if(! isset($this->filename)){
if (!isset($this->filename)) {
$notEnclosureMimeTypes = array(null,'text/html','application/xhtml+xml');
$mimetype = $this->mimetype;
if($mimetype != null){
......@@ -394,32 +358,8 @@ class File extends Managed_DataObject
if($semicolon){
$mimetype = substr($mimetype,0,$semicolon);
}
if(in_array($mimetype,$notEnclosureMimeTypes)){
// Never treat generic HTML links as an enclosure type!
// But if we have oEmbed info, we'll consider it golden.
$oembed = File_oembed::getKV('file_id',$this->id);
if($oembed && in_array($oembed->type, array('photo', 'video'))){
$mimetype = strtolower($oembed->mimetype);
$semicolon = strpos($mimetype,';');
if($semicolon){
$mimetype = substr($mimetype,0,$semicolon);
}
// @fixme uncertain if this is right.
// we want to expose things like YouTube videos as
// viewable attachments, but don't expose them as
// downloadable enclosures.....?
//if (in_array($mimetype, $notEnclosureMimeTypes)) {
// return false;
//} else {
if($oembed->mimetype) $enclosure->mimetype=$oembed->mimetype;
if($oembed->url) $enclosure->url=$oembed->url;
if($oembed->title) $enclosure->title=$oembed->title;
if($oembed->modified) $enclosure->modified=$oembed->modified;
unset($oembed->size);
//}
} else {
return false;
}
if (in_array($mimetype, $notEnclosureMimeTypes)) {
Event::handle('FileEnclosureMetadata', array($file, &$enclosure));
}
}
return $enclosure;
......
......@@ -257,9 +257,6 @@ class File_redirection extends Managed_DataObject
// Save file and embedding data about it!
$file = File::saveNew($redir_data, $long_url);
$file_id = $file->id;
if (!empty($redir_data['oembed']['json'])) {
File_oembed::saveNew($redir_data['oembed']['json'], $file_id);
}
} else if (is_string($redir_data)) {
// The file is a known redirect target.
$file = File::getKV('url', $redir_data);
......
......@@ -67,7 +67,6 @@ $classes = array('Schema_version',
'Group_inbox',
'Group_member',
'File',
'File_oembed',
'File_redirection',
'File_thumbnail',
'File_to_post',
......
......@@ -166,6 +166,11 @@ class Action extends HTMLOutputter // lawsuit
return $this->scoped;
}
public function getActionName()
{
return $this->action;
}
/**
* Show page, a template method.
*
......
......@@ -134,8 +134,6 @@ class AttachmentListItem extends Widget
var $attachment = null;
var $oembed = null;
/**
* @param File $attachment the attachment we will display
*/
......@@ -143,21 +141,10 @@ class AttachmentListItem extends Widget
{
parent::__construct($out);
$this->attachment = $attachment;
$this->oembed = File_oembed::getKV('file_id', $this->attachment->id);
}
function title() {
if (empty($this->attachment->title)) {
if (empty($this->oembed->title)) {
$title = $this->attachment->filename;
} else {
$title = $this->oembed->title;
}
} else {
$title = $this->attachment->title;
}
return $title;
return $this->attachment->title ?: $this->attachment->filename;
}
function linkTitle() {
......@@ -238,40 +225,19 @@ class AttachmentListItem extends Widget
class Attachment extends AttachmentListItem
{
function showLink() {
$this->out->elementStart('div', array('id' => 'attachment_view',
'class' => 'hentry'));
$this->out->elementStart('div', 'entry-title');
$this->out->element('a', $this->linkAttr(), $this->linkTitle());
$this->out->elementEnd('div');
if (Event::handle('StartShowAttachmentLink', array($this->out, $this->attachment))) {
$this->out->elementStart('div', array('id' => 'attachment_view',
'class' => 'hentry'));
$this->out->elementStart('div', 'entry-title');
$this->out->element('a', $this->linkAttr(), $this->linkTitle());
$this->out->elementEnd('div');
$this->out->elementStart('div', 'entry-content');
$this->showRepresentation();
$this->out->elementEnd('div');
if (!empty($this->oembed->author_name) || !empty($this->oembed->provider)) {
$this->out->elementStart('div', array('id' => 'oembed_info',
'class' => 'entry-content'));
if (!empty($this->oembed->author_name)) {
$this->out->elementStart('div', 'fn vcard author');
if (empty($this->oembed->author_url)) {
$this->out->text($this->oembed->author_name);
} else {
$this->out->element('a', array('href' => $this->oembed->author_url,
'class' => 'url'), $this->oembed->author_name);
}
}
if (!empty($this->oembed->provider)) {
$this->out->elementStart('div', 'fn vcard');
if (empty($this->oembed->provider_url)) {
$this->out->text($this->oembed->provider);
} else {
$this->out->element('a', array('href' => $this->oembed->provider_url,
'class' => 'url'), $this->oembed->provider);
}
}
$this->out->elementStart('div', 'entry-content');
$this->showRepresentation();
$this->out->elementEnd('div');
Event::handle('EndShowAttachmentLink', array($this->out, $this->attachment));
$this->out->elementEnd('div');
}
$this->out->elementEnd('div');
}
function show() {
......@@ -288,28 +254,7 @@ class Attachment extends AttachmentListItem
function showRepresentation() {
if (Event::handle('StartShowAttachmentRepresentation', array($this->out, $this->attachment))) {
if (!empty($this->oembed->type)) {
switch ($this->oembed->type) {
case 'rich':
case 'video':
case 'link':
if (!empty($this->oembed->html)) {
require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
$config = array(
'safe'=>1,
'elements'=>'*+object+embed');
$this->out->raw(htmLawed($this->oembed->html,$config));
}
break;
case 'photo':
$this->out->element('img', array('src' => $this->oembed->url, 'width' => $this->oembed->width, 'height' => $this->oembed->height, 'alt' => 'alt'));
break;
default:
Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
}
} elseif (!empty($this->attachment->mimetype)) {
if (!empty($this->attachment->mimetype)) {
switch ($this->attachment->mimetype) {
case 'image/gif':
case 'image/png':
......
......@@ -303,6 +303,7 @@ $default =
'ClientSideShorten' => array(),
'EmailAuthentication' => array(),
'Event' => array(),
'Oembed' => array(),
'OpenID' => array(),
'Poll' => array(),
'QnA' => array(),
......
......@@ -168,9 +168,6 @@ class Router
$m->connect('main/tagprofile', array('action' => 'tagprofile'));
$m->connect('main/oembed',
array('action' => 'oembed'));
$m->connect('main/xrds',
array('action' => 'publicxrds'));
......
Depends on the oEmbed plugin (Oembed).
Depends on the oEmbed plugin (Oembed)
So far this is still in $config['site']['oembed'].
oembed
--------
oEmbed endpoint for multimedia attachments (links in posts). Will also
work as 'oohembed' for backwards compatibility.
endpoint: oohembed endpoint using http://oohembed.com/ software. Defaults to
'http://oohembed.com/oohembed/'.
order: Array of methods to check for OEmbed data. Methods include 'built-in'
(use a built-in function to simulate oEmbed for some sites),
'well-known' (use well-known public oEmbed endpoints),
'discovery' (discover using <link> headers in HTML), 'service' (use
a third-party service, like oohembed or embed.ly. Default is
array('built-in', 'well-known', 'service', 'discovery'). Note that very
few sites implement oEmbed; 'discovery' is going to fail 99% of the
time.
<?php
class OembedPlugin extends Plugin
{
public function onCheckSchema()
{
$schema = Schema::get();
$schema->ensureTable('file_oembed', File_oembed::schemaDef());
return true;
}
public function onRouterInitialized(URLMapper $m)
{
$m->connect('main/oembed', array('action' => 'oembed'));
}
public function onEndShowHeadElements(Action $action)
{
switch ($action->getActionName()) {
case 'attachment':
$action->element('link',array('rel'=>'alternate',
'type'=>'application/json+oembed',
'href'=>common_local_url(
'oembed',
array(),
array('format'=>'json', 'url'=>
common_local_url('attachment',
array('attachment' => $action->attachment->id)))),
'title'=>'oEmbed'),null);
$action->element('link',array('rel'=>'alternate',
'type'=>'text/xml+oembed',
'href'=>common_local_url(
'oembed',
array(),
array('format'=>'xml','url'=>
common_local_url('attachment',
array('attachment' => $action->attachment->id)))),
'title'=>'oEmbed'),null);
break;
case 'shownotice':
$this->element('link',array('rel'=>'alternate',
'type'=>'application/json+oembed',
'href'=>common_local_url(
'oembed',
array(),
array('format'=>'json','url'=>$this->notice->getUrl())),
'title'=>'oEmbed'),null);
$this->element('link',array('rel'=>'alternate',
'type'=>'text/xml+oembed',
'href'=>common_local_url(
'oembed',
array(),
array('format'=>'xml','url'=>$this->notice->getUrl())),
'title'=>'oEmbed'),null);
break;
}
return true;
}
/**
* Save embedding information for a File, if applicable.
*
* Normally this event is called through File::saveNew()
*
* @param File $file The newly inserted File object.
* @param array $redir_data lookup data eg from File_redirection::where()
* @param string $given_url
*
* @return boolean success
*/
public function onEndFileSaveNew(File $file, array $redir_data, $given_url)
{
$fo = File_oembed::getKV('file_id', $file->id);
if ($fo instanceof File_oembed) {
common_log(LOG_WARNING, "Strangely, a File_oembed object exists for new file $file_id", __FILE__);
return true;
}
if (isset($redir_data['oembed']['json'])
&& !empty($redir_data['oembed']['json'])) {
File_oembed::saveNew($redir_data['oembed']['json'], $file->id);
} elseif (isset($redir_data['type'])
&& (('text/html' === substr($redir_data['type'], 0, 9)
|| 'application/xhtml+xml' === substr($redir_data['type'], 0, 21)))) {
try {
$oembed_data = File_oembed::_getOembed($given_url);
if ($oembed_data === false) {
throw new Exception('Did not get oEmbed data from URL');
}
} catch (Exception $e) {
return true;
}
File_oembed::saveNew($oembed_data, $file->id);
}
return true;
}
public function onEndShowAttachmentLink(HTMLOutputter $out, File $file)
{
$oembed = File_oembed::getKV('file_id', $file->id);
if (empty($oembed->author_name) && empty($oembed->provider)) {
return true;
}
$out->elementStart('div', array('id'=>'oembed_info', 'class'=>'entry-content'));
if (!empty($oembed->author_name)) {
$out->elementStart('div', 'fn vcard author');
if (empty($oembed->author_url)) {
$out->text($oembed->author_name);
} else {
$out->element('a', array('href' => $oembed->author_url,
'class' => 'url'),
$oembed->author_name);
}
}
if (!empty($oembed->provider)) {
$out->elementStart('div', 'fn vcard');
if (empty($oembed->provider_url)) {
$out->text($oembed->provider);
} else {
$out->element('a', array('href' => $oembed->provider_url,
'class' => 'url'),
$oembed->provider);
}
}
$out->elementEnd('div');
}
public function onFileEnclosureMetadata(File $file, &$enclosure)
{
// Never treat generic HTML links as an enclosure type!
// But if we have oEmbed info, we'll consider it golden.
$oembed = File_oembed::getKV('file_id', $file->id);
if (!$oembed instanceof File_oembed || !in_array($oembed->type, array('photo', 'video'))) {
return true;
}
foreach (array('mimetype', 'url', 'title', 'modified') as $key) {
if (!empty($oembed->{$key})) {
$enclosure->{$key} = $oembed->{$key};
}
}
return true;
}
public function onStartShowAttachmentRepresentation(HTMLOutputter $out, File $file)
{
$oembed = File_oembed::getKV('file_id', $file->id);
if (empty($oembed->type)) {
return true;
}
switch ($oembed->type) {
case 'rich':
case 'video':
case 'link':
if (!empty($oembed->html)) {
require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
$config = array(
'safe'=>1,
'elements'=>'*+object+embed');
$out->raw(htmLawed($oembed->html,$config));
}
break;
case 'photo':
$out->element('img', array('src' => $oembed->url, 'width' => $oembed->width, 'height' => $oembed->height, 'alt' => 'alt'));
break;
default:
Event::handle('ShowUnsupportedAttachmentRepresentation', array($out, $file));
}
}
public function onPluginVersion(array &$versions)
{
$versions[] = array('name' => 'Oembed',
'version' => GNUSOCIAL_VERSION,
'author' => 'Mikael Nordfeldth',
'homepage' => 'http://gnu.io/',
'description' =>
// TRANS: Plugin description.
_m('Plugin for using and representing Oembed data.'));
return true;
}
}
It's really called oEmbed.
......@@ -18,7 +18,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
$longoptions = array('dry-run');
......@@ -62,11 +62,8 @@ while ($f->fetch()) {
$f->decache();
if (is_array($data)) {
if ($f->saveOembed($data, $f->url)) {
echo " (ok)\n";
} else {
echo " (ok, no embedding data)\n";
}
Event::handle('EndFileSaveNew', array($f, $data, $f->url));
echo " (ok)\n";
} else {
echo " (ok, but embedding lookup failed)\n";
}
......
......@@ -5,7 +5,7 @@ if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
exit();
}
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
define('GNUSOCIAL', true);
define('STATUSNET', true); // compatibility
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment