/** * Any migration code in here is wrapped inside of a transaction. * * @return bool */ public function safeUp() { craft()->db->createCommand()->dropIndex('retour_redirects', 'redirectSrcUrl', true); craft()->db->createCommand()->createIndex('retour_redirects', 'redirectSrcUrlParsed', true); craft()->db->createCommand()->dropIndex('retour_static_redirects', 'redirectSrcUrl', true); craft()->db->createCommand()->createIndex('retour_static_redirects', 'redirectSrcUrlParsed', true); RetourPlugin::log("Updated Indexes for retour_redirects & retour_static_redirects", LogLevel::Info, true); return true; }
private function _addColumnsAfter($tableName, $newColumns, $afterColumnHandle) { // this is a foreach loop, enough said foreach ($newColumns as $columnName => $columnType) { // check if the column does NOT exist if (!craft()->db->columnExists($tableName, $columnName)) { $this->addColumnAfter($tableName, $columnName, array('column' => $columnType, 'null' => false), $afterColumnHandle); // log that we created the new column RetourPlugin::log("Created the `{$columnName}` in the `{$tableName}` table.", LogLevel::Info, true); } else { // tell craft that we couldn't create the column as it alredy exists. RetourPlugin::log("Column `{$columnName}` already exists in the `{$tableName}` table.", LogLevel::Info, true); } } }
/** * @param string $urld The input URL * @return mixed The redirect */ public function getRedirectFromCache($url) { $cacheKey = "retour_cache_" . md5($url); $result = craft()->cache->get($cacheKey); RetourPlugin::log("Cached Redirect hit: " . print_r($result, true), LogLevel::Info, false); return $result; }
/** * @inheritDoc IFieldType::onAfterElementSave() * * @return null */ public function onAfterElementSave() { $fieldHandle = $this->model->handle; $attributes = $this->element->content->attributes; $retourModel = null; if (isset($attributes[$fieldHandle])) { $retourModel = $attributes[$fieldHandle]; } $value = $this->prepValueFromPost($retourModel); if ($value) { RetourPlugin::log("Resaving Retour field data", LogLevel::Info, false); /* -- If the redirectSrcUrl is empty, don't save it, and delete any existing record */ if ($value->redirectSrcUrl == "") { craft()->retour->deleteRedirectByElementId($value->associatedElementId, $value->locale); } else { $error = craft()->cache->flush(); RetourPlugin::log("Cache flushed: " . print_r($error, true), LogLevel::Info, false); craft()->retour->saveRedirect($value); } } parent::onAfterElementSave(); }
/** * @return mixed */ public function init() { /* -- Listen for exceptions */ craft()->onException = function (\CExceptionEvent $event) { if ($event->exception instanceof \CHttpException && $event->exception->statusCode == 404) { if (craft()->request->isSiteRequest() && !craft()->request->isLivePreview()) { /* -- See if we should redirect */ $url = urldecode(craft()->request->getRequestUri()); $noQueryUrl = UrlHelper::stripQueryString($url); /* -- Redirect if we find a match, otherwise let Craft handle it */ $redirect = craft()->retour->findRedirectMatch($url); if (isset($redirect)) { craft()->retour->incrementStatistics($url, true); $event->handled = true; RetourPlugin::log("Redirecting " . $url . " to " . $redirect['redirectDestUrl'], LogLevel::Info, false); craft()->request->redirect($redirect['redirectDestUrl'], true, $redirect['redirectHttpCode']); } else { /* -- Now try it without the query string, too, otherwise let Craft handle it */ $redirect = craft()->retour->findRedirectMatch($noQueryUrl); if (isset($redirect)) { craft()->retour->incrementStatistics($url, true); $event->handled = true; RetourPlugin::log("Redirecting " . $url . " to " . $redirect['redirectDestUrl'], LogLevel::Info, false); craft()->request->redirect($redirect['redirectDestUrl'], true, $redirect['redirectHttpCode']); } else { craft()->retour->incrementStatistics($url, false); } } } } }; /* -- Listen for structure changes so we can regenerated our FieldType's URLs */ craft()->on('structures.onMoveElement', function (Event $e) { $element = $e->params['element']; $elemType = $element->getElementType(); if ($element) { if ($elemType == ElementType::Entry) { /* -- Check the field layout, so that we only do this for FieldLayouts that have our Retour fieldtype in them */ $fieldLayouts = $element->fieldLayout->getFields(); foreach ($fieldLayouts as $fieldLayout) { $field = craft()->fields->getFieldById($fieldLayout->fieldId); if ($field->type == "Retour") { craft()->elements->saveElement($element); RetourPlugin::log("Resaved moved structure element", LogLevel::Info, false); break; } } } } }); /* -- Listen for entries whose slug changes */ craft()->on('entries.onBeforeSaveEntry', function (Event $e) { $this->originalUris = array(); if (!$e->params['isNewEntry']) { $entry = $e->params['entry']; $thisSection = $entry->getSection(); if ($thisSection->hasUrls) { $this->originalUris = craft()->retour->getLocalizedUris($entry); } } }); craft()->on('entries.onSaveEntry', function (Event $e) { if (!$e->params['isNewEntry']) { $entry = $e->params['entry']; $newUris = craft()->retour->getLocalizedUris($entry); foreach ($newUris as $newUri) { $oldUri = current($this->originalUris); next($this->originalUris); if (strcmp($oldUri, $newUri) != 0 && $oldUri != "") { $record = new Retour_StaticRedirectsRecord(); /* -- Set the record attributes for our new auto-redirect */ $record->locale = $entry->locale; $record->redirectMatchType = 'exactmatch'; $record->redirectSrcUrl = $oldUri; if ($record->redirectMatchType == "exactmatch" && $record->redirectSrcUrl != "") { $record->redirectSrcUrl = '/' . ltrim($record->redirectSrcUrl, '/'); } $record->redirectSrcUrlParsed = $record->redirectSrcUrl; $record->redirectDestUrl = $newUri; if ($record->redirectMatchType == "exactmatch" && $record->redirectDestUrl != "") { $record->redirectDestUrl = '/' . ltrim($record->redirectDestUrl, '/'); } $record->redirectHttpCode = '301'; $record->hitLastTime = DateTimeHelper::currentUTCDateTime(); $record->associatedElementId = 0; $result = craft()->retour->saveStaticRedirect($record); } } } }); }
/** */ public function actionClearStatistics() { $error = craft()->retour->clearStatistics(); RetourPlugin::log("Statistics cleared: " . print_r($error, true), LogLevel::Info, false); $error = craft()->cache->flush(); RetourPlugin::log("Cache flushed: " . print_r($error, true), LogLevel::Info, false); craft()->userSession->setNotice(Craft::t('Statistics Cleared.')); craft()->request->redirect('statistics'); }