<?php
/*
 * Created on 11.02.2012
 * Oliver Soop(osoop1@ut.ee)
 */
 include_once("BaseCrawler.php");
 include_once("../model/RSSBaseTagger.php");
 include_once("../database/RSSDatabaseService.php");
 
 class MainCrawler extends BaseCrawler {
 	
 	
 	public function __construct($url, $channelId) {
 		$this->url = $url;
 		$this->channelId = $channelId;
 	}
 	
 	/**
 	 * Funktsioon käivitamaks RSS feed-i roomamist objekti konstrueerimisel kaasa antud URL-ilt
 	 */
 	public function crawl() {
 		$xml = parent::getWebPage($this->url);
 		$this->parse($xml);
 		RSSDatabaseService :: setCrawlDateToCurrent($this->url);
 	}
 	
 	/**
 	 * Funktsioon parsimaks <item> märgendiga objekti Story objektiks
 	 */
 	protected function parseItem($domElement, $nameSpaces, $rssTagger) {
 		
		$story = $rssTagger->getStory($domElement);
		
		
		foreach($nameSpaces as $tagger) {
			$story = $tagger->updateNewsStory($story, $domElement);
		}
 		
		return $story;
 	}
 	
 	/**
 	 * Funktsioon määramaks PHP skripi käivitusperioodi pikkust, mille vaikimisi väärtus määratakse
 	 * php.ini failis
 	 */
 	public function setTimeLimit($seconds) {
		set_time_limit($seconds);
	}
 	
 	/**
 	 * Funktsioon, mis töötleb kogu RSS-voo(XML) dokumenti
 	 */
 	protected function parse($xml){
 		$document = new DOMDocument();
 		@$document->loadXML($xml->getContent());
 		
 		if(!$this->isRSS($document)) {
 			echo "Pole RSS feed: " . $this->url;
 			return;
 		}
 		
 		if($this->channelId == null) {
 			$this->channelId = $this->getChannelId($document);
 		}
 		if($this->languageId == null) {
 			$this->languageId = $this->getChannelLanguage($document);
 		}
 		$nameSpaces = array();
 		
 		//Leia RSS versioon, mida kasutatakse
 		$rssTag = $document->getElementsByTagName(RSSBaseTagger::$rssTag);
 		if($rssTag == null) {
 			$rssTag = $document->getElementsByTagName(RSSBaseTagger::$rssv10Tag);
 			if($rssTag == null) {
 				return;
 			}
 			$rssAttribue = $rssTag;
 		} else {
 			$rssAttribute = $rssTag->item(0)->getAttributeNode(RSSBaseTagger::$version);
 		}
 		
 		$rssVersion = XMLBaseTagger::getRSSTagger($rssAttribute);
 		
 		//Leia kõik nimeruumid, mida dokumendis kasutatakse
 		$xpath = new DOMXPath($document);
 		$context = $document->documentElement;
		foreach( $xpath->query('namespace::*', $context) as $node ) {
		  	$val = XMLBaseTagger::getNamespaceTagger($node);
   			if($val != null) {
   				array_push($nameSpaces, $val);
   			}
		}
 		
 		$items = $document->getElementsByTagName(RSSBaseTagger::$itemTag);
 		
 		//Parsi iga uudisobjekt
 		foreach($items as $item) {
 			$story = $this->parseItem($item, $nameSpaces, $rssVersion);
 			$story->channelId($this->channelId);
 			$story->html($this->cleanHtml($xml->getContent()));
 			$story->fullArticle(0);
 			$story->language($this->languageId);
 			if(!RSSDatabaseService::checkIfStoryExists($story->title())) {
	 			RSSDatabaseService::insertNewArticle($story);
 			}
 		}
 	}
 	
 	/**
 	 * Funktsioon leidmaks vaatluse all oleva kanali identifikaatorit, milleks on kanali pealkiri
 	 */
 	private function getChannelId($document) {
 		$channel = $document->getElementsByTagName(RSSBaseTagger::$channelTag);
 		return $channel->item(0)->getElementsByTagName(RSSBaseTagger::$titleTag)->item(0)->nodeValue;
 	}
 	
 	/**
 	 * Funktsioon vaatluse all oleva kanali keele märgendi <language> väärtuse leidmiseks
 	 */
 	private function getChannelLanguage($document) {
 		$channel = $document->getElementsByTagName(RSSBaseTagger::$channelTag);
 		$lang = XMLBaseTagger::getTagValue($channel->item(0)->getElementsByTagName(RSSBaseTagger::$languageTag));
 		
 		if($lang == 'et') {
 			return 1;
 		} else if($lang == 'en') {
 			return 2;
 		} else {
 			return 0;
 		}
 	}
 	
 	/**
 	 * Funktsoon, mis eemaldab sisendiks antud tekstist HTML koodi
 	 */
 	private function cleanHtml($html){
		// Remove newlines
		$html = str_replace("\n", "", $html);
		$html = str_replace("\r", "", $html);
		$html = preg_replace("/(<.+?>)(\s+?)(<.+?>)/", "$1$3", $html);
		return $html;
	}
	
	/**
	 * Funktsioon, mis kontrollib, kas sisendiks antud dokument edastab RSS-voogu
	 */
	private function isRSS($document) {
		return $document->getElementsByTagName(RSSBaseTagger::$rssTag)->length > 0;
	}
 	
 }
?>
