media_youtube.module
11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
/**
* @file media_youtube/media_youtube.module
*
* Media: YouTube provides a stream wrapper and formatters for videos provided
* by YouTube, available at http://youtube.com/.
*
* @TODO:
* Get the YouTube tab working.
* - Search by video name, user name, and channel.
* - Default to user's channel videos.
* Review all code and remove cruft.
* Assess if M:YT 3.x could be formatters added on to the OEmbed module.
*/
/**
* This is the rest point for the YouTube api.
*
* Avoid using the gdata api url when possible. Too many calls will result in
* throttling and 403 errors.
*/
define('MEDIA_YOUTUBE_REST_API', 'https://gdata.youtube.com/feeds/api/videos');
// Hooks and callbacks for integrating with File Entity module for display.
require_once dirname(__FILE__) . '/includes/media_youtube.formatters.inc';
/**
* Implements hook_media_internet_providers().
*/
function media_youtube_media_internet_providers() {
$info['MediaInternetYouTubeHandler'] = array(
'title' => t('YouTube'),
);
return $info;
}
/**
* Implements hook_stream_wrappers().
*/
function media_youtube_stream_wrappers() {
return array(
'youtube' => array(
'name' => t('YouTube videos'),
'class' => 'MediaYouTubeStreamWrapper',
'description' => t('Videos provided by YouTube.'),
'type' => STREAM_WRAPPERS_READ_VISIBLE,
),
);
}
/**
* Implements hook_theme().
*/
function media_youtube_theme($existing, $type, $theme, $path) {
return array(
'media_youtube_video' => array(
'variables' => array('uri' => NULL, 'options' => array()),
'file' => 'media_youtube.theme.inc',
'path' => $path . '/themes',
'template' => 'media-youtube-video',
),
);
}
/**
* Implements hook_media_parse().
*
* @todo This hook should be deprecated. Refactor Media module to not call it
* any more, since media_internet should be able to automatically route to the
* appropriate handler.
*/
function media_youtube_media_parse($embed_code) {
$handler = new MediaInternetYouTubeHandler($embed_code);
return $handler->parse($embed_code);
}
/**
* Implements hook_file_mimetype_mapping_alter().
*
* Register the video/youtube mimetype.
*/
function media_youtube_file_mimetype_mapping_alter(&$mapping) {
$mapping['mimetypes'][] = 'video/youtube';
}
/**
* YouTube search tab for the Media browser.
*/
/**
* Implements hook_media_browser_plugin_info().
*
* Commented out for release versions, active in dev versions. To enable the
* YouTube media browser tab, uncomment this function.
*/
function media_youtube_media_browser_plugin_info() {
$info['youtube'] = array(
'title' => t('YouTube'),
'class' => 'MediaYouTubeBrowser',
);
return $info;
}
/**
* Provides a form for adding media items from YouTube search.
*/
function media_youtube_add($form, &$form_state = array()) {
module_load_include('inc', 'media', 'includes/media.browser');
// Our search term can come from the form, or from the pager.
$term = isset($form_state['input']['search']) ? $form_state['input']['search'] : (isset($_GET['search']) ? $_GET['search'] : '');
$form['search'] = array(
'#type' => 'textfield',
'#title' => t('Search'),
'#description' => t('Input a phrase or tags to search.'),
'#default_value' => $term,
);
$form['apply'] = array(
'#type' => 'button',
'#value' => t('Apply'),
);
// This is our half-assed pager.
$page = isset($_GET['page-yt']) ? $_GET['page-yt'] : 0;
if (isset($form_state['input']['search'])) {
// Reset the pager when we press apply.
$page = 0;
}
if (!empty($term)) {
$search = media_youtube_video_search(array('q' => $term, 'max-results' => 12, 'start-index' => $page * 12 + 1));
}
$form['videos']['#prefix'] = '<div id="container"><div id="scrollbox"><ul id="media-browser-library-list" class="media-list-thumbnails">';
$form['videos']['#suffix'] = '</ul><div id="status"></div></div></div>';
$empty = FALSE;
$files = array();
if (!isset($search['entry'])) {
$empty = TRUE;
}
else {
foreach ($search['entry'] as $video) {
try {
$uri = media_parse_to_uri($video['link'][0]['@attributes']['href']);
}
catch (Exception $e) {
// Ignore invalid videos.
continue;
}
// Create a temporary file object for our retrieved video.
$file = file_uri_to_object($uri);
$file->type = 'video';
if (!isset($file->fid)) {
$file->fid = 0;
}
media_browser_build_media_item($file);
$file->preview = l($file->preview, 'media/browser', array(
'html' => TRUE,
'attributes' => array(
'data-uri' => $uri,
),
'query' => array('render' => 'media-popup', 'uri' => $uri),
));
$form['videos'][$uri] = array(
'#markup' => $file->preview,
'#prefix' => '<li>',
'#suffix' => '</li>',
);
$files[$uri] = $file;
}
}
if (!count($files)) {
$empty= TRUE;
}
if ($empty) {
$form['empty'] = array(
'#markup' => '<div class="empty-message">' . t('No videos match your search criteria. Please try again.') . '</div>',
);
}
$query = $_GET;
if ($term !== '') {
$query['search'] = $term;
}
$dest = $query['q'];
unset($query['q']);
$prev = $next = '';
if ($page) {
$query['page-yt'] = $page - 1;
$prev = l(t('previous'), $dest, array('query' => $query));
}
$query['page-yt'] = $page + 1;
if (!$empty) {
$next = l(t('next'), $dest, array('query' => $query));
}
$form['pager']= array(
'#markup' => $prev . ' ' . $next,
);
$form['submitted-video'] = array(
'#type' => 'hidden',
'#default_value' => FALSE,
);
// Add the files to JS so that they are accessible inside the browser
drupal_add_js(array('media' => array('files' => $files)), 'setting');
// Add media browser javascript and CSS.
drupal_add_js(drupal_get_path('module', 'media_youtube') . '/js/media-youtube.browser.js');
// @TODO: Remove deprecated library js and css. They're removed in Media,
// so let's comment out for now.
// drupal_add_js(drupal_get_path('module', 'media') . '/js/plugins/media.library.js');
// drupal_add_css(drupal_get_path('module', 'media') . '/js/plugins/media.library.css');
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* Allow stream wrappers to have their chance at validation.
*
* Any module that implements hook_media_parse will have an
* opportunity to validate this.
*
* @see media_parse_to_uri()
*/
function media_youtube_add_validate($form, &$form_state) {
if ($form_state['values']['op'] == t('Apply')) {
return;
}
$uri = $form_state['values']['submitted-video'];
try {
$file = file_uri_to_object($uri, TRUE);
}
catch (Exception $e) {
form_set_error('url', $e->getMessage());
return;
}
if (!$file->uri) {
form_set_error('url', t('Please select a video.'));
return;
}
$validators = $form['#validators'];
if ($validators) {
// Check for errors. @see media_add_upload_validate calls file_save_upload().
// this code is ripped from file_save_upload because we just want the validation part.
// Call the validation functions specified by this function's caller.
$errors = file_validate($file, $validators);
if (!empty($errors)) {
$message = t('%uri could not be added.', array('%uri' => $uri));
if (count($errors) > 1) {
$message .= theme('item_list', array('items' => $errors));
}
else {
$message .= ' ' . array_pop($errors);
}
form_set_error('url', $message);
return FALSE;
}
}
// @TODO: Validate that if we have no $uri that this is a valid file to
// save. For instance, we may only be interested in images, and it would
// be helpful to let the user know they passed the HTML page containing
// the image accidentally. That would also save us from saving the file
// in the submit step.
// This is kinda a hack of the same.
// This should use the file_validate routines that the upload form users.
// We need to fix the media_parse_to_file routine to allow for a validation.
}
/**
* @TODO: Document this function.
*/
function media_youtube_add_submit($form, &$form_state) {
$uri = $form_state['values']['submitted-video'];
try {
// Save the remote file
$file = file_uri_to_object($uri, TRUE);
file_save($file);
}
catch (Exception $e) {
form_set_error('url', $e->getMessage());
return;
}
if (!$file->fid) {
form_set_error('url', t('The file %file could not be saved. An unknown error has occurred.', array('%file' => $uri)));
return;
}
else {
$form_state['file'] = $file;
}
// Redirect to the file edit page after submission.
// @TODO: media_access() is a wrapper for file_entity_access(). Switch to the
// new function when Media 1.x is deprecated.
if (media_access('update', $file)) {
$destination = array('destination' => 'admin/content/file');
if (isset($_GET['destination'])) {
$destination = drupal_get_destination();
unset($_GET['destination']);
}
$form_state['redirect'] = array('file/' . $file->fid . '/edit', array('query' => $destination));
}
else {
$form_state['redirect'] = 'admin/content/file';
}
}
/**
* @TODO: Document this function.
*/
function media_youtube_video_search($options = array()) {
$options['v'] = 2;
$request = drupal_http_request(url(MEDIA_YOUTUBE_REST_API, array('query' => $options)));
if (!isset($request->error)) {
$entry = simplexml_load_string($request->data);
}
else {
throw new Exception("Error Processing Request. (Error: {$request->code}, {$request->error})");
//if request wasn't successful, create object for return to avoid errors
$entry = new SimpleXMLElement();
}
return media_youtube_unserialize_xml($entry);
}
/**
* Recursively converts a SimpleXMLElement object into an array.
*
* @param object $xml
* The original XML object.
*/
function media_youtube_unserialize_xml($xml) {
if ($xml instanceof SimpleXMLElement) {
$xml = (array) $xml;
}
if (is_array($xml)) {
foreach ($xml as $key => $item) {
$xml[$key] = media_youtube_unserialize_xml($item);
}
}
return $xml;
}
/**
* Check to ensure that a given id is valid.
*
* @param string $id
* The YouTube video id.
* @param boolean $refresh
* (Defaults to FALSE) If TRUE, then reset the value from the cache.
* @return boolean
* Returns TRUE if the video is valid.
*
* @TODO: How does this compare to MediaInternetYouTubeHandler's validId
* method, and can we refactor the code to rely on only one of them?
*/
function media_youtube_valid_id($id, $refresh = FALSE) {
$ids = &drupal_static(__FUNCTION__, array());
// Return our cached id if allowed, and it exists.
if (!$refresh && isset($ids[$id])) {
return $ids[$id];
}
elseif (!$refresh && !isset($ids[$id])) {
return $id;
}
elseif (!$refresh && $cache = cache_get('media_youtube:id:' . $id, 'cache_media_xml')) {
$ids[$id] = $cache->data;
return $ids[$id];
}
$url = url(MEDIA_YOUTUBE_REST_API . '/' . $id);
$response = drupal_http_request($url, array('method' => 'HEAD'));
$ids[$id] = ($response->code == 200);
cache_set('media_youtube:id:' . $id, $ids[$id], 'cache_media_xml', media_variable_get('xml_cache_expire', 3600));
return $ids[$id];
}