<commit_before><?php

/**
 * This file is part of the pimcore-sitemap-plugin package.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Byng\Pimcore\Sitemap\Generator;

use Byng\Pimcore\Sitemap\Gateway\DocumentGateway;
use Byng\Pimcore\Sitemap\Notifier\GoogleNotifier;
use Byng\Pimcore\Sitemap\SitemapPlugin;
use Pimcore\Config;
use Pimcore\Model\Document;
use Pimcore\Model\Site;
use SimpleXMLElement;

/**
 * Sitemap Generator
 *
 * @author <NAME> <<EMAIL>>
 */
final class SitemapGenerator
{
    /**
     * @var string
     */
    private $hostUrl;

    /**
     * @var SimpleXMLElement
     */
    private $xml;

    /**
     * @var DocumentGateway
     */
    private $documentGateway;

    /**
     * @var array
     */
    private $sitesRoots = array();


    /**
     * SitemapGenerator constructor.
     */
    public function __construct()
    {
        $this->documentGateway = new DocumentGateway();
    }

    /**
     * Generates the sitemap.xml file
     *
     * @return void
     */
    public function generateXml()
    {
        // Retrieve site trees
        $siteRoots = new Site\Listing();
        $siteRoots = $siteRoots->load();

        // Build siteRoots table: [ ID => Domain ]
        /* @var Site $siteRoot */
        foreach ($siteRoots as $siteRoot){
            $this->sitesRoots[$siteRoot->getRootId()] = $siteRoot->getMainDomain();
        }

        // Also append the default tree
        $this->sitesRoots[1] = Config::getSystemConfig()->get("general")->get("domain");

        foreach($this->sitesRoots as $siteRootID => $siteRootDomain){
            $this->generateSiteXml($siteRootID, $siteRootDomain);
        }

        if (Config::getSystemConfig()->get("general")->get("environment") === "production") {
            $this->notifySearchEngines();
        }
    }

    /**
     * Generate a sitemap xml file for a defined site, with a specific hostUrl
     *
     * @param $rootId
     * @param $hostUrl
     * @return string
     */
    private function generateSiteXml($rootId, $hostUrl)
    {
        $this->xml = new SimpleXMLElement(<commit_msg>Add todo concerning notifying Google for each subsite sitexml<commit_after><?php

/**
 * This file is part of the pimcore-sitemap-plugin package.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Byng\Pimcore\Sitemap\Generator;

use Byng\Pimcore\Sitemap\Gateway\DocumentGateway;
use Byng\Pimcore\Sitemap\Notifier\GoogleNotifier;
use Byng\Pimcore\Sitemap\SitemapPlugin;
use Pimcore\Config;
use Pimcore\Model\Document;
use Pimcore\Model\Site;
use SimpleXMLElement;

/**
 * Sitemap Generator
 *
 * @author <NAME> <<EMAIL>>
 */
final class SitemapGenerator
{
    /**
     * @var string
     */
    private $hostUrl;

    /**
     * @var SimpleXMLElement
     */
    private $xml;

    /**
     * @var DocumentGateway
     */
    private $documentGateway;

    /**
     * @var array
     */
    private $sitesRoots = array();


    /**
     * SitemapGenerator constructor.
     */
    public function __construct()
    {
        $this->documentGateway = new DocumentGateway();
    }

    /**
     * Generates the sitemap.xml file
     *
     * @return void
     */
    public function generateXml()
    {
        // Retrieve site trees
        $siteRoots = new Site\Listing();
        $siteRoots = $siteRoots->load();

        // Build siteRoots table: [ ID => Domain ]
        /* @var Site $siteRoot */
        foreach ($siteRoots as $siteRoot){
            $this->sitesRoots[$siteRoot->getRootId()] = $siteRoot->getMainDomain();
        }

        // Also append the default tree
        $this->sitesRoots[1] = Config::getSystemConfig()->get("general")->get("domain");

        foreach($this->sitesRoots as $siteRootID => $siteRootDomain){
            $this->generateSiteXml($siteRootID, $siteRootDomain);
        }

        // @ToDo: should we also notify Google for each subsites sitemaps?
        if (Config::getSystemConfig()->get("general")->get("environment") === "production") {
            $this->notifySearchEngines();
        }
    }

    /**
     * Generate a sitemap xml file for a defined site, with a specific hostUrl
     *
     * @param $rootId
     * @param $hostUrl
     * @return string
     */
    private function generateSiteXml($rootId, $hostUrl)
    {
        $this->xml = new SimpleXMLElement(
{'fraction_non_alphanumeric': 0.11797862001943635, 'fraction_numerical': 0.00038872691933916425, 'mean_word_length': 2.608695652173913, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 9}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4437550', 'n_tokens_mistral': 1510, 'n_tokens_neox': 1492, 'n_words': 413}