2 rcs_id('$Id: YouTube.php,v 1.2 2007-05-24 19:03:07 rurban Exp $');
4 Copyright 2007 Reini Urban
10 * Browse: Daily pick, Most Recent, Most Viewed, Top Rated, Most Discussed, Top Favorites, Most Linked,
11 * Recently Featured, Most Responded, Watch on Mobile
12 * Time: Today, This Week, This Month, All Time
13 * Category: All, Autos & Vehicles, Comedy, Entertainment, Film & Animation,
14 * Gadgets & Games, Howto & DIY, Music, News & Politics, People & Blogs, Pets & Animals,
15 * Sports, Travel & Places
16 * Language: All, English, Spanish, Japanese, German, Chinese, French
17 * @author: Reini Urban
20 class WikiPlugin_YouTube
27 function getDescription () {
28 return _("Embed YouTube videos");
31 function getVersion() {
32 return preg_replace("/[Revision: $]/", '',"\$Revision: 1.2 $");
35 function getDefaultArguments() {
36 return array('v' => "",
37 'browse' => '', // see above
38 'time' => '', // only if browse
39 'category' => '', // only if browse
40 'language' => '', // only if browse
41 'index' => 0, // only if browse
42 'style' => 'inline', // or link. link links to youtube.
43 'size' => 'medium', // or large, medium or small
49 function run($dbi, $argstr, &$request, $basepage) {
50 $args = $this->getArgs($argstr, $request);
52 if (empty($args['v'])) {
53 if (empty($args['browse']))
54 return $this->error(fmt("Required argument %s missing", "v"));
55 $this->_browse = array("Most Recent" => "mr",
56 "Most Viewed" => "mp",
58 "Most Discussed"=> "md",
59 "Top Favorites" => "mf",
60 "Most Linked" => "mrd",
61 "Recently Featured"=> "rf",
62 "Most Responded" => "ms",
63 "Watch on Mobile" => "mv");
64 $this->browse = array_keys($this->_browse);
65 array_unshift($this->browse, "Daily Pick");
66 $this->_time = array("Today" => "t",
70 $this->_category = array("All" => "0",
71 "Autos & Vehicles" => "2",
73 "Entertainment" => "24",
74 "Film & Animation" => "1",
75 "Gadgets & Games" => "20",
76 "Howto & DIY" => "26",
78 "News & Politics" => "25",
79 "People & Blogs" => "22",
80 "Pets & Animals" => "15",
82 "Travel & Places" => "19");
83 $this->_language = array("All" => "",
90 if (!in_array($browse,$this->browse))
91 return $this->error(fmt("Invalid argument %s", "browse"));
92 if ($time and !in_array($time,array_keys($this->_time)))
93 return $this->error(fmt("Invalid argument %s", "time"));
94 if ($category and !in_array($category,$this->category))
95 return $this->error(fmt("Invalid argument %s", "category"));
96 if ($language and !in_array($language,$this->language))
97 return $this->error(fmt("Invalid argument %s", "language"));
98 if ($browse == "Daily Pick")
99 $v = $this->Daily_pick();
101 $s = $this->_browse[$browse];
102 $t = $time ? $this->_time[$time] : 't';
103 $c = $category ? $this->_category[$category] : '0';
104 $l = $language ? $this->_language[$language] : '';
105 $url = "http://www.youtube.com/browse?s=$s&t=$t&c=$c&l=$l";
107 if ($xml = url_get_contents($url)) {
109 if (preg_match_all('/<div class="vtitle">.*?\n.*?<a href="\/watch\?v=(\w+)" onclick=/s', $xml, $m))
113 if (preg_match('/<div class="vtitle">.*?\n.*?<a href="\/watch\?v=(\w+)" onclick=/s', $xml, $m))
120 if (strlen($v) < 10 or strlen($v) > 12)
121 return $this->error(fmt("Invalid argument %s", "v"));
122 if (strcspn($v,"-_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))
123 return $this->error(fmt("Invalid argument %s", "v"));
124 $url = "http://www.youtube.com/v/" . $v;
126 $url .= "?autoplay=1";
127 if ($size != 'medium') {
128 if ($size == 'large') { $width = 640; $height = 526; }
129 elseif ($size == 'small') { $width = 240; $height = 200; }
131 unset($args['size']);
132 unset($args['style']);
135 if ($style == 'link') {
136 if ($size == 'medium') { $width = 130; $height = 97; }
137 elseif ($size == 'large') { $width = 320; $height = 240; }
138 elseif ($size == 'small') { $width = 90; $height = 60; }
139 // img: http://img.youtube.com/vi/KKTDRqQtPO8/2.jpg or 0.jpg
140 $link = HTML::a(array('href' => $url),
141 HTML::img(array('src' => "http://img.youtube.com/vi/".
142 $v."/".(($size == 'large')?"0":"2").".jpg",
144 'height' => $height)));
147 $object = HTML::object(array('class' => 'inlineobject',
151 $attrs = array('src' => $url,
152 'type' => 'application/x-shockwave-flash',
153 'wmode' => 'transparent',
155 'height' => $height);
156 if (isBrowserSafari()) {
157 return HTML::embed($attrs);
159 $object->pushContent(HTML::param(array('name' => 'movie', 'value' => $url)));
160 $object->pushContent(HTML::param(array('name' => 'wmode', 'value' => 'transparent')));
161 $object->pushContent(HTML::embed($attrs));
164 //<object width="$w" height="$h"><param name="movie" value="http://www.youtube.com/v/$v"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/$v" type="application/x-shockwave-flash" wmode="transparent" width="$w" height="$h"></embed></object>
167 function Daily_pick() {
168 if ($xml = url_get_contents("http://www.youtube.com/categories")) {
169 if (preg_match('/<div class="heading"><b>Pick of The Day<\/b><\/div>.*?<a href="\/watch\?v=(\w+)">/s', $xml, $m))
176 // $Log: not supported by cvs2svn $
177 // Revision 1.1 2007/05/24 18:40:30 rurban
187 // c-hanging-comment-ender-p: nil
188 // indent-tabs-mode: nil