<?php
set_time_limit(0);
/*
101: Sitemap direcotry or sitemap file doesn't have write permissions
*/

class SitemapCommand extends CConsoleCommand {
	protected $urlCount = 50000;
	protected $sitemapDir;
	protected $sitemapFile;

	public function init() {
		$this->sitemapDir = Yii::app()->basePath . '/../sitemap/';
		$this->sitemapFile = Yii::app()->basePath. '/../sitemap.xml';
	}

	public function actionIndex() {
		$command = Yii::app() -> db -> createCommand();
		if(!is_writable($this->sitemapDir) OR !is_writable($this->sitemapFile)) {
			Yii::log("Make sure {$this->sitemapDir} and {$this->sitemapFile} is writeable", 'error', 'application.command.sitemap.index');
			return 101;
		}
		$total = $command
			-> select('count(*)')
			-> from ('{{webdata_main}}')
			-> queryScalar();

		$totalSitemaps = ceil($total / $this->urlCount);
		if(!$totalSitemaps) {
			return 0;
		}
		$this->sitemapIndex($totalSitemaps);
	}

	protected function sitemapIndex($count) {
		$xml = new DOMDocument('1.0', 'UTF-8');

		$sitemapIndex = $xml->createElement('sitemapindex');
		$attrNode = $xml->createAttribute('xmlns');
		$attrNode->value = 'http://www.sitemaps.org/schemas/sitemap/0.9';
		$sitemapIndex->appendChild($attrNode);

		$date = date('c', time());

		$this->cleanSitemapDirectory();

		for($i = 0; $i < $count; $i++) {
			$sitemap = $xml->createElement('sitemap');
			$url = Yii::app()->getRequest()->getHostInfo().Yii::app()->getRequest()->getBaseUrl()."/sitemap/sitemap$i.xml";

			$this->createSitemap($i);

			$sitemap->appendChild($xml->createElement('loc'))->appendChild($xml->createTextNode($url));
			$sitemap->appendChild($xml->createElement('lastmod'))->appendChild($xml->createTextNode($date));

			$sitemapIndex -> appendChild($sitemap);
		}

		$newNode = $xml->appendChild($sitemapIndex);
		file_put_contents(Yii::app() -> basePath . "/../sitemap.xml", $xml->saveXML(), LOCK_EX);
		return 0;
	}

	protected function createSitemap($i) {
		$xml = new DOMDocument('1.0', 'UTF-8');
		$urlset = $xml->createElement('urlset');
		$attrNode = $xml->createAttribute('xmlns');
		$attrNode->value = 'http://www.sitemaps.org/schemas/sitemap/0.9';
		$xml->appendChild($urlset);
		$urlset->appendChild($attrNode);

		$websites = Yii::app()->db->createCommand()
			->select('domain, modified_at')
			->from('{{webdata_main}}')
			->limit($this->urlCount)
			->offset($i * $this->urlCount)
			->queryAll();
		foreach($websites as $website) {
			$link = Yii::app()->getRequest()->getHostInfo() . Yii::app() -> urlManager -> createUrl("website/show", array(
				"domain" => CHtml::encode($website['domain']),
			));
			$lastmod = date('c', strtotime($website['modified_at']));
			$url = $xml->createElement('url');
			$url->appendChild($xml->createElement('loc'))->appendChild($xml->createTextNode($link));
			$url->appendChild($xml->createElement('lastmod'))->appendChild($xml->createTextNode($lastmod));
			$urlset->appendChild($url);
		}
		file_put_contents($this->sitemapDir."sitemap$i.xml", $xml->saveXML(), LOCK_EX);
	}

	protected function cleanSitemapDirectory() {
		if(!$files = glob($this->sitemapDir.'*')) {
			return true;
		}
		foreach($files as $file){
			if(is_file($file))
				unlink($file);
		}
	}
}