예제 #1
0
 /**
  * Call 'VideoHandlerHelper::getVideoDetail' on the video wiki for each of a list of video titles.  Returns a list
  * of video details for each title passed
  *
  * @param array $videos A list of video titles
  * @return array
  */
 public function getVideoDetailFromVideoWiki($videos)
 {
     $videoDetails = [];
     if (!empty($videos)) {
         $helper = new \VideoHandlerHelper();
         $videoDetails = $helper->getVideoDetailFromWiki($this->wg->WikiaVideoRepoDBName, $videos, self::$videoOptions);
     }
     return $videoDetails;
 }
 /**
  * Correctly formats response as expected by VET, and inflates video data on each result.
  *
  * @param array $searchResponse
  * @return array
  */
 protected function postProcessSearchResponse(array $searchResponse)
 {
     $config = $this->getConfig();
     $data = [];
     $start = $config->getStart();
     $pos = $start;
     $videoOptions = ['thumbWidth' => $this->getWidth(), 'thumbHeight' => $this->getHeight(), 'getThumbnail' => true, 'thumbOptions' => ['forceSize' => 'small']];
     // Determine the source for the search
     $isLocalSearch = $this->getSearchType() === 'local';
     $helper = new VideoHandlerHelper();
     foreach ($searchResponse['items'] as $singleVideoData) {
         if (empty($singleVideoData['title'])) {
             continue;
         }
         // Get data about this video from the video wiki
         if ($isLocalSearch) {
             $videosDetail = $helper->getVideoDetail($singleVideoData, $videoOptions);
         } else {
             $videosDetail = $helper->getVideoDetailFromWiki(F::app()->wg->WikiaVideoRepoDBName, $singleVideoData['title'], $videoOptions);
         }
         $trimTitle = $this->getTrimTitle();
         if (!empty($trimTitle)) {
             $videosDetail['fileTitle'] = mb_substr($singleVideoData['title'], 0, $trimTitle);
         }
         $singleVideoData['pos'] = $pos++;
         $data[] = $videosDetail;
     }
     return ['totalItemCount' => $searchResponse['total'], 'nextStartFrom' => $start + $config->getLimit(), 'items' => $data];
 }
 /**
  * Search for related video titles
  * @param Title|string $title - Either a Title object or title text
  * @param bool $test - Operate in test mode.  Allows commandline scripts to implement --test
  * @param bool $verbose - Whether to output more debugging information
  * @return array - A list of suggested videos
  */
 public function suggestionSearch($title, $test = false, $verbose = false)
 {
     wfProfileIn(__METHOD__);
     // Accept either a title string or title object here
     $titleObj = is_object($title) ? $title : Title::newFromText($title, NS_FILE);
     $articleId = $titleObj->getArticleID();
     if (!$test) {
         wfSetWikiaPageProp(WPP_LVS_SUGGEST_DATE, $articleId, time());
     }
     // flag for kept video
     $pageStatus = $this->getPageStatus($articleId);
     $isKeptVideo = $this->isStatusKeep($pageStatus);
     // get videos that have been suggested (kept videos)
     $historicalSuggestions = array_flip($this->getHistoricalSuggestions($articleId));
     // get current suggestions
     $suggestTitles = array();
     $suggest = wfGetWikiaPageProp(WPP_LVS_SUGGEST, $articleId);
     $validSuggest = [];
     if (!empty($suggest)) {
         if ($verbose) {
             echo "\tExamining the " . count($suggest) . " current match(es)\n";
         }
         foreach ($suggest as $video) {
             // Do some data integrity checking; clean up for VID-1446
             if (!array_key_exists('title', $video)) {
                 continue;
             }
             if ($isKeptVideo && array_key_exists($video['title'], $historicalSuggestions)) {
                 if ($verbose) {
                     echo "\t\t[FILTER] Match was already suggested in the past: '" . $video['title'] . "'\n";
                 }
                 continue;
             }
             $suggestTitles[$video['title']] = 1;
             $validSuggest[] = $video;
         }
         // See if we cleaned up any data
         if (!$test && count($validSuggest) != count($suggest)) {
             // We call wfSetWikiaPageProp below, but only if we get new suggestions.  Write out here
             // to make sure we get the cleaned version written when necessary.
             wfSetWikiaPageProp(WPP_LVS_SUGGEST, $articleId, $validSuggest);
         }
     }
     $readableTitle = $titleObj->getText();
     $params = array('title' => $readableTitle);
     /* Comment this out until the minseconds/maxseconds params are handled correctly by
     		   the searchVidoesByTitle method (MAIN-1276)
     		$file = wfFindFile( $titleObj );
     		if ( !empty( $file ) ) {
     			$serializedMetadata = $file->getMetadata();
     			if ( !empty( $serializedMetadata ) ) {
     				$metadata = unserialize( $serializedMetadata );
     				if ( !empty( $metadata['duration'] ) ) {
     					$duration = $metadata['duration'];
     					$params['minseconds'] = $duration - min( [ $duration, self::DURATION_DELTA ] );
     					$params['maxseconds'] = $duration + min( [ $duration, self::DURATION_DELTA ] );
     				}
     			}
     		}
     		*/
     // get search results
     $videoRows = $this->app->sendRequest('WikiaSearchController', 'searchVideosByTitle', $params)->getData();
     if ($verbose) {
         echo "\tSearch found " . count($videoRows) . " potential new match(es)\n";
     }
     $videos = array();
     $count = 0;
     $isNew = false;
     $titleTokenized = $this->getNormalizedTokens($readableTitle);
     // Reuse code from VideoHandlerHelper
     $helper = new VideoHandlerHelper();
     foreach ($videoRows as $videoInfo) {
         $rowTitle = preg_replace('/^File:/', '', $videoInfo['title']);
         $videoRowTitleTokenized = $this->getNormalizedTokens($rowTitle);
         $similarity = $this->getJaccard()->similarity($titleTokenized, $videoRowTitleTokenized);
         if ($similarity < self::MIN_JACCARD_SIMILARITY) {
             if ($verbose) {
                 echo "\t\t[FILTER] Below Jaccard similarity threshold ({$similarity} < " . self::MIN_JACCARD_SIMILARITY . "): '{$rowTitle}'\n";
             }
             continue;
         }
         $videoTitle = preg_replace('/.+File:/', '', urldecode($videoInfo['url']));
         // skip if the video has already been suggested (from kept videos)
         if ($isKeptVideo) {
             if (array_key_exists($videoTitle, $historicalSuggestions)) {
                 if ($verbose) {
                     echo "\t\t[FILTER] New suggestion was suggested previously: '{$videoTitle}'\n";
                 }
                 continue;
             } else {
                 $isNew = true;
             }
         }
         // skip if the video exists in the current suggestions
         if (array_key_exists($videoTitle, $suggestTitles)) {
             if ($verbose) {
                 echo "\t\t[FILTER] New suggestion is already suggested: '{$videoTitle}'\n";
             }
             continue;
         } else {
             $isNew = true;
         }
         // get video detail
         $videoDetail = $helper->getVideoDetailFromWiki($this->wg->WikiaVideoRepoDBName, $videoInfo['title'], $this->defaultVideoOptions);
         // Go to the next suggestion if we can't get any details for this one
         if (empty($videoDetail)) {
             if ($verbose) {
                 echo "\t\t[FILTER] Can't find video detail for '{$videoTitle}'\n";
             }
             continue;
         }
         $videos[] = $videoDetail;
         $count++;
         if ($count >= self::NUM_SUGGESTIONS) {
             break;
         }
     }
     // combine current suggestions and new suggestions
     $videos = array_slice(array_merge($videos, $validSuggest), 0, self::NUM_SUGGESTIONS);
     // If we're just testing don't set any page props
     if (!$test) {
         // Cache these suggestions
         if (empty($videos)) {
             wfSetWikiaPageProp(WPP_LVS_EMPTY_SUGGEST, $articleId, 1);
         } else {
             wfDeleteWikiaPageProp(WPP_LVS_EMPTY_SUGGEST, $articleId);
             wfSetWikiaPageProp(WPP_LVS_SUGGEST, $articleId, $videos);
             // set page status
             if (!$this->isStatusSwap($pageStatus) && !$this->isStatusForever($pageStatus) && $isNew) {
                 $this->setPageStatusNew($articleId);
             }
         }
     }
     wfProfileOut(__METHOD__);
     // The first video in the array is the top choice.
     return $videos;
 }