Extensions/Youtube

From Sidvind
Jump to: navigation, search

Code: youtube.php (view, download)

  1. <?php
  2. # YouTube Videos
  3. #
  4. # Tag:
  5. #   <youtube>v</youtube>
  6. # Ex:
  7. #   from url http://www.youtube.com/watch?v=WZpeeRSk-0A
  8. #   <youtube>WZpeeRSk-0A</youtube>
  9.  
  10. $wgExtensionFunctions[] = 'wfYouTube';
  11. $wgExtensionCredits['parserhook'][] = array(
  12.         'path'           => __FILE__,
  13.         'name'           => 'YouTube',
  14.         'version'        => '1.0',
  15.         'author'         => 'David Sveningsson',
  16.         'description'    => 'Display YouTube video (based on [http://www.mediawiki.org/wiki/Extension:YouTube_(Iubito) Sylvain Macheferts extension].)',
  17.         'url'            => 'https://sidvind.com/wiki/Extensions/Youtube');
  18.  
  19. function wfYouTube() {
  20.         global $wgParser;
  21.         $wgParser->setHook('youtube', 'renderYouTube');
  22. }
  23.  
  24. function youtubeError($str){
  25.         return "<div style=\"border: solid red 1px; padding: .5em;\">$str</div>";
  26. }
  27.  
  28. function renderYouTube($input, array $args, Parser $parser, PPFrame $frame) {
  29.         /* default size (small) */
  30.         $width = 560;
  31.         $height = 315;
  32.  
  33.         /* default props */
  34.         $frame = isset($args['frame']);
  35.  
  36.         /* parse size argument */
  37.         if ( isset($args['size']) ){
  38.                 $size = strtolower($args['size']);
  39.                 if ( strcmp($size, "tiny") == 0 )        { $width =  320; $height = 180; }
  40.                 else if ( strcmp($size, "small") == 0 )  { $width =  560; $height = 315; }
  41.                 else if ( strcmp($size, "medium") == 0 ) { $width =  640; $height = 360; }
  42.                 else if ( strcmp($size, "large") == 0 )  { $width =  853; $height = 480; }
  43.                 else if ( strcmp($size, "huge") == 0 )   { $width = 1280; $height = 720; }
  44.                 else if ( sscanf($size, "%dx%d", $width, $height) != 2 ){
  45.                         return youtubeError("invalid size (valid sizes are <tt>tiny</tt>, <tt>small</tt>, <tt>medium</tt>, <tt>large</tt>, <tt>huge</tt>, <tt>WxH</tt>)");
  46.                 }
  47.         }
  48.  
  49.         /* validate the video ID */
  50.         if ( preg_match('%[^A-Za-z0-9_\\-]%', $input) ) {
  51.                 return youtubeError('bad video ID');
  52.         }
  53.  
  54.         /* video element */
  55.         $url = "https://www.youtube-nocookie.com/embed/{$input}?rel=0";
  56.         $video = Xml::openElement('iframe', array('width'=>$width, 'height'=>$height, 'src'=>$url, 'frameborder'=>0, "style"=>"float: left", 'allowfullscreen')) . Xml::closeElement('iframe');
  57.  
  58.         /* caption */
  59.         $caption = '';
  60.         if ( isset($args['caption']) ){
  61.                 $caption = Xml::openElement('div', array('class'=>'thumbcaption')) .
  62.                         $parser->recursiveTagParse($args['caption']) .
  63.                         Xml::closeElement('div');
  64.         }
  65.  
  66.         if ( $frame ){
  67.                 return
  68.                         Xml::openElement('div', array('class'=>'thumb tright')) .
  69.                         Xml::openElement('div', array('class'=>'thumbinner')) .
  70.                         $video . $caption .
  71.                         Xml::closeElement('div') .
  72.                         Xml::closeElement('div');
  73.         } else {
  74.                 return $video;
  75.         }
  76. }