Skip to content

Commit e226d29

Browse files
dev
1 parent aed190a commit e226d29

9 files changed

Lines changed: 211 additions & 264 deletions

File tree

CmsSeoComponent.php

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,10 @@
88
namespace skeeks\cms\seo;
99
use skeeks\cms\base\Component;
1010

11-
use skeeks\cms\base\components\Descriptor;
12-
use skeeks\cms\base\Module;
1311
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;
1912
use Yii;
2013
use yii\base\Event;
2114
use yii\helpers\ArrayHelper;
22-
use yii\web\UploadedFile;
2315
use yii\web\View;
2416
use yii\widgets\ActiveForm;
2517

@@ -103,22 +95,46 @@ public function rules()
10395
public function attributeLabels()
10496
{
10597
return ArrayHelper::merge(parent::attributeLabels(), [
106-
'enableKeywordsGenerator' => 'Автоматическая генерация ключевых слов',
107-
'minKeywordLenth' => 'Минимальная длина ключевого слова',
108-
'maxKeywordsLength' => 'Длинна ключевых слов',
109-
'robotsContent' => 'Robots.txt файл',
110-
'countersContent' => 'Коды счетчиков',
98+
'enableKeywordsGenerator' => \Yii::t('skeeks/seo', 'Automatic generation of keywords'),
99+
'minKeywordLenth' => \Yii::t('skeeks/seo', 'The minimum length of the keyword'),
100+
'maxKeywordsLength' => \Yii::t('skeeks/seo', 'Length keywords'),
101+
'robotsContent' => 'Robots.txt',
102+
'countersContent' => \Yii::t('skeeks/seo', 'Codes counters'),
103+
]);
104+
}
105+
106+
public function attributeHints()
107+
{
108+
return ArrayHelper::merge(parent::attributeHints(), [
109+
'enableKeywordsGenerator' => \Yii::t('skeeks/seo', 'If the page is not specified keywords, they will generate is for her, according to certain rules automatically'),
110+
'minKeywordLenth' => \Yii::t('skeeks/seo', 'The minimum length of the keyword, which is listed by the key (automatic generation)'),
111+
'maxKeywordsLength' => \Yii::t('skeeks/seo', 'The maximum length of the string of keywords (automatic generation)'),
112+
'robotsContent' => \Yii::t('skeeks/seo', 'This value is added to the automatically generated file robots.txt, in the case where it is not physically created on the server'),
111113
]);
112114
}
113115

114116

115117

116118
public function renderConfigForm(ActiveForm $form)
117119
{
118-
echo \Yii::$app->view->renderFile(__DIR__ . '/seo/_form.php', [
119-
'form' => $form,
120-
'model' => $this
121-
], $this);
120+
echo $form->fieldSet(\Yii::t('skeeks/seo', 'Keywords'));
121+
122+
echo $form->field($this, 'enableKeywordsGenerator')->checkbox(\Yii::$app->formatter->booleanFormat);
123+
124+
echo $form->field($this, 'minKeywordLenth');
125+
echo $form->field($this, 'maxKeywordsLength');
126+
127+
128+
echo $form->fieldSetEnd();
129+
130+
echo $form->fieldSet(\Yii::t('skeeks/seo', 'Indexing'));
131+
echo $form->field($this, 'robotsContent')->textarea(['rows' => 7]);
132+
echo $form->fieldSetEnd();
133+
134+
echo $form->fieldSet(\Yii::t('skeeks/seo', 'Codes counters'));
135+
echo $form->field($this, 'countersContent')->textarea(['rows' => 20]);
136+
echo $form->fieldSetEnd();
137+
122138
}
123139

124140

_ide/YiiApplication.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22
/**
3+
*
4+
* The pseudo-only IDE tips
5+
*
36
* @author Semenov Alexander <[email protected]>
47
* @link http://skeeks.com/
58
* @copyright 2010 SkeekS (СкикС)

assets/CmsSeoAsset.php

Lines changed: 7 additions & 242 deletions
Original file line numberDiff line numberDiff line change
@@ -3,251 +3,16 @@
33
* @author Semenov Alexander <[email protected]>
44
* @link http://skeeks.com/
55
* @copyright 2010 SkeekS (СкикС)
6-
* @date 16.04.2016
6+
* @date 15.04.2016
77
*/
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;
8+
namespace skeeks\cms\seo\assets;
9+
use yii\web\AssetBundle;
2510

2611
/**
27-
* Class CmsSeoComponent
28-
* @package skeeks\cms\seo
12+
* Class CmsSeoAsset
13+
* @package skeeks\cms\search\assets
2914
*/
30-
class CmsSeoComponent extends Component
15+
class CmsSeoAsset extends AssetBundle
3116
{
32-
/**
33-
* Можно задать название и описание компонента
34-
* @return array
35-
*/
36-
static public function descriptorConfig()
37-
{
38-
return array_merge(parent::descriptorConfig(), [
39-
'name' => '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-
17+
public $sourcePath = '@skeeks/cms/seo/assets';
25318
}

assets/icons/seo.png

696 Bytes
Loading

config/main.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
]
2323
],
2424

25-
/*'urlManager' => [
25+
'urlManager' => [
2626
'rules' => [
27-
'search' => 'cmsSearch/result',
27+
'robots.txt' => '/seo/robots/on-request',
28+
'sitemap.xml' => '/seo/sitemap/on-request',
29+
2830
]
29-
]*/
31+
]
3032
],
3133

3234
'modules' =>

controllers/RobotsController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use skeeks\cms\seo\models\CmsSearchPhrase;
1313
use skeeks\cms\models\Tree;
1414
use Yii;
15+
use yii\web\Controller;
1516
use yii\web\Response;
1617

1718
/**
@@ -23,7 +24,7 @@ class RobotsController extends Controller
2324
/**
2425
* @return string
2526
*/
26-
public function actionIndex()
27+
public function actionOnRequest()
2728
{
2829
echo \Yii::$app->seo->robotsContent;
2930
\Yii::$app->response->format = Response::FORMAT_RAW;

0 commit comments

Comments
 (0)