core-bundle/src/Resources/contao/library/Contao/Automator.php
public function generateSitemap($intId=0)
	{
		$time = Date::floorToMinute();
		$objDatabase = Database::getInstance();

		$this->purgeXmlFiles();

		// Only root pages should have sitemap names
		$objDatabase->execute("UPDATE tl_page SET createSitemap='', sitemapName='' WHERE type!='root'");

		// Get a particular root page
		if ($intId > 0)
		{
			do
			{
				$objRoot = $objDatabase->prepare("SELECT * FROM tl_page WHERE id=?")
									   ->limit(1)
									   ->execute($intId);

				if ($objRoot->numRows < 1)
				{
					break;
				}

				$intId = $objRoot->pid;
			}
			while ($objRoot->type != 'root' && $intId > 0);

			// Make sure the page is published
			if (!$objRoot->published || ($objRoot->start != '' && $objRoot->start > $time) || ($objRoot->stop != '' && $objRoot->stop <= ($time + 60)))
			{
				return;
			}

			// Check the sitemap name
			if (!$objRoot->createSitemap || !$objRoot->sitemapName)
			{
				return;
			}

			$objRoot->reset();
		}

		// Get all published root pages
		else
		{
			$objRoot = $objDatabase->execute("SELECT id, language, sitemapName FROM tl_page WHERE type='root' AND createSitemap='1' AND sitemapName!='' AND (start='' OR start<='$time') AND (stop='' OR stop>'" . ($time + 60) . "') AND published='1'");
		}

		// Return if there are no pages
		if ($objRoot->numRows < 1)
		{
			return;
		}

		// Create the XML file
		while ($objRoot->next())
		{
			$objFile = new File(StringUtil::stripRootDir(System::getContainer()->getParameter('contao.web_dir')) . '/share/' . $objRoot->sitemapName . '.xml');

			$objFile->truncate();
			$objFile->append('<?xml version="1.0" encoding="UTF-8"?>');
			$objFile->append('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">');

			// Find the searchable pages
			$arrPages = Backend::findSearchablePages($objRoot->id, '', true);

			// HOOK: take additional pages
			if (isset($GLOBALS['TL_HOOKS']['getSearchablePages']) && \is_array($GLOBALS['TL_HOOKS']['getSearchablePages']))
			{
				foreach ($GLOBALS['TL_HOOKS']['getSearchablePages'] as $callback)
				{
					$this->import($callback[0]);
					$arrPages = $this->{$callback[0]}->{$callback[1]}($arrPages, $objRoot->id, true, $objRoot->language);
				}
			}

			// Add pages
			foreach ($arrPages as $strUrl)
			{
				$strUrl = explode('/', $strUrl, 4);

				if (isset($strUrl[3]))
				{
					$strUrl[3] = rawurlencode($strUrl[3]);
					$strUrl[3] = str_replace(array('%2F', '%3F', '%3D', '%26', '%5B', '%5D', '%25'), array('/', '?', '=', '&', '[', ']', '%'), $strUrl[3]);
				}

				$strUrl = implode('/', $strUrl);
				$strUrl = ampersand($strUrl, true);

				$objFile->append('  <url><loc>' . $strUrl . '</loc></url>');
			}

			$objFile->append('</urlset>');
			$objFile->close();

			// Add a log entry
			$this->log('Generated sitemap "' . $objRoot->sitemapName . '.xml"', __METHOD__, TL_CRON);
		}
	}
public function generateSitemap($intId=0)
	{
		$time = Date::floorToMinute();
		$objDatabase = Database::getInstance();

		$this->purgeXmlFiles();

		// Only root pages should have sitemap names
		$objDatabase->execute("UPDATE tl_page SET createSitemap='', sitemapName='' WHERE type!='root'");

		// Get a particular root page
		if ($intId > 0)
		{
			do
			{
				$objRoot = $objDatabase->prepare("SELECT * FROM tl_page WHERE id=?")
									   ->limit(1)
									   ->execute($intId);

				if ($objRoot->numRows < 1)
				{
					break;
				}

				$intId = $objRoot->pid;
			}
			while ($objRoot->type != 'root' && $intId > 0);

			// Make sure the page is published
			if (!$objRoot->published || ($objRoot->start != '' && $objRoot->start > $time) || ($objRoot->stop != '' && $objRoot->stop <= ($time + 60)))
			{
				return;
			}

			// Check the sitemap name
			if (!$objRoot->createSitemap || !$objRoot->sitemapName)
			{
				return;
			}

			$objRoot->reset();
		}

		// Get all published root pages
		else
		{
			$objRoot = $objDatabase->execute("SELECT id, language, sitemapName FROM tl_page WHERE type='root' AND createSitemap='1' AND sitemapName!='' AND (start='' OR start<='$time') AND (stop='' OR stop>'" . ($time + 60) . "') AND published='1'");
		}

		// Return if there are no pages
		if ($objRoot->numRows < 1)
		{
			return;
		}

		// Create the XML file
		while ($objRoot->next())
		{
			$objFile = new File(StringUtil::stripRootDir(System::getContainer()->getParameter('contao.web_dir')) . '/share/' . $objRoot->sitemapName . '.xml');

			$objFile->truncate();
			$objFile->append('<?xml version="1.0" encoding="UTF-8"?>');
			$objFile->append('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">');

			// Find the searchable pages
			$arrPages = Backend::findSearchablePages($objRoot->id, '', true);

			// HOOK: take additional pages
			if (isset($GLOBALS['TL_HOOKS']['getSearchablePages']) && \is_array($GLOBALS['TL_HOOKS']['getSearchablePages']))
			{
				foreach ($GLOBALS['TL_HOOKS']['getSearchablePages'] as $callback)
				{
					$this->import($callback[0]);
					$arrPages = $this->{$callback[0]}->{$callback[1]}($arrPages, $objRoot->id, true, $objRoot->language);
				}
			}

			// Add pages
			foreach ($arrPages as $strUrl)
			{
				$strUrl = explode('/', $strUrl, 4);

				if (isset($strUrl[3]))
				{
					$strUrl[3] = rawurlencode($strUrl[3]);
					$strUrl[3] = str_replace(array('%2F', '%3F', '%3D', '%26', '%5B', '%5D', '%25'), array('/', '?', '=', '&', '[', ']', '%'), $strUrl[3]);
				}

				$strUrl = implode('/', $strUrl);
				$strUrl = ampersand($strUrl, true);

				$objFile->append('  <url><loc>' . $strUrl . '</loc></url>');
			}

			$objFile->append('</urlset>');
			$objFile->close();

			// Add a log entry
			$this->log('Generated sitemap "' . $objRoot->sitemapName . '.xml"', __METHOD__, TL_CRON);
		}
	}
public function generateSitemap ( $ intId = 0 ) { $ time = Date :: floorToMinute ( ) ; $ objDatabase = Database :: getInstance ( ) ; $ this -> purgeXmlFiles ( ) ; // Only root pages should have sitemap names $ objDatabase -> execute ( "UPDATE tl_page SET createSitemap='', sitemapName='' WHERE type!='root'" ) ; // Get a particular root page if ( $ intId > 0 ) { do { $ objRoot = $ objDatabase -> prepare ( "SELECT * FROM tl_page WHERE id=?" ) -> limit ( 1 ) -> execute ( $ intId ) ; if ( $ objRoot -> numRows < 1 ) { break ; } $ intId = $ objRoot -> pid ; } while ( $ objRoot -> type != 'root' && $ intId > 0 ) ; // Make sure the page is published if ( ! $ objRoot -> published || ( $ objRoot -> start != '' && $ objRoot -> start > $ time ) || ( $ objRoot -> stop != '' && $ objRoot -> stop <= ( $ time + 60 ) ) ) { return ; } // Check the sitemap name if ( ! $ objRoot -> createSitemap || ! $ objRoot -> sitemapName ) { return ; } $ objRoot -> reset ( ) ; } // Get all published root pages else { $ objRoot = $ objDatabase -> execute ( "SELECT id, language, sitemapName FROM tl_page WHERE type='root' AND createSitemap='1' AND sitemapName!='' AND (start='' OR start<='$time') AND (stop='' OR stop>'" . ( $ time + 60 ) . "') AND published='1'" ) ; } // Return if there are no pages if ( $ objRoot -> numRows < 1 ) { return ; } // Create the XML file while ( $ objRoot -> next ( ) ) { $ objFile = new File ( StringUtil :: stripRootDir ( System :: getContainer ( ) -> getParameter ( 'contao.web_dir' ) ) . '/share/' . $ objRoot -> sitemapName . '.xml' ) ; $ objFile -> truncate ( ) ; $ objFile -> append ( '<?xml version="1.0" encoding="UTF-8"?>' ) ; $ objFile -> append ( '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' ) ; // Find the searchable pages $ arrPages = Backend :: findSearchablePages ( $ objRoot -> id , '' , true ) ; // HOOK: take additional pages if ( isset ( $ GLOBALS [ 'TL_HOOKS' ] [ 'getSearchablePages' ] ) && \ is_array ( $ GLOBALS [ 'TL_HOOKS' ] [ 'getSearchablePages' ] ) ) { foreach ( $ GLOBALS [ 'TL_HOOKS' ] [ 'getSearchablePages' ] as $ callback ) { $ this -> import ( $ callback [ 0 ] ) ; $ arrPages = $ this -> { $ callback [ 0 ] } -> { $ callback [ 1 ] } ( $ arrPages , $ objRoot -> id , true , $ objRoot -> language ) ; } } // Add pages foreach ( $ arrPages as $ strUrl ) { $ strUrl = explode ( '/' , $ strUrl , 4 ) ; if ( isset ( $ strUrl [ 3 ] ) ) { $ strUrl [ 3 ] = rawurlencode ( $ strUrl [ 3 ] ) ; $ strUrl [ 3 ] = str_replace ( array ( '%2F' , '%3F' , '%3D' , '%26' , '%5B' , '%5D' , '%25' ) , array ( '/' , '?' , '=' , '&' , '[' , ']' , '%' ) , $ strUrl [ 3 ] ) ; } $ strUrl = implode ( '/' , $ strUrl ) ; $ strUrl = ampersand ( $ strUrl , true ) ; $ objFile -> append ( '  <url><loc>' . $ strUrl . '</loc></url>' ) ; } $ objFile -> append ( '</urlset>' ) ; $ objFile -> close ( ) ; // Add a log entry $ this -> log ( 'Generated sitemap "' . $ objRoot -> sitemapName . '.xml"' , __METHOD__ , TL_CRON ) ; } }
Generate the Google XML sitemaps

@param integer $intId The root page ID
Generate the Google XML sitemaps