Index: trunk/extensions/YouTube/YouTube.php |
— | — | @@ -0,0 +1,375 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Parser hook-based extension to show audio and video players |
| 5 | + * from YouTube and other similar sites. |
| 6 | + * |
| 7 | + * @file |
| 8 | + * @ingroup Extensions |
| 9 | + * @author Przemek Piotrowski <ppiotr@wikia-inc.com> for Wikia, Inc. |
| 10 | + * @copyright © 2006-2008, Wikia Inc. |
| 11 | + * @licence GNU General Public Licence 2.0 or later |
| 12 | + * |
| 13 | + * This program is free software; you can redistribute it and/or modify |
| 14 | + * it under the terms of the GNU General Public License as published by |
| 15 | + * the Free Software Foundation; either version 2 of the License, or |
| 16 | + * (at your option) any later version. |
| 17 | + * |
| 18 | + * This program is distributed in the hope that it will be useful, but |
| 19 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | + * General Public License for more details. |
| 22 | + * |
| 23 | + * You should have received a copy of the GNU General Public License |
| 24 | + * along with this program; if not, write to the Free Software |
| 25 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 |
| 26 | + * USA |
| 27 | + * |
| 28 | + * @todo one class (family) to rule 'em all |
| 29 | + * @todo make width/height_max != width/height_default; aoaudio height may be large - long playlist |
| 30 | + * @todo smart <video> and <audio> tag |
| 31 | + */ |
| 32 | + |
| 33 | +if( !defined( 'MEDIAWIKI' ) ) { |
| 34 | + echo "This is a MediaWiki extension.\n"; |
| 35 | + exit( 1 ); |
| 36 | +} |
| 37 | + |
| 38 | +$wgExtensionCredits['parserhook'][] = array( |
| 39 | + 'name' => 'YouTube', |
| 40 | + 'version' => '1.8.1', |
| 41 | + 'author' => 'Przemek Piotrowski', |
| 42 | + 'description' => 'Embeds YouTube and Google Video movies + Archive.org audio and video + WeGame and Gametrailers video + Tangler forum + GoGreenTube video', |
| 43 | +); |
| 44 | + |
| 45 | +$wgHooks['ParserFirstCallInit'][] = 'wfYouTube'; |
| 46 | + |
| 47 | +function wfYouTube( &$parser ) { |
| 48 | + $parser->setHook( 'youtube', 'embedYouTube' ); |
| 49 | + $parser->setHook( 'gvideo', 'embedGoogleVideo' ); |
| 50 | + $parser->setHook( 'aovideo', 'embedArchiveOrgVideo' ); |
| 51 | + $parser->setHook( 'aoaudio', 'embedArchiveOrgAudio' ); |
| 52 | + $parser->setHook( 'wegame', 'embedWeGame' ); |
| 53 | + $parser->setHook( 'tangler', 'embedTangler' ); |
| 54 | + $parser->setHook( 'gtrailer', 'embedGametrailers' ); |
| 55 | + $parser->setHook( 'nicovideo', 'embedNicovideo' ); |
| 56 | + $parser->setHook( 'ggtube', 'embedGoGreenTube' ); |
| 57 | + return true; |
| 58 | +} |
| 59 | + |
| 60 | +function embedYouTube_url2ytid( $url ) { |
| 61 | + $id = $url; |
| 62 | + |
| 63 | + if ( preg_match( '/^http:\/\/www\.youtube\.com\/watch\?v=(.+)$/', $url, $preg ) ) { |
| 64 | + $id = $preg[1]; |
| 65 | + } elseif( preg_match( '/^http:\/\/www\.youtube\.com\/v\/([^&]+)(&autoplay=[0-1])?$/', $url, $preg ) ) { |
| 66 | + $id = $preg[1]; |
| 67 | + } |
| 68 | + |
| 69 | + preg_match( '/([0-9A-Za-z_-]+)/', $id, $preg ); |
| 70 | + $id = $preg[1]; |
| 71 | + |
| 72 | + return $id; |
| 73 | +} |
| 74 | + |
| 75 | +function embedYouTube( $input, $argv, $parser ) { |
| 76 | + $ytid = ''; |
| 77 | + $width = $width_max = 425; |
| 78 | + $height = $height_max = 355; |
| 79 | + |
| 80 | + if ( !empty( $argv['ytid'] ) ) { |
| 81 | + $ytid = embedYouTube_url2ytid( $argv['ytid'] ); |
| 82 | + } elseif ( !empty( $input ) ) { |
| 83 | + $ytid = embedYouTube_url2ytid( $input ); |
| 84 | + } |
| 85 | + if ( !empty( $argv['width'] ) && settype( $argv['width'], 'integer' ) && ( $width_max >= $argv['width'] ) ) { |
| 86 | + $width = $argv['width']; |
| 87 | + } |
| 88 | + if ( !empty( $argv['height'] ) && settype( $argv['height'], 'integer' ) && ( $height_max >= $argv['height'] ) ) { |
| 89 | + $height = $argv['height']; |
| 90 | + } |
| 91 | + |
| 92 | + if ( !empty( $ytid ) ) { |
| 93 | + $url = "http://www.youtube.com/v/{$ytid}"; |
| 94 | + return "<object type=\"application/x-shockwave-flash\" data=\"{$url}\" width=\"{$width}\" height=\"{$height}\"><param name=\"movie\" value=\"{$url}\"/><param name=\"wmode\" value=\"transparent\"/></object>"; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +function embedYouTube_url2gvid( $url ) { |
| 99 | + $id = $url; |
| 100 | + |
| 101 | + if ( preg_match( '/^http:\/\/video\.google\.com\/videoplay\?docid=([^&]+)(&hl=.+)?$/', $url, $preg ) ) { |
| 102 | + $id = $preg[1]; |
| 103 | + } elseif ( preg_match( '/^http:\/\/video\.google\.com\/googleplayer\.swf\?docId=(.+)$/', $url, $preg ) ) { |
| 104 | + $id = $preg[1]; |
| 105 | + } |
| 106 | + |
| 107 | + preg_match( '/([0-9-]+)/', $id, $preg ); |
| 108 | + $id = $preg[1]; |
| 109 | + |
| 110 | + return $id; |
| 111 | +} |
| 112 | + |
| 113 | +function embedGoogleVideo( $input, $argv, $parser ) { |
| 114 | + $gvid = ''; |
| 115 | + $width = $width_max = 400; |
| 116 | + $height = $height_max = 326; |
| 117 | + |
| 118 | + if ( !empty( $argv['gvid'] ) ) { |
| 119 | + $gvid = embedYouTube_url2gvid( $argv['gvid'] ); |
| 120 | + } elseif ( !empty( $input ) ) { |
| 121 | + $gvid = embedYouTube_url2gvid( $input ); |
| 122 | + } |
| 123 | + if ( !empty( $argv['width'] ) && settype( $argv['width'], 'integer' ) && ( $width_max >= $argv['width'] ) ) { |
| 124 | + $width = $argv['width']; |
| 125 | + } |
| 126 | + if ( !empty( $argv['height'] ) && settype( $argv['height'], 'integer' ) && ( $height_max >= $argv['height'] ) ) { |
| 127 | + $height = $argv['height']; |
| 128 | + } |
| 129 | + |
| 130 | + if ( !empty( $gvid ) ) { |
| 131 | + $url = "http://video.google.com/googleplayer.swf?docId={$gvid}"; |
| 132 | + return "<object type=\"application/x-shockwave-flash\" data=\"{$url}\" width=\"{$width}\" height=\"{$height}\"><param name=\"movie\" value=\"{$url}\"/><param name=\"wmode\" value=\"transparent\"/></object>"; |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +function embedYouTube_url2aovid( $url ) { |
| 137 | + $id = $url; |
| 138 | + |
| 139 | + if ( preg_match( '/http:\/\/www\.archive\.org\/download\/(.+)\.flv$/', $url, $preg ) ) { |
| 140 | + $id = $preg[1]; |
| 141 | + } |
| 142 | + |
| 143 | + preg_match( '/([0-9A-Za-z_\/.]+)/', $id, $preg ); |
| 144 | + $id = $preg[1]; |
| 145 | + |
| 146 | + return $id; |
| 147 | +} |
| 148 | + |
| 149 | +function embedArchiveOrgVideo( $input, $argv, $parser ) { |
| 150 | + $aovid = ''; |
| 151 | + $width = $width_max = 320; |
| 152 | + $height = $height_max = 263; |
| 153 | + |
| 154 | + if ( !empty( $argv['aovid'] ) ) { |
| 155 | + $aovid = embedYouTube_url2aovid( $argv['aovid'] ); |
| 156 | + } elseif ( !empty( $input ) ) { |
| 157 | + $aovid = embedYouTube_url2aovid( $input ); |
| 158 | + } |
| 159 | + if ( !empty( $argv['width'] ) && settype( $argv['width'], 'integer' ) && ( $width_max >= $argv['width'] ) ) { |
| 160 | + $width = $argv['width']; |
| 161 | + } |
| 162 | + if ( !empty( $argv['height'] ) && settype( $argv['height'], 'integer' ) && ( $height_max >= $argv['height'] ) ) { |
| 163 | + $height = $argv['height']; |
| 164 | + } |
| 165 | + |
| 166 | + if ( !empty( $aovid ) ) { |
| 167 | + $url = "http://www.archive.org/download/{$aovid}.flv"; |
| 168 | + 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>"; |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +function embedYouTube_url2aoaid( $url ) { |
| 173 | + $id = $url; |
| 174 | + |
| 175 | + if ( preg_match( '/http:\/\/www\.archive\.org\/details\/(.+)$/', $url, $preg ) ) { |
| 176 | + $id = $preg[1]; |
| 177 | + } |
| 178 | + |
| 179 | + preg_match( '/([0-9A-Za-z_\/.]+)/', $id, $preg ); |
| 180 | + $id = $preg[1]; |
| 181 | + |
| 182 | + return $id; |
| 183 | +} |
| 184 | + |
| 185 | +function embedArchiveOrgAudio( $input, $argv, $parser ) { |
| 186 | + $aoaid = ''; |
| 187 | + $width = $width_max = 400; |
| 188 | + $height = $height_max = 170; |
| 189 | + |
| 190 | + if ( !empty( $argv['aoaid'] ) ) { |
| 191 | + $aoaid = embedYouTube_url2aoaid( $argv['aoaid'] ); |
| 192 | + } elseif ( !empty( $input ) ) { |
| 193 | + $aoaid = embedYouTube_url2aoaid( $input ); |
| 194 | + } |
| 195 | + if ( !empty( $argv['width'] ) && settype( $argv['width'], 'integer' ) && ( $width_max >= $argv['width'] ) ) { |
| 196 | + $width = $argv['width']; |
| 197 | + } |
| 198 | + if ( !empty( $argv['height'] ) && settype( $argv['height'], 'integer' ) && ( $height_max >= $argv['height'] ) ) { |
| 199 | + $height = $argv['height']; |
| 200 | + } |
| 201 | + |
| 202 | + if ( !empty( $aoaid ) ) { |
| 203 | + $url = urlencode( "http://www.archive.org/audio/xspf-maker.php?identifier={$aoaid}" ); |
| 204 | + 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>"; |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +function embedYouTube_url2weid( $url ) { |
| 209 | + $id = $url; |
| 210 | + |
| 211 | + if ( preg_match( '/^http:\/\/www\.wegame\.com\/watch\/(.+)\/$/', $url, $preg ) ) { |
| 212 | + $id = $preg[1]; |
| 213 | + } |
| 214 | + |
| 215 | + preg_match( '/([0-9A-Za-z_-]+)/', $id, $preg ); |
| 216 | + $id = $preg[1]; |
| 217 | + |
| 218 | + return $id; |
| 219 | +} |
| 220 | + |
| 221 | +function embedWeGame( $input, $argv, $parser ) { |
| 222 | + $weid = ''; |
| 223 | + $width = $width_max = 488; |
| 224 | + $height = $height_max = 387; |
| 225 | + |
| 226 | + if ( !empty( $argv['weid'] ) ) { |
| 227 | + $weid = embedYouTube_url2weid( $argv['weid'] ); |
| 228 | + } elseif ( !empty( $input ) ) { |
| 229 | + $weid = embedYouTube_url2weid( $input ); |
| 230 | + } |
| 231 | + if ( !empty( $argv['width'] ) && settype( $argv['width'], 'integer' ) && ( $width_max >= $argv['width'] ) ) { |
| 232 | + $width = $argv['width']; |
| 233 | + } |
| 234 | + if ( !empty( $argv['height'] ) && settype( $argv['height'], 'integer' ) && ( $height_max >= $argv['height'] ) ) { |
| 235 | + $height = $argv['height']; |
| 236 | + } |
| 237 | + |
| 238 | + if ( !empty( $weid ) ) { |
| 239 | + 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>"; |
| 240 | + } |
| 241 | +} |
| 242 | + |
| 243 | +function embedYouTube_url2tgid( $input ) { |
| 244 | + $tid = $gid = 0; |
| 245 | + |
| 246 | + if ( preg_match( '/^id=([0-9]+)\|gId=([0-9]+)$/i', $input, $preg ) ) { |
| 247 | + $tid = $preg[1]; |
| 248 | + $gid = $preg[2]; |
| 249 | + } elseif ( preg_match( '/^gId=([0-9]+)\|id=([0-9]+)$/i', $input, $preg ) ) { |
| 250 | + $tid = $preg[2]; |
| 251 | + $gid = $preg[1]; |
| 252 | + } elseif ( preg_match( '/^([0-9]+)\|([0-9]+)$/', $input, $preg ) ) { |
| 253 | + $tid = $preg[1]; |
| 254 | + $gid = $preg[2]; |
| 255 | + } |
| 256 | + |
| 257 | + return array( $tid, $gid ); |
| 258 | +} |
| 259 | + |
| 260 | +function embedTangler( $input, $argv, $parser ) { |
| 261 | + $tid = $gid = ''; |
| 262 | + |
| 263 | + if ( !empty( $argv['tid'] ) && !empty( $argv['gid'] ) ) { |
| 264 | + list( $tid, $gid ) = embedYouTube_url2tgid( "{$argv['tid']}|{$argv['gid']}" ); |
| 265 | + } elseif ( !empty( $input ) ) { |
| 266 | + list( $tid, $gid ) = embedYouTube_url2tgid( $input ); |
| 267 | + } |
| 268 | + |
| 269 | + if ( !empty( $tid ) && !empty( $gid ) ) { |
| 270 | + return "<p style=\"width: 410px; height: 480px\" id=\"tangler-embed-topic-{$tid}\"></p><script src=\"http://www.tangler.com/widget/embedtopic.js?id={$tid}&gId={$gid}\"></script>"; |
| 271 | + } |
| 272 | +} |
| 273 | + |
| 274 | +function embedYouTube_url2gtid( $url ) { |
| 275 | + $id = $url; |
| 276 | + |
| 277 | + if ( preg_match( '/^http:\/\/www\.gametrailers\.com\/player\/(.+)\.html$/', $url, $preg ) ) { |
| 278 | + $id = $preg[1]; |
| 279 | + } elseif ( preg_match( '/^http:\/\/www\.gametrailers\.com\/remote_wrap\.php\?mid=(.+)$/', $url, $preg ) ) { |
| 280 | + $id = $preg[1]; |
| 281 | + } |
| 282 | + |
| 283 | + preg_match( '/([0-9]+)/', $id, $preg ); |
| 284 | + $id = $preg[1]; |
| 285 | + |
| 286 | + return $id; |
| 287 | +} |
| 288 | + |
| 289 | +function embedGametrailers( $input, $argv, $parser ) { |
| 290 | + $gtid = ''; |
| 291 | + $width = $width_max = 480; |
| 292 | + $height = $height_max = 392; |
| 293 | + |
| 294 | + if ( !empty( $argv['gtid'] ) ) { |
| 295 | + $gtid = embedYouTube_url2gtid( $argv['gtid'] ); |
| 296 | + } elseif ( !empty( $input ) ) { |
| 297 | + $gtid = embedYouTube_url2gtid( $input ); |
| 298 | + } |
| 299 | + if ( !empty( $argv['width'] ) && settype( $argv['width'], 'integer' ) && ( $width_max >= $argv['width'] ) ) { |
| 300 | + $width = $argv['width']; |
| 301 | + } |
| 302 | + if ( !empty( $argv['height'] ) && settype( $argv['height'], 'integer' ) && ( $height_max >= $argv['height'] ) ) { |
| 303 | + $height = $argv['height']; |
| 304 | + } |
| 305 | + |
| 306 | + if ( !empty( $gtid ) ) { |
| 307 | + $url = "http://www.gametrailers.com/remote_wrap.php?mid={$gtid}"; |
| 308 | + // return "<object type=\"application/x-shockwave-flash\" width=\"{$width}\" height=\"{$height}\"><param name=\"movie\" value=\"{$url}\"/></object>"; |
| 309 | + // gametrailers' flash doesn't work on FF with object tag alone )-: weird, yt and gvideo are ok )-: valid xhtml no more )-: |
| 310 | + return "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" id=\"gtembed\" width=\"{$width}\" height=\"{$height}\"> <param name=\"allowScriptAccess\" value=\"sameDomain\" /> <param name=\"allowFullScreen\" value=\"true\" /> <param name=\"movie\" value=\"{$url}\"/> <param name=\"quality\" value=\"high\" /> <embed src=\"{$url}\" swLiveConnect=\"true\" name=\"gtembed\" align=\"middle\" allowScriptAccess=\"sameDomain\" allowFullScreen=\"true\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"{$width}\" height=\"{$height}\"></embed> </object>"; |
| 311 | + } |
| 312 | +} |
| 313 | + |
| 314 | +function embedYouTube_url2nvid( $url ) { |
| 315 | + $id = $url; |
| 316 | + |
| 317 | + preg_match( '/([0-9A-Za-z]+)/', $id, $preg ); |
| 318 | + $id = $preg[1]; |
| 319 | + |
| 320 | + return $id; |
| 321 | +} |
| 322 | + |
| 323 | +function embedNicovideo( $input, $argv, $parser ) { |
| 324 | + $nvid = ''; |
| 325 | + |
| 326 | + if ( !empty( $argv['nvid'] ) ) { |
| 327 | + $nvid = embedYouTube_url2nvid( $argv['nvid'] ); |
| 328 | + } elseif ( !empty( $input ) ) { |
| 329 | + $nvid = embedYouTube_url2nvid( $input ); |
| 330 | + } |
| 331 | + |
| 332 | + if ( !empty( $nvid ) ) { |
| 333 | + $url = "http://ext.nicovideo.jp/thumb_watch/{$nvid}"; |
| 334 | + return "<script type=\"text/javascript\" src=\"{$url}\"></script>"; |
| 335 | + } |
| 336 | +} |
| 337 | + |
| 338 | +function embedYouTube_url2ggid( $url ) { |
| 339 | + $id = $url; |
| 340 | + |
| 341 | + if( preg_match( '/^http:\/\/www\.gogreentube\.com\/watch\.php\?v=(.+)$/', $url, $preg ) ) { |
| 342 | + $id = $preg[1]; |
| 343 | + } elseif( preg_match( '/^http:\/\/www\.gogreentube\.com\/embed\/(.+)$/', $url, $preg ) ) { |
| 344 | + $id = $preg[1]; |
| 345 | + } |
| 346 | + |
| 347 | + preg_match( '/([0-9A-Za-z]+)/', $id, $preg ); |
| 348 | + $id = $preg[1]; |
| 349 | + |
| 350 | + return $id; |
| 351 | +} |
| 352 | + |
| 353 | +function embedGoGreenTube( $input, $argv, $parser ) { |
| 354 | + $ggid = ''; |
| 355 | + $width = $width_max = 432; |
| 356 | + $height = $height_max = 394; |
| 357 | + |
| 358 | + if( !empty( $argv['ggid'] ) ) { |
| 359 | + $ggid = embedYouTube_url2ggid( $argv['ggid'] ); |
| 360 | + } elseif( !empty( $input ) ) { |
| 361 | + $ggid = embedYouTube_url2ggid( $input ); |
| 362 | + } |
| 363 | + |
| 364 | + if( !empty( $argv['width'] ) && settype( $argv['width'], 'integer' ) && ( $width_max >= $argv['width'] ) ) { |
| 365 | + $width = $argv['width']; |
| 366 | + } |
| 367 | + |
| 368 | + if( !empty( $argv['height'] ) && settype( $argv['height'], 'integer' ) && ( $height_max >= $argv['height'] ) ) { |
| 369 | + $height = $argv['height']; |
| 370 | + } |
| 371 | + |
| 372 | + if( !empty( $ggid ) ) { |
| 373 | + $url = "http://www.gogreentube.com/embed/{$ggid}"; |
| 374 | + return "<script type=\"text/javascript\" src=\"{$url}\"></script>"; |
| 375 | + } |
| 376 | +} |
\ No newline at end of file |
Property changes on: trunk/extensions/YouTube/YouTube.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 377 | + native |