Example #1
0
    /**
     * Gets called when crawler runs.
     *
     * About crawler exclusivity (mutex usage):
     * When launched by an admin, no other user, admin or not, will be able to launch a crawl until this one is done.
     * When launched by a non-admin, we first check that no admin run is under way, and if that's the case,
     * we launch a crawl for the current user only.
     * No user will be able to launch two crawls in parallel, but different non-admin users crawls can run in parallel.
     */
    public function crawl() {
        if (!Session::isLoggedIn() ) {
            throw new UnauthorizedUserException('You need a valid session to launch the crawler.');
        }
        $mutex_dao = DAOFactory::getDAO('MutexDAO');
        $owner_dao = DAOFactory::getDAO('OwnerDAO');
        $owner = $owner_dao->getByEmail(Session::getLoggedInUser());
        if (empty($owner)) {
            throw new UnauthorizedUserException('You need a valid session to launch the crawler.');
        }

        // are we in an upgrading state
        if(UpgradeController::isUpgrading(true, 'Crawler')) {
            throw new InstallerException("ThinkUp needs a database migration, so we are unable to run the crawler.");
        }

        $global_mutex_name = 'crawler';

        // Everyone needs to check the global mutex
        $lock_successful = $mutex_dao->getMutex($global_mutex_name);

        if ($lock_successful) {
            // Global mutex was free, which means no admin crawls are under way
            if ($owner->is_admin) {
                // Nothing more needs to be done, since admins use the global mutex
                $mutex_name = $global_mutex_name;
            } else {
                // User is a non-admin; let's use a user mutex.
                $mutex_name = 'crawler-'.$owner->id;
                $lock_successful = $mutex_dao->getMutex($mutex_name);
                $mutex_dao->releaseMutex($global_mutex_name);
            }
        }

        if ($lock_successful) {
            $this->emitObjectMethod('crawl');
            $mutex_dao->releaseMutex($mutex_name);
        } else {
            throw new CrawlerLockedException("Error starting crawler; another crawl is already in progress.");
        }
    }
 /**
  * Invoke the controller
  *
  * Always use this method, not control(), to invoke the controller.
  * @TODO show get 500 error template on Exception
  * (if debugging is true, pass the exception details to the 500 template)
  */
 public function go()
 {
     try {
         $this->initalizeApp();
         // are we in need of a database migration?
         $classname = get_class($this);
         if ($classname != 'InstallerController' && $classname != 'BackupController' && UpgradeController::isUpgrading($this->isAdmin(), $classname)) {
             $this->setViewTemplate('install.upgradeneeded.tpl');
             $this->disableCaching();
             $option_dao = DAOFactory::getDAO('OptionDAO');
             $option_dao->clearSessionData(OptionDAO::APP_OPTIONS);
             return $this->generateView();
         } else {
             $results = $this->control();
             if ($this->profiler_enabled && !isset($this->json_data) && strpos($this->content_type, 'text/javascript') === false && strpos($this->content_type, 'text/csv') === false) {
                 $end_time = microtime(true);
                 $total_time = $end_time - $this->start_time;
                 $profiler = Profiler::getInstance();
                 $this->disableCaching();
                 $profiler->add($total_time, "total page execution time, running " . $profiler->total_queries . " queries.");
                 $this->setViewTemplate('_profiler.tpl');
                 $this->addToView('profile_items', $profiler->getProfile());
                 return $results . $this->generateView();
             } else {
                 return $results;
             }
         }
     } catch (ControllerAuthException $e) {
         Utils::setDefaultTimezonePHPini();
         $this->setErrorTemplateState();
         $this->addToView('error_type', get_class($e));
         $config = Config::getInstance();
         $message = 'You must <a href="' . $config->getValue('site_root_path') . 'session/login.php">log in</a> to do this.';
         $this->addErrorMessage($message, null, true);
         return $this->generateView();
     } catch (ConfigurationException $e) {
         $this->setErrorTemplateState();
         $this->addToView('error_type', get_class($e));
         $message = 'ThinkUp\'s configuration file does not exist! Try <a href="' . THINKUP_BASE_URL . 'install/">installing ThinkUp.</a>';
         $this->addErrorMessage($message, null, true);
         return $this->generateView();
     } catch (Exception $e) {
         Utils::setDefaultTimezonePHPini();
         $this->setErrorTemplateState();
         $this->addToView('error_type', get_class($e));
         $disable_xss = false;
         // if we are an installer exception, don't filter XSS, we have markup, and we trust this content
         if (get_class($e) == 'InstallerException') {
             $disable_xss = true;
         }
         $this->addErrorMessage($e->getMessage(), null, $disable_xss);
         return $this->generateView();
     }
 }
 /**
  * Invoke the controller
  *
  * Always use this method, not control(), to invoke the controller.
  * @TODO show get 500 error template on Exception
  * (if debugging is true, pass the exception details to the 500 template)
  */
 public function go()
 {
     try {
         $this->initalizeApp();
         // are we in need of a database migration?
         $classname = get_class($this);
         if ($classname != 'InstallerController' && $classname != 'BackupController' && UpgradeController::isUpgrading($this->isAdmin(), $classname)) {
             $this->setViewTemplate('install.upgradeneeded.tpl');
             $this->disableCaching();
             $option_dao = DAOFactory::getDAO('OptionDAO');
             $option_dao->clearSessionData(OptionDAO::APP_OPTIONS);
             return $this->generateView();
         } else {
             $results = $this->control();
             if ($this->profiler_enabled && !isset($this->json_data) && strpos($this->content_type, 'text/javascript') === false && strpos($this->content_type, 'text/csv') === false) {
                 $end_time = microtime(true);
                 $total_time = $end_time - $this->start_time;
                 $profiler = Profiler::getInstance();
                 $this->disableCaching();
                 $profiler->add($total_time, "total page execution time, running " . $profiler->total_queries . " queries.");
                 $this->setViewTemplate('_profiler.tpl');
                 $this->addToView('profile_items', $profiler->getProfile());
                 return $results . $this->generateView();
             } else {
                 return $results;
             }
         }
     } catch (Exception $e) {
         //Explicitly set TZ (before we have user's choice) to avoid date() warning about using system settings
         date_default_timezone_set('America/Los_Angeles');
         $content_type = $this->content_type;
         if (strpos($content_type, ';') !== false) {
             $exploded = explode(';', $content_type);
             $content_type = array_shift($exploded);
         }
         switch ($content_type) {
             case 'application/json':
                 $this->setViewTemplate('500.json.tpl');
                 break;
             case 'text/plain':
                 $this->setViewTemplate('500.txt.tpl');
                 break;
             default:
                 $this->setViewTemplate('500.tpl');
         }
         $this->addToView('error_type', get_class($e));
         $this->addErrorMessage($e->getMessage());
         return $this->generateView();
     }
 }