Coder Social home page Coder Social logo

sitemap's Introduction

Thepixeldeveloper\Sitemap

codecov License Latest Stable Version Total Downloads

A tool to generate XML sitemaps. Integrates with Symfony via SitemapBundle

Installation

composer require "thepixeldeveloper/sitemap"

Basic Usage

Generating a typical (<urlset>) sitemap.

<?php declare(strict_types=1);

use Thepixeldeveloper\Sitemap\Urlset;
use Thepixeldeveloper\Sitemap\Url;
use Thepixeldeveloper\Sitemap\Drivers\XmlWriterDriver;

$url = new Url($loc);
$url->setLastMod($lastMod);
$url->setChangeFreq($changeFreq);
$url->setPriority($priority);

$urlset = new Urlset();
$urlset->add($url);

$driver = new XmlWriterDriver();
$urlset->accept($driver);

echo $driver->output();

Generating a parent (<sitemapindex>) sitemap.

<?php declare(strict_types=1);

use Thepixeldeveloper\Sitemap\SitemapIndex;
use Thepixeldeveloper\Sitemap\Sitemap;
use Thepixeldeveloper\Sitemap\Drivers\XmlWriterDriver;

// Sitemap entry.
$url = new Sitemap($loc);
$url->setLastMod($lastMod);

// Add it to a collection.
$urlset = new SitemapIndex();
$urlset->add($url);

$driver = new XmlWriterDriver();
$urlset->accept($driver);

echo $driver->output();

Extensions

The following extensions are supported: Image, Link, Mobile, News and Video. They work in the following way (taking image as an example):

<?php declare(strict_types=1);

use Thepixeldeveloper\Sitemap\Urlset;
use Thepixeldeveloper\Sitemap\Url;
use Thepixeldeveloper\Sitemap\Extensions\Image;

$url = new Url($loc);
$url->setLastMod($lastMod);
$url->setChangeFreq($changeFreq);
$url->setPriority($priority);

$image = new Image('https://image-location.com');

$url->addExtension($image);

...

Advanced Usage

Processing Instructions

You can add processing instructions on the output as such.

<?php declare(strict_types=1);

use Thepixeldeveloper\Sitemap\Drivers\XmlWriterDriver;

$driver = new XmlWriterDriver();
$driver->addProcessingInstructions('xml-stylesheet', 'type="text/xsl" href="/path/to/xslt/main-sitemap.xsl"');

Which will add before the document starts.

<?xml-stylesheet type="text/xsl" href="/path/to/xslt/main-sitemap.xsl"?>

Comments

Comments are useful for information such as when the file was created.

<?php declare(strict_types=1);

use Thepixeldeveloper\Sitemap\Drivers\XmlWriterDriver;

$date = date('Y-m-d H:i:s');

$driver = new XmlWriterDriver();
$driver->addComment('This XML file was written on ' . $date . '. Bye!');

Which will render out.

<?xml version="1.0" encoding="UTF-8"?>
<!--This XML file was written on 2018-06-24 15:57:23. Bye!-->

sitemap's People

Contributors

aterbonus avatar handybitesize avatar hkdobrev avatar jacobemerick avatar johnblackmore avatar lushc avatar marcw avatar masnathan avatar nicolas-brousse avatar nucreativa avatar psociety avatar redjam13 avatar shashikreddy avatar sherault avatar svenluijten avatar the7th avatar thepixeldeveloper avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sitemap's Issues

Add public constants for available changeFreq

Is your feature request related to a problem? Please describe.
It would be nice to have constants for changeFreq available options are: always hourly daily weekly monthly yearly never

Describe the solution you'd like
In Url.php add constants for changeFreq

public const CHANGE_FREQ_HOURLY = 'hourly'

We could then also have the setter validate if the set is valid?

README typo

In the readme, the second basic usage example includes this line:
$urlSet->add($url);

The "Set" should be lowercase like this:
$urlset->add($url);

Suggestion: factory methods or classes to avoid "boilerplate" code

Is your feature request related to a problem? Please describe.
reduce need for "boilerplate" functions

Describe the solution you'd like
I found myself implementing static factory functions to easily assemble a SitemapIndex or UrlSet:

SitemapIndex::getInstanceForSitemaps($sitemaps): static
UrlSet::getInstanceForUrls($urls): static

Describe alternatives you've considered
Instead of using static factory functions, we might use dedicated factory classes:

SitemapIndexFactory::getInstance($sitemaps = null): Sitemap
UrlSetFactory::getInstance($urls = null): UrlSet

Additional context
Implementation is quite trivial, I currently have:

class SitemapIndex extends \Thepixeldeveloper\Sitemap\SitemapIndex
{
    public static function getInstanceForSitemaps($sitemaps) : self
    {
        $sitemapIndex = new static();
        foreach ($sitemaps as $sitemap) {
            $sitemapIndex->addSitemap($sitemap);
        }

        return $sitemapIndex;
    }
}
class UrlSet extends \Thepixeldeveloper\Sitemap\UrlSet
{
    /**
     * @param OutputInterface[] $urls
     *
     * @return static
     */
    public static function getInstanceForUrls($urls) : self
    {
        $urlSet = new static();
        foreach ($urls as $url) {
            $urlSet->addUrl($url);
        }

        return $urlSet;
    }
}

I can provide a PR, would it be considered? Any suggestions / preference?

Improve poor installation instruction

For example using code in "Basic Usage" with an array ['https://www.example.com/my-url', '2010-05-29T01:17:35+00:00', 'monthly', '0.5'] I got on my screen only output like this:

https://www.example.com/my-url2010-05-29T01:17:35+00:00monthly0.5

No XML document created. I don't know what to do. No tutorial online, no better code sample. Typical documentation made by geek which sucks :D Philosophy: "You should know"

Where in "basic usage" is written how to set sitemap.xml URL for example? How it know domain name?

Here #52 you are asking "Where are you stuck, specifically?" so ask somebody to create sitemap.xml for his MVC OOP PHP project with current level instructions on https://github.com/ThePixelDeveloper/Sitemap and you will see ;-)

These are good instructions: https://github.com/samdark/sitemap but this class does not work with images as your one claims this feature.

Feature Request: allow adding XML processing instructions to enable support for XSLT

First of all a big thanks for this great library, drastically reduces complexity in generating XML Sitemaps!

When migrating our home-brew DOMDocument based functionality, I noticed that currently it is not possible to inject "Processing Instructions" into the XML. This can be useful to add XML like:

<?xml-stylesheet type="text/xsl" href="/path/to/xslt/main-sitemap.xsl"?>

When an browser that supports XSLT opens such a sitemap, it applies the transformation as defined in the XSLT. We use that to make the sitemap human-readable for non-techies.

For example:

Also see

I could not find a way to hook into the XMLWriter object to inject such processing instructions.

Workaround
A dirty workaround that works for now is (using DOMDocument):

$xml =  (new Thepixeldeveloper\Sitemap\Output())->getOutput($sitemapIndex);
$doc = new DOMDocument();
$doc->loadXML($xml);
$xslt = $doc->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="/path/to/xslt/main-sitemap.xsl"');
$doc->insertBefore($xslt, $doc->documentElement); //Insert before the root element
echo $doc->saveXML();

Usage Example Error

There is a small error in the Readme.md usage example:

$basic = new \Sitemap\SitemapEntry;

must be:

$basic = new \Sitemap\Sitemap\SitemapEntry;

Can't install

Hi, when i try to install this, composer get stuck. When i do composer -vvv, it got stuck when git remote set-url origin 'https://www.devkit.net/thepixeldeveloper/sitemap.git' && git remote update --prune origin. Can you switch git repo from devkit.net to github? thanks

Use HTTPS for schemaLocation of siteindex.xsd

When loading an XML sitemap in Google Chrome, it complains that it contains 'unsafe scripts'.

screen shot 2017-07-10 at 20 59 53

Note that the XSLT transformation feature is used to transform the XML in 'human friendly' HTML.

I suspect this section:

<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">

The namespace http://www.sitemaps.org/schemas/sitemap/0.9 should not change, but the XML Schema is currently loaded from http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd

That URL already automatically redirects to the https variant:
https://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd

So we might want to use that last URL in the schemaLocation attribute:

<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 https://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">

Since a sitemap is not something typically used by a human using a browser, this is probably a low priority issue. But good to know ๐Ÿ˜„

Update of the Packagist version needed

There is a bug in the latest version on Packagist. This bug has been fixed in this repository, but it is not reflected in the version of Packagist.

Is there any way to update the Packagist version?

Use XML Scheme to have valid xml

http://www.sitemaps.org/protocol.html

Use the xml scheme to have valid xml for http://www.validome.org/xml/validate/

Like

 <urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

     xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"

     xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"

     xmlns:example="http://www.example.com/schemas/example_schema">

and

 <sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

     xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd"

     xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

Missing xmlns:image even \Image sub-element was used in sitemap

Hi there!

Maybe I'm wrong... here is an example which generates 2 <url> tags with additional Image and Link parts. The resulting XML does not contains an expected namespaces xmlns:xhtml and xmlns:image.

But if you change the order of adding (addUrl) urls to Urlset:

$urlSet->addUrl($urlB);
$urlSet->addUrl($urlA);

All will be fine, the missing namespaces appears in the output file.

Feature Request: allow adding comments

Basically the same as #32, but now for XML comments.

In some cases it is usefull to inject comments, for example about the time and memory it took to generate a sitemap. This can currently be done using a custom processing instruction, but a comment is a more elegant approach...

$output->addProcessingInstruction('comment', sprintf('generationTime: %f seconds', $generationTime));

If agreed that this is a reasonable request, I can try to come up with a PR.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.