/**
  * @param $out OutputPage
  * @param $text String
  * @return bool
  */
 public function beforePageDisplayHTML(&$out, &$text)
 {
     global $wgContLang, $wgRequest, $wgMemc, $wgUser;
     wfProfileIn(__METHOD__);
     $userAgent = $_SERVER['HTTP_USER_AGENT'];
     $acceptHeader = isset($_SERVER["HTTP_ACCEPT"]) ? $_SERVER["HTTP_ACCEPT"] : '';
     $uAmd5 = md5($userAgent);
     $key = wfMemcKey('mobile', 'ua', $uAmd5);
     $props = null;
     try {
         $props = $wgMemc->get($key);
         if (!$props) {
             $wurflConfigFile = RESOURCES_DIR . 'wurfl-config.xml';
             $wurflConfig = new WURFL_Configuration_XmlConfig($wurflConfigFile);
             $wurflManagerFactory = new WURFL_WURFLManagerFactory($wurflConfig);
             $wurflManager = $wurflManagerFactory->create();
             $device = $wurflManager->getDeviceForHttpRequest($_SERVER);
             if ($device->isSpecific() === true) {
                 $props = $device->getAllCapabilities();
                 $wgMemc->set($key, $props, 86400);
             } else {
                 $wgMemc->set($key, 'generic', 86400);
                 $props = 'generic';
             }
         }
     } catch (Exception $e) {
         // echo $e->getMessage();
     }
     // Note: The WebRequest Class calls are made in this block because
     // since PHP 5.1.x, all objects have their destructors called
     // before the output buffer callback function executes.
     // Thus, globalized objects will not be available as expected in the function.
     // This is stated to be intended behavior, as per the following: [http://bugs.php.net/bug.php?id=40104]
     $xDevice = isset($_SERVER['HTTP_X_DEVICE']) ? $_SERVER['HTTP_X_DEVICE'] : '';
     self::$useFormat = $wgRequest->getText('useformat');
     $mobileAction = $wgRequest->getText('mobileaction');
     $action = $wgRequest->getText('action');
     if (self::$useFormat !== 'mobile' && self::$useFormat !== 'mobile-wap' && !$xDevice) {
         wfProfileOut(__METHOD__);
         return true;
     }
     if ($action === 'edit' || $mobileAction === 'view_normal_site') {
         wfProfileOut(__METHOD__);
         return true;
     }
     self::$title = $out->getTitle();
     if (self::$title->isMainPage()) {
         self::$isMainPage = true;
     }
     if (self::$title->getNamespace() == NS_FILE) {
         self::$isFilePage = true;
     }
     self::$htmlTitle = $out->getHTMLTitle();
     self::$disableImages = $wgRequest->getText('disableImages', 0);
     self::$enableImages = $wgRequest->getText('enableImages', 0);
     self::$displayNoticeId = $wgRequest->getText('noticeid', '');
     if (self::$disableImages == 1) {
         $wgRequest->response()->setcookie('disableImages', 1);
         $location = str_replace('?disableImages=1', '', str_replace('&disableImages=1', '', $wgRequest->getFullRequestURL()));
         $location = str_replace('&mfi=1', '', str_replace('&mfi=0', '', $location));
         $location = $this->getRelativeURL($location);
         $wgRequest->response()->header('Location: ' . $location . '&mfi=0');
     } elseif (self::$disableImages == 0) {
         $disableImages = $wgRequest->getCookie('disableImages');
         if ($disableImages) {
             self::$disableImages = $disableImages;
         }
     }
     if (self::$enableImages == 1) {
         $disableImages = $wgRequest->getCookie('disableImages');
         if ($disableImages) {
             $wgRequest->response()->setcookie('disableImages', '');
         }
         $location = str_replace('?enableImages=1', '', str_replace('&enableImages=1', '', $wgRequest->getFullRequestURL()));
         $location = str_replace('&mfi=1', '', str_replace('&mfi=0', '', $location));
         $location = $this->getRelativeURL($location);
         $wgRequest->response()->header('Location: ' . $location . '&mfi=1');
     }
     self::$format = $wgRequest->getText('format');
     self::$callback = $wgRequest->getText('callback');
     self::$requestedSegment = $wgRequest->getText('seg', 0);
     self::$search = $wgRequest->getText('search');
     self::$searchField = $wgRequest->getText('search', '');
     $device = new DeviceDetection();
     if ($xDevice) {
         $formatName = $xDevice;
     } else {
         $formatName = $device->formatName($userAgent, $acceptHeader);
     }
     self::$device = $device->format($formatName);
     if (self::$device['view_format'] === 'wml') {
         $this->contentFormat = 'WML';
     } elseif (self::$device['view_format'] === 'html') {
         $this->contentFormat = 'XHTML';
     }
     if (self::$useFormat === 'mobile-wap') {
         $this->contentFormat = 'WML';
     }
     if ($mobileAction == 'leave_feedback') {
         echo $this->renderLeaveFeedbackXHTML();
         wfProfileOut(__METHOD__);
         exit;
     }
     if ($mobileAction == 'leave_feedback_post') {
         $this->getMsg();
         $subject = $wgRequest->getText('subject', '');
         $message = $wgRequest->getText('message', '');
         $token = $wgRequest->getText('edittoken', '');
         $title = Title::newFromText(self::$messages['mobile-frontend-feedback-page']);
         if ($title->userCan('edit') && !$wgUser->isBlockedFrom($title) && $wgUser->matchEditToken($token)) {
             $article = new Article($title, 0);
             $rawtext = $article->getRawText();
             $rawtext .= "\n== {$subject} == \n {$message} ~~~~ \n <small>User agent: {$userAgent}</small> ";
             $article->doEdit($rawtext, '');
         }
         $location = str_replace('&mobileaction=leave_feedback_post', '', $wgRequest->getFullRequestURL() . '&noticeid=1&useformat=mobile');
         $location = $this->getRelativeURL($location);
         $wgRequest->response()->header('Location: ' . $location);
         wfProfileOut(__METHOD__);
         exit;
     }
     if ($mobileAction == 'disable_mobile_site' && $this->contentFormat == 'XHTML') {
         echo $this->renderDisableMobileSiteXHTML();
         wfProfileOut(__METHOD__);
         exit;
     }
     if ($mobileAction == 'opt_in_mobile_site' && $this->contentFormat == 'XHTML') {
         echo $this->renderOptInMobileSiteXHTML();
         wfProfileOut(__METHOD__);
         exit;
     }
     if ($mobileAction == 'opt_out_mobile_site' && $this->contentFormat == 'XHTML') {
         echo $this->renderOptOutMobileSiteXHTML();
         wfProfileOut(__METHOD__);
         exit;
     }
     if ($mobileAction == 'opt_in_cookie') {
         wfIncrStats('mobile.opt_in_cookie_set');
         $this->setOptInOutCookie('1');
         $this->disableCaching();
         $location = wfExpandUrl(Title::newMainPage()->getFullURL(), PROTO_CURRENT);
         $wgRequest->response()->header('Location: ' . $location);
     }
     if ($mobileAction == 'opt_out_cookie') {
         $this->setOptInOutCookie('');
     }
     $this->getMsg();
     $this->disableCaching();
     $this->sendXDeviceVaryHeader();
     $this->sendApplicationVersionVaryHeader();
     $this->checkUserStatus();
     $this->checkUserLoggedIn();
     if (self::$title->isSpecial('Userlogin') && self::$isBetaGroupMember) {
         self::$wsLoginToken = $wgRequest->getSessionData('wsLoginToken');
         $q = array('action' => 'submitlogin', 'type' => 'login');
         $returnToVal = $wgRequest->getVal('returnto');
         if ($returnToVal) {
             $q['returnto'] = $returnToVal;
         }
         self::$wsLoginFormAction = self::$title->getLocalURL($q);
     }
     $this->setDefaultLogo();
     ob_start(array($this, 'DOMParse'));
     wfProfileOut(__METHOD__);
     return true;
 }