Skip to content

Commit

Permalink
Merge pull request #8 from EvgenyGavrilov/master
Browse files Browse the repository at this point in the history
disallow urls
  • Loading branch information
zhelyabuzhsky committed Jul 22, 2015
2 parents 8e5131e + b9f6409 commit 5b1c15a
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 5 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ install:
- travis_retry composer self-update
- travis_retry composer install --prefer-dist --no-interaction

before_script:
- mysql -e 'create database yii2_sitemap;'

script:
- vendor/bin/phpunit --verbose $PHPUNIT_FLAGS
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover --verbose $PHPUNIT_FLAGS

after_script:
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
- if [[ $TRAVIS_PHP_VERSION != 'hhvm' && $TRAVIS_PHP_VERSION != '7.0' ]]; then php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover; fi
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ $ composer require zhelyabuzhsky/yii2-sitemap
* multiple sitemaps (large sites)
* index sitemap
* gzip
* disallow urls (Regular expression array)

## Usage

Expand All @@ -38,6 +39,10 @@ public function actionCreateSitemap()
\Yii::$app->sitemap
->addModel(Item::class)
->addModel(Category::class)
->setDisallowUrls([
'#url1#',
'#url2$#',
])
->create();
}
```
Expand Down
36 changes: 36 additions & 0 deletions src/components/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class Sitemap extends Component
*/
protected $dataSources = [];

/**
* @var array
*/
protected $disallowUrls = [];

/**
* Create index file sitemap.xml.
*/
Expand Down Expand Up @@ -180,6 +185,9 @@ public function create()
/** @var \yii\db\ActiveQuery $dataSource */
foreach ($dataSource->batch(100) as $entities) {
foreach ($entities as $entity) {
if ($this->isDisallowUrl($entity->getSitemapLoc())) {
continue;
}
if ($this->urlCount === $this->maxUrlsCountInFile) {
$this->urlCount = 0;
$this->closeFile();
Expand All @@ -200,6 +208,34 @@ public function create()
$this->updateSitemaps();
}

/**
* Set disallow pattern url.
*
* @param array $urls
* @return $this
*/
public function setDisallowUrls($urls)
{
$this->disallowUrls = $urls;
return $this;
}

/**
* Checking for validity.
*
* @param string $url
* @return bool
*/
protected function isDisallowUrl($url)
{
foreach ($this->disallowUrls as $disallowUrl) {
if (preg_match($disallowUrl, $url) != false) {
return true;
}
}
return false;
}

/**
* Write entity to sitemap file.
*
Expand Down
36 changes: 34 additions & 2 deletions tests/SitemapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,37 @@
namespace zhelyabuzhsky\sitemap\tests;

use zhelyabuzhsky\sitemap\components\Sitemap;
use zhelyabuzhsky\sitemap\tests\models\Category;

class SitemapTest extends \PHPUnit_Framework_TestCase
{
public function testCreate()

private $path = 'tests/runtime';

public function setUp()
{
\Yii::$app->db
->createCommand(file_get_contents(__DIR__ . '/db/mysql.sql'))
->execute();
file_put_contents($this->path . '/' . 'sitemap.xml', 'test');
}

public function testInterface()
{
$this->setExpectedException('\Exception');
$mock = $this->getMock('Fake');
$sitemap = new Sitemap();
$sitemapDirectory = $sitemap->sitemapDirectory = 'tests/runtime';
$sitemap->addModel(get_class($mock));
}

public function testCreate()
{
$sitemap = new Sitemap([
'maxUrlsCountInFile' => 1
]);
$sitemapDirectory = $sitemap->sitemapDirectory = $this->path;
$sitemap->addModel(Category::className());
$sitemap->setDisallowUrls(['#category_2#']);
$sitemap->create();
$sitemapFileNames = Array();
foreach (glob("$sitemapDirectory/sitemap*") as $sitemapFilePath) {
Expand All @@ -20,7 +44,15 @@ public function testCreate()
'sitemap.xml.gz',
'sitemap1.xml',
'sitemap1.xml.gz',
'sitemap2.xml',
'sitemap2.xml.gz',
));
$xmlData = file_get_contents("$sitemapDirectory/sitemap.xml");
$this->assertNotFalse(strpos($xmlData, '<?xml version="1.0" encoding="UTF-8"?>'));

$xmlData = file_get_contents("$sitemapDirectory/sitemap2.xml");
$this->assertNotFalse(strpos($xmlData, '<loc>http://localhost/category_3</loc>'));

foreach (glob("$sitemapDirectory/sitemap*") as $file) {
unlink($file);
}
Expand Down
13 changes: 12 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
$config = yii\helpers\ArrayHelper::merge(
['id' => 'unit'],
['basePath' => __DIR__]
['basePath' => __DIR__],
[
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2_sitemap',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
],
]
);
new \yii\web\Application($config);
14 changes: 14 additions & 0 deletions tests/db/mysql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
DROP TABLE IF EXISTS `category`;

CREATE TABLE `category` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`slug` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1;

INSERT INTO `category` (`name`, `slug`)
VALUES
('Category 1', 'category_1'),
('Category 2', 'category_2'),
('Category 3', 'category_3');
49 changes: 49 additions & 0 deletions tests/models/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace zhelyabuzhsky\sitemap\tests\models;

use yii\db\ActiveRecord;
use zhelyabuzhsky\sitemap\models\SitemapEntityInterface;

class Category extends ActiveRecord implements SitemapEntityInterface
{

/**
* @inheritdoc
*/
public function getSitemapLastmod()
{
return date('c');
}

/**
* @inheritdoc
*/
public function getSitemapChangefreq()
{
return 'daily';
}

/**
* @inheritdoc
*/
public function getSitemapPriority()
{
return 0.5;
}

/**
* @inheritdoc
*/
public function getSitemapLoc()
{
return 'http://localhost/' . $this->slug;
}

/**
* @inheritdoc
*/
public static function getSitemapDataSource()
{
return self::find();
}
}

0 comments on commit 5b1c15a

Please sign in to comment.