Extension:NoEmbed

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
NoEmbed
Release status: beta
Implementation Parser function
Description Embeds links using the noEmbed.com service
Author(s) Jason
Latest version 1
License No license specified
Download See Below
Example {{#embed: your link here}}
Translate the NoEmbed extension if it is available at translatewiki.net
Check usage and version matrix.

This extension takes a URL and attempts to embed the content into the page using the service at noEmbed.com Many different websites are valid. Check [noembed website] for compatability, or to add more sites.

Installation[edit]

Grab the source code below. Create a folder in your extensions named NoEmbed. Place following files in that folder. Then in localsettings.php add the line:

require_once "$IP/extensions/NoEmbed/NoEmbed.php";

Usage[edit]

Then all you need to do is to run the command in the editor:

{{#embed: your url here}}

Which will generate on the page this HTML (dependant on which media provider you are trying to embed). Style the Div elements as you please.

 <div class="noembed-embed ">
   <div class="noembed-wrapper">
     
     <div class="noembed-embed-inner noembed-vimeo">
       <iframe src="//player.vimeo.com/video/116549926" width="1112" height="626"     frameborder="0" title="Coming Home" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
     </div>
 
     <table class="noembed-meta-info">
       <tr>
         <td class="favicon"><img src="https://noembed.com/favicon/Vimeo.png"></td>
         <td>Vimeo</td>
         <td align="right">
           <a title="https://vimeo.com/116549926" href="https://vimeo.com/116549926">https://vimeo.com/116549926</a>
         </td>
       </tr>
     </table>
   </div>
 </div>

Source code[edit]

NoEmbed.php

<?php
// Customize your secret key here!
$NoEmbedKey1 = "Here is a secret key!";
$NoEmbedKey2 = "This too is a key. Just in case...";

// Parser to allow easy embedding of media thanks to noembed.com (great site by the way!)
// Usage:  {{#embed: url here }}
if ( !defined( 'MEDIAWIKI' ) ) {
	die( 'Not here...' );
}

$wgExtensionCredits['parserhook'][] = array(
   'path' => __FILE__,
   'name' => 'NoEmbed',
   'description' => 'A generic embedding function. Utilizing noembed.com',
   'version' => 1,
   'author' => 'Jason Dixon',
   'url' => 'http://www.mediawiki.org/wiki/Extenson:noembed',

);

// Register i18n.
$wgExtensionMessagesFiles['NoEmbed'] = __DIR__ . '/NoEmbed.i18n.php';

// Register hooks.
$wgHooks['ParserFirstCallInit'][] = 'NoEmbedSetup'; #grab text from parser
$wgHooks['ParserAfterTidy'][] = 'NoEmbedDecode'; # put it onto the page

// Scramble keys.
$NoEmbedKey1 = md5($NoEmbedKey1);
$NoEmbedKey2 = md5($NoEmbedKey2);

// Register Parser.
function NoEmbedSetup( &$parser ) {
   $parser->setFunctionHook( 'embed', 'NoEmbedURL' );
   return true;
}

// Encode the html.
function NoEmbedEncode( $html ){
	global $NoEmbedKey1, $NoEmbedKey2;
	$encode = base64_encode($html);
	return "$NoEmbedKey1$encode$NoEmbedKey2";
}

// Decode the html.
function NoEmbedDecode( &$parser, &$text ){
	global $NoEmbedKey1, $NoEmbedKey2;
	$text = preg_replace( "/$NoEmbedKey1([0-9a-zA-Z\\+].*?)$NoEmbedKey2/esm", 'base64_decode("$1")', $text );
	return true;
}

// Get embed information.
function NoEmbedURL( $parser, $url ) {
	$encodeMarkers = array();
	$noEmbed = "http://noembed.com/embed?url="; #Url for noembed service

	$result = json_decode(file_get_contents("$noEmbed$url"),TRUE);
	if($result && isset($result['html'])){
		return NoEmbedEncode($result['html']);
	} else {
		return $url;
	}

}

// Check compatability (todo: get this working)
function NoEmbedCheck( $url ){
	$reg = json_decode(file_get_contents("http://noembed.com/providers"),true);
	if($reg){
		foreach( $reg as $patterns){
			foreach( $patterns['patterns'] as $pattern){
				print $pattern;
				if(preg_match("/$pattern/i", $url)){
					return true;
				}
			}
		}
	}
	return false;
}

NoEmbed.i18n.php

<?php
$magicWords = array();
// English
$magicWords['en'] = array(
	'embed' => array(0, "embed")
);

Feel free to add improvements[edit]

There are some things I'd like to add, such as universal sizing options. But otherwise the plan is to keep it super simple and utilizing the noembed service, super broad.