Sitemap Component

A sitemap generator that saves all of your links, tracks any updates, and makes the entire site searchable.

use BootPress\Sitemap\Component as Sitemap;

Packagist License MIT HHVM Tested PHP 7 Supported Build Status Code Climate Test Coverage


The BootPress Sitemap Component generates the sitemap.xml files by saving all of your website's links, and keeping track of any updates. Any 404 pages are removed as they come to our attention (via $page->send(404)). It also has convenience methods for adding or removing multiple pages at once, and searching the entire site or any parts thereof. The only heads up is to watch what you Sitemap::add() to the database. Listing or product pages that have constantly changing or updated content are not good candidates for the sitemap. It will screw up your searches, and you only want Google to index real content pages anyways.

public object $db ;

@var

The $page->file('Sitemap.db')'s \BootPress\SQLite\Component instance.

public __construct ( void )

Opens the Sitemap database.

@example
$sitemap = new Sitemap();

public __destruct ( void )

Wraps up any pending transactions, and closes the database connection. Always unset($sitemap) when you are done using it.

@example
unset($sitemap);

public static mixed page ( [ int $limit = 10000 [, int $expires = 0 ]] )

Generate the sitemap.xml files, and remove 404 pages as they appear. Made static to save you the hassle of opening and closing the database.

@param $limit

The number of links to display per page.

@param $expires

The number of seconds you would like to cache the response.

@return

Either false or a \Symfony\Component\HttpFoundation\Response for you to send.

@example
if ($xml = Sitemap::page()) {
    $page->send($xml);
}

public static add ( string $category , string $content [, array $save ] )

Include the current page in the sitemap if it is an HTML page, and has no query string. Made static to save you the hassle of opening and closing the database.

Use this method when you want to "set it, and forget it". When adding (and updating) $content dynamically, then some of your links may be a bit outdated as we can only update them when they are accessed. If this is unacceptable to you, then use the 'reset', 'upsert', and 'delete' methods to keep everything up-to-date. Using the static add method would be equivalent to the following code:

if (empty($page->url['query'])) {
    $sitemap = new Sitemap();
    $sitemap->upsert($category, array_merge(array(
        'path' => $page->url['path'],
        'title' => $page->title,
        'description' => $page->description,
        'keywords' => $page->keywords,
        'image' => $page->image,
        'content' => $content,
    ), $save));
    unset($sitemap);
}
@param $category

To group related links.

@param $content

The main body of your page.

@param $save

Any additional information that you consider to be important, and would like to include with your search results.

@example
Sitemap::add('pages', $html);

public reset ( string $category )

Reset a $category before $this->upsert()ing everything in it, so that you can $this->delete() any missing links after. Always unset($sitemap) explicitly when doing this.

@param $category

The sitemap section you are working on.

@example
$sitemap = new Sitemap;
$sitemap->reset('pages');
// $sitemap->upsert('pages', ...); // everything
$sitemap->delete(); // the missing 'pages'
unset($sitemap); // wrap up the database, and close the connection

public upsert ( string $category , string[] $save )

Upsert a page into the Sitemap. Always unset($sitemap) explicitly when doing this.

When upserting everything into a $category, you can $this->reset($category) beforehand, then $this->delete() any missing $category links afterwards.

@param $category

The sitemap section you are working on.

@param $save

An array of data to save for each link:

  • 'updated' => A timestamp integer (if known).
  • 'path' => Of the url, without any suffix.
  • 'title' => Of the page.
  • 'description' => The meta description.
  • 'keywords' => A comma-separated list of tags.
  • 'image' => An image url.
  • 'content' => The main content section of the page.
    • We strip_tags() in house for searching, but deliver the original content with your search results.
  • '...' => Anything else you deem appropriate.
@example
$sitemap = new Sitemap;
$sitemap->upsert('pages', array(
    'path' => 'beautiful-terrible-storm',
    'title' => 'I Watched The Storm, So Beautiful Yet Terrific',
    'image' => 'http://example.com/storm.jpg',
    'content' => '<p>It was amazing.</p>',
));
unset($sitemap); // wrap up the database, and close the connection

public delete ( [ string $path = null ] )

Delete a specific $path (if specified), or everything that was not $this->upsert()ed after $this->reset()ing your sitemap category. Always unset($sitemap) explicitly when doing this.

@example
$sitemap = new Sitemap;
$sitemap->delete('beautiful-terrible-storm');
unset($sitemap); // wrap up the database, and close the connection

public int count ( string $phrase [, string $category [, string $where ]] )

Get the total number of search results for a given $phrase.

@param $phrase

The search term.

@param $category

A specific sitemap section.

@param $where

Adds additional WHERE qualifiers to the query. Prepend search table fields with an 's.', and sitemap table fields with an 'm.'.

@return

The total count.

@example
if (!$pagination->set()) {
  $pagination->total($sitemap->count('words'));
}

public array search ( string $phrase [, string $category [, int|string $limit [, int|float[] $weights [, string $where ]]]] )

Get the search results for a given $phrase, from the most relevant to the least.

@param $phrase

The search term.

@param $category

A specific sitemap section.

@param $limit

If you are not paginating results and only want the top whatever, then this is an integer. Otherwise just pass the $pagination->limit ' LIMIT offset, length' string.

@param $weights

An array of importance that you would like to place on the fields searched. The order is: 'path', 'title', 'description', 'keywords', and 'content'. The default weights are array(1,1,1,1,1), every field being of equal importance. If you only want to search the keywords, then you can specify array(0,0,0,1,0). Please note that with this arrangement, the most relevant results will be returned first (with the search term being found among the keywords), but all of the other results will also be returned with a rank of 0 if the search term could be found anywhere else.

@param $where

Adds additional WHERE qualifiers to the query. Prepend search table fields with an 's.', and sitemap table fields with an 'm.'.

@return

An associative array of results.

@example
foreach ($sitemap->search('words', '', $pagination->limit) as $row) {
    print_r($row);
}

public array words ( string $phrase , int $docid )

Once your search results are in, if you would like to know the specific word(s) which made a given page relevant, you may obtain them through this method.

@param $phrase

The original search term.

@param $docid

The sitemap's docid which is returned with every search result.

@return

The unique search words found which made the $phrase relevant.

@example
print_r($sitemap->words('words', $row['docid']));
Document Your Code

Installation

Add the following to your composer.json file.

{
    "require": {
        "bootpress/sitemap": "^1.0"
    }
}

Example Usage

<?php

use BootPress\Page\Component as Page;
use BootPress\Sitemap\Component as Sitemap;

$page = Page::html();

if ($xml = Sitemap::page()) {
    $page->send($xml);
}

$html = '<p>Content</p>';

Sitemap::add('pages', $html);

$page->send($page->display($html));