prestaSitemapPlugin - 0.0.3

Generate XML sitemaps

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


« Back to the Plugins Home

Signin


Forgot your password?
Create an account

Tools

Stats

advanced search
Information Readme Releases Changelog Contribute
Show source

prestaSitemapPlugin

The prestaSitemapPlugin provides an easy way to generate a sitemap.xml.

Features

  • Quick to use
  • Cached (for generated files and urls sections during generation)
  • Compliant with sitemap protocol v0.9 as defined by http://www.sitemaps.org
  • Works with more than 50.000 urls (usage of sitemap index)
  • Manage file limit of 10Mo
  • Multi-domain support (http://fr.foo.bar/sitemap.xml and http://en.foo.bar/sitemap.xml may produce different sitemap contents)

License

The prestaSitemapPlugin is licensed under the MIT License.

Installation

  • Install the plugin:

    $ symfony plugin:install prestaSitemapPlugin
    $ symfony cc

Usage

In order to register methods and or functions that should add URLs to the sitemap, we should add listeners for the event 'presta_sitemap.generate_urls'.

In frontendConfiguration.class.php, we declare one or more listeners in configure() method:

$this->dispatcher->connect( 'presta_sitemap.generate_urls', array( 'miscTools', 'generateSitemapEntries' ) );

We add the listener method:

class miscTools
{   
    /**
     * Listens to the presta_sitemap.generate_urls event.
     *
     * @param  sfEvent $event  An sfEvent instance
     */
    public static function generateSitemapEntries( sfEvent $event )
    {
        // create a new prestaSitemapSection object
        $o_sitemapSection   = new prestaSitemapSection( 'miscSectionName' );
        // check is it is up-to-date
        if( !$o_sitemapSection->isUpToDate() )
        {
            // add prestaSitemapUrl to the prestaSitemapSection
            $o_sitemapSection->addUrl( new prestaSitemapUrl( '@homepage', new DateTime(), prestaSitemapUrl::CHANGE_FREQUENCY_HOURLY, 0.9 ) );
            // add other urls
            for( $i = 0; $i< 10; $i++ )
            {
                $o_sitemapSection->addUrl( new prestaSitemapUrl( ... ) );
            }
        }
    }
}

Enabled prestaSitemap module in your settings.yml:

all:
  .settings:    
    enabled_modules: [default, prestaSitemap]

You can now see your sitemap index file in /sitemap.xml. That's all, you now have a sitemap.

prestaSitemapUrl

A prestaSitemapUrl object expect 4 parameters that can be passed to the constructor or defined using setters methods:

  • The url. This is the only parameter that should be defined in order to see it appears in the sitemap.xml

    Constructor's 1st parameter or defined by ->setLocation().

    This can be a route or an absolute url. It will use url_for().

    If you specificly need to use url_for1() or url_for2() methods you can use setLocation1() (for url_for1()) or setLocation2() (for url_for2())

  • A last modification date. You should give a DateTime object.

    Constructor's 2nd parameter or defined by ->setLastModificationDate().

  • A change frequency.

    Constructor's 3rd parameter or defined by ->setChangeFrequency().

    Valid values are 'always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly' and 'never'.

    It is recommended that you use prestaSitemapUrl::CHANGE_FREQUENCY_* constants.

    Invalid values will be ignored.

  • A priority.

    Constructor's 4th parameter or defined by ->setPriority().

    This must be a number between 0 and 1.

    Invalid values will be ignored.

So the 2 examples below will produce the same results

$o_prestaSitemapUrl1    = new prestaSitemapUrl( $miscUrl, $o_date, $changeFrequency, $priority );
$o_prestaSitemapUrl2    = new prestaSitemapUrl();
$o_prestaSitemapUrl2->
  setLocation( $miscUrl )->
  setLastModificationDate( $o_date )->
  setChangeFrequency( $changeFrequency )->
  setPriority( $priority );

Caching

There si 2 cache levels: a generated xml file cache and a prestaSitemapSection cache

First the xml files are cached (for 1 hour by default).

When they expires, the 'presta_sitemap.generate_urls' event is triggered and prestaSitemapSection object are instanciated.

If they're up-to-date there is no need to do action for retrieving all possible urls and instanciting prestaSitemapUrl object for each.

By default generated xml files are cached for 3600 seconds. Here is the associated config used internally and that can be redefined in project's config.yml

all:
  prestaSitemapPlugin:
    mainCache:    # determine usage of generated xml files
      enabled:  on      # cache enable
      lifetime: 3600    # valid for 1 hour
    sectionCache: # determine usage of internal cached datas at prestaSitemapSection level
      enabled:  on      # enabled or disable internal cache usage for all prestaSitemapSection objects
      lifetime: 86400   # default lifetime used for section when cache secitonCache is enabled (can be respecified for each sitemapSection) 

Notice that in all cases (no matter the cache configuration) no datas will be generated when accessing to a section's sitemap (eg: sitemap.main.xml). Datas generation will only be triggered when calling sitemap.xml, this will ensure datas consistency and improved performances.

So if you want to see modifications in sitemap.main.xml don't forget to call sitemap.xml before.

Notice that if you want to see your latest modifications in url generation you can do manually a "symfony clear-cache" or add the follwing code to your app.yml in order to always see updated datas in your dev environement.

dev:
  prestaSitemapPlugin:
    mainCache:
      enabled:  off      # disable main cache
    sectionCache:
      enabled:  off      # disable section cache

prestaSitemapSection

A prestaSitemapSection is a group of url that will be valid for a given lifetime and will be displayed in the sitemap 'sitemap.sectionName.xml'

Here is a little example about prestaSitemapSection

$o_sitemapSection   = new prestaSitemapSection( 'sectionName', array( 'lifetime' => 172800 ) );
if( !$o_sitemapSection->isUpToDate() )
{
    // the code here will be executed each 48 hours, and the url will be outputed in 'sitemap.sectionName.xml'
}
$o_sitemapSection   = new prestaSitemapSection( 'sectionName', array( 'lifetime' => 86400 ) );
if( !$o_sitemapSection->isUpToDate() )
{
    // the code here will be executed each 24 hours, and the url will be outputed in 'sitemap.sectionName.xml'
}
$o_sitemapSection   = new prestaSitemapSection( 'anotherSectionName', array( 'lifetime' => 86400 ) );
if( !$o_sitemapSection->isUpToDate() )
{
    // the code here will be executed each 24 hours, and the url will be outputed in 'sitemap.anotherSectionName.xml'
}
$o_sitemapSection   = new prestaSitemapSection();
if( !$o_sitemapSection->isUpToDate() )
{
    // the code here will have a lifetime as defined by 'app_prestaSitemapPlugin_sectionCache[enabled]', and the url will be outputed in 'sitemap.main.xml'
}

Changelog

2009-08-04 | 0.0.3-beta

  • More robust routing rules definition (force default config for used sfRoutes).

2009-08-04 | 0.0.2-beta

  • Initial internal beta release.