Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

+41 -32
View File
@@ -11,16 +11,25 @@
namespace IPS;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use DateInterval;
use IPS\Http\Url;
use XMLWriter;
use function count;
use function defined;
use function in_array;
use function is_array;
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Sitemap generator class
*/
class _Sitemap
class Sitemap
{
/**
* @brief Maximum number of entries to include per file
@@ -31,22 +40,22 @@ class _Sitemap
/**
* @brief Count options
*/
public static $counts = array( 0 => 0, 100 => 100, 500 => 500, 1000 => 1000, 5000 => 5000, 10000 => 10000 );
public static array $counts = array( 0 => 0, 100 => 100, 500 => 500, 1000 => 1000, 5000 => 5000, 10000 => 10000 );
/**
* @brief Priority options
*/
public static $priorities = array( '1.0' => '1.0', '0.9' => '0.9', '0.8' => '0.8', '0.7' => '0.7', '0.6' => '0.6', '0.5' => '0.5', '0.4' => '0.4', '0.3' => '0.3', '0.2' => '0.2', '0.1' => '0.1' );
public static array $priorities = array( '1.0' => '1.0', '0.9' => '0.9', '0.8' => '0.8', '0.7' => '0.7', '0.6' => '0.6', '0.5' => '0.5', '0.4' => '0.4', '0.3' => '0.3', '0.2' => '0.2', '0.1' => '0.1' );
/**
* @brief "Log" entries for this execution
*/
public $log = array();
public array $log = array();
/**
* @brief URL to our sitemap index file
*/
public $sitemapUrl = '';
public string $sitemapUrl = '';
/**
* Constructor
@@ -56,23 +65,23 @@ class _Sitemap
public function __construct()
{
/* Figure out the sitemap URL */
$this->sitemapUrl = ( \IPS\Settings::i()->sitemap_url ) ? rtrim( \IPS\Settings::i()->sitemap_url, '/' ) : rtrim( \IPS\Settings::i()->base_url, '/' ) . '/sitemap.php';
$this->sitemapUrl = ( Settings::i()->sitemap_url ) ? rtrim( Settings::i()->sitemap_url, '/' ) : rtrim( Settings::i()->base_url, '/' ) . '/sitemap.php';
}
/**
* @brief Store the sitemap files we can build
*/
protected $sitemapFilesToBuild = NULL;
protected ?array $sitemapFilesToBuild = NULL;
/**
* Build the sitemap index file
*
* @return bool
*/
public function buildNextSitemap()
public function buildNextSitemap(): bool
{
/* Get our extensions */
$extensions = \IPS\Application::allExtensions( 'core', 'Sitemap', new \IPS\Member, 'core' );
$extensions = Application::allExtensions( 'core', 'Sitemap', new Member, 'core' );
/* If we haven't figured out which files we can/should build, do that first */
if( $this->sitemapFilesToBuild === NULL )
@@ -87,23 +96,23 @@ class _Sitemap
}
/* Delete any that aren't supported */
\IPS\Db::i()->delete( 'core_sitemap', \IPS\Db::i()->in( 'sitemap', $files, TRUE ) );
Db::i()->delete( 'core_sitemap', Db::i()->in( 'sitemap', $files, TRUE ) );
/* Now figure out which one hasn't run in the longest period of time. */
$sitemapsNotBuilt = array_diff( $files, iterator_to_array( \IPS\Db::i()->select( 'sitemap', 'core_sitemap' ) ) );
if ( \count( $sitemapsNotBuilt ) )
$sitemapsNotBuilt = array_diff( $files, iterator_to_array( Db::i()->select( 'sitemap', 'core_sitemap' ) ) );
if ( count( $sitemapsNotBuilt ) )
{
$this->sitemapFilesToBuild = $sitemapsNotBuilt;
}
foreach( \IPS\Db::i()->select( 'sitemap', 'core_sitemap', array( 'updated < ?', ( new \IPS\DateTime )->sub( new \DateInterval( 'PT1H' ) )->getTimestamp() ), 'updated ASC' ) as $sitemapFile )
foreach(Db::i()->select( 'sitemap', 'core_sitemap', array( 'updated < ?', ( new DateTime)->sub( new DateInterval( 'PT1H' ) )->getTimestamp() ), 'updated ASC' ) as $sitemapFile )
{
$this->sitemapFilesToBuild[] = $sitemapFile;
}
}
/* If there are no files to build, return now */
if( !\count( $this->sitemapFilesToBuild ) )
if( !count( $this->sitemapFilesToBuild ) )
{
return FALSE;
}
@@ -116,7 +125,7 @@ class _Sitemap
/* Call the plugin to generate this sitemap file */
foreach( $extensions as $extension )
{
if( \in_array( $toBuild, $extension->getFilenames() ) )
if( in_array( $toBuild, $extension->getFilenames() ) )
{
$extension->generateSitemap( $toBuild, $this );
}
@@ -133,23 +142,23 @@ class _Sitemap
/**
* Build a sitemap file and store it
*
* @param string $filename Filename
* @param array $entries The entries to add. Each entry should be an array with at least the key 'url'. Optional keys 'lastmod', 'priority' and 'changefreq' are also supported.
* @param int $lastId The last ID we built. This is used for content items to allow us to more efficiently fetch the next batch of items to build.
* @param array $namespaces Array of additional namespaces to define
* @param string $filename Filename
* @param array $entries The entries to add. Each entry should be an array with at least the key 'url'. Optional keys 'lastmod', 'priority' and 'changefreq' are also supported.
* @param int $lastId The last ID we built. This is used for content items to allow us to more efficiently fetch the next batch of items to build.
* @param array $namespaces Array of additional namespaces to define
* @return void
*/
public function buildSitemapFile( $filename, $entries, $lastId=0, $namespaces=array() )
public function buildSitemapFile( string $filename, array $entries, int $lastId=0, array $namespaces=array() ) : void
{
/* Start XML Document, set encoding, and create the namespaced index element */
$xmlWriter = new \XMLWriter();
$xmlWriter = new XMLWriter();
$xmlWriter->openMemory();
$xmlWriter->setIndent( TRUE );
$xmlWriter->startDocument( '1.0', 'UTF-8' );
$xmlWriter->startElementNS( NULL, 'urlset', "http://www.sitemaps.org/schemas/sitemap/0.9" );
$xmlWriter->startElementNS( NULL, 'urlset', "https://www.sitemaps.org/schemas/sitemap/0.9" );
if( \count( $namespaces ) )
if( count( $namespaces ) )
{
foreach( $namespaces as $prefix => $urn )
{
@@ -157,22 +166,22 @@ class _Sitemap
}
}
$defaultLanguage = \IPS\Lang::load( \IPS\Lang::defaultLanguage() );
$defaultLanguage = Lang::load( Lang::defaultLanguage() );
if( \count( $entries ) )
if( count( $entries ) )
{
foreach( $entries as $entry )
{
$xmlWriter->startElement( 'url' );
$xmlWriter->startElement( 'loc' );
$xmlWriter->text( preg_replace( '/^' . preg_quote( \IPS\Settings::i()->base_url, '/' ) . '/', '{base_url}', $entry['url'] ) );
$xmlWriter->text( preg_replace( '/^' . preg_quote( Settings::i()->base_url, '/' ) . '/', '{base_url}', $entry['url'] ) );
$xmlWriter->endElement();
if( isset( $entry['lastmod'] ) AND $entry['lastmod'] )
{
$xmlWriter->startElement( 'lastmod' );
$xmlWriter->text( ( $entry['lastmod'] instanceof \IPS\DateTime ) ? $entry['lastmod']->format( 'c', $defaultLanguage ) : \IPS\DateTime::ts( $entry['lastmod'] )->format( 'c', $defaultLanguage ) );
$xmlWriter->text( ( $entry['lastmod'] instanceof DateTime) ? $entry['lastmod']->format( 'c', $defaultLanguage ) : DateTime::ts( $entry['lastmod'] )->format( 'c', $defaultLanguage ) );
$xmlWriter->endElement();
}
@@ -190,7 +199,7 @@ class _Sitemap
$xmlWriter->endElement();
}
if( \count( $namespaces ) )
if( count( $namespaces ) )
{
foreach( $entry as $key => $value )
{
@@ -206,7 +215,7 @@ class _Sitemap
$pieces = explode( ':', $k );
$xmlWriter->startElementNS( $pieces[0], $pieces[1], NULL );
if( \is_array( $v ) )
if( is_array( $v ) )
{
foreach( $v as $index => $value )
{
@@ -246,7 +255,7 @@ class _Sitemap
}
/* Store */
\IPS\Db::i()->replace( 'core_sitemap', array(
Db::i()->replace( 'core_sitemap', array(
'sitemap' => $filename,
'data' => $content,
'updated' => time(),