• Stars
    star
    525
  • Rank 84,404 (Top 2 %)
  • Language
    PHP
  • License
    BSD 3-Clause "New...
  • Created over 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Sitemap and sitemap index builder

Sitemap

XML Sitemap and XML Sitemap Index builder.

GitHub release (latest SemVer) GitHub Workflow Status (branch) Packagist PHP Version Support Packagist Downloads Packagist Downloads GitHub

Features

  • Create sitemap files: either regular or gzipped.
  • Create multi-language sitemap files.
  • Create sitemap index files.
  • Use custom stylesheet.
  • Automatically creates new file if either URL limit or file size limit is reached.
  • Fast and memory efficient.

Installation

Installation via Composer is very simple:

composer require samdark/sitemap

After that, make sure your application autoloads Composer classes by including vendor/autoload.php.

How to use it

use samdark\sitemap\Sitemap;
use samdark\sitemap\Index;

// create sitemap
$sitemap = new Sitemap(__DIR__ . '/sitemap.xml');

// add some URLs
$sitemap->addItem('http://example.com/mylink1');
$sitemap->addItem('http://example.com/mylink2', time());
$sitemap->addItem('http://example.com/mylink3', time(), Sitemap::HOURLY);
$sitemap->addItem('http://example.com/mylink4', time(), Sitemap::DAILY, 0.3);

// set sitemap stylesheet (see example-sitemap-stylesheet.xsl)
$sitemap->setStylesheet('http://example.com/css/sitemap.xsl');

// write it
$sitemap->write();

// get URLs of sitemaps written
$sitemapFileUrls = $sitemap->getSitemapUrls('http://example.com/');

// create sitemap for static files
$staticSitemap = new Sitemap(__DIR__ . '/sitemap_static.xml');

// add some URLs
$staticSitemap->addItem('http://example.com/about');
$staticSitemap->addItem('http://example.com/tos');
$staticSitemap->addItem('http://example.com/jobs');

// set optional stylesheet (see example-sitemap-stylesheet.xsl)
$staticSitemap->setStylesheet('http://example.com/css/sitemap.xsl');

// write it
$staticSitemap->write();

// get URLs of sitemaps written
$staticSitemapUrls = $staticSitemap->getSitemapUrls('http://example.com/');

// create sitemap index file
$index = new Index(__DIR__ . '/sitemap_index.xml');

// set index stylesheet (see example in repo)
$index->setStylesheet('http://example.com/css/sitemap.xsl');

// add URLs
foreach ($sitemapFileUrls as $sitemapUrl) {
    $index->addSitemap($sitemapUrl);
}

// add more URLs
foreach ($staticSitemapUrls as $sitemapUrl) {
    $index->addSitemap($sitemapUrl);
}

// write it
$index->write();

Multi-language sitemap

use samdark\sitemap\Sitemap;

// create sitemap
// be sure to pass `true` as second parameter to specify XHTML namespace
$sitemap = new Sitemap(__DIR__ . '/sitemap_multi_language.xml', true);

// Set URL limit to fit in default limit of 50000 (default limit / number of languages) 
$sitemap->setMaxUrls(25000);

// add some URLs
$sitemap->addItem('http://example.com/mylink1');

$sitemap->addItem([
    'ru' => 'http://example.com/ru/mylink2',
    'en' => 'http://example.com/en/mylink2',
], time());

$sitemap->addItem([
    'ru' => 'http://example.com/ru/mylink3',
    'en' => 'http://example.com/en/mylink3',
], time(), Sitemap::HOURLY);

$sitemap->addItem([
    'ru' => 'http://example.com/ru/mylink4',
    'en' => 'http://example.com/en/mylink4',
], time(), Sitemap::DAILY, 0.3);

// set stylesheet (see example-sitemap-stylesheet.xsl)
$sitemap->setStylesheet('http://example.com/css/sitemap.xsl');

// write it
$sitemap->write();

Options

There are methods to configure Sitemap instance:

  • setMaxUrls($number). Sets maximum number of URLs to write in a single file. Default is 50000 which is the limit according to specification and most of existing implementations.
  • setMaxBytes($number). Sets maximum size of a single site map file. Default is 10MiB which should be compatible with most current search engines.
  • setBufferSize($number). Sets number of URLs to be kept in memory before writing it to file. Default is 10. Bigger values give marginal benefits. On the other hand when the file size limit is hit, the complete buffer must be written to the next file.
  • setUseIndent($bool). Sets if XML should be indented. Default is true.
  • setUseGzip($bool). Sets whether the resulting sitemap files will be gzipped or not. Default is false. zlib extension must be enabled to use this feature.
  • setStylesheet($string). Sets the xml-stylesheet tag. By default, tag is not generated. See example example-sitemap-stylesheet.xsl

There is a method to configure Index instance:

  • setUseGzip($bool). Sets whether the resulting index file will be gzipped or not. Default is false. zlib extension must be enabled to use this feature.
  • setStylesheet($string). Sets the xml-stylesheet tag. By default, tag is not generated. See example example-sitemap-stylesheet.xsl

Running tests

In order to run tests perform the following commands:

composer install
./vendor/bin/phpunit

More Repositories

1

yii2-cookbook

Yii 2.0 Community Cookbook
Makefile
1,439
star
2

yii2-shop

Example project implementing simple shop using Yii 2.0
PHP
456
star
3

the-modal

Proper modal boxes
JavaScript
307
star
4

opensource-hate

Hate in OpenSource
281
star
5

yii2-webshell

Web shell allows to run yii console commands using a browser
PHP
225
star
6

php-fpm_tuner

A script to tune PHP-FPM
PHP
160
star
7

yii2-minimal

Yii 2 minimal application template
PHP
139
star
8

intellij-visual-studio-code-dark-plus

Visual Studio Code Dark Plus theme for JetBrains IDEs
135
star
9

hydrator

Allows extracting data from objects and getting objects from data
PHP
116
star
10

yiifeed

Pre-moderated news aggregator
CSS
103
star
11

Typograph

Класс для автоматического применения правил русской типографики для веб
PHP
100
star
12

Yeeki

Yii 1.1-based wiki
PHP
96
star
13

yiipowered

Yii powered websites showcase
PHP
91
star
14

yiiframework_ru_cookbook

Russian Yii 1.1 cookbook
85
star
15

yii-application-cookbook-2nd-edition-code

Code of the second edition of Yii Application Development Cookbook
PHP
82
star
16

yii2-psr-log-target

Yii 2.0 log target that is able to write messages to PSR-3 compatible logger
PHP
80
star
17

realpath_cache_tuner

Simple script that helps tuning PHP realpath cache
PHP
77
star
18

wnmp-dev

Development environment: Windows + nginx + MySQL + PHP
Batchfile
52
star
19

yiiframework-ru

yiiframework.ru
PHP
52
star
20

codemirror-autosuggest

CodeMirror autosuggest addon
JavaScript
47
star
21

intl-icu-data-tables

PHP intl extension, ICU data tables
PHP
37
star
22

codemirror-buttons

CodeMirror buttons addon
JavaScript
31
star
23

yii-jobs

A module and an application for Yii that provides a job-portal-like functionality
PHP
29
star
24

yii2-league-oauth2-server

Yii 2.0 implementation of PHP league OAuth2 server interfaces
PHP
28
star
25

slides

Alexander Makarov conference slides
HTML
26
star
26

a-guide-to-yii-grids-lists-and-data-providers

A guide to Yii framework grids, lists and data providers
25
star
27

yii-db-profiler

Yii profiler adjusted to deal with MySQL performance optimizations
PHP
24
star
28

icls-vs-code-dark-plus

Visual Studio Code Dark Plus-like color scheme for PhpStorm and other JetBrains IDEs
23
star
29

yii-fullajax

fullajax Yii playground
PHP
21
star
30

sack

This package implements "0-1 Knapsack Problem" algorithm i.e. allows to find the best way to fill a knapsack of a specified volume with items of a certain volume and value.
PHP
9
star
31

samdark

8
star
32

yii2-iconized-menu-widget

Menu with favicons for each item.
PHP
8
star
33

yiiframework-ru-phpbb-style

yiiframework.ru phpBB forum style
CSS
7
star
34

markdownru

markdown.ru website
PHP
7
star
35

github-actions-cookbook

7
star
36

opera-xdebug

Opera Xdebug extension
JavaScript
5
star
37

dotfiles

Various configs
Shell
4
star
38

yiifeed-redesign

HTML
4
star
39

samdark.github.io_source

Alexander Makarov English blog
HTML
3
star
40

arguments

Helps deciding A vs B
JavaScript
1
star
41

opencollective-calculator

Various calculations on exported OpenCollective CSV
PHP
1
star
42

phpunit_runInSeparateProcess_bug

PHP
1
star
43

samdark.github.io

HTML
1
star
44

yiipress

Blog / publishing engine powered by Yii3
PHP
1
star
45

plain_to_amnezia

Plain text domains list to AmneziaVPN converter
PHP
1
star