4 Copyright 2007 Reini Urban
5 Copyright 2008 Marc-Etienne Vargenau, Alcatel-Lucent
11 * Browse: Daily pick, Most Recent, Most Viewed, Top Rated, Most Discussed, Top Favorites, Most Linked,
12 * Recently Featured, Most Responded, Watch on Mobile
13 * Time: Today, This Week, This Month, All Time
14 * Category: All, Autos & Vehicles, Comedy, Entertainment, Film & Animation,
15 * Gadgets & Games, Howto & DIY, Music, News & Politics, People & Blogs, Pets & Animals,
16 * Sports, Travel & Places
17 * Language: All, English, Spanish, Japanese, German, Chinese, French
18 * @author: Reini Urban
21 class WikiPlugin_YouTube
28 function getDescription () {
29 return _("Embed YouTube videos");
32 function getVersion() {
33 return preg_replace("/[Revision: $]/", '',"\$Revision$");
36 function getDefaultArguments() {
37 return array('v' => "",
38 'browse' => '', // see above
39 'time' => '', // only if browse
40 'category' => '', // only if browse
41 'language' => '', // only if browse
42 'index' => 0, // only if browse
43 'style' => 'inline', // or link. link links to youtube.
44 'size' => 'medium', // or large, medium or small
50 function run($dbi, $argstr, &$request, $basepage) {
51 $args = $this->getArgs($argstr, $request);
53 if (empty($args['v'])) {
54 if (empty($args['browse']))
55 return $this->error(fmt("Required argument %s missing", "v"));
56 $this->_browse = array("Most Recent" => "mr",
57 "Most Viewed" => "mp",
59 "Most Discussed"=> "md",
60 "Top Favorites" => "mf",
61 "Most Linked" => "mrd",
62 "Recently Featured"=> "rf",
63 "Most Responded" => "ms",
64 "Watch on Mobile" => "mv");
65 $this->browse = array_keys($this->_browse);
66 array_unshift($this->browse, "Daily Pick");
67 $this->_time = array("Today" => "t",
71 $this->_category = array("All" => "0",
72 "Autos & Vehicles" => "2",
74 "Entertainment" => "24",
75 "Film & Animation" => "1",
76 "Gadgets & Games" => "20",
77 "Howto & DIY" => "26",
79 "News & Politics" => "25",
80 "People & Blogs" => "22",
81 "Pets & Animals" => "15",
83 "Travel & Places" => "19");
84 $this->_language = array("All" => "",
91 if (!in_array($browse,$this->browse))
92 return $this->error(fmt("Invalid argument %s", "browse"));
93 if ($time and !in_array($time,array_keys($this->_time)))
94 return $this->error(fmt("Invalid argument %s", "time"));
95 if ($category and !in_array($category,$this->category))
96 return $this->error(fmt("Invalid argument %s", "category"));
97 if ($language and !in_array($language,$this->language))
98 return $this->error(fmt("Invalid argument %s", "language"));
99 if ($browse == "Daily Pick")
100 $v = $this->Daily_pick();
102 $s = $this->_browse[$browse];
103 $t = $time ? $this->_time[$time] : 't';
104 $c = $category ? $this->_category[$category] : '0';
105 $l = $language ? $this->_language[$language] : '';
106 $url = "http://www.youtube.com/browse?s=$s&t=$t&c=$c&l=$l";
108 if ($xml = url_get_contents($url)) {
110 if (preg_match_all('/<div class="vtitle">.*?\n.*?<a href="\/watch\?v=(\w+)" onclick=/s', $xml, $m))
114 if (preg_match('/<div class="vtitle">.*?\n.*?<a href="\/watch\?v=(\w+)" onclick=/s', $xml, $m))
121 if (strlen($v) < 10 or strlen($v) > 12)
122 return $this->error(fmt("Invalid argument %s", "v"));
123 if (strcspn($v,"-_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))
124 return $this->error(fmt("Invalid argument %s", "v"));
125 $url = "http://www.youtube.com/v/" . $v;
127 $url .= "?autoplay=1";
128 if ($size != 'medium') {
129 if ($size == 'large') { $width = 640; $height = 526; }
130 elseif ($size == 'small') { $width = 240; $height = 200; }
132 unset($args['size']);
133 unset($args['style']);
136 if ($style == 'link') {
137 if ($size == 'medium') { $width = 130; $height = 97; }
138 elseif ($size == 'large') { $width = 320; $height = 240; }
139 elseif ($size == 'small') { $width = 90; $height = 60; }
140 // img: http://img.youtube.com/vi/KKTDRqQtPO8/2.jpg or 0.jpg
141 $link = HTML::a(array('href' => $url),
142 HTML::img(array('src' => "http://img.youtube.com/vi/".
143 $v."/".(($size == 'large')?"0":"2").".jpg",
146 'alt' => "YouTube video $v")));
149 $object = HTML::object(array('class' => 'inlineobject',
153 $attrs = array('src' => $url,
154 'type' => 'application/x-shockwave-flash',
155 'wmode' => 'transparent',
157 'height' => $height);
158 if (isBrowserSafari()) {
159 return HTML::embed($attrs);
161 $object->pushContent(HTML::param(array('name' => 'movie', 'value' => $url)));
162 $object->pushContent(HTML::param(array('name' => 'wmode', 'value' => 'transparent')));
163 $object->pushContent(HTML::embed($attrs));
166 //<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>
169 function Daily_pick() {
170 if ($xml = url_get_contents("http://www.youtube.com/categories")) {
171 if (preg_match('/<div class="heading"><b>Pick of The Day<\/b><\/div>.*?<a href="\/watch\?v=(\w+)">/s', $xml, $m))
183 // c-hanging-comment-ender-p: nil
184 // indent-tabs-mode: nil