|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @author Semenov Alexander <[email protected]> |
| 4 | + * @link http://skeeks.com/ |
| 5 | + * @copyright 2010 SkeekS (СкикС) |
| 6 | + * @date 30.03.2015 |
| 7 | + */ |
| 8 | +namespace skeeks\cms\seo; |
| 9 | +use skeeks\cms\base\Component; |
| 10 | + |
| 11 | +use skeeks\cms\base\components\Descriptor; |
| 12 | +use skeeks\cms\base\Module; |
| 13 | +use skeeks\cms\helpers\StringHelper; |
| 14 | +use skeeks\cms\models\Site; |
| 15 | +use skeeks\cms\models\StorageFile; |
| 16 | +use skeeks\cms\models\Tree; |
| 17 | +use skeeks\cms\models\TreeType; |
| 18 | +use skeeks\cms\models\User; |
| 19 | +use Yii; |
| 20 | +use yii\base\Event; |
| 21 | +use yii\helpers\ArrayHelper; |
| 22 | +use yii\web\UploadedFile; |
| 23 | +use yii\web\View; |
| 24 | +use yii\widgets\ActiveForm; |
| 25 | + |
| 26 | +/** |
| 27 | + * Class CmsSeoComponent |
| 28 | + * @package skeeks\cms\seo |
| 29 | + */ |
| 30 | +class CmsSeoComponent extends Component |
| 31 | +{ |
| 32 | + /** |
| 33 | + * Можно задать название и описание компонента |
| 34 | + * @return array |
| 35 | + */ |
| 36 | + static public function descriptorConfig() |
| 37 | + { |
| 38 | + return array_merge(parent::descriptorConfig(), [ |
| 39 | + 'name' => \Yii::t('skeeks/seo', 'Seo'), |
| 40 | + ]); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * |
| 45 | + * длина ключевых слов |
| 46 | + * |
| 47 | + * @var int |
| 48 | + */ |
| 49 | + public $maxKeywordsLength = 1000; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var int минимальная длина слова которая попадет в списко ключевых слов |
| 53 | + */ |
| 54 | + public $minKeywordLenth = 8; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var array |
| 58 | + */ |
| 59 | + public $keywordsStopWords = []; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var bool |
| 63 | + */ |
| 64 | + public $enableKeywordsGenerator = true; |
| 65 | + |
| 66 | + |
| 67 | + /** |
| 68 | + * @var string |
| 69 | + */ |
| 70 | + public $robotsContent = "User-agent: *"; |
| 71 | + |
| 72 | + |
| 73 | + /** |
| 74 | + * @var string |
| 75 | + */ |
| 76 | + public $countersContent = ""; //Содержимое счетчиков |
| 77 | + |
| 78 | + |
| 79 | + /** |
| 80 | + * @var array |
| 81 | + */ |
| 82 | + public $keywordsPriority = |
| 83 | + [ |
| 84 | + "title" => 8, |
| 85 | + "h1" => 6, |
| 86 | + "h2" => 4, |
| 87 | + "h3" => 3, |
| 88 | + "h5" => 2, |
| 89 | + "h6" => 2, |
| 90 | + //"b" => 2, |
| 91 | + //"strong" => 2, |
| 92 | + ]; |
| 93 | + |
| 94 | + public function rules() |
| 95 | + { |
| 96 | + return ArrayHelper::merge(parent::rules(), [ |
| 97 | + [['enableKeywordsGenerator', 'minKeywordLenth', 'maxKeywordsLength'], 'integer'], |
| 98 | + ['robotsContent', 'string'], |
| 99 | + ['countersContent', 'string'], |
| 100 | + ]); |
| 101 | + } |
| 102 | + |
| 103 | + public function attributeLabels() |
| 104 | + { |
| 105 | + return ArrayHelper::merge(parent::attributeLabels(), [ |
| 106 | + 'enableKeywordsGenerator' => 'Автоматическая генерация ключевых слов', |
| 107 | + 'minKeywordLenth' => 'Минимальная длина ключевого слова', |
| 108 | + 'maxKeywordsLength' => 'Длинна ключевых слов', |
| 109 | + 'robotsContent' => 'Robots.txt файл', |
| 110 | + 'countersContent' => 'Коды счетчиков', |
| 111 | + ]); |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + public function renderConfigForm(ActiveForm $form) |
| 117 | + { |
| 118 | + echo \Yii::$app->view->renderFile(__DIR__ . '/seo/_form.php', [ |
| 119 | + 'form' => $form, |
| 120 | + 'model' => $this |
| 121 | + ], $this); |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + public function init() |
| 127 | + { |
| 128 | + parent::init(); |
| 129 | + |
| 130 | + if (!$this->enableKeywordsGenerator) |
| 131 | + { |
| 132 | + return $this; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Генерация SEO метатегов. |
| 137 | + * */ |
| 138 | + \Yii::$app->view->on(View::EVENT_END_PAGE, function (Event $e) { |
| 139 | + if (!\Yii::$app->request->isAjax && !\Yii::$app->request->isPjax) { |
| 140 | + $this->generateBeforeOutputPage($e->sender); |
| 141 | + } |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + public function generateBeforeOutputPage(\yii\web\View $view) |
| 147 | + { |
| 148 | + $content = ob_get_contents(); |
| 149 | + |
| 150 | + if (!isset($view->metaTags['keywords'])) |
| 151 | + { |
| 152 | + $view->registerMetaTag([ |
| 153 | + "name" => 'keywords', |
| 154 | + "content" => $this->keywords($content) |
| 155 | + ], 'keywords'); |
| 156 | + } |
| 157 | + |
| 158 | + \Yii::$app->response->content = $content; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * |
| 163 | + * Генерация ключевых слов |
| 164 | + * |
| 165 | + * @param string $content |
| 166 | + * @return string |
| 167 | + */ |
| 168 | + public function keywords($content = "") |
| 169 | + { |
| 170 | + $result = ""; |
| 171 | + |
| 172 | + |
| 173 | + $content = $this->_processPriority($content); |
| 174 | + if($content) |
| 175 | + { |
| 176 | + //Избавляем от тегов и разбиваем в массив |
| 177 | + $content = preg_replace("!<script(.*?)</script>!si","",$content); |
| 178 | + $content = preg_replace('/(&\w+;)|\'/', ' ', strtolower(strip_tags($content))); |
| 179 | + $words = preg_split('/(\s+)|([\.\,\:\(\)\"\'\!\;])/m', $content); |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | + foreach ($words as $n => $word) |
| 184 | + { |
| 185 | + if (strlen($word) < $this->minKeywordLenth || |
| 186 | + (int)$word || |
| 187 | + strpos($word, '/')!==false || |
| 188 | + strpos($word, '@')!==false || |
| 189 | + strpos($word, '_')!==false || |
| 190 | + strpos($word, '=')!==false || |
| 191 | + in_array(StringHelper::strtolower($word), $this->keywordsStopWords) |
| 192 | + ) { |
| 193 | + unset($words[$n]); |
| 194 | + } |
| 195 | + } |
| 196 | + // получаем массив с числом каждого слова |
| 197 | + $words = array_count_values($words); |
| 198 | + arsort($words); // сортируем - наиболее частые - вперед |
| 199 | + $words = array_keys($words); |
| 200 | + |
| 201 | + $count = 0; |
| 202 | + foreach ($words as $word) { |
| 203 | + if (strlen($result) > $this->maxKeywordsLength) break; |
| 204 | + |
| 205 | + $count ++; |
| 206 | + if($count>1) |
| 207 | + { |
| 208 | + $result .= ', '. StringHelper::strtolower($word); |
| 209 | + } else |
| 210 | + $result .= StringHelper::strtolower($word); |
| 211 | + } |
| 212 | + } |
| 213 | + return $result; |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * |
| 218 | + * Обработка текста согласно приоритетам и тегам H1 и прочим |
| 219 | + * |
| 220 | + * @param string $content |
| 221 | + * @return string |
| 222 | + */ |
| 223 | + public function _processPriority($content = "") |
| 224 | + { |
| 225 | + $contentNewResult = ""; |
| 226 | + |
| 227 | + foreach($this->keywordsPriority as $tag => $prioryty) |
| 228 | + { |
| 229 | + if(preg_match_all("!<{$tag}(.*?)\>(.*?)</{$tag}>!si", $content, $words)) |
| 230 | + { |
| 231 | + $contentNew = ""; |
| 232 | + if(isset($words[2])) |
| 233 | + { |
| 234 | + foreach($words[2] as $num => $string) |
| 235 | + { |
| 236 | + $contentNew .= $string; |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + if($contentNew) |
| 241 | + { |
| 242 | + for($i = 1; $i <= $prioryty; $i ++) |
| 243 | + { |
| 244 | + $contentNewResult .= " " . $contentNew; |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + return $contentNewResult . $content; |
| 251 | + } |
| 252 | + |
| 253 | +} |
0 commit comments