/** * Invokes system_retrieve_file() in several scenarios. */ function testFileRetrieving() { // Test 404 handling by trying to fetch a randomly named file. drupal_mkdir($sourcedir = 'public://' . $this->randomMachineName()); $filename = 'Файл для тестирования ' . $this->randomMachineName(); $url = file_create_url($sourcedir . '/' . $filename); $retrieved_file = system_retrieve_file($url); $this->assertFalse($retrieved_file, 'Non-existent file not fetched.'); // Actually create that file, download it via HTTP and test the returned path. file_put_contents($sourcedir . '/' . $filename, 'testing'); $retrieved_file = system_retrieve_file($url); // URLs could not contains characters outside the ASCII set so $filename // has to be encoded. $encoded_filename = rawurlencode($filename); $this->assertEqual($retrieved_file, 'public://' . $encoded_filename, 'Sane path for downloaded file returned (public:// scheme).'); $this->assertTrue(is_file($retrieved_file), 'Downloaded file does exist (public:// scheme).'); $this->assertEqual(filesize($retrieved_file), 7, 'File size of downloaded file is correct (public:// scheme).'); file_unmanaged_delete($retrieved_file); // Test downloading file to a different location. drupal_mkdir($targetdir = 'temporary://' . $this->randomMachineName()); $retrieved_file = system_retrieve_file($url, $targetdir); $this->assertEqual($retrieved_file, "{$targetdir}/{$encoded_filename}", 'Sane path for downloaded file returned (temporary:// scheme).'); $this->assertTrue(is_file($retrieved_file), 'Downloaded file does exist (temporary:// scheme).'); $this->assertEqual(filesize($retrieved_file), 7, 'File size of downloaded file is correct (temporary:// scheme).'); file_unmanaged_delete($retrieved_file); file_unmanaged_delete_recursive($sourcedir); file_unmanaged_delete_recursive($targetdir); }
/** * {@inheritdoc} */ public function download($destination, $url) { return system_retrieve_file($url, $destination); }
/** * {@inheritdoc} */ public function download($destination, $url) { $local_file = ''; list($file_url, $orignal_name) = explode('@@@', $url); $local_file = system_retrieve_file($file_url, $destination . '/' . $orignal_name); return $local_file; }
function BraftonVideoImporter() { if (!isset($errors)) { $errors = new BraftonErrorReport(variable_get('brafton_api_key'), variable_get('brafton_api_root'), (bool) variable_get('brafton_debug_mode')); } else { $errors->level = 1; } //Gathers feed type, Api and Video Keys, and archive file information from the Brafton module settings page. $import_list = array(); $feed_type = variable_get('brafton_feed_type'); $is_video_public = variable_get('brafton_video_public_key'); $is_video_secret = variable_get('brafton_video_secret_key'); $overwrite = variable_get('brafton_overwrite'); $is_published = variable_get('brafton_published'); if ($is_video_public && $is_video_secret) { $domain = 'http://' . variable_get('brafton_api_root'); switch ($domain) { case 'http://api.brafton.com': $baseURL = 'http://livevideo.api.brafton.com/v2/'; $photoURI = "http://pictures.brafton.com/v2/"; break; case 'http://api.contentlead.com': $baseURL = 'http://livevideo.api.contentlead.com/v2/'; $photoURI = "http://pictures.contentlead.com/v2/"; break; case 'http://api.castleford.com.au': $baseURL = 'http://livevideo.api.castleford.com.au/v2/'; $photoURI = "http://pictures.castleford.com.au/v2/"; break; default: $baseURL = 'http://livevideo.api.brafton.com/v2/'; $photoURI = "http://pictures.brafton.com/v2/"; break; } $videoClient = new AdferoVideoClient($baseURL, $is_video_public, $is_video_secret); $videoOutClient = $videoClient->videoOutputs(); $client = new AdferoClient($baseURL, $is_video_public, $is_video_secret); $photoClient = new AdferoPhotoClient($photoURI); $photos = $client->ArticlePhotos(); $feeds = $client->Feeds(); $feedList = $feeds->ListFeeds(0, 10); $feedNum = variable_get('brafton_video_feednum'); $articles = $client->Articles(); $articleList = $articles->ListForFeed($feedList->items[$feedNum]->id, 'live', 0, 100); $sitemap = array(); foreach ($articleList->items as $value) { $id = $value->id; $categories = $client->Categories(); $check = check_if_article_exists($id, 'b_video'); if (!empty($check) && $overwrite == 1) { $nid = key($check['node']); $node = node_load($nid); } elseif (empty($check)) { $node = new stdClass(); } else { continue; } $thisArticle = $client->Articles()->Get($id); if ($categories->ListForArticle($id, 0, 100)->items) { $categoryId = $categories->ListForArticle($id, 0, 100)->items[0]->id; $category = $categories->Get($categoryId); $categories = set_article_categories($value, 'b_video', $category); } $presplash = $thisArticle->fields['preSplash']; $postsplash = $thisArticle->fields['postSplash']; $cta_option = variable_get('brafton_video_ctas'); $pause_cta_text = variable_get('brafton_video_pause_cta_text'); $pause_cta_link = variable_get('brafton_video_pause_cta_link'); $end_cta_title = variable_get('brafton_video_end_cta_title'); $end_cta_subtitle = variable_get('brafton_video_end_cta_subtitle'); $end_cta_link = variable_get('brafton_video_end_cta_link'); $end_cta_text = variable_get('brafton_video_end_cta_text'); $videoList = $videoOutClient->ListForArticle($id, 0, 10); $list = $videoList->items; $embedCode = sprintf("<video id='video-%s' class=\"ajs-default-skin atlantis-js\" controls preload=\"auto\" width='512' height='288' poster='%s' >", $id, $presplash); foreach ($list as $listItem) { $output = $videoOutClient->Get($listItem->id); $type = $output->type; $path = $output->path; $resolution = $output->height; $source = generate_source_tag($path, $resolution); $embedCode .= $source; } $embedCode .= '</video>'; $script = '<script type="text/javascript">'; $script .= 'var atlantisVideo = AtlantisJS.Init({'; $script .= 'videos: [{'; $script .= 'id: "video-' . $id . '"'; if ($cta_option) { $marpro = ''; $pause_asset_id = variable_get('brafton_video_pause_cta_asset_gateway_id'); if ($pause_asset_id != '') { $marpro = "assetGateway: { id: '{$pause_asset_id}' },"; } $endingBackground = ''; $end_background_image = variable_get('brafton_video_end_cta_background_url'); if ($end_background_image != '') { $end_background_image = file_create_url($end_background_image); $endingBackground = "background: '{$end_background_image}',"; } $end_asset_id = variable_get('brafton_video_end_cta_asset_gateway_id'); if ($end_asset_id != '') { $endingBackground .= "assetGateway: { id: '{$end_asset_id}' },"; } $buttonImage = ''; $button_image_url = variable_get('brafton_video_end_cta_button_image_url'); if ($button_image_url != '') { $button_image_url = file_create_url($button_image_url); $buttonImage = "image: '{$button_image_url}',"; } $button_image_postition = variable_get('brafton_video_end_cta_button_placement'); if ($button_image_postition) { switch ($button_image_postition) { case 'tl': $postion = '{pos: "top", val: "15px"},{pos: "left", val: "15px"}'; break; case 'tr': $postion = '{pos: "top", val: "15px"},{pos: "right", val: "15px"}'; break; case 'br': $postion = '{pos: "bottom", val: "15px"},{pos: "right", val: "15px"}'; break; case 'bl': $postion = '{pos: "bottom", val: "15px"},{pos: "lelft", val: "15px"}'; } $buttonImage .= "position: [ " . $postion . " ]"; } $script .= ','; $script .= <<<EOT \t\t\t\t\t pauseCallToAction: { {$marpro} link: "{$pause_cta_link}", \t\t\t\t\t text: "{$pause_cta_text}" \t\t\t\t\t }, \t\t\t\t\t endOfVideoOptions: { {$endingBackground} \t\t\t\t\t callToAction: { \t\t\t\t\t title: "{$end_cta_title}", \t\t\t\t\t subtitle: "{$end_cta_subtitle}", \t\t\t\t\t button: { \t\t\t\t\t link: "{$end_cta_link}", \t\t\t\t\t text: "{$end_cta_text}", {$buttonImage} \t\t\t\t\t } \t\t\t\t\t } \t\t\t\t\t } EOT; } $script .= '}]'; $script .= '});'; $script .= '</script>'; $embedCode .= $script; //Wraps a Div around the embed code $embed_code = "<div id='post-single-video'>" . $embedCode . "</div>"; //Gets the image data from the feed $photoCheckId = $photos->ListForArticle($id, 0, 100); if ($photoCheckId->items[0]->id) { $image = get_image_attributes(NULL, 'video', $photoClient, $photos, $id); } else { $image = array('url' => '', 'alt' => '', 'title' => ''); } //Creates the video node and inserts the values from the feed $node->type = 'b_video'; $node->language = LANGUAGE_NONE; $node->title = $thisArticle->fields['title']; $import_list[]['title'] = $node->title; $node->uid = variable_get('brafton_author'); $node->status = $is_published == 1 ? 0 : 1; $node->created = strtotime($thisArticle->fields['lastModifiedDate']); $node->promote = 0; $node->sticky = 0; $node->comment = variable_get('brafton_comments'); $node->body[$node->language][0] = array('value' => $thisArticle->fields['content'], 'summary' => $thisArticle->fields['extract'], 'format' => 'full_html'); $node->field_brafton_video[$node->language][0] = array('value' => $embed_code, 'format' => 'full_html'); if ($image) { $node->field_brafton_image[$node->language][0] = (array) system_retrieve_file($image['url'], NULL, TRUE, FILE_EXISTS_REPLACE); $node->field_brafton_image[$node->language][0]['alt'] = $image['alt']; $node->field_brafton_image[$node->language][0]['title'] = $image['title']; } $node->field_brafton_id[$node->language][0]['value'] = $id; if ($categories) { foreach ($categories as $category) { $node->field_brafton_video_term[$node->language][]['tid'] = $category; } } node_save($node); taxonomy_node_insert($node); $nid = $node->nid; $alias = drupal_get_path_alias("node/" . $nid); $sitemap_url = $GLOBALS['base_url'] . '/' . $alias; $sitemapaddition = array("url" => $sitemap_url, "location" => $path, "title" => $node->title, "thumbnail" => $presplash, "description" => $thisArticle->fields['content'], "publication" => $thisArticle->fields['lastModifiedDate']); $sitemap[] = $sitemapaddition; ++$errors->level; } } brafton_add_URLs($sitemap); return $import_list; }
/** * Importing nodes from a demo CSV. * * @pram $import * The file name(without .csv) */ public static function ImportCsv($type) { global $base_url; $importers = array('blog' => 'os_blog_csv', 'news' => 'os_news', 'event' => 'os_ical', 'page' => 'os_pages', 'class' => 'os_classes', 'faq' => 'os_faq', 'presentation' => 'os_presentation', 'software_project' => 'os_software', 'person' => 'os_people', 'link' => 'os_links', 'media_gallery' => 'os_gallery'); // Specify a specific encoding of importers files. This meant for testing the // import of files in various encodings. $encodes = array('blog' => 'WINDOWS-1255', 'news' => 'WINDOWS-1254'); $ending = $type == 'event' ? 'ics' : 'csv'; $url = $base_url . '/' . drupal_get_path('module', 'os_migrate_demo') . '/includes/import_csv/' . $type . '.' . $ending; // todo: Use system_retrieve_file(). $file = system_retrieve_file($url, 'public://', TRUE); $source = feeds_source($importers[$type]); $fetcher_config = $source->getConfigFor($source->importer->fetcher); $fetcher_config['source'] = $file->uri; $fetcher_config['file'] = $file; // Add encoding for specific files. if (isset($encodes[$type])) { $fetcher_config['encode'] = $encodes[$type]; } $source->setConfigFor($source->importer->fetcher, $fetcher_config); $source->save(); feeds_source($importers[$type], 0)->import(); }
/** * Loops through each video article and saves it as Drupal node. * * @return void */ public function run_video_loop() { $this->errors->set_section('Main video loop'); $counter = 0; $import_list = array(); foreach ($this->article_list->items as $article) { // $article is an object containing just the brafton id $brafton_id = $article->id; $existing_posts = $this->brafton_post_exists($brafton_id); if (!empty($existing_posts) && $this->overwrite == 1) { $nid = reset($existing_posts); $new_node = \Drupal\node\Entity\Node::load($nid); } elseif (empty($existing_posts)) { $new_node = \Drupal\node\Entity\Node::create(array('type' => 'brafton_video')); } else { continue; } // $this_article contains all the actual info. $this_article = $this->articles->Get($brafton_id); $category_names = $this->get_video_tax_names($brafton_id); $category_ids = $this->load_tax_terms($category_names); $image = $this->get_video_image($brafton_id); $date = $this->video_date_setting == 'lastmodified' ? strtotime($this_article->fields['lastModifiedDate']) : strtotime($this_article->fields['date']); $embed_code = $this->create_embed($brafton_id); $new_node->uid = $this->video_author_id; $new_node->title = $this_article->fields['title']; $new_node->field_brafton_body = array('value' => $this_article->fields['content'], 'summary' => $this_article->fields['extract'], 'format' => 'full_html'); $new_node->field_brafton_video = array('value' => $embed_code, 'format' => 'full_html'); $new_node->status = $this->publish_status; $new_node->created = $date; $new_node->field_brafton_id = $brafton_id; $new_node->field_brafton_term = $category_ids; if ($image) { $new_node->field_brafton_image = system_retrieve_file($image['url'], NULL, TRUE, FILE_EXISTS_REPLACE); $new_node->field_brafton_image->alt = $image['alt']; } $new_node->save(); $import_list['items'][] = array('title' => $this_article->fields['title'], 'url' => $new_node->url()); ++$counter; } $import_list['counter'] = $counter; $this->display_import_message($import_list); }
/** * Loops through articles and saves them as Drupal nodes * * @param array $article_array Array containing individual NewsItem Objects. * * @return void */ public function run_article_loop($article_array) { $counter = 0; $import_list = array('items' => array(), 'counter' => $counter); foreach ($article_array as $article) { $brafton_id = $article->getId(); $this->errors->set_section('Individual Article loop for ' . $brafton_id); $existing_posts = $this->brafton_post_exists($brafton_id); if ($this->overwrite == 1 && !empty($existing_posts)) { $nid = reset($existing_posts); $new_node = \Drupal\node\Entity\Node::load($nid); } elseif (empty($existing_posts)) { $new_node = \Drupal\node\Entity\Node::create(array('type' => 'brafton_article')); } else { continue; } $author_id = $this->get_author($article->getByLine()); $date = $this->get_article_date($article); $category_names = $this->get_article_tax_names($article->getCategories()); $category_ids = $this->load_tax_terms($category_names); $title = $article->getHeadline(); $body = $article->getText(); $summary = !empty($article->getExtract()) ? $article->getExtract() : $article->getHtmlMetaDescription(); if (!empty($article->getPhotos())) { $image = $this->get_article_image($article->getPhotos()[0]); $new_node->field_brafton_image = system_retrieve_file($image['url'], NULL, TRUE, FILE_EXISTS_REPLACE); $new_node->field_brafton_image->alt = $image['alt']; } $new_node->status = $this->publish_status; $new_node->title = $title; $new_node->uid = $author_id; $new_node->created = strtotime($date); $new_node->field_brafton_body = array('value' => $body, 'summary' => $summary, 'format' => 'full_html'); $new_node->field_brafton_id = $brafton_id; $new_node->field_brafton_term = $category_ids; $new_node->save(); $import_list['items'][] = array('title' => $title, 'url' => $new_node->url()); ++$counter; } $import_list['counter'] = $counter; $this->display_import_message($import_list); }