public function onOutputPageBeforeHTML(&$out, &$text) { $wgJsMimeType = $this->app->getGlobal('wgJsMimeType'); $wgExtensionsPath = $this->app->getGlobal('wgExtensionsPath'); $out->addScript("<script type=\"{$wgJsMimeType}\" src=\"{$wgExtensionsPath}/wikia/hacks/UserActivityDiscovery/js/uad.js\"></script>\n"); return true; }
public function testDefaultRegistryAndHookDispatcherInstances() { $application = new WikiaApp(); $this->assertInstanceOf('WikiaHookDispatcher', $application->getHookDispatcher()); $this->assertInstanceOf('WikiaGlobalRegistry', $application->getGlobalRegistry()); $this->assertInstanceOf('WikiaLocalRegistry', $application->getLocalRegistry()); }
/** * Get page type for the current page (ad-wise). * Take into account type of the page and user status. * Return one of the PAGE_TYPE_* constants * * @return string */ public function getPageType() { $title = null; if (WikiaPageType::isActionPage() || $this->wg->Request->getBool('noexternals', $this->wg->NoExternals) || $this->wg->Request->getBool('noads', false) || $this->wg->ShowAds === false || $this->wg->EnableAdEngineExt === false || !$this->app->checkSkin(['oasis', 'wikiamobile', 'venus'])) { $pageLevel = self::PAGE_TYPE_NO_ADS; return $pageLevel; } $runAds = WikiaPageType::isFilePage() || WikiaPageType::isForum() || WikiaPageType::isSearch() || WikiaPageType::isWikiaHub(); if (!$runAds) { if ($this->wg->Title) { $title = $this->wg->Title; $namespace = $title->getNamespace(); $runAds = in_array($namespace, $this->wg->ContentNamespaces) || isset($this->wg->ExtraNamespaces[$namespace]) || BodyController::isBlogListing() || BodyController::isBlogPost() || defined('NS_WIKIA_PLAYQUIZ') && $title->inNamespace(NS_WIKIA_PLAYQUIZ) || defined('NS_CATEGORY') && $namespace == NS_CATEGORY || defined('NS_PROJECT') && $namespace == NS_PROJECT || $title->isSpecial('Leaderboard') || $title->isSpecial('Maps') || $title->isSpecial('Newimages') || $title->isSpecial('Videos'); } } if (!$runAds) { $pageLevel = self::PAGE_TYPE_NO_ADS; return $pageLevel; } $user = $this->wg->User; if (!$user->isLoggedIn() || $user->getGlobalPreference('showAds')) { // Only leaderboard, medrec and invisible on corporate sites for anonymous users if (WikiaPageType::isCorporatePage()) { $pageLevel = self::PAGE_TYPE_CORPORATE; return $pageLevel; } if (WikiaPageType::isSearch()) { $pageLevel = self::PAGE_TYPE_SEARCH; return $pageLevel; } if ($title && $title->isSpecial('Maps')) { $pageLevel = self::PAGE_TYPE_MAPS; return $pageLevel; } // All ads everywhere else $pageLevel = self::PAGE_TYPE_ALL_ADS; return $pageLevel; } // Logged in users get some ads on the main pages (except on the corporate sites) if (!WikiaPageType::isCorporatePage() && WikiaPageType::isMainPage()) { $pageLevel = self::PAGE_TYPE_HOMEPAGE_LOGGED; return $pageLevel; } // Override ad level for a (set of) specific page(s) // Use case: sponsor ads on a landing page targeted to Wikia editors (=logged in) if ($title && !empty($this->wg->PagesWithNoAdsForLoggedInUsersOverriden) && in_array($title->getDBkey(), $this->wg->PagesWithNoAdsForLoggedInUsersOverriden)) { $pageLevel = self::PAGE_TYPE_CORPORATE; return $pageLevel; } // And no other ads $pageLevel = self::PAGE_TYPE_NO_ADS; return $pageLevel; }
/** * Request the reactivation of the user's account * * This involves confirming they should be allowed to request this, * generating a token, and emailing the link with the token, so we * can confirm that they own this account. * * @param User $user The user account to reactivate * @param WikiaApp $app An instance of WikiaApp * @return boolean True if the reactivation was successfully requested, * False otherwise */ public function requestReactivation(User $user, $app) { wfProfileIn(__METHOD__); // Not scheduled for closure or not email confirmed? if (!$this->isScheduledForClosure($user) || !$user->isEmailConfirmed()) { wfProfileOut(__METHOD__); return false; } $emailTextTemplate = $app->renderView('CloseMyAccountSpecial', 'email', ['language' => $user->getGlobalPreference('language')]); $response = $user->sendConfirmationMail('reactivateaccount', 'ReactivationMail', 'closemyaccount-reactivation-email', true, $emailTextTemplate); $this->track($user, 'request-reactivation'); wfProfileOut(__METHOD__); return $response->isGood(); }
/** * @param WikiaApp $app * @param SpecialCssModel $model * @param integer $articleId * * @return boolean */ private static function shouldRedirect($app, $model, $title) { $result = false; // currently special:css cannot handle undo mode if ($app->wg->Request->getInt('undo') > 0 || $app->wg->Request->getInt('undoafter') > 0) { return $result; } if ($app->wg->EnableSpecialCssExt) { $specialCss = new SpecialCssController(); /** @noinspection PhpUndefinedVariableInspection * SpecialCssModel::$suppoertedSkins is defined -- lint has issues with it */ $result = $model->isWikiaCssTitle($title) && $app->checkSkin($model::$supportedSkins) && $specialCss->userCanExecute($app->wg->User); } return $result; }
/** * get db handler * @return DatabaseBase */ protected function getDb($type = DB_MASTER) { return $this->app->runFunction('wfGetDB', $type, array(), $this->app->getGlobal('wgExternalDatawareDB')); }
/** * Gets controller from request, and delegates to given controller - checking of access permissions * * @return bool * @throws WikiaException * @throws ControllerNotFoundException */ protected function accessAllowedByController() { // Code for instantination of controller copied from method WikiaDispatcher::dispatch $originalRequest = RequestContext::getMain()->getRequest(); $app = WikiaApp::app(); global $wgAutoloadClasses; // Determine the "base" name for the controller, stripping off Controller/Service/Module $controllerName = $app->getBaseName($originalRequest->getVal('controller')); if (empty($controllerName)) { return false; } // Service classes must be dispatched by full name otherwise we look for a controller. if ($app->isService($originalRequest->getVal('controller'))) { $controllerClassName = $app->getServiceClassName($controllerName); } else { $controllerClassName = $app->getControllerClassName($controllerName); } if (empty($wgAutoloadClasses[$controllerClassName])) { return false; } // Determine the final name for the controller and method based on any routing rules $controller = new $controllerClassName(); /* @var $controller WikiaController */ $accessAllowedByController = $controller->isAnonAccessAllowedInCurrentContext(); return $accessAllowedByController; }
/** * dispatch the request * * @param WikiaApp $app * @param WikiaRequest $request * @throws WikiaException * @throws Exception * @throws WikiaHttpException * @throws WikiaDispatchedException * @return WikiaResponse */ public function dispatch(WikiaApp $app, WikiaRequest $request) { wfProfileIn(__METHOD__); global $wgAutoloadClasses; if (empty($wgAutoloadClasses)) { wfProfileOut(__METHOD__); throw new WikiaException("wgAutoloadClasses is empty, cannot dispatch Request"); } $format = $request->getVal('format', WikiaResponse::FORMAT_HTML); $response = new WikiaResponse($format, $request); $controller = null; $profilename = 'unset'; // Main dispatch is a loop because Controllers can forward to each other // Error condition is also handled via forwarding to the error controller do { // First time through the loop we skip this section // If we got through the dispatch loop and have a nextCall, then call it. // Request and Response are re-used, Response data can be optionally reset if ($controller && $controller->hasNext()) { $nextCall = $controller->getNext(); $request->setVal('controller', $nextCall['controller']); $request->setVal('method', $nextCall['method']); if ($nextCall['reset']) { $response->resetData(); } } $profilename = null; try { // Determine the "base" name for the controller, stripping off Controller/Service/Module $controllerName = $app->getBaseName($request->getVal('controller')); if (empty($controllerName)) { throw new WikiaException("Controller parameter missing or invalid: {$controllerName}"); } // Service classes must be dispatched by full name otherwise we look for a controller. if ($app->isService($request->getVal('controller'))) { $controllerClassName = $app->getServiceClassName($controllerName); } else { $controllerClassName = $app->getControllerClassName($controllerName); } if (empty($wgAutoloadClasses[$controllerClassName])) { throw new ControllerNotFoundException($controllerName); } // Determine the final name for the controller and method based on any routing rules $callNext = $this->applyRouting($app, $response, $controllerClassName, $request->getVal('method', self::DEFAULT_METHOD_NAME)); $controllerClassName = $response->getControllerName(); // might have been changed $controllerName = $app->getBaseName($controllerClassName); // chop off Service/Controller $method = $response->getMethodName(); // might have been changed $profilename = __METHOD__ . " ({$controllerClassName}_{$method})"; wfProfileIn($profilename); $controller = new $controllerClassName(); /* @var $controller WikiaController */ $response->setTemplateEngine($controllerClassName::DEFAULT_TEMPLATE_ENGINE); if ($callNext) { list($nextController, $nextMethod, $resetData) = explode("::", $callNext); $controller->forward($nextController, $nextMethod, $resetData); } // map X to executeX method names for things that used to be modules if (!method_exists($controller, $method)) { $method = ucfirst($method); $hookMethod = $method; // the original module hook naming scheme does not use the "Execute" part // This will throw an exception if the template is missing // Refactor the offending class to not use executeXYZ methods or set format in request params // Warning: this means you can't use the new Dispatcher routing to switch templates in modules if ($format == WikiaResponse::FORMAT_HTML) { try { $response->getView()->setTemplate($controllerName, $method); } catch (WikiaException $e) { throw new MethodNotFoundException("{$controllerClassName}::{$method}"); } } $method = "execute{$method}"; $params = $request->getParams(); // old modules expect params in a different place } else { $hookMethod = $method; $params = array(); } if (!$request->isInternal() && !$controller->allowsExternalRequests() || in_array($method, array('allowsExternalRequests', 'getRequest', 'setRequest', 'getResponse', 'setResponse', 'getApp', 'setApp', 'init')) || !method_exists($controller, $method) || !is_callable(array($controller, $method))) { throw new MethodNotFoundException("{$controllerClassName}::{$method}"); } if (!$request->isInternal()) { $this->testIfUserHasPermissionsOrThrow($app, $controller, $method); } // Initialize the RequestContext object if it is not already set // SpecialPageController context is already set by SpecialPageFactory::execute by the time it gets here if ($controller->getContext() === null) { $controller->setContext(RequestContext::getMain()); } // If a SpecialPageController is dispatching a request to itself, preserve the original request context // TODO: come up with a better fix for this (it's because of the setInstance call in WikiaSpecialPageController) $originalRequest = $controller->getRequest(); $originalResponse = $controller->getResponse(); $controller->setRequest($request); $controller->setResponse($response); $controller->setApp($app); $controller->init(); if (method_exists($controller, 'preventBlockedUsage') && $controller->preventBlockedUsage($controller->getContext()->getUser(), $method)) { $result = false; } elseif (method_exists($controller, 'userAllowedRequirementCheck') && $controller->userAllowedRequirementCheck($controller->getContext()->getUser(), $method)) { $result = false; } else { // Actually call the controller::method! $result = $controller->{$method}($params); } if ($result === false) { // skip template rendering when false returned $controller->skipRendering(); } // Preserve original request (this is for SpecialPageControllers) if ($originalRequest != null) { $controller->setRequest($originalRequest); } if ($originalResponse != null) { $controller->setResponse($originalResponse); } // keep the AfterExecute hooks for now, refactor later using "after" dispatching $app->runHook("{$controllerName}{$hookMethod}AfterExecute", array(&$controller, &$params)); wfProfileOut($profilename); } catch (WikiaHttpException $e) { if ($request->isInternal()) { //if it is internal call rethrow it so we can apply normal handling wfProfileOut(__METHOD__); throw $e; } else { wfProfileOut($profilename); $response->setException($e); $response->setFormat('json'); $response->setCode($e->getCode()); $response->setVal('error', get_class($e)); $details = $e->getDetails(); if (!empty($details)) { $response->setVal('message', $details); } } } catch (Exception $e) { if ($profilename) { wfProfileOut($profilename); } $response->setException($e); Wikia::log(__METHOD__, $e->getMessage()); // if we catch an exception, forward to the WikiaError controller unless we are already dispatching Error if (empty($controllerClassName) || $controllerClassName != 'WikiaErrorController') { $controller = new WikiaErrorController(); $controller->forward('WikiaError', 'error', false); // keep params for error controller $response->getView()->setTemplatePath(null); // response is re-used so skip the original template } } } while ($controller && $controller->hasNext()); if ($request->isInternal() && $response->hasException() && $request->getExceptionMode() !== WikiaRequest::EXCEPTION_MODE_RETURN) { Wikia::logBacktrace(__METHOD__ . '::exception'); wfProfileOut(__METHOD__); switch ($request->getExceptionMode()) { case WikiaRequest::EXCEPTION_MODE_THROW: throw $response->getException(); // case WikiaRequest::EXCEPTION_MODE_WRAP_AND_THROW: // case WikiaRequest::EXCEPTION_MODE_WRAP_AND_THROW: default: throw new WikiaDispatchedException("Internal Throw ({$response->getException()->getMessage()})", $response->getException()); } } wfProfileOut(__METHOD__); return $response; }
/** * get application object * @return WikiaApp */ public static function app() { if (!isset(self::$appInstance)) { self::$appInstance = new WikiaApp(); } return self::$appInstance; }
/** * Wrapper for WikiaApp::registerHook * @param string $event * @param string $class * @param string $method * @deprecated */ public function registerHook($event, $class, $method) { $this->app->registerHook($event, $class, $method); }
public function __construct(WikiaApp $app) { $this->app = $app; $this->allowedNamespaces = $app->getLocalRegistry()->get('UserProfilePageNamespaces'); $this->title = $app->wg->Title; }
/** * dispatch the request * * @param WikiaApp $app * @param WikiaRequest $request * @return WikiaResponse */ public function dispatch(WikiaApp $app, WikiaRequest $request) { $autoloadClasses = $app->wg->AutoloadClasses; if (empty($autoloadClasses)) { throw new WikiaException("wgAutoloadClasses is empty, cannot dispatch Request"); } $format = $request->getVal('format', WikiaResponse::FORMAT_HTML); $response = F::build('WikiaResponse', array('format' => $format, 'request' => $request)); if ($app->wg->EnableSkinTemplateOverride && $app->isSkinInitialized()) { $response->setSkinName($app->wg->User->getSkin()->getSkinName()); } // Main dispatch is a loop because Controllers can forward to each other // Error condition is also handled via dispatching to the error controller do { $request->setDispatched(true); try { $method = $this->getMethodName($request); // Determine the "base" name for the controller, stripping off Controller/Service/Module $controllerName = $app->getBaseName($request->getVal('controller')); // Service classes must be dispatched by full name otherwise we look for a controller. if ($app->isService($request->getVal('controller'))) { $controllerClassName = $app->getServiceClassName($controllerName); } else { $controllerClassName = $app->getControllerClassName($controllerName); } $profilename = __METHOD__ . " ({$controllerName}_{$method})"; if (empty($controllerName)) { throw new WikiaException("Invalid controller name: {$controllerName}"); } if (empty($autoloadClasses[$controllerClassName])) { throw new WikiaException("Controller class does not exist: {$controllerClassName}"); } $app->wf->profileIn($profilename); $response->setControllerName($controllerClassName); $response->setMethodName($method); $controller = F::build($controllerClassName); /* @var $controller WikiaController */ // map X to executeX method names for things that used to be modules if (!method_exists($controller, $method)) { $method = ucfirst($method); // This will throw an exception if the template is missing // Refactor the offending class to not use executeXYZ methods or set format in request params if ($format == WikiaResponse::FORMAT_HTML) { $response->getView()->setTemplate($controllerName, $method); } $method = "execute{$method}"; $params = $request->getParams(); // old modules expect params in a different place } if (!$request->isInternal() && !$controller->allowsExternalRequests() || in_array($method, array('allowsExternalRequests', 'getRequest', 'setRequest', 'getResponse', 'setResponse', 'getApp', 'setApp', 'init')) || !method_exists($controller, $method) || !is_callable(array($controller, $method))) { throw new WikiaException("Could not dispatch {$controllerClassName}::{$method}"); } // Initialize the RequestContext object if it is not already set // SpecialPageController context is already set by SpecialPageFactory::execute by the time it gets here if ($controller->getContext() === null) { $controller->setContext(RequestContext::getMain()); } // If a SpecialPageController is dispatching a request to itself, preserve the original request context // TODO: come up with a better fix for this (it's because of the setInstance call in WikiaSpecialPageController) $originalRequest = $controller->getRequest(); $originalResponse = $controller->getResponse(); $controller->setRequest($request); $controller->setResponse($response); $controller->setApp($app); $controller->init(); // BugId:5125 - keep old hooks naming convention $hookMethod = ucfirst($this->getMethodName($request)); $hookResult = $app->runHook("{$controllerName}{$hookMethod}BeforeExecute", array(&$controller, &$params)); if ($hookResult) { $result = $controller->{$method}($params); if ($result === false) { // skip template rendering when false returned $controller->skipRendering(); } } // Preserve original request (this is for SpecialPageControllers) if ($originalRequest != null) { $controller->setRequest($originalRequest); } if ($originalResponse != null) { $controller->setResponse($originalResponse); } // we ignore the result of the AfterExecute hook $app->runHook("{$controllerName}{$hookMethod}AfterExecute", array(&$controller, &$params)); $app->wf->profileOut($profilename); } catch (Exception $e) { $app->wf->profileOut($profilename); $response->setException($e); Wikia::log(__METHOD__, $e->getMessage()); // if we catch an exception, redirect to the WikiaError controller if ($controllerClassName != 'WikiaErrorController' && $method != 'error') { $response->getView()->setTemplatePath(null); $request->setVal('controller', 'WikiaError'); $request->setVal('method', 'error'); $request->setDispatched(false); } } } while (!$request->isDispatched()); if ($request->isInternal() && $response->hasException()) { Wikia::logBacktrace(__METHOD__ . '::exception'); throw new WikiaDispatchedException("Internal Throw ({$response->getException()->getMessage()})", $response->getException()); } return $response; }