* Instagr.am added
* Global maxwidth/maxheight setting
*Microblogging:
* Minimal optical changes
This commit is contained in:
Grischa Brockhaus 2011-12-16 11:44:55 +01:00
parent 46fda7303d
commit b1ea56b430
10 changed files with 91 additions and 39 deletions

View file

@ -42,6 +42,11 @@ class OEmbedDatabase {
}
return null;
}
function clear_cache() {
global $serendipity;
$q = "delete from {$serendipity['dbPrefix']}" . PLUGIN_OEMBED_DATABASEVNAME;
serendipity_db_schema_import($q);
}
function install(&$obj) {
global $serendipity;
@ -80,6 +85,7 @@ class OEmbedDatabase {
}
function cleanup_html( $str ) {
$str = trim($str);
// Clear unicode stuff
$str=str_ireplace("\u003C","<",$str);
$str=str_ireplace("\u003E",">",$str);

View file

@ -8,6 +8,10 @@
@define('PLUGIN_EVENT_OEMBED_NAME', 'oEmbed Plugin');
@define('PLUGIN_EVENT_OEMBED_DESC', 'oEmbed is a format for allowing an embedded representation of a URL on your blog. It allows blog articles to display embedded content (such as tweets, photos or videos) when a user posts a link to that resource, without having to parse the resource directly.');
/*
@define('PLUGIN_EVENT_OEMBED_EXPAND_TWEETBACKS', 'Expand tweetbacks');
@define('PLUGIN_EVENT_OEMBED_EXPAND_TWEETBACKS_DESC', 'If enabled the plugin will detect tweetbacks and set their content to the oEmbed content delivered from twitter.');
*/
@define('PLUGIN_EVENT_OEMBED_INFO', '<h3>oEmbed Plugin</h3>' .
'<p>'.
'This plugin expands URLs to pages of known services to a representation of that URL. It shows i.e. the video for a youtube URL or the image instead of a flickr URL.<br/>' .

View file

@ -5,12 +5,12 @@
{elseif $oembed.type=='video'}
{$oembed.html}
{elseif $oembed.type=='image'}
<a href="{$oembed.url}"><img src="{$oembed.thumbnail_url}"/></a>
<a href="{$oembed.url}"><img src="{$oembed.thumbnail_url}""{if $oembed.title} title="{$oembed.title}" alt="{$oembed.title}"{/if}/></a>
{elseif $oembed.type=='photo'}
<img src="{$oembed.url}"/>
<img src="{$oembed.url}"{if $oembed.title} title="{$oembed.title}" alt="{$oembed.title}"{/if}/>
{elseif $oembed.type=='link'}
<a href="{$oembedurl}" title="{$oembed.title}">{$oembed.author_name}</a>
{else}
<a href="{$oembedurl}">{$oembedurl}</a>
<a href="{$oembedurl}" target="_blank">{$oembedurl}</a>
{/if}
</span>

View file

@ -2,11 +2,15 @@
abstract class EmbedProvider {
public $url;
public $endpoint;
public $maxwidth;
public $maxheight;
public abstract function match($url);
public abstract function provide($url,$format="json");
// public abstract function register();
public function __construct($url,$endpoint){
public function __construct($url,$endpoint, $maxwidth=null, $maxheight=null){
$this->url = $url;
$this->endpoint = $endpoint;
$this->maxwidth = $maxwidth;
$this->maxheight = $maxheight;
}
}

View file

@ -3,12 +3,14 @@ class OEmbedProvider extends EmbedProvider{
private $urlRegExp;
private $jsonEndpoint;
private $xmlEndpoint;
private $dimensionsSupported = true;
private $onlyJson = false;
public function __construct($url,$endpoint, $onlyJson=false){
parent::__construct($url,$endpoint);
public function __construct($url,$endpoint, $onlyJson=false, $maxwidth=null, $maxheight=null, $dimensionsSupported=true){
parent::__construct($url,$endpoint,$maxwidth,$maxheight);
$this->onlyJson = $onlyJson;
$this->dimensionsSupported = $dimensionsSupported;
$this->urlRegExp=preg_replace(array("/\*/","/\//","/\.\*\./"),array(".*","\/",".*"),$url);
$this->urlRegExp="/".$this->urlRegExp."/";
if (preg_match("/\{format\}/",$endpoint)){
@ -20,6 +22,16 @@ class OEmbedProvider extends EmbedProvider{
$this->jsonEndpoint=$endpoint."?url={url}&format=json";
$this->xmlEndpoint=$endpoint."?url={url}&format=xml";
}
if ($this->dimensionsSupported) {
if (!empty($this->maxwidth)) {
$this->jsonEndpoint.= '&maxwidth=' . $this->maxwidth;
$this->xmlEndpoint.= '&maxwidth=' . $this->maxwidth;
}
if (!empty($this->maxheight)) {
$this->jsonEndpoint.= '&maxwidth=' . $this->maxheight;
$this->xmlEndpoint.= '&maxwidth=' . $this->maxheight;
}
}
}
public function getUrlRegExp(){ return $this->urlRegExp; }

View file

@ -2,13 +2,14 @@
class ProviderManager{
private $providers;
private static $_instance;
private function __construct(){
private function __construct($maxwidth=null, $maxheight=null){
$this->providers=array();
$xml = simplexml_load_file(PLUGIN_OEMBED_PROVIDER_XML_FILE);// PROVIDER_XML comes from config.php
foreach($xml->provider as $provider){
if(!isset($provider->class) && isset($provider->endpoint)){
$onlyJson = isset($provider->jsononly);
$this->register(new OEmbedProvider($provider->url,$provider->endpoint, $onlyJson));
$dimensionsSupported = !isset($provider->nodimensionsupport);
$this->register(new OEmbedProvider($provider->url,$provider->endpoint, $onlyJson, $maxwidth, $maxheight, $dimensionsSupported));
} else {
$classname="".$provider->class; // force to be string :)
$reflection = new ReflectionClass($classname);
@ -16,9 +17,9 @@ class ProviderManager{
}
}
}
static function getInstance(){
static function getInstance($maxwidth=null, $maxheight=null){
if(!isset($_instance) || $_instance==null){
$_instance = new ProviderManager();
$_instance = new ProviderManager($maxwidth, $maxheight);
}
return $_instance;
}

View file

@ -74,7 +74,7 @@ class YouTubeProvider extends EmbedProvider {
}
public function __construct($config){
parent::__construct("http://youtube.com","");
public function __construct($config,$maxwidth=null, $maxheight=null){
parent::__construct("http://youtube.com","", $maxwidth, $maxheight);
}
}

View file

@ -13,7 +13,7 @@
-->
<provider>
<name>Twitter Status</name>
<url>https://*.twitter.com/*/status/*</url>
<url>https?://*.twitter.com/*/status(es)?/*</url>
<endpoint>https://api.twitter.com/1/statuses/oembed.{format}</endpoint>
</provider>
<provider>
@ -52,6 +52,7 @@
<provider>
<name>My Opera</name>
<nodimensionsupport/>
<url>http://my.opera.com/*</url>
<endpoint>http://my.opera.com/service/oembed</endpoint>
</provider>
@ -79,6 +80,13 @@
<endpoint>http://www.yfrog.com/api/oembed</endpoint>
</provider>
<provider>
<name>Instragr.am</name>
<jsononly/>
<url>http://*.instagr.am/*</url>
<endpoint>http://api.instagram.com/api/v1/oembed/</endpoint>
</provider>
<provider>
<name>DailyMotion</name>
<url>http://*.dailymotion.com/*</url>

View file

@ -37,12 +37,10 @@ class serendipity_event_oembed extends serendipity_event
));
$propbag->add('groups', array('FRONTEND_EXTERNAL_SERVICES'));
$propbag->add('event_hooks', array(
// 'backend_publish' => true, // An entry was puplished (was draft before or saved from the scratch).
// 'backend_save' => true, // An entry was saved.
'frontend_display' => true,
));
$propbag->add('configuration', array('info'));
$configuration = $configuration = array('info','maxwidth','maxheight');
$propbag->add('configuration', $configuration);
}
function introspect_config_item($name, &$propbag)
@ -52,7 +50,20 @@ class serendipity_event_oembed extends serendipity_event
$propbag->add('type', 'content');
$propbag->add('default', sprintf(PLUGIN_EVENT_OEMBED_INFO, ProviderList::ul_providernames(true)));
break;
case 'maxwidth':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_EVENT_OEMBED_MAXWIDTH);
$propbag->add('description', PLUGIN_EVENT_OEMBED_MAXWIDTH_DESC);
$propbag->add('default', '');
break;
case 'maxheight':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_EVENT_OEMBED_MAXHEIGHT);
$propbag->add('description', PLUGIN_EVENT_OEMBED_MAXHEIGHT_DESC);
$propbag->add('default', '');
break;
}
return true;
}
function event_hook($event, &$bag, &$eventData) {
@ -72,14 +83,10 @@ class serendipity_event_oembed extends serendipity_event
if (isset($hooks[$event])) {
switch($event) {
case 'frontend_display':
case 'backend_publish':
case 'backend_save':
if (!isset($eventData['body']) && !isset($eventData['extended'])) {
// Do not use for user comments, html nuggets, static pages etc.
return false;
break;
if (isset($eventData['body']) && isset($eventData['extended'])) {
$this->update_entry($eventData, $simplePatterns, 'body');
$this->update_entry($eventData, $simplePatterns, 'extended');
}
$this->update_entry($eventData, $simplePatterns);
return true;
}
}
@ -88,27 +95,34 @@ class serendipity_event_oembed extends serendipity_event
}
function update_entry(&$eventData, &$patterns) {
if (!empty($eventData['body'])) {
$eventData['body'] = preg_replace_callback(
function update_entry(&$eventData, &$patterns, $dateType) {
if (!empty($eventData[$dateType])) {
$eventData[$dateType] = preg_replace_callback(
$patterns['simpleTweet'],
array( $this, "oembedRewriteCallback"),
$eventData['body']);
}
if (!empty($eventData['extended'])) {
$eventData['extended'] = preg_replace_callback(
$patterns['simpleTweet'],
array( $this, "oembedRewriteCallback"),
$eventData['extended']);
$eventData[$dateType]);
}
}
function oembedRewriteCallback($match) {
$url = $match[1];
$maxwidth = $this->get_config('maxwidth','');
$maxheight = $this->get_config('maxheight','');
$obj = $this->expand($url, $maxwidth, $maxheight);
return OEmbedTemplater::fetchTemplate('oembed.tpl',$obj, $url);
}
/**
* This method can be used by other plugins. It will expand an URL to an oembed object (or null if not supported).
* @param string $url The url to be expanded
* @param string $maxwidth Maximum width of returned object (if service supports this). May be left empty
* @param string $maxheight Maximum height of returned object (if service supports this). May be left empty
* @return OEmbed or null
*/
function expand($url, $maxwidth=null, $maxheight=null) {
$obj = OEmbedDatabase::load_oembed($url);
$html = '';
if (empty($obj)) {
$manager = ProviderManager::getInstance();
$manager = ProviderManager::getInstance($maxwidth,$maxheight);
try {
$obj=$manager->provide($url,"object");
if (isset($obj)) {
@ -120,8 +134,9 @@ class serendipity_event_oembed extends serendipity_event
//return $e;
}
}
return OEmbedTemplater::fetchTemplate('oembed.tpl',$obj, $url);
return $obj;
}
function cleanup_html( $str ) {
// Clear unicode stuff
$str=str_ireplace("\u003C","<",$str);
@ -134,6 +149,8 @@ class serendipity_event_oembed extends serendipity_event
}
function cleanup() {
OEmbedDatabase::install($this);
OEmbedDatabase::clear_cache();
echo '<div class="serendipityAdminMsgSuccess">Cleared oembed cache.</div>';
}
function install() {
OEmbedDatabase::install($this);

View file

@ -625,7 +625,7 @@ class serendipity_event_twitter extends serendipity_plugin {
$propbag->add('name', PLUGIN_EVENT_TWITTER_CONSUMER_KEY);
$propbag->add('description', PLUGIN_EVENT_TWITTER_CONSUMER_KEY_DESC);
$propbag->add('default', '');
break;
break;
case 'general_oa_consumersecret':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_EVENT_TWITTER_CONSUMER_SECRET);
@ -1245,7 +1245,7 @@ a.twitter_update_time {
}
if (empty($highest_ids[$article_id]['last_info']) || empty($highest_ids[$article_id]['last_info']['lasttweetid']) || "{$entry[TWITTER_SEARCHRESULT_ID]}">$highest_ids[$article_id]['last_info']['lasttweetid']) {
if ($complete) { // This is called from admin interface
echo "Found new tweetback for article $article_id: tweetid: {$entry[TWITTER_SEARCHRESULT_ID]}<br/>";
echo "<div class='serendipityAdminMsgSuccess'>Found new tweetback for article $article_id: tweetid: {$entry[TWITTER_SEARCHRESULT_ID]}</div><br/>";
}
$this->check_tweetbacks_save_comment($article_id, $entry, $comment_type, true);
$comment_saved = true;