public function track() { $amgId = intval($this->getVal('amgid')); $gracenoteId = intval($this->getVal('gracenoteid')); // make a request to LyricFind API $service = new LyricFindTrackingService(); $status = $service->track($amgId, $gracenoteId, $this->wg->Title); // don't try to find a template for this controller's method $this->skipRendering(); // debug response headers if (!$status->isOK()) { $errors = $status->getErrorsArray(); $this->response->setHeader('X-LyricFind-API-Error', reset($errors)[0]); } if (!empty($status->value)) { $this->response->setHeader('X-LyricFind-API-Code', $status->value); } if ($status->isOK()) { // emit blank image - /skins/common/blank.gif $this->response->setCode(self::RESPONSE_OK); $this->response->setContentType('image/gif'); // emit raw GIF content when not in CLI mode // i.e. not running unit tests if (php_sapi_name() != 'cli') { echo file_get_contents($this->wg->StyleDirectory . '/common/blank.gif'); } } else { $this->response->setCode(self::RESPONSE_ERR); } }
/** * @group Slow * @slowExecutionTime 0.11328 ms * @dataProvider trackResponseCodeProvider * @param $amgId * @param $trackId * @param $res */ public function testTrackResponseCode($amgId, $apiResponse, $res) { // mock API response $respMock = is_array($apiResponse) ? json_encode($apiResponse) : $apiResponse; $this->mockStaticMethod('Http', 'post', $respMock); $this->mockGlobalVariable('wgTitle', $this->mockClassWithMethods('Title', ['getArticleID' => 666, 'getText' => 'Paradise_Lost:Forever_Failure'])); $service = new LyricFindTrackingService(); $status = $service->track($amgId, 0, $this->app->wg->Title); $this->assertEquals($res, $status->isOK(), 'API response code should match expected value'); }
/** * Blocks view source page & make it so that users cannot create/edit * pages that are on the takedown list. * * @param EditPage $editPage edit page instance * @return bool show edit page form? */ public static function onAlternateEdit(EditPage $editPage) { $wg = F::app()->wg; $wf = F::app()->wf; $title = $editPage->getTitle(); // Block view-source on the certain pages. if ($title->exists()) { // Look at the page-props to see if this page is blocked. if (!$wg->user->isAllowed('editlyricfind')) { // some users (staff/admin) will be allowed to edit these to prevent vandalism/spam issues. $removedProp = $wf->GetWikiaPageProp(WPP_LYRICFIND_MARKED_FOR_REMOVAL, $title->getArticleID()); if (!empty($removedProp)) { $wg->Out->addHTML(Wikia::errorbox(wfMessage('lyricfind-editpage-blocked'))); $blockEdit = true; } } } else { // Page is being created. Prevent this if page is prohibited by LyricFind. $blockEdit = LyricFindTrackingService::isPageBlockedViaApi($amgId = "", $gracenoteId = "", $title->getText()); if ($blockEdit) { $wg->Out->addHTML(Wikia::errorbox(wfMessage('lyricfind-creation-blocked'))); } } return !$blockEdit; }
/** * WARNING: This function makes an API request and is therefore slow. * * This function is intended only for use to test if not-yet-created pages are blocked * (so that we can prevent their creation). If calling-code just needs to check if an * already-existing page is blocked, check the page properties using * GetWikiaPageProp(WPP_LYRICFIND_MARKED_FOR_REMOVAL) instead. */ public static function isPageBlockedViaApi($amgId = "", $gracenoteId = "", $pageTitleText = "") { wfProfileIn(__METHOD__); $isBlocked = false; $app = F::app(); $service = new LyricFindTrackingService(); // format trackid parameter $trackId = $service->formatTrackId(['amg' => $amgId, 'gracenote' => $gracenoteId, 'title' => $pageTitleText]); $url = $app->wg->LyricFindApiUrl . '/lyric.do'; $data = ['apikey' => $app->wg->LyricFindApiKeys['display'], 'reqtype' => 'offlineviews', 'count' => 1, 'trackid' => $trackId, 'output' => 'json', 'useragent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : self::DEFAULT_USER_AGENT]; wfDebug(__METHOD__ . ': ' . json_encode($data) . "\n"); $resp = Http::post($url, ['postData' => $data]); if ($resp !== false) { wfDebug(__METHOD__ . ": API response - {$resp}\n"); } // get the code from API response if ($resp !== false) { $json = json_decode($resp, true); $code = !empty($json['response']['code']) ? intval($json['response']['code']) : false; switch ($code) { case self::CODE_LYRIC_IS_BLOCKED: $isBlocked = true; break; case self::CODE_SUCCESS_NO_LYRICS: case self::CODE_LRC_IS_AVAILABLE: case self::CODE_LYRIC_IS_INSTRUMENTAL: case self::CODE_LYRIC_IS_AVAILABLE: break; default: self::log(__METHOD__, "got #{$code} response code from API (track amg#{$amgId} / gn#{$gracenoteId} / '{$pageTitleText}')"); } } else { self::log(__METHOD__, "LyricFind API request failed in isPageBlockedViaApi()!"); } wfProfileOut(__METHOD__); return $isBlocked; }