A sitemap generator that saves all of your links, tracks any updates, and makes the entire site searchable.
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.
The $page->file('Sitemap.db')
's \BootPress\SQLite\Component instance.
Opens the Sitemap database.
$sitemap = new Sitemap();
Wraps up any pending transactions, and closes the database connection. Always unset($sitemap)
when you are done using it.
unset($sitemap);
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.
The number of links to display per page.
The number of seconds you would like to cache the response.
Either false
or a \Symfony\Component\HttpFoundation\Response for you to send.
if ($xml = Sitemap::page()) {
$page->send($xml);
}
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);
}
To group related links.
The main body of your page.
Any additional information that you consider to be important, and would like to include with your search results.
Sitemap::add('pages', $html);
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.
The sitemap section you are working on.
$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
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.
The sitemap section you are working on.
An array of data to save for each link:
strip_tags()
in house for searching, but deliver the original content with your search results.$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
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.
$sitemap = new Sitemap;
$sitemap->delete('beautiful-terrible-storm');
unset($sitemap); // wrap up the database, and close the connection
Get the total number of search results for a given $phrase.
The search term.
A specific sitemap section.
Adds additional WHERE qualifiers to the query. Prepend search table fields with an 's.', and sitemap table fields with an 'm.'.
The total count.
if (!$pagination->set()) {
$pagination->total($sitemap->count('words'));
}
Get the search results for a given $phrase, from the most relevant to the least.
The search term.
A specific sitemap section.
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.
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.
Adds additional WHERE qualifiers to the query. Prepend search table fields with an 's.', and sitemap table fields with an 'm.'.
An associative array of results.
foreach ($sitemap->search('words', '', $pagination->limit) as $row) {
print_r($row);
}
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.
The original search term.
The sitemap's docid which is returned with every search result.
The unique search words found which made the $phrase relevant.
print_r($sitemap->words('words', $row['docid']));
Add the following to your composer.json
file.
{
"require": {
"bootpress/sitemap": "^1.0"
}
}
<?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));