public function testGetMediaDetail()
 {
     $fileTitle = __METHOD__ . time();
     $request = $this->getMockBuilder('WikiaRequest')->disableOriginalConstructor()->setMethods(['getVal'])->getMock();
     $request->expects($this->at(0))->method('getVal')->with('fileTitle', '')->will($this->returnValue($fileTitle));
     $request->expects($this->at(1))->method('getVal')->with('isInline', false)->will($this->returnValue(false));
     $request->expects($this->at(2))->method('getVal')->with('width', $this->anything())->will($this->returnValue(\LightboxController::CONTEXT_DEFAULT_WIDTH));
     $request->expects($this->at(3))->method('getVal')->with('height', $this->anything())->will($this->returnValue(\LightboxController::CONTEXT_DEFAULT_HEIGHT));
     $request->expects($this->at(4))->method('getVal')->with('width', $this->anything())->will($this->returnValue(\LightboxController::CONTEXT_DEFAULT_WIDTH));
     $userMock = $this->getMock('stdClass', ['isAnon', 'isAllowed', 'isItemLoaded', 'getOption', 'getStubThreshold']);
     $userMock->expects($this->any())->method('isAnon')->will($this->returnValue(false));
     $userMock->expects($this->once())->method('isAllowed')->with('read')->will($this->returnValue(true));
     $userMock->expects($this->any())->method('isItemLoaded')->with($this->anything())->will($this->returnValue(false));
     $userMock->expects($this->any())->method('getStubThreshold')->will($this->returnValue(0));
     $this->mockGlobalVariable('wgUser', $userMock);
     $width = LightboxController::CONTEXT_DEFAULT_WIDTH;
     $height = LightboxController::CONTEXT_DEFAULT_HEIGHT;
     $isInline = false;
     $config = ['imageMaxWidth' => 1000, 'contextWidth' => $width, 'contextHeight' => $height, 'userAvatarWidth' => 16, 'isInline' => $isInline];
     $title = \Title::newFromText($fileTitle, NS_FILE);
     $data = \WikiaFileHelper::getMediaDetail($title, $config);
     $articles = $data['articles'];
     list($smallerArticleList, $articleListIsSmaller) = \WikiaFileHelper::truncateArticleList($articles, LightboxController::POSTED_IN_ARTICLES);
     $mediaType = $data['mediaType'];
     $videoEmbedCode = $data['videoEmbedCode'];
     $playerAsset = $data['playerAsset'];
     $imageUrl = $data['imageUrl'];
     $fileUrl = $data['fileUrl'];
     $rawImageUrl = $data['rawImageUrl'];
     $userThumbUrl = $data['userThumbUrl'];
     $userName = $data['userName'];
     $userPageUrl = $data['userPageUrl'];
     $isPostedIn = !empty($smallerArticleList);
     // Bool to tell mustache to print "posted in" section
     $providerName = $data['providerName'];
     $exists = $data['exists'];
     $isAdded = $data['isAdded'];
     $extraHeight = $data['extraHeight'];
     $format = \WikiaResponse::FORMAT_JSON;
     global $wgLang;
     $views = wfMessage('lightbox-video-views', $wgLang->formatNum($data['videoViews']))->parse();
     $mediaDetail = ['mediaType' => $mediaType, 'videoEmbedCode' => $videoEmbedCode, 'playerAsset' => $playerAsset, 'imageUrl' => $imageUrl, 'fileUrl' => $fileUrl, 'rawImageUrl' => $rawImageUrl, 'userThumbUrl' => $userThumbUrl, 'userName' => $userName, 'userPageUrl' => $userPageUrl, 'articles' => $articles, 'providerName' => $providerName, 'views' => $views, 'exists' => $exists, 'isAdded' => $isAdded, 'extraHeight' => $extraHeight, 'title' => $title->getDBkey(), 'fileTitle' => $title->getText(), 'isPostedIn' => $isPostedIn, 'smallerArticleList' => $smallerArticleList, 'articleListIsSmaller' => $articleListIsSmaller];
     $lightboxController = new \LightboxController();
     $lightboxController->setRequest($request);
     $response = new \WikiaResponse($format);
     $lightboxController->setResponse($response);
     $lightboxController->getMediaDetail();
     // Inspect response object
     foreach ($mediaDetail as $key => $value) {
         $this->assertEquals($value, $lightboxController->getResponse()->getVal($key), "mismatch on {$key}");
     }
     $this->assertEquals($format, $lightboxController->getResponse()->getFormat());
 }
 /**
  * Returns complete details about a single media (file).  JSON only, no associated template to this method.
  * @requestParam string fileTitle
  * @requestParam boolean isInline (optional) - Determines whether the media file should show inline
  * @requestParam int width (optional) - Context width
  * @requestParam int height (optional) - Context height
  * @responseParam string mediaType - media type.  either image or video
  * @responseParam string videoEmbedCode - embed html code if video
  * @responseParam string imageUrl - thumb image url that is hard scaled
  * @responseParam string fileUrl - url to file page
  * @responseParam string caption - video caption
  * @responseParam string userThumbUrl - user avatar thumbUrl scaled to 30x30
  * @responseParam string userName - user name
  * @responseParam string userPageUrl - url to user profile page
  * @responseParam array articles - array of articles that has title and url
  * @responseParam string providerName - provider name for videos or '' for others
  * @responseParam boolean exists - check if the file exists
  * @responseParam boolean isAdded - check if the file is added to the wiki
  */
 public function getMediaDetail()
 {
     $fileTitle = urldecode($this->request->getVal('fileTitle', ''));
     $isInline = $this->request->getVal('isInline', false);
     // BugId:32939
     // There is no sane way to check whether $fileTitle is OK other
     // than an attempt to create a Title object and then a check
     // whether the object has been created.
     $title = Title::newFromText($fileTitle, NS_FILE);
     // BugId:32939
     // Can't create a valid Title object based on $fileTitle. This method
     // only changes $this's properties. Leave them unchanged.
     if (!$title instanceof Title) {
         return;
     }
     if (!$this->wg->User->isAllowed('read')) {
         return;
     }
     $config = ['imageMaxWidth' => 1000, 'contextWidth' => $this->request->getVal('width', self::CONTEXT_DEFAULT_WIDTH), 'contextHeight' => $this->request->getVal('height', self::CONTEXT_DEFAULT_HEIGHT), 'userAvatarWidth' => 16, 'isInline' => $isInline];
     // set max height if play in lightbox
     if ($this->request->getVal('width', 0) == 0) {
         $config['maxHeight'] = self::CONTEXT_DEFAULT_HEIGHT;
     }
     $data = WikiaFileHelper::getMediaDetail($title, $config);
     $articles = $data['articles'];
     list($smallerArticleList, $articleListIsSmaller) = WikiaFileHelper::truncateArticleList($articles, self::POSTED_IN_ARTICLES);
     // file details
     $this->views = wfMessage('lightbox-video-views', $this->wg->Lang->formatNum($data['videoViews']))->parse();
     $this->title = $title->getDBKey();
     $this->fileTitle = $title->getText();
     $this->mediaType = $data['mediaType'];
     $this->videoEmbedCode = $data['videoEmbedCode'];
     $this->playerAsset = $data['playerAsset'];
     $this->imageUrl = $data['imageUrl'];
     $this->fileUrl = $data['fileUrl'];
     $this->rawImageUrl = $data['rawImageUrl'];
     $this->userThumbUrl = $data['userThumbUrl'];
     $this->userName = User::isIP($data['userName']) ? wfMessage('oasis-anon-user')->plain() : $data['userName'];
     $this->userPageUrl = $data['userPageUrl'];
     $this->articles = $articles;
     $this->isPostedIn = !empty($smallerArticleList);
     // Bool to tell mustache to print "posted in" section
     $this->smallerArticleList = $smallerArticleList;
     $this->articleListIsSmaller = $articleListIsSmaller;
     $this->providerName = $data['providerName'];
     $this->exists = $data['exists'];
     $this->isAdded = $data['isAdded'];
     $this->extraHeight = $data['extraHeight'];
     // Make sure that a request with missing &format=json does not throw a "template not found" exception
     $this->response->setFormat('json');
     // set cache control to 15 minutes
     $this->response->setCacheValidity(900);
 }
 /**
  * get video detail
  * @param array $videoInfo [ array( 'title' => title, 'addedAt' => addedAt , 'addedBy' => addedBy ) ]
  * @param array $options
  *   [ array( 'thumbWidth' => int, 'thumbHeight' => int, 'postedInArticles' => int, 'getThumbnail' => bool, 'thumbOptions' => array ) ]
  *   Keys:
  *     thumbWidth - the width of the thumbnail to return (required)
  *     thumbHeight - the height of the thumbnail to return (required)
  *     getThumbnail - whether to return a fully formed html thumbnail of the video or not
  *     thumbOptions - the option of the thumbnail to return
  *     postedInArticles - the number of "posted in" article details to return
  * @return array $videoDetail
  */
 public function getVideoDetail($videoInfo, $options)
 {
     wfProfileIn(__METHOD__);
     $videoDetail = array();
     /** @var Title $title */
     $title = $videoInfo['title'];
     /** @var LocalFile|WikiaLocalFileShared $file */
     $file = WikiaFileHelper::getVideoFileFromTitle($title);
     if ($file) {
         // get thumbnail
         $thumb = $file->transform(['width' => $options['thumbWidth'], 'height' => $options['thumbHeight']]);
         $thumbUrl = $thumb->getUrl();
         // get user
         if (empty($videoInfo['addedBy'])) {
             $userName = '';
             $userUrl = '';
         } else {
             $user = User::newFromId($videoInfo['addedBy']);
             $userName = User::isIP($user->getName()) ? wfMessage('oasis-anon-user')->text() : $user->getName();
             $userUrl = $user->getUserPage()->getFullURL();
         }
         $thumbnail = '';
         if (!empty($options['getThumbnail'])) {
             $thumbOptions = empty($options['thumbOptions']) ? [] : $options['thumbOptions'];
             if (empty($thumbOptions['alt'])) {
                 $thumbOptions['alt'] = htmlspecialchars($title->getText());
             }
             $thumbnail = $thumb->toHtml($thumbOptions);
         }
         // get article list
         if (empty($options['postedInArticles'])) {
             $isTruncated = 0;
             $truncatedList = array();
         } else {
             $mediaQuery = new ArticlesUsingMediaQuery($title);
             $articleList = $mediaQuery->getArticleList();
             list($truncatedList, $isTruncated) = WikiaFileHelper::truncateArticleList($articleList, $options['postedInArticles']);
         }
         // video details
         $videoDetail = array('title' => $title->getDBKey(), 'fileTitle' => $title->getText(), 'description' => $this->getVideoDescription($file), 'fileUrl' => $title->getFullURL(), 'thumbUrl' => $thumbUrl, 'userName' => $userName, 'userUrl' => $userUrl, 'truncatedList' => $truncatedList, 'isTruncated' => $isTruncated, 'timestamp' => empty($videoInfo['addedAt']) ? '' : $videoInfo['addedAt'], 'duration' => (double) $file->getMetadataDuration(), 'viewsTotal' => empty($videoInfo['viewsTotal']) ? 0 : $videoInfo['viewsTotal'], 'provider' => $file->getProviderName(), 'embedUrl' => $file->getHandler()->getEmbedUrl(), 'videoId' => $file->getVideoId(), 'thumbnail' => $thumbnail, 'regionalRestrictions' => $file->getRegionalRestrictions());
     } else {
         Wikia::Log(__METHOD__, false, "No file found for '" . $videoInfo['title'] . "'");
     }
     wfProfileOut(__METHOD__);
     return $videoDetail;
 }