Skip to content

Commit

Permalink
Merge pull request #63 from ParitoshBh/master
Browse files Browse the repository at this point in the history
Option to include xml stylesheet tag in generated index, sitemap files
  • Loading branch information
craftyshaun authored Sep 28, 2021
2 parents b71d60f + acd02f7 commit 2a31036
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
28 changes: 27 additions & 1 deletion Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function __construct($filePath)
$this->filePath = $filePath;
}

/**
* @var string path of the xml stylesheet
*/
private $stylesheet;

/**
* Creates new file
*/
Expand All @@ -41,6 +46,11 @@ private function createNewFile()
$this->writer = new XMLWriter();
$this->writer->openMemory();
$this->writer->startDocument('1.0', 'UTF-8');
// Use XML stylesheet, if available
if (isset($this->stylesheet)) {
$this->writer->writePi('xml-stylesheet', "type=\"text/xsl\" href=\"" . $this->stylesheet . "\"");
$this->writer->writeRaw("\n");
}
$this->writer->setIndent(true);
$this->writer->startElement('sitemapindex');
$this->writer->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
Expand Down Expand Up @@ -110,4 +120,20 @@ public function setUseGzip($value)
}
$this->useGzip = $value;
}
}

/**
* Sets stylesheet for the XML file.
* Default is to not generate XML-stylesheet tag.
* @param string $stylesheetUrl Stylesheet URL.
*/
public function setStylesheet($stylesheetUrl)
{
if (false === filter_var($stylesheetUrl, FILTER_VALIDATE_URL)) {
throw new \InvalidArgumentException(
"The stylesheet URL is not valid. You have specified: {$stylesheetUrl}."
);
} else {
$this->stylesheet = $stylesheetUrl;
}
}
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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.

Expand Down Expand Up @@ -65,6 +66,9 @@ $staticSitemapUrls = $staticSitemap->getSitemapUrls('http://example.com/');
// create sitemap index file
$index = new Index(__DIR__ . '/sitemap_index.xml');

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

// add URLs
foreach ($sitemapFileUrls as $sitemapUrl) {
$index->addSitemap($sitemapUrl);
Expand Down Expand Up @@ -131,11 +135,13 @@ There are methods to configure `Sitemap` instance:
- `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.

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.

Running tests
-------------
Expand Down
28 changes: 27 additions & 1 deletion Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class Sitemap
*/
private $filePath;

/**
* @var string path of the XML stylesheet
*/
private $stylesheet;

/**
* @var integer number of files written
*/
Expand Down Expand Up @@ -158,6 +163,11 @@ private function createNewFile()
$this->writer = new XMLWriter();
$this->writer->openMemory();
$this->writer->startDocument('1.0', 'UTF-8');
// Use XML stylesheet, if available
if (isset($this->stylesheet)) {
$this->writer->writePi('xml-stylesheet', "type=\"text/xsl\" href=\"" . $this->stylesheet . "\"");
$this->writer->writeRaw("\n");
}
$this->writer->setIndent($this->useIndent);
$this->writer->startElement('urlset');
$this->writer->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
Expand Down Expand Up @@ -491,4 +501,20 @@ public function setUseGzip($value)
}
$this->useGzip = $value;
}
}

/**
* Sets stylesheet for the XML file.
* Default is to not generate XML stylesheet tag.
* @param string $stylesheetUrl Stylesheet URL.
*/
public function setStylesheet($stylesheetUrl)
{
if (false === filter_var($stylesheetUrl, FILTER_VALIDATE_URL)) {
throw new \InvalidArgumentException(
"The stylesheet URL is not valid. You have specified: {$stylesheetUrl}."
);
} else {
$this->stylesheet = $stylesheetUrl;
}
}
}

0 comments on commit 2a31036

Please sign in to comment.