/**
  * Examine every non-premium video on this wiki and add suggestions where needed.
  * @return array - An associative array of stats from processing the videos
  */
 private function processVideoList()
 {
     wfProfileIn(__METHOD__);
     $suggestDateProp = WPP_LVS_SUGGEST_DATE;
     $suggestExpire = time() - LicensedVideoSwapHelper::SUGGESTIONS_TTL;
     $pageNS = NS_FILE;
     // Only select videos with nonexistent or expired suggestions unless --refresh is on
     $whereExpired = '';
     if (!$this->refresh) {
         $whereExpired = " AND (props IS NULL OR props <= {$suggestExpire})";
     }
     // A list of all videos, returning the video title, its file page ID and
     $sql = "SELECT video_title as title,\n\t\t\t\t\t   page.page_id as page_id,\n\t\t\t\t\t   props as suggest_date\n\t\t\t\t  FROM video_info\n\t\t\t\t  JOIN page\n\t\t\t\t    ON video_title = page_title\n\t\t\t\t   AND page_namespace = {$pageNS}\n\t\t\t\t  LEFT JOIN page_wikia_props\n\t\t\t\t    ON page.page_id = page_wikia_props.page_id\n\t\t\t\t   AND propname = {$suggestDateProp}\n\t\t\t\t WHERE removed = 0\n\t\t\t\t   AND premium = 0\n\t\t\t\t   {$whereExpired}";
     $db = wfGetDB(DB_SLAVE);
     $results = $db->query($sql, __METHOD__);
     $lvsHelper = new LicensedVideoSwapHelper();
     $vidsFound = 0;
     $vidsWithSugggestions = 0;
     $totalSuggestions = 0;
     // Get the total count of relevant videos
     while ($row = $db->fetchObject($results)) {
         $vidsFound++;
         $title = $row->title;
         $this->debug("Processing '{$title}'\n");
         // This sets page_wikia_props for WPP_LVS_SUGGEST_DATE, WPP_LVS_EMPTY_SUGGEST and WPP_LVS_SUGGEST
         $suggestions = $lvsHelper->suggestionSearch($title, $this->test, $this->verbose);
         if ($suggestions) {
             $vidsWithSugggestions++;
             $totalSuggestions += count($suggestions);
             $this->debug("\tFound " . count($suggestions) . " suggestion(s)\n");
         } else {
             $this->debug("\tNo suggestions found\n");
         }
     }
     // clear cache for total videos
     $lvsHelper->invalidateCacheTotalVideos();
     // clear cache for total new videos
     $lvsHelper->invalidateCacheTotalNewVideos();
     wfProfileOut(__METHOD__);
     return array('vidsFound' => $vidsFound, 'vidsWithSuggestions' => $vidsWithSugggestions, 'totalSuggestions' => $totalSuggestions);
 }
 /**
  * restore video after swapping or keeping it
  * @requestParam string videoTitle
  * @requestParam string newTitle
  * @requestParam integer currentPage
  * @responseParam string result [ok/error]
  * @responseParam string msg - result message
  * @responseParam string html
  * @responseParam integer totalVideos - total videos with suggestions
  * @responseParam string redirect - redirect url
  */
 public function restoreVideo()
 {
     $videoTitle = $this->request->getVal('videoTitle', '');
     $currentPage = $this->getVal('currentPage', 1);
     $sort = 'recent';
     // set default value for response params
     $this->setDefaultParams();
     // validate action
     $response = $this->sendRequest('LicensedVideoSwapSpecial', 'validateAction', array('videoTitle' => $videoTitle));
     $msg = $response->getVal('msg', '');
     if (!empty($msg)) {
         $this->result = 'error';
         $this->msg = $msg;
     }
     $file = WikiaFileHelper::getVideoFileFromTitle($videoTitle, true);
     // check if file exists
     if (empty($file)) {
         $this->result = 'error';
         $this->msg = wfMessage('videohandler-error-video-no-exist')->text();
         return;
     }
     $helper = new LicensedVideoSwapHelper();
     // get the LVS status of this file page
     $articleId = $videoTitle->getArticleID();
     $pageStatusInfo = $helper->getPageStatusInfo($articleId);
     if (empty($pageStatusInfo['status'])) {
         $this->result = 'error';
         $this->msg = wfMessage('lvs-error-invalid-page-status')->text();
         return;
     }
     if ($helper->isStatusSwapExact($pageStatusInfo['status'])) {
         $status = $helper->undeletePage($videoTitle, true);
         if (!$status->isOK()) {
             $this->html = '';
             $this->result = 'error';
             $this->msg = $status->getMessage();
             return;
         }
     } else {
         if ($helper->isStatusSwapNorm($pageStatusInfo['status'])) {
             $newTitle = $this->request->getVal('newTitle', '');
             /** @var WikiPage $article */
             $article = Article::newFromID($videoTitle->getArticleID());
             $redirect = $article->getRedirectTarget();
             if ($article->isRedirect() && !empty($redirect) && $redirect->getDBKey() == $newTitle) {
                 $status = $helper->undeletePage($videoTitle);
                 if (!$status->isOK()) {
                     $this->result = 'error';
                     $this->msg = $status->getMessage();
                     return;
                 }
                 $result = $helper->removeRedirectLink($article);
                 if (!$result->isOK()) {
                     $this->result = 'error';
                     $this->msg = $result->getMessage();
                     return;
                 }
             }
         }
     }
     // clear the LVS status of this file page
     $helper->clearPageStatus($articleId);
     // delete the LVS status info of this file page
     $helper->deletePageStatusInfo($articleId);
     // add log for undo swapping video only
     if (!$helper->isStatusKeep($pageStatusInfo['status'])) {
         $reason = wfMessage('lvs-log-restore', $file->getTitle()->getText())->text();
         $helper->addLog($file->getTitle(), 'licensedvideoswap_restore', $reason);
     }
     // clear cache for total videos
     $helper->invalidateCacheTotalVideos();
     // get video list
     $useMaster = true;
     $videoList = $helper->getRegularVideoList($sort, $currentPage, $useMaster);
     // get total videos with suggestions
     $this->totalVideos = $helper->getUnswappedVideoTotal($useMaster);
     if (empty($videoList)) {
         $this->redirect = $helper->getRedirectUrl($currentPage, $sort);
     } else {
         $this->html = $this->app->renderView('LicensedVideoSwapSpecial', 'row', array('videoList' => $videoList));
         $this->html .= $helper->getPagination($this->totalVideos, $currentPage, $sort);
     }
     $this->result = 'ok';
     $this->msg = wfMessage('lvs-restore-video-success')->text();
 }