/**
  * Checks if a the video already exists on Wikia. If so, and reupload is on, save the name of that
  * video in $this->oldName. This will be used later in the ingestion process.
  * @throws FeedIngesterSkippedException
  */
 public function checkVideoExistsOnWikia()
 {
     $duplicates = WikiaFileHelper::findVideoDuplicates($this->videoData['provider'], $this->videoData['videoId'], static::$REMOTE_ASSET);
     if (count($duplicates) > 0) {
         $oldName = $duplicates[0]['img_name'];
         if (!$this->reupload) {
             $msg = "Skipping {$oldName} (Id: {$this->videoData['videoId']}, {$this->videoData['provider']}) - ";
             $msg .= "video already exists on Wikia and reupload is disabled.\n";
             throw new FeedIngesterSkippedException($msg);
         }
         $this->oldName = $oldName;
         echo "Video already exists, using it's old name: {$this->oldName}\n";
     } else {
         $this->oldName = null;
     }
 }
 foreach ($videos as $video) {
     $cnt++;
     $videoTitle = trim($video['name']);
     $msg = "[Page {$page}: {$cnt} of {$total}] Video: {$videoTitle} ({$video['embed_code']})";
     if (empty($video['metadata']['sourceid'])) {
         $skipped++;
         echo "{$msg}...SKIPPED (Empty sourceid).\n";
         continue;
     }
     if (!empty($video['preview_image_url'])) {
         $skipped++;
         echo "{$msg}...SKIPPED (Thumbnail url already exists).\n";
         continue;
     }
     // get wiki title
     $duplicates = WikiaFileHelper::findVideoDuplicates($provider, $video['embed_code']);
     if (count($duplicates) < 1) {
         $skipped++;
         echo "{$msg}...SKIPPED (Cannot find the file).\n";
         continue;
     }
     $wikiVideoTitle = $duplicates[0]['img_name'];
     // get thumbnail from IVA
     $resp = getVideoThumbnailIva($video['metadata']['sourceid']);
     if ($resp === false) {
         continue;
     }
     $data['thumbnail'] = $resp;
     if (empty($data['thumbnail'])) {
         $skipped++;
         echo "{$msg}...SKIPPED (No thumbnail found).\n";
 public function createVideo(array $data, &$msg, $params = array())
 {
     wfProfileIn(__METHOD__);
     $debug = !empty($params['debug']);
     $ignoreRecent = !empty($params['ignorerecent']) ? $params['ignorerecent'] : 0;
     if ($debug) {
         print "data after initial processing: \n";
         foreach (explode("\n", var_export($data, 1)) as $line) {
             print ":: {$line}\n";
         }
     }
     $addlCategories = !empty($params['addlCategories']) ? $params['addlCategories'] : array();
     $id = $data['videoId'];
     $name = $this->generateName($data);
     $metadata = $this->generateMetadata($data, $msg);
     if (!empty($msg)) {
         print "Error when generating metadata\n";
         var_dump($msg);
         wfProfileOut(__METHOD__);
         return 0;
     }
     $duplicates = WikiaFileHelper::findVideoDuplicates(static::$PROVIDER, $id);
     $dup_count = count($duplicates);
     $previousFile = null;
     if ($dup_count > 0) {
         if ($this->reupload === false) {
             // if reupload is disabled finish now
             if ($debug) {
                 print "Not uploading - video already exists and reupload is disabled\n";
             }
             return 0;
         }
         // if there are duplicates use name of one of them as reference
         // instead of generating new one
         $name = $duplicates[0]['img_name'];
         echo "Video already exists, using it's old name: {$name}\n";
         $previousFile = Title::newFromText($name, NS_FILE);
     } else {
         // sanitize name
         $name = VideoFileUploader::sanitizeTitle($name);
         // make sure the name is unique
         $name = $this->getUniqueName($name);
     }
     $metadata['destinationTitle'] = $name;
     if (!$this->validateTitle($id, $name, $msg, $debug)) {
         wfProfileOut(__METHOD__);
         return 0;
     }
     // prepare wiki categories string (eg [[Category:MyCategory]] )
     $categories = $this->generateCategories($data, $addlCategories);
     $categories[] = wfMsgForContent('videohandler-category');
     $categories = array_unique($categories);
     $categoryStr = '';
     foreach ($categories as $categoryName) {
         $category = Category::newFromName($categoryName);
         if ($category instanceof Category) {
             $categoryStr .= '[[' . $category->getTitle()->getFullText() . ']]';
         }
     }
     // parepare article body
     $apiWrapper = new static::$API_WRAPPER($id, $metadata);
     $descriptionHeader = '==' . F::app()->wf->Msg('videohandler-description') . '==';
     $body = $categoryStr . "\n" . $descriptionHeader . "\n" . $apiWrapper->getDescription();
     if ($debug) {
         print "Ready to create video\n";
         print "id:          {$id}\n";
         print "name:        {$name}\n";
         print "categories:  " . implode(',', $categories) . "\n";
         print "metadata:\n";
         foreach (explode("\n", var_export($metadata, 1)) as $line) {
             print ":: {$line}\n";
         }
         print "body:\n";
         foreach (explode("\n", $body) as $line) {
             print ":: {$line}\n";
         }
         wfProfileOut(__METHOD__);
         return 1;
     } else {
         if (!empty($ignoreRecent) && !is_null($previousFile)) {
             $revId = $previousFile->getLatestRevID();
             $revision = Revision::newFromId($revId);
             $time = $revision->getTimestamp();
             $timeUnix = intval(wfTimestamp(TS_UNIX, $time));
             $timeNow = intval(wfTimestamp(TS_UNIX, time()));
             if ($timeUnix + $ignoreRecent >= $timeNow) {
                 print "Recently uploaded, ignoring\n";
                 return 0;
             }
         }
         $uploadedTitle = null;
         $result = VideoFileUploader::uploadVideo(static::$PROVIDER, $id, $uploadedTitle, $body, false, $metadata);
         if ($result->ok) {
             $fullUrl = WikiFactory::getLocalEnvURL($uploadedTitle->getFullURL());
             print "Ingested {$uploadedTitle->getText()} from partner clip id {$id}. {$fullUrl}\n\n";
             wfWaitForSlaves(self::THROTTLE_INTERVAL);
             wfProfileOut(__METHOD__);
             return 1;
         }
     }
     wfProfileOut(__METHOD__);
     return 0;
 }
/**
 * Update metadata in Video wiki
 * @global int $failedWiki
 * @param string $videoId
 * @param array $newValues
 * @return boolean
 */
function updateMetadataVideoWiki($videoId, $newValues)
{
    global $failedWiki;
    $resp = false;
    $asset = OoyalaAsset::getAssetById($videoId);
    if ($asset['asset_type'] == 'remote_asset') {
        $isRemoteAsset = true;
        $provider = $asset['metadata']['source'];
    } else {
        $isRemoteAsset = false;
        $provider = 'ooyala';
    }
    $duplicates = WikiaFileHelper::findVideoDuplicates($provider, $asset['embed_code'], $isRemoteAsset);
    if (count($duplicates) > 0) {
        $resp = updateMetadataWiki($duplicates[0], $newValues);
    } else {
        echo "\tError: VideoId: {$videoId} - FILE not found.\n";
        $failedWiki++;
    }
    return $resp;
}