/**
  * This is the main method to build the master string tables with the original strings.
  * It will search for existent modules that use the i18n feature, parse the _t() calls
  * and write the resultant files in the lang folder of each module.
  *
  * @uses DataObject->collectI18nStatics()
  *
  * @param HTTPRequest $request
  */
 public function run($request)
 {
     increase_time_limit_to();
     $collector = i18nTextCollector::create($request->getVar('locale'));
     $merge = $this->getIsMerge($request);
     // Custom writer
     $writerName = $request->getVar('writer');
     if ($writerName) {
         $writer = Injector::inst()->get($writerName);
         $collector->setWriter($writer);
     }
     // Get restrictions
     $restrictModules = $request->getVar('module') ? explode(',', $request->getVar('module')) : null;
     $collector->run($restrictModules, $merge);
     Debug::message(__CLASS__ . " completed!", false);
 }
 /**
  * Writes all changes to this object to the database.
  *  - It will insert a record whenever ID isn't set, otherwise update.
  *  - All relevant tables will be updated.
  *  - $this->onBeforeWrite() gets called beforehand.
  *  - Extensions such as Versioned will ammend the database-write to ensure that a version is saved.
  *
  *  @uses DataExtension->augmentWrite()
  *
  * @param boolean $showDebug Show debugging information
  * @param boolean $forceInsert Run INSERT command rather than UPDATE, even if record already exists
  * @param boolean $forceWrite Write to database even if there are no changes
  * @param boolean $writeComponents Call write() on all associated component instances which were previously
  *                                 retrieved through {@link getComponent()}, {@link getComponents()} or
  *                                 {@link getManyManyComponents()} (Default: false)
  * @return int The ID of the record
  * @throws ValidationException Exception that can be caught and handled by the calling function
  */
 public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false)
 {
     $now = DBDatetime::now()->Rfc2822();
     // Execute pre-write tasks
     $this->preWrite();
     // Check if we are doing an update or an insert
     $isNewRecord = !$this->isInDB() || $forceInsert;
     // Check changes exist, abort if there are none
     $hasChanges = $this->updateChanges($isNewRecord);
     if ($hasChanges || $forceWrite || $isNewRecord) {
         // New records have their insert into the base data table done first, so that they can pass the
         // generated primary key on to the rest of the manipulation
         $baseTable = $this->baseTable();
         $this->writeBaseRecord($baseTable, $now);
         // Write the DB manipulation for all changed fields
         $this->writeManipulation($baseTable, $now, $isNewRecord);
         // If there's any relations that couldn't be saved before, save them now (we have an ID here)
         $this->writeRelations();
         $this->onAfterWrite();
         $this->changed = array();
     } else {
         if ($showDebug) {
             Debug::message("no changes for DataObject");
         }
         // Used by DODs to clean up after themselves, eg, Versioned
         $this->invokeWithExtensions('onAfterSkippedWrite');
     }
     // Ensure Created and LastEdited are populated
     if (!isset($this->record['Created'])) {
         $this->record['Created'] = $now;
     }
     $this->record['LastEdited'] = $now;
     // Write relations as necessary
     if ($writeComponents) {
         $this->writeComponents(true);
     }
     // Clears the cache for this object so get_one returns the correct object.
     $this->flushCache();
     return $this->record['ID'];
 }
 /**
  * Allows the display and benchmarking of queries as they are being run
  *
  * @param string $sql Query to run, and single parameter to callback
  * @param callable $callback Callback to execute code
  * @param array $parameters Parameters for any parameterised query
  * @return mixed Result of query
  */
 protected function benchmarkQuery($sql, $callback, $parameters = array())
 {
     if (isset($_REQUEST['showqueries']) && Director::isDev()) {
         $this->queryCount++;
         $starttime = microtime(true);
         $result = $callback($sql);
         $endtime = round(microtime(true) - $starttime, 4);
         // replace parameters as closely as possible to what we'd expect the DB to put in
         if (strtolower($_REQUEST['showqueries']) == 'inline') {
             $sql = DB::inline_parameters($sql, $parameters);
         }
         Debug::message("\n{$sql}\n{$endtime}s\n", false);
         return $result;
     } else {
         return $callback($sql);
     }
 }
 /**
  * Add methods from the {@link ViewableData::$failover} object, as well as wrapping any methods prefixed with an
  * underscore into a {@link ViewableData::cachedCall()}.
  *
  * @throws LogicException
  */
 public function defineMethods()
 {
     if ($this->failover && !is_object($this->failover)) {
         throw new LogicException("ViewableData::\$failover set to a non-object");
     }
     if ($this->failover) {
         $this->addMethodsFrom('failover');
         if (isset($_REQUEST['debugfailover'])) {
             Debug::message("{$this->class} created with a failover class of {$this->failover->class}");
         }
     }
     parent::defineMethods();
 }
 /**
  * Prepare the response (we can receive an assortment of response types (strings/objects/HTTPResponses) and
  * changes the controller response object appropriately
  *
  * @param HTTPResponse|Object $response
  */
 protected function prepareResponse($response)
 {
     if ($response instanceof HTTPResponse) {
         if (isset($_REQUEST['debug_request'])) {
             Debug::message("Request handler returned HTTPResponse object to {$this->class} controller;" . "returning it without modification.");
         }
         $this->setResponse($response);
     } else {
         if ($response instanceof Object && $response->hasMethod('getViewer')) {
             if (isset($_REQUEST['debug_request'])) {
                 Debug::message("Request handler {$response->class} object to {$this->class} controller;" . "rendering with template returned by {$response->class}::getViewer()");
             }
             $response = $response->getViewer($this->getAction())->process($response);
         }
         $this->getResponse()->setBody($response);
     }
     //deal with content if appropriate
     ContentNegotiator::process($this->getResponse());
     //add cache headers
     HTTP::add_cache_headers($this->getResponse());
 }
 /**
  * Determine the best module to be given ownership over this key
  *
  * @param array $entitiesByModule
  * @param string $key
  * @return string Best module, if found
  */
 protected function getBestModuleForKey($entitiesByModule, $key)
 {
     // Check classes
     $class = current(explode('.', $key));
     if (array_key_exists($class, $this->classModuleCache)) {
         return $this->classModuleCache[$class];
     }
     $owner = $this->findModuleForClass($class);
     if ($owner) {
         $this->classModuleCache[$class] = $owner;
         return $owner;
     }
     // @todo - How to determine ownership of templates? Templates can
     // exist in multiple locations with the same name.
     // Display notice if not found
     Debug::message("Duplicate key {$key} detected in no / multiple modules with no obvious owner", false);
     // Fall back to framework then cms modules
     foreach (array('framework', 'cms') as $module) {
         if (isset($entitiesByModule[$module][$key])) {
             $this->classModuleCache[$class] = $module;
             return $module;
         }
     }
     // Do nothing
     $this->classModuleCache[$class] = null;
     return null;
 }
 /**
  * @param HTTPRequest $request
  * @return array
  */
 protected function findAction($request)
 {
     $handlerClass = $this->class ? $this->class : get_class($this);
     // We stop after RequestHandler; in other words, at ViewableData
     while ($handlerClass && $handlerClass != 'SilverStripe\\View\\ViewableData') {
         $urlHandlers = Config::inst()->get($handlerClass, 'url_handlers', Config::UNINHERITED);
         if ($urlHandlers) {
             foreach ($urlHandlers as $rule => $action) {
                 if (isset($_REQUEST['debug_request'])) {
                     Debug::message("Testing '{$rule}' with '" . $request->remaining() . "' on {$this->class}");
                 }
                 if ($request->match($rule, true)) {
                     if (isset($_REQUEST['debug_request'])) {
                         Debug::message("Rule '{$rule}' matched to action '{$action}' on {$this->class}. " . "Latest request params: " . var_export($request->latestParams(), true));
                     }
                     return array('rule' => $rule, 'action' => $action);
                 }
             }
         }
         $handlerClass = get_parent_class($handlerClass);
     }
     return null;
 }