Generator.php 2.65 KB
Newer Older
Bostjan Skufca committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
<?php



namespace Teon\Sitemap\Generator\BruteForce;



use Teon\Sitemap\Generator\BruteForce\Config;
use Symfony\Component\Yaml\Parser as YamlParser;

# use Symfony\Component\Console\Output\OutputInterface;



class Generator
{



    /**
     * Set console output object to use
     *
     *
     */
    public function __construct ()
    {
    }



    /**
     * GENERATE: txt variant
     *
     *
     */
    public function generateTxt ()
    {
        $baseUri   = $this->getBaseUri();
        $transData = $this->getTransData();

        foreach ($transData as $pageData) {
            if (200 == $pageData["statusCode"]) {
                echo $baseUri . $pageData['relativeUri'] ."\n";
            }
        }
    }



    /**
     * GENERATE: xml variant
     *
     *
     */
    public function generateXml ()
    {
        $baseUri   = $this->getBaseUri();
        $transData = $this->getTransData();

        $xmlDoc    = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>');
        $xmlUrlset = $xmlDoc;

        foreach ($transData as $pageData) {
            if (200 == $pageData["statusCode"]) {
                $xmlUrl = $xmlUrlset->addChild('url');
                $xmlUrl->addChild("loc",        htmlentities($baseUri . $pageData['relativeUri']));
                $xmlUrl->addChild("changefreq", $pageData['changeFreq']);
            }
        }

        // This additional step using Dom
        $dom = dom_import_simplexml($xmlDoc)->ownerDocument;
        $dom->formatOutput = true;
        return $dom->saveXML();

/*
        $xmlDoc = '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
';

        foreach ($transData['sitemap'] as $pageData) {
            if (200 == $pageData["statusCode"]) {
                $xmlDoc .= "
    <url>
        <loc>". $baseUri . $pageData['relativeUri'] ."</loc>
        <changefreq>". $pageData['changeFreq'] ."</changefreq>
    </url>
";
            }
        }
        $xmlDoc .= "</urlset>\n";

        return $xmlDoc;
*/
    }



    /**
     * GET: baseUri
     */
    public function getBaseUri ()
    {
        $baseUri = Config::get('baseUri');
        if (preg_match('#/$#', $baseUri)) {
            $baseUri = substr($baseUri, 0, -1);
        }
        return $baseUri;
    }



    /**
     * GET: transData
     */
    public function getTransData ()
    {
        $transDataFile = Config::get('transDataFile');
        $YamlParser    = new YamlParser();
        $transDataYaml = file_get_contents($transDataFile);
        $transData     = $YamlParser->parse($transDataYaml);
        return $transData;
    }
}