4 * Copyright 2007 Reini Urban
5 * Copyright 2008 Marc-Etienne Vargenau, Alcatel-Lucent
7 * This file is part of PhpWiki.
9 * PhpWiki is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * PhpWiki is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * Embed YouTube videos
27 * Browse: Daily pick, Most Recent, Most Viewed, Top Rated, Most Discussed, Top Favorites, Most Linked,
28 * Recently Featured, Most Responded, Watch on Mobile
29 * Time: Today, This Week, This Month, All Time
30 * Category: All, Autos & Vehicles, Comedy, Entertainment, Film & Animation,
31 * Gadgets & Games, Howto & DIY, Music, News & Politics, People & Blogs, Pets & Animals,
32 * Sports, Travel & Places
33 * Language: All, English, Spanish, Japanese, German, Chinese, French
34 * @author: Reini Urban
37 class WikiPlugin_YouTube
40 function getDescription()
42 return _("Embed YouTube videos.");
45 function getDefaultArguments()
47 return array('v' => "",
48 'browse' => '', // see above
49 'time' => '', // only if browse
50 'category' => '', // only if browse
51 'language' => '', // only if browse
52 'index' => 0, // only if browse
53 'style' => 'inline', // or link. link links to youtube.
54 'size' => 'medium', // or large, medium or small
60 function run($dbi, $argstr, &$request, $basepage)
62 $args = $this->getArgs($argstr, $request);
64 if (empty($args['v'])) {
65 if (empty($args['browse']))
66 return $this->error(fmt("Required argument %s missing", "v"));
67 $this->_browse = array("Most Recent" => "mr",
68 "Most Viewed" => "mp",
70 "Most Discussed" => "md",
71 "Top Favorites" => "mf",
72 "Most Linked" => "mrd",
73 "Recently Featured" => "rf",
74 "Most Responded" => "ms",
75 "Watch on Mobile" => "mv");
76 $this->browse = array_keys($this->_browse);
77 array_unshift($this->browse, "Daily Pick");
78 $this->_time = array("Today" => "t",
82 $this->_category = array("All" => "0",
83 "Autos & Vehicles" => "2",
85 "Entertainment" => "24",
86 "Film & Animation" => "1",
87 "Gadgets & Games" => "20",
88 "Howto & DIY" => "26",
90 "News & Politics" => "25",
91 "People & Blogs" => "22",
92 "Pets & Animals" => "15",
94 "Travel & Places" => "19");
95 $this->_language = array("All" => "",
102 if (!in_array($browse, $this->browse))
103 return $this->error(fmt("Invalid argument %s", "browse"));
104 if ($time and !in_array($time, array_keys($this->_time)))
105 return $this->error(fmt("Invalid argument %s", "time"));
106 if ($category and !in_array($category, $this->category))
107 return $this->error(fmt("Invalid argument %s", "category"));
108 if ($language and !in_array($language, $this->language))
109 return $this->error(fmt("Invalid argument %s", "language"));
110 if ($browse == "Daily Pick")
111 $v = $this->Daily_pick();
113 $s = $this->_browse[$browse];
114 $t = $time ? $this->_time[$time] : 't';
115 $c = $category ? $this->_category[$category] : '0';
116 $l = $language ? $this->_language[$language] : '';
117 $url = "http://www.youtube.com/browse?s=$s&t=$t&c=$c&l=$l";
119 if ($xml = url_get_contents($url)) {
121 if (preg_match_all('/<div class="vtitle">.*?\n.*?<a href="\/watch\?v=(\w+)" onclick=/s', $xml, $m))
124 if (preg_match('/<div class="vtitle">.*?\n.*?<a href="\/watch\?v=(\w+)" onclick=/s', $xml, $m))
131 if (strlen($v) < 10 or strlen($v) > 12)
132 return $this->error(fmt("Invalid argument %s", "v"));
133 if (strcspn($v, "-_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))
134 return $this->error(fmt("Invalid argument %s", "v"));
135 $url = "http://www.youtube.com/v/" . $v;
137 $url .= "?autoplay=1";
138 if ($size != 'medium') {
139 if ($size == 'large') {
142 } elseif ($size == 'small') {
147 unset($args['size']);
148 unset($args['style']);
151 if ($style == 'link') {
152 if ($size == 'medium') {
155 } elseif ($size == 'large') {
158 } elseif ($size == 'small') {
162 // img: http://img.youtube.com/vi/KKTDRqQtPO8/2.jpg or 0.jpg
163 $link = HTML::a(array('href' => $url),
164 HTML::img(array('src' => "http://img.youtube.com/vi/" .
165 $v . "/" . (($size == 'large') ? "0" : "2") . ".jpg",
168 'alt' => "YouTube video $v")));
171 $object = HTML::object(array('class' => 'inlineobject',
175 $attrs = array('data' => $url,
176 'type' => 'application/x-shockwave-flash',
178 'height' => $height);
179 if (isBrowserSafari()) {
180 return HTML::object($attrs);
182 $object->pushContent(HTML::param(array('name' => 'movie', 'value' => $url)));
183 $object->pushContent(HTML::param(array('name' => 'wmode', 'value' => 'transparent')));
184 $object->pushContent(HTML::object($attrs));
188 private function Daily_pick()
190 if ($xml = url_get_contents("http://www.youtube.com/categories")) {
191 if (preg_match('/<div class="heading"><b>Pick of The Day<\/b><\/div>.*?<a href="\/watch\?v=(\w+)">/s', $xml, $m))
202 // c-hanging-comment-ender-p: nil
203 // indent-tabs-mode: nil