Extension youtube source

From MediaWiki extension testing
Jump to: navigation, search
<?php

/**
 * Parser hook-based extension to show audio and video players
 * from YouTube and other similar sites.
 *
 * @package MediaWiki
 * @subpackage Extensions
 * @author Przemek Piotrowski <ppiotr@wikia.com> for Wikia, Inc.
 * @copyright (C) 2006-2008, Wikia Inc.
 * @licence GNU General Public Licence 2.0 or later
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
 * USA
 *
 * @todo one class (family) to rule 'em all
 * @todo make width/height_max != width/height_default; aoaudio height may be large - long playlist
 * @todo smart <video> and <audio> tag
 */

if (!defined('MEDIAWIKI'))
{
        echo "This is MediaWiki extension.\n";
        exit(1);
}

$wgExtensionFunctions[] = 'wfYouTube';
$wgExtensionCredits['parserhook'][] = array
(
        'name'        => 'YouTube',
        'version'     => '1.4',
        'author'      => 'Przemek Piotrowski',
        'url'         => 'http://www.wikia.com/wiki/Help:YouTube',
        'description' => 'embeds YouTube and Google Video movies + Archive.org audio and video',
);

function wfYouTube() 
{
        global $wgParser;

        $wgParser->setHook('youtube', 'embedYouTube');
        $wgParser->setHook('gvideo',  'embedGoogleVideo');
        $wgParser->setHook('aovideo', 'embedArchiveOrgVideo');
        $wgParser->setHook('aoaudio', 'embedArchiveOrgAudio');
        $wgParser->setHook('wegame', 'embedWeGame');
}

function embedYouTube_url2ytid($url)
{
        $id = $url;

        if (preg_match('/^http:\/\/www\.youtube\.com\/watch\?v=(.+)$/', $url, $preg))
        {
                $id = $preg[1];
        } elseif (preg_match('/^http:\/\/www\.youtube\.com\/v\/([^&]+)(&autoplay=[0-1])?$/', $url, $preg))
        {
                $id = $preg[1];
        }

        preg_match('/([0-9A-Za-z_-]+)/', $id, $preg);
        $id = $preg[1];

        return $id;
}

function embedYouTube($input, $argv, &$parser)
{
        //$parser->disableCache();

        $ytid   = '';
        $width  = $width_max  = 425;
        $height = $height_max = 355;

        if (!empty($argv['ytid']))
        {
                $ytid = embedYouTube_url2ytid($argv['ytid']);
        } elseif (!empty($input))
        {
                $ytid = embedYouTube_url2ytid($input);
        }
        if (!empty($argv['width']) && settype($argv['width'], 'integer') && ($width_max >= $argv['width']))
        {
                $width = $argv['width'];
        }
        if (!empty($argv['height']) && settype($argv['height'], 'integer') && ($height_max >= $argv['height']))
        {
                $height = $argv['height'];
        }

        if (!empty($ytid))
        {
                $url = "http://www.youtube.com/v/{$ytid}";
                return "<object type=\"application/x-shockwave-flash\" data=\"{$url}\" width=\"{$width}\" height=\"{$height}\"><param name=\"movie\" value=\"{$url}\"/></object>";
        }
}

function embedYouTube_url2gvid($url)
{
        $id = $url;

        if (preg_match('/^http:\/\/video\.google\.com\/videoplay\?docid=([^&]+)(&hl=.+)?$/', $url, $preg))
        {
                $id = $preg[1];
        } elseif (preg_match('/^http:\/\/video\.google\.com\/googleplayer\.swf\?docId=(.+)$/', $url, $preg))
        {
                $id = $preg[1];
        }

        preg_match('/([0-9-]+)/', $id, $preg);
        $id = $preg[1];

        return $id;
}

function embedGoogleVideo($input, $argv, &$parser)
{
        $gvid   = '';
        $width  = $width_max  = 400;
        $height = $height_max = 326;

        if (!empty($argv['gvid']))
        {
                $gvid = embedYouTube_url2gvid($argv['gvid']);
        } elseif (!empty($input))
        {
                $gvid = embedYouTube_url2gvid($input);
        }
        if (!empty($argv['width']) && settype($argv['width'], 'integer') && ($width_max >= $argv['width']))
        {
                $width = $argv['width'];
        }
        if (!empty($argv['height']) && settype($argv['height'], 'integer') && ($height_max >= $argv['height']))
        {
                $height = $argv['height'];
        }

        if (!empty($gvid))
        {
                $url = "http://video.google.com/googleplayer.swf?docId={$gvid}";
                return "<object type=\"application/x-shockwave-flash\" data=\"{$url}\" width=\"{$width}\" height=\"{$height}\"><param name=\"movie\" value=\"{$url}\"/></object>";
        }
}

function embedYouTube_url2aovid($url)
{
        $id = $url;

        if (preg_match('/http:\/\/www\.archive\.org\/download\/(.+)\.flv$/', $url, $preg))
        {
                $id = $preg[1];
        }

        preg_match('/([0-9A-Za-z_\/.]+)/', $id, $preg);
        $id = $preg[1];

        return $id;
}

function embedArchiveOrgVideo($input, $argv, &$parser)
{
        $aovid   = '';
        $width  = $width_max  = 320;
        $height = $height_max = 263;

        if (!empty($argv['aovid']))
        {
                $aovid = embedYouTube_url2aovid($argv['aovid']);
        } elseif (!empty($input))
        {
                $aovid = embedYouTube_url2aovid($input);
        }
        if (!empty($argv['width']) && settype($argv['width'], 'integer') && ($width_max >= $argv['width']))
        {
                $width = $argv['width'];
        }
        if (!empty($argv['height']) && settype($argv['height'], 'integer') && ($height_max >= $argv['height']))
        {
                $height = $argv['height'];
        }

        if (!empty($aovid))
        {
                $url = "http://www.archive.org/download/{$aovid}.flv";
                return "<object type=\"application/x-shockwave-flash\" data=\"http://www.archive.org/flv/FlowPlayerWhite.swf\" width=\"{$width}\" height=\"{$height}\"><param name=\"movie\" value=\"http://www.archive.org/flv/FlowPlayerWhite.swf\"/><param name=\"flashvars\" value=\"config={loop: false, videoFile: '{$url}', autoPlay: false}\"/></object>";
        }
}

function embedYouTube_url2aoaid($url)
{
        $id = $url;

        if (preg_match('/http:\/\/www\.archive\.org\/details\/(.+)$/', $url, $preg))
        {
                $id = $preg[1];
        }

        preg_match('/([0-9A-Za-z_\/.]+)/', $id, $preg);
        $id = $preg[1];

        return $id;
}

function embedArchiveOrgAudio($input, $argv, &$parser)
{
        $aoaid   = '';
        $width  = $width_max  = 400;
        $height = $height_max = 170;

        if (!empty($argv['aoaid']))
        {
                $aoaid = embedYouTube_url2aoaid($argv['aoaid']);
        } elseif (!empty($input))
        {
                $aoaid = embedYouTube_url2aoaid($input);
        }
        if (!empty($argv['width']) && settype($argv['width'], 'integer') && ($width_max >= $argv['width']))
        {
                $width = $argv['width'];
        }
        if (!empty($argv['height']) && settype($argv['height'], 'integer') && ($height_max >= $argv['height']))
        {
                $height = $argv['height'];
        }

        if (!empty($aoaid))
        {
                $url = urlencode("http://www.archive.org/audio/xspf-maker.php?identifier={$aoaid}");
                return "<object type=\"application/x-shockwave-flash\" data=\"http://www.archive.org/audio/xspf_player.swf?playlist_url={$url}\" width=\"{$width}\" height=\"{$height}\"><param name=\"movie\" value=\"http://www.archive.org/audio/xspf_player.swf?playlist_url={$url}\"/></object>";
        }
}

function embedYouTube_url2weid($url)
{
        $id = $url;

        if (preg_match('/^http:\/\/www\.wegame\.com\/watch\/(.+)\/$/', $url, $preg))
        {
                $id = $preg[1];
        }

        preg_match('/([0-9A-Za-z_-]+)/', $id, $preg);
        $id = $preg[1];

        return $id;
}

function embedWeGame($input, $argv, &$parser)
{
        $weid   = '';
        $width  = $width_max  = 488;
        $height = $height_max = 387;

        if (!empty($argv['weid']))
        {
                $weid = embedYouTube_url2weid($argv['weid']);
        } elseif (!empty($input))
        {
                $weid = embedYouTube_url2weid($input);
        }
        if (!empty($argv['width']) && settype($argv['width'], 'integer') && ($width_max >= $argv['width']))
        {
                $width = $argv['width'];
        }
        if (!empty($argv['height']) && settype($argv['height'], 'integer') && ($height_max >= $argv['height']))
        {
                $height = $argv['height'];
        }

        if (!empty($weid))
        {
                return "<object type=\"application/x-shockwave-flash\" data=\"http://www.wegame.com/static/flash/player2.swf\" width=\"{$width}\" height=\"{$height}\"><param name=\"flashvars\" value=\"tag={$weid}\"/></object>";

        }
}
Personal tools
Namespaces
Variants
Actions
navigation
Toolbox