Skip to content

Commit aed190a

Browse files
init project
0 parents  commit aed190a

13 files changed

Lines changed: 746 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CHANGELOG
2+
==============
3+
4+
1.0.0-alpha
5+
-----------------
6+
* Can be used

CmsSeoComponent.php

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
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+
}

CmsSeoModule.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* @author Semenov Alexander <[email protected]>
4+
* @link http://skeeks.com/
5+
* @copyright 2010 SkeekS (СкикС)
6+
* @date 15.04.2016
7+
*/
8+
namespace skeeks\cms\seo;
9+
/**
10+
* Class CmsSeoModule
11+
* @package skeeks\cms\seo
12+
*/
13+
class CmsSeoModule extends \skeeks\cms\base\Module
14+
{
15+
public $controllerNamespace = 'skeeks\cms\seo\controllers';
16+
}

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Component seo for SkeekS CMS
2+
===================================
3+
4+
Installation
5+
------------
6+
7+
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
8+
9+
Either run
10+
11+
```
12+
php composer.phar require --prefer-dist skeeks/cms-seo "*"
13+
```
14+
15+
or add
16+
17+
```
18+
"skeeks/cms-seo": "*"
19+
```
20+
21+
Configuration app
22+
----------
23+
24+
```php
25+
26+
'components' =>
27+
[
28+
'cmsSeo' => [
29+
'class' => 'skeeks\cms\seo\CmsSeoComponent',
30+
],
31+
32+
'i18n' => [
33+
'translations' =>
34+
[
35+
'skeeks/seo' => [
36+
'class' => 'yii\i18n\PhpMessageSource',
37+
'basePath' => '@skeeks/cms/seo/messages',
38+
'fileMap' => [
39+
'skeeks/seo' => 'main.php',
40+
],
41+
]
42+
]
43+
],
44+
],
45+
46+
'modules' =>
47+
[
48+
'cmsSeo' => [
49+
'class' => 'skeeks\cms\seo\CmsSearchModule',
50+
"controllerNamespace" => 'skeeks\cms\seo\console\controllers'
51+
]
52+
]
53+
54+
```
55+
56+
___
57+
58+
> [![skeeks!](https://gravatar.com/userimage/74431132/13d04d83218593564422770b616e5622.jpg)](http://skeeks.com)
59+
<i>SkeekS CMS (Yii2) — quickly, easily and effectively!</i>
60+
[skeeks.com](http://skeeks.com) | [en.cms.skeeks.com](http://en.cms.skeeks.com) | [cms.skeeks.com](http://cms.skeeks.com) | [marketplace.cms.skeeks.com](http://marketplace.cms.skeeks.com)
61+
62+

_ide/YiiApplication.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* @author Semenov Alexander <[email protected]>
4+
* @link http://skeeks.com/
5+
* @copyright 2010 SkeekS (СкикС)
6+
* @date 10.09.2015
7+
*/
8+
namespace yii\web;
9+
use skeeks\cms\seo\CmsSeoComponent;
10+
11+
/**
12+
* @property CmsSeoComponent $seo
13+
14+
* Class Application
15+
* @package yii\web
16+
*/
17+
class Application
18+
{}

0 commit comments

Comments
 (0)