public function readRequest(WikiaRequest $request)
 {
     $this->users = $request->getVal('users');
     $this->usersData = null;
     $this->time = $request->getVal('time');
     $this->priority = $request->getVal('priority');
 }
Beispiel #2
0
 public function testGetBool()
 {
     $testCases = [[[], true, true], [[], false, false], [[], true, 1], [[], false, 0], [[], false, null], [[], false, ""], [[], true, "test"], [['param' => true], true, false], [['param' => false], false, true], [['param' => null], false, false], [['param' => "true"], true, false], [['param' => "false"], false, true], [['param' => "false"], false, false], [['param' => 0], false, true], [['param' => 1], true, false]];
     foreach ($testCases as $tc) {
         $r = new WikiaRequest($tc[0]);
         $this->assertEquals($tc[1], $r->getBool('param', $tc[2]), 'Expected new WikiaRequest(' . var_export($tc[0], true) . ")->getBool(\"param\", " . var_export($tc[2], true) . ") to be " . var_export($tc[1], true) . "\n");
     }
 }
Beispiel #3
0
 protected function isTestLocation()
 {
     static $result = null;
     if ($result === null) {
         $result = stripos($this->request->getScriptUrl(), '/api/test') !== false;
     }
     return $result;
 }
 /**
  * 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;
 }
Beispiel #5
0
 /**
  * Prepares and sends a request to a Controller
  *
  * @param string $controllerName The name of the controller, without the 'Controller' or 'Model' suffix
  * @param string $methodName The name of the Controller method to call
  * @param array $params An array with the parameters to pass to the specified method
  * @param boolean $internal whether it's an internal (PHP to PHP) or external request
  * @param int $exceptionMode exception mode
  *
  * @return WikiaResponse a response object with the data produced by the method call
  */
 public function sendRequest($controllerName = null, $methodName = null, $params = array(), $internal = true, $exceptionMode = null)
 {
     wfProfileIn(__METHOD__);
     $values = array();
     if (!empty($controllerName)) {
         $values['controller'] = $controllerName;
     }
     if (!empty($methodName)) {
         $values['method'] = $methodName;
     }
     $params = array_merge((array) $params, $values);
     if (empty($methodName) || empty($controllerName)) {
         $params = array_merge($params, $_POST, $_GET);
     }
     $request = new WikiaRequest($params);
     $request->setInternal($internal);
     if ($exceptionMode !== null) {
         $request->setExceptionMode($exceptionMode);
     }
     $out = $this->getDispatcher()->dispatch($this, $request);
     wfProfileOut(__METHOD__);
     return $out;
 }
 /**
  * Logging methods
  */
 private function logResponseException(Exception $e, WikiaRequest $request)
 {
     $this->error('FlagsLog Exception', ['exception' => $e, 'prms' => $request->getParams()]);
 }
 /**
  * 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;
 }