2009-06-23 00:26:18 +01:00
|
|
|
#!/usr/bin/env php
|
2008-07-13 01:49:36 +01:00
|
|
|
<?php
|
2009-06-23 00:26:18 +01:00
|
|
|
/*
|
2009-08-25 23:12:20 +01:00
|
|
|
* StatusNet - a distributed open-source microblogging tool
|
|
|
|
* Copyright (C) 2008, 2009, StatusNet, Inc.
|
2009-06-23 00:26:18 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2008-08-13 16:46:03 +01:00
|
|
|
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
$shortoptions = 'f:d:u:';
|
|
|
|
|
|
|
|
$helptext = <<<END_OF_SITEMAP_HELP
|
|
|
|
Script for creating sitemaps files per http://sitemaps.org/
|
|
|
|
|
|
|
|
-f <indexfile> Use <indexfile> as output file
|
|
|
|
-d <outputdir> Use <outputdir> for new sitemaps
|
|
|
|
-u <outputurl> Use <outputurl> as root for URLs
|
|
|
|
|
|
|
|
END_OF_SITEMAP_HELP;
|
|
|
|
|
|
|
|
require_once INSTALLDIR . '/scripts/commandline.inc';
|
2008-07-13 01:49:36 +01:00
|
|
|
|
|
|
|
$output_paths = parse_args();
|
|
|
|
|
|
|
|
standard_map();
|
|
|
|
notices_map();
|
|
|
|
user_map();
|
|
|
|
index_map();
|
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// ------------------------------------------------------------------------------
|
|
|
|
// Main functions: get data out and turn them into sitemaps
|
|
|
|
// ------------------------------------------------------------------------------
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// Generate index sitemap of all other sitemaps.
|
2008-12-23 19:33:23 +00:00
|
|
|
function index_map()
|
|
|
|
{
|
2008-12-23 19:19:07 +00:00
|
|
|
global $output_paths;
|
|
|
|
$output_dir = $output_paths['output_dir'];
|
|
|
|
$output_url = $output_paths['output_url'];
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
foreach (glob("$output_dir*.xml") as $file_name) {
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// Just the file name please.
|
2008-12-23 19:19:07 +00:00
|
|
|
$file_name = preg_replace("|$output_dir|", '', $file_name);
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
$index_urls .= sitemap(
|
|
|
|
array(
|
|
|
|
'url' => $output_url . $file_name,
|
|
|
|
'changefreq' => 'daily'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
write_file($output_paths['index_file'], sitemapindex($index_urls));
|
2008-07-13 01:49:36 +01:00
|
|
|
}
|
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// Generate sitemap of standard site elements.
|
2008-12-23 19:33:23 +00:00
|
|
|
function standard_map()
|
|
|
|
{
|
2008-12-23 19:19:07 +00:00
|
|
|
global $output_paths;
|
|
|
|
|
|
|
|
$standard_map_urls .= url(
|
|
|
|
array(
|
|
|
|
'url' => common_local_url('public'),
|
|
|
|
'changefreq' => 'daily',
|
|
|
|
'priority' => '1',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$standard_map_urls .= url(
|
|
|
|
array(
|
|
|
|
'url' => common_local_url('publicrss'),
|
|
|
|
'changefreq' => 'daily',
|
|
|
|
'priority' => '0.3',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
$docs = array('about', 'faq', 'contact', 'im', 'openid', 'openmublog',
|
2009-02-21 01:17:20 +00:00
|
|
|
'privacy', 'source', 'badge');
|
2008-12-23 19:19:07 +00:00
|
|
|
|
|
|
|
foreach($docs as $title) {
|
|
|
|
$standard_map_urls .= url(
|
|
|
|
array(
|
|
|
|
'url' => common_local_url('doc', array('title' => $title)),
|
|
|
|
'changefreq' => 'monthly',
|
|
|
|
'priority' => '0.2',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$urlset_path = $output_paths['output_dir'] . 'standard.xml';
|
|
|
|
|
|
|
|
write_file($urlset_path, urlset($standard_map_urls));
|
2008-07-13 01:49:36 +01:00
|
|
|
}
|
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// Generate sitemaps of all notices.
|
2008-12-23 19:33:23 +00:00
|
|
|
function notices_map()
|
|
|
|
{
|
2008-12-23 19:19:07 +00:00
|
|
|
global $output_paths;
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
$notices = DB_DataObject::factory('notice');
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
$notices->query('SELECT id, uri, url, modified FROM notice where is_local = 1');
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
$notice_count = 0;
|
|
|
|
$map_count = 1;
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
while ($notices->fetch()) {
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// Maximum 50,000 URLs per sitemap file.
|
2008-12-23 19:19:07 +00:00
|
|
|
if ($notice_count == 50000) {
|
|
|
|
$notice_count = 0;
|
|
|
|
$map_count++;
|
|
|
|
}
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// remote notices have an URL
|
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
if (!$notices->url && $notices->uri) {
|
|
|
|
$notice = array(
|
|
|
|
'url' => ($notices->uri) ? $notices->uri : common_local_url('shownotice', array('notice' => $notices->id)),
|
|
|
|
'lastmod' => common_date_w3dtf($notices->modified),
|
|
|
|
'changefreq' => 'never',
|
|
|
|
'priority' => '1',
|
|
|
|
);
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
$notice_list[$map_count] .= url($notice);
|
|
|
|
$notice_count++;
|
|
|
|
}
|
|
|
|
}
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// Make full sitemaps from the lists and save them.
|
2008-12-23 19:19:07 +00:00
|
|
|
array_to_map($notice_list, 'notice');
|
2008-07-13 01:49:36 +01:00
|
|
|
}
|
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// Generate sitemaps of all users.
|
2008-12-23 19:33:23 +00:00
|
|
|
function user_map()
|
|
|
|
{
|
2008-12-23 19:19:07 +00:00
|
|
|
global $output_paths;
|
|
|
|
|
|
|
|
$users = DB_DataObject::factory('user');
|
|
|
|
|
|
|
|
$users->query('SELECT id, nickname FROM user');
|
|
|
|
|
|
|
|
$user_count = 0;
|
|
|
|
$map_count = 1;
|
|
|
|
|
|
|
|
while ($users->fetch()) {
|
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// Maximum 50,000 URLs per sitemap file.
|
2008-12-23 19:19:07 +00:00
|
|
|
if ($user_count == 50000) {
|
|
|
|
$user_count = 0;
|
|
|
|
$map_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user_args = array('nickname' => $users->nickname);
|
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// Define parameters for generating <url></url> elements.
|
2008-12-23 19:19:07 +00:00
|
|
|
$user = array(
|
|
|
|
'url' => common_local_url('showstream', $user_args),
|
|
|
|
'changefreq' => 'daily',
|
|
|
|
'priority' => '1',
|
|
|
|
);
|
|
|
|
|
|
|
|
$user_rss = array(
|
|
|
|
'url' => common_local_url('userrss', $user_args),
|
|
|
|
'changefreq' => 'daily',
|
|
|
|
'priority' => '0.3',
|
|
|
|
);
|
|
|
|
|
|
|
|
$all = array(
|
|
|
|
'url' => common_local_url('all', $user_args),
|
|
|
|
'changefreq' => 'daily',
|
|
|
|
'priority' => '1',
|
|
|
|
);
|
|
|
|
|
|
|
|
$all_rss = array(
|
|
|
|
'url' => common_local_url('allrss', $user_args),
|
|
|
|
'changefreq' => 'daily',
|
|
|
|
'priority' => '0.3',
|
|
|
|
);
|
|
|
|
|
|
|
|
$replies = array(
|
|
|
|
'url' => common_local_url('replies', $user_args),
|
|
|
|
'changefreq' => 'daily',
|
|
|
|
'priority' => '1',
|
|
|
|
);
|
|
|
|
|
|
|
|
$replies_rss = array(
|
|
|
|
'url' => common_local_url('repliesrss', $user_args),
|
|
|
|
'changefreq' => 'daily',
|
|
|
|
'priority' => '0.3',
|
|
|
|
);
|
|
|
|
|
|
|
|
$foaf = array(
|
|
|
|
'url' => common_local_url('foaf', $user_args),
|
|
|
|
'changefreq' => 'weekly',
|
|
|
|
'priority' => '0.5',
|
|
|
|
);
|
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// Construct a <url></url> element for each user facet and add it
|
|
|
|
// to our existing list of those.
|
2008-12-23 19:19:07 +00:00
|
|
|
$user_list[$map_count] .= url($user);
|
|
|
|
$user_rss_list[$map_count] .= url($user_rss);
|
|
|
|
$all_list[$map_count] .= url($all);
|
|
|
|
$all_rss_list[$map_count] .= url($all_rss);
|
|
|
|
$replies_list[$map_count] .= url($replies);
|
|
|
|
$replies_rss_list[$map_count] .= url($replies_rss);
|
|
|
|
$foaf_list[$map_count] .= url($foaf);
|
|
|
|
|
|
|
|
$user_count++;
|
|
|
|
}
|
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// Make full sitemaps from the lists and save them.
|
|
|
|
// Possible factoring: put all the lists into a master array, thus allowing
|
|
|
|
// calling with single argument (i.e., array_to_map('user')).
|
2008-12-23 19:19:07 +00:00
|
|
|
array_to_map($user_list, 'user');
|
|
|
|
array_to_map($user_rss_list, 'user_rss');
|
|
|
|
array_to_map($all_list, 'all');
|
|
|
|
array_to_map($all_rss_list, 'all_rss');
|
|
|
|
array_to_map($replies_list, 'replies');
|
|
|
|
array_to_map($replies_rss_list, 'replies_rss');
|
|
|
|
array_to_map($foaf_list, 'foaf');
|
2008-07-13 01:49:36 +01:00
|
|
|
}
|
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// ------------------------------------------------------------------------------
|
|
|
|
// XML generation functions
|
|
|
|
// ------------------------------------------------------------------------------
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// Generate a <url></url> element.
|
2008-12-23 19:33:23 +00:00
|
|
|
function url($url_args)
|
|
|
|
{
|
2009-06-23 00:26:18 +01:00
|
|
|
$url = preg_replace('/&/', '&', $url_args['url']); // escape ampersands for XML
|
2008-12-23 19:19:07 +00:00
|
|
|
$lastmod = $url_args['lastmod'];
|
|
|
|
$changefreq = $url_args['changefreq'];
|
|
|
|
$priority = $url_args['priority'];
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
if (is_null($url)) {
|
|
|
|
error("url() arguments require 'url' value.");
|
|
|
|
}
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
$url_out = "\t<url>\n";
|
|
|
|
$url_out .= "\t\t<loc>$url</loc>\n";
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
if ($changefreq) {
|
|
|
|
$url_out .= "\t\t<changefreq>$changefreq</changefreq>\n";
|
|
|
|
}
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
if ($lastmod) {
|
|
|
|
$url_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
|
|
|
|
}
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
if ($priority) {
|
|
|
|
$url_out .= "\t\t<priority>$priority</priority>\n";
|
|
|
|
}
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
$url_out .= "\t</url>\n";
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
return $url_out;
|
2008-07-13 01:49:36 +01:00
|
|
|
}
|
|
|
|
|
2008-12-23 19:33:23 +00:00
|
|
|
function sitemap($sitemap_args)
|
|
|
|
{
|
2009-06-23 00:26:18 +01:00
|
|
|
$url = preg_replace('/&/', '&', $sitemap_args['url']); // escape ampersands for XML
|
2008-12-23 19:19:07 +00:00
|
|
|
$lastmod = $sitemap_args['lastmod'];
|
2008-08-11 21:00:58 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
if (is_null($url)) {
|
|
|
|
error("url() arguments require 'url' value.");
|
|
|
|
}
|
2008-08-11 21:00:58 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
$sitemap_out = "\t<sitemap>\n";
|
|
|
|
$sitemap_out .= "\t\t<loc>$url</loc>\n";
|
2008-08-11 21:00:58 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
if ($lastmod) {
|
|
|
|
$sitemap_out .= "\t\t<lastmod>$lastmod</lastmod>\n";
|
|
|
|
}
|
2008-08-11 21:00:58 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
$sitemap_out .= "\t</sitemap>\n";
|
2008-08-11 21:00:58 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
return $sitemap_out;
|
2008-08-11 21:00:58 +01:00
|
|
|
}
|
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// Generate a <urlset></urlset> element.
|
2008-12-23 19:33:23 +00:00
|
|
|
function urlset($urlset_text)
|
|
|
|
{
|
2008-12-23 19:19:07 +00:00
|
|
|
$urlset = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
|
|
|
|
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" .
|
|
|
|
$urlset_text .
|
|
|
|
'</urlset>';
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
return $urlset;
|
2008-07-13 01:49:36 +01:00
|
|
|
}
|
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// Generate a <urlset></urlset> element.
|
2008-12-23 19:33:23 +00:00
|
|
|
function sitemapindex($sitemapindex_text)
|
|
|
|
{
|
2008-12-23 19:19:07 +00:00
|
|
|
$sitemapindex = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" .
|
|
|
|
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" .
|
|
|
|
$sitemapindex_text .
|
|
|
|
'</sitemapindex>';
|
2008-08-11 21:00:58 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
return $sitemapindex;
|
2008-08-11 21:00:58 +01:00
|
|
|
}
|
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// Generate a sitemap from an array containing <url></url> elements and write it to a file.
|
2008-12-23 19:33:23 +00:00
|
|
|
function array_to_map($url_list, $filename_prefix)
|
|
|
|
{
|
2008-12-23 19:19:07 +00:00
|
|
|
global $output_paths;
|
|
|
|
|
|
|
|
if ($url_list) {
|
2009-06-23 00:26:18 +01:00
|
|
|
// $map_urls is a long string containing concatenated <url></url> elements.
|
2008-12-23 19:19:07 +00:00
|
|
|
while (list($map_idx, $map_urls) = each($url_list)) {
|
|
|
|
$urlset_path = $output_paths['output_dir'] . "$filename_prefix-$map_idx.xml";
|
2009-06-23 00:26:18 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
write_file($urlset_path, urlset($map_urls));
|
|
|
|
}
|
|
|
|
}
|
2008-07-13 01:49:36 +01:00
|
|
|
}
|
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// ------------------------------------------------------------------------------
|
|
|
|
// Internal functions
|
|
|
|
// ------------------------------------------------------------------------------
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// Parse command line arguments.
|
2008-12-23 19:33:23 +00:00
|
|
|
function parse_args()
|
|
|
|
{
|
2009-06-23 00:26:18 +01:00
|
|
|
$index_file = get_option_value('f');
|
|
|
|
$output_dir = get_option_value('d');
|
|
|
|
$output_url = get_option_value('u');
|
2008-12-23 19:19:07 +00:00
|
|
|
|
|
|
|
if (file_exists($output_dir)) {
|
2008-12-23 19:44:28 +00:00
|
|
|
if (is_writable($output_dir) === false) {
|
2008-12-23 19:19:07 +00:00
|
|
|
error("$output_dir is not writable.");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error("output directory $output_dir does not exist.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$paths = array(
|
|
|
|
'index_file' => $index_file,
|
|
|
|
'output_dir' => trailing_slash($output_dir),
|
|
|
|
'output_url' => trailing_slash($output_url),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $paths;
|
2008-07-13 01:49:36 +01:00
|
|
|
}
|
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// Ensure paths end with a "/".
|
2008-12-23 19:33:23 +00:00
|
|
|
function trailing_slash($path)
|
|
|
|
{
|
2008-12-23 19:19:07 +00:00
|
|
|
if (preg_match('/\/$/', $path) == 0) {
|
|
|
|
$path .= '/';
|
|
|
|
}
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
return $path;
|
2008-07-13 01:49:36 +01:00
|
|
|
}
|
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// Write data to disk.
|
2008-12-23 19:33:23 +00:00
|
|
|
function write_file($path, $data)
|
|
|
|
{
|
2008-12-23 19:19:07 +00:00
|
|
|
if (is_null($path)) {
|
|
|
|
error('No path specified for writing to.');
|
|
|
|
} elseif (is_null($data)) {
|
|
|
|
error('No data specified for writing.');
|
|
|
|
}
|
|
|
|
|
2008-12-23 19:44:28 +00:00
|
|
|
if (($fh_out = fopen($path,'w')) === false) {
|
2008-12-23 19:19:07 +00:00
|
|
|
error("couldn't open $path for writing.");
|
|
|
|
}
|
|
|
|
|
2008-12-23 19:44:28 +00:00
|
|
|
if (fwrite($fh_out, $data) === false) {
|
2008-12-23 19:19:07 +00:00
|
|
|
error("couldn't write to $path.");
|
|
|
|
}
|
2008-07-13 01:49:36 +01:00
|
|
|
}
|
|
|
|
|
2009-06-23 00:26:18 +01:00
|
|
|
// Display an error message and exit.
|
2008-12-23 19:33:23 +00:00
|
|
|
function error ($error_msg)
|
|
|
|
{
|
2008-12-23 19:19:07 +00:00
|
|
|
if (is_null($error_msg)) {
|
|
|
|
$error_msg = 'error() was called without any explanation!';
|
|
|
|
}
|
2008-07-13 01:49:36 +01:00
|
|
|
|
2008-12-23 19:19:07 +00:00
|
|
|
echo "Error: $error_msg\n";
|
|
|
|
exit(1);
|
2008-07-13 01:49:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|