/** * Publish applicable redirects * * @return int Number of published redirects */ public function publish() { if (file_exists($this->redirectsFile)) { unlink($this->redirectsFile); } /** @var Collection $redirects */ $redirects = Redirect::query()->where('is_enabled', '=', 1)->orderBy('sort_order')->get(['id', 'match_type', 'target_type', 'from_url', 'to_url', 'cms_page', 'static_page', 'status_code', 'requirements', 'from_date', 'to_date']); // TODO: Throw proper exception try { $writer = Writer::createFromPath($this->redirectsFile, 'w+'); $writer->insertAll($redirects->toArray()); } catch (\Exception $e) { // .. } return $redirects->count(); }
/** * {@inheritdoc} */ public function importData($results, $sessionKey = null) { foreach ($results as $row => $data) { try { $source = Redirect::make(); $except = ['id']; foreach (array_except($data, $except) as $attribute => $value) { if ($attribute === 'requirements') { $value = json_decode($value); } $source->setAttribute($attribute, $value); } $source->forceSave(); $this->logCreated(); } catch (\Exception $e) { $this->logError($row, $e->getMessage()); } } }
/** * @param Redirect $model * @return RedirectRule * @throws \InvalidArgumentException */ public static function createWithModel(Redirect $model) { return new self([$model->getAttribute('id'), $model->getAttribute('match_type'), $model->getAttribute('target_type'), $model->getAttribute('from_url'), $model->getAttribute('to_url'), $model->getAttribute('cms_page'), $model->getAttribute('static_page'), $model->getAttribute('status_code'), json_encode($model->getAttribute('requirements')), $model->getAttribute('from_date'), $model->getAttribute('to_date')]); }
/** * Create CMS page type * * @return void */ private function createRedirect() { Redirect::create(['match_type' => Redirect::TYPE_EXACT, 'target_type' => Redirect::TARGET_TYPE_CMS_PAGE, 'from_url' => $this->getOriginalUrl(), 'to_url' => null, 'cms_page' => $this->page->getBaseFileName(), 'status_code' => 301, 'is_enabled' => true, 'system' => true]); }
/** * Create Redirects from Request Log items * * @return array */ public function onCreateRedirectFromRequestLogItems() { $checkedIds = $this->getCheckedIds(); $redirectsCreated = 0; foreach ($checkedIds as $checkedId) { /** @var RequestLog $requestLog */ $requestLog = RequestLog::find($checkedId); $path = parse_url($requestLog->getAttribute('url'), PHP_URL_PATH); if ($path === false || $path === '/' || $path === '') { continue; } Redirect::create(['match_type' => Redirect::TYPE_EXACT, 'target_type' => Redirect::TARGET_TYPE_PATH_URL, 'from_url' => $path, 'to_url' => '/', 'status_code' => 301, 'is_enabled' => false]); $redirectsCreated++; } if ((bool) Request::get('andDelete', false)) { RequestLog::destroy($checkedIds); } if ($redirectsCreated > 0) { Event::fire('redirects.changed'); Flash::success(Lang::get('adrenth.redirect::lang.flash.success_created_redirects', ['count' => $redirectsCreated])); } return $this->listRefresh(); }
public function testScheduledRedirectOnlyToDate() { $redirect = new Redirect(['match_type' => Redirect::TYPE_EXACT, 'target_type' => Redirect::TARGET_TYPE_PATH_URL, 'from_url' => '/this-should-be-source', 'to_url' => '/this-should-be-target', 'requirements' => null, 'status_code' => 302, 'is_enabled' => 1, 'from_date' => null, 'to_date' => Carbon::today()->addMonth()]); self::assertTrue($redirect->save()); $rule = RedirectRule::createWithModel($redirect); self::assertInstanceOf(RedirectRule::class, $rule); $manager = RedirectManager::createWithRule($rule); self::assertInstanceOf(RedirectManager::class, $manager); // Test date equals `to_date` self::assertInstanceOf(RedirectRule::class, $manager->setMatchDate(Carbon::today()->addMonth())->match('/this-should-be-source')); // Test date less than `to_date` self::assertInstanceOf(RedirectRule::class, $manager->setMatchDate(Carbon::today()->addMonth()->subDay())->match('/this-should-be-source')); // Test date greater than `to_date` self::assertFalse($manager->setMatchDate(Carbon::today()->addMonth()->addDay())->match('/this-should-be-source')); }
/** * Update database statistics * * @param int $redirectId */ private function updateStatistics($redirectId) { $now = Carbon::now(); /** @var Redirect $redirect */ $redirect = Redirect::find($redirectId); if ($redirect === null) { return; } $redirect->update(['hits' => DB::raw('hits + 1'), 'last_used_at' => $now]); $crawlerDetect = new CrawlerDetect(); Client::create(['redirect_id' => $redirectId, 'timestamp' => $now, 'day' => $now->day, 'month' => $now->month, 'year' => $now->year, 'crawler' => $crawlerDetect->isCrawler() ? $crawlerDetect->getMatches() : null]); }