Location: PHPKode > projects > Simple Way to Usenet > swun/libs/swun/search/newzbin/nb_rssv2.class.php
<?php
/**
 *  
 * @author  Benjamin Gillissen <hide@address.com>
 * 
 *	**************************************************************

	Copyright (C) 2009  Benjamin Gillissen
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details at:
	http://www.gnu.org/copyleft/gpl.html

 *	**************************************************************
 */

class nb_rssv2 {
	
	const item_depth = 2;
	private $file;
	private $XML;
	
	public function __construct($file){
		if ( !chrono::isinit('nb_rss') ){ chrono::init('nb_rss'); }
		$chr = chrono::start('nb_rss');
		if ( ! CORE::isphpModLoaded('XMLReader') ){
			if ( ! CORE::LoadphpMod('XMLReader') ){ 
				errors::raise('NewzBin.com RSS Parser (nb_rssv2) require PHP XMLReader extension, please contact your system administrator.', CORE_LOG_ALERT, 'NB_FETCH');
			}
		}
		$this->file = $file;
		unset($file);
		$this->XML = new XMLReader();
		if ( FALSE === $this->XML->open( $this->file ) ){
			errors::raise( "XML RSS file : $file could not be loaded", CORE_LOG_ERROR, 'NB');
			unset($this->XML); 
			chrono::pause('nb_rss', $chr);
			return; 
		}
		//$this->XML->setRelaxNGSchema(); 
		$this->XML->setParserProperty(XMLReader::LOADDTD,  FALSE);
		$this->XML->setParserProperty(XMLReader::VALIDATE, FALSE);
		chrono::pause('nb_rss', $chr);		
	}
		
	public function _destruct(){
		if ( !isset($this->_XML) ){ return; }
		$this->XML->close();
		unset($this->XML, $this->file, $this);
	}
	
	public function fetch_items($debug=FALSE){
		if ( !isset($this->XML) ){ return FALSE; }
		$chr = chrono::start('nb_rss');
		while ( $this->XML->read() ){
			if ( $this->XML->name == 'item'  && $this->XML->depth == self::item_depth ){
				$r = $this->parse_report();
				chrono::pause('nb_rss', $chr);
				return $r;
			}
		}
		chrono::pause('nb_rss', $chr);
		return FALSE;
	}
	
	private function parse_report(){
		$needed = Array('title', 'report:id', 'report:category', 'report:attributes', 'report:groups',
						'report:progress', 'report:moreinfo', 'report:nfo','report:size', 'report:postdate');
		while ( $this->XML->read() ){
			if ( $this->XML->depth == self::item_depth ){ break; }
			if ( $this->XML->nodeType == 1 OR $this->XML->nodeType == 3 ){ 
				$k = array_search($this->XML->name, $needed);
				if ( FALSE !==  $k ){
					switch($k){
						case 3	:	$o['attributes'] = $this->parse_attribs();		
									break;	//attribs
						case 4	:	$o['groups'] = $this->parse_groups();
									break;	//groups
						case 7	:	$o['nfoid'] = $this->parse_nfo();
									break;	//nfo
						default :	$k = str_replace('report:', '', $this->XML->name);
									$this->XML->read();
									$o[$k] = $this->XML->value;
									break;  
					}
				}
			}
		}
		if ( isset($o) ){ return $o; }
		return FALSE;
	}
	
	private function parse_attribs(){
		$depth = $this->XML->depth;
		while ( $this->XML->read() ){
			if ( $this->XML->depth == $depth ){ break; }
			if ( $this->XML->nodeType == 1 OR $this->XML->nodeType == 3 ){ 
				if ( $this->XML->name == 'report:attribute' ){ 
					$k = $this->XML->getAttribute('type');
					$this->XML->read();
					$o[$k][] = $this->XML->value;
				}
			}
		}
		if ( isset($o) ){ return $o; }
		return Array();
	}
	
	private function parse_groups(){
		$depth = $this->XML->depth;
		while ( $this->XML->read() ){
			if ( $this->XML->depth == $depth ){ break; }
			if ( $this->XML->nodeType == 1 OR $this->XML->nodeType == 3 ){ 
				if ( $this->XML->name == 'report:group' ){ 
					$this->XML->read();
					$o[] = $this->XML->value;
				}
			}
		}
		if ( isset($o) ){ return $o; }
		return Array();
	}

	private function parse_nfo(){
		$depth = $this->XML->depth;
		while ( $this->XML->read() ){
			if ( $this->XML->depth == $depth ){ break; }
			if ( $this->XML->nodeType == 1 OR $this->XML->nodeType == 3 ){ 
				if ( $this->XML->name == 'report:fileid' ){ 
					$this->XML->read();
					return $this->XML->value;
				}
			}
		}
		return '';
	}
	
	
}
Return current item: Simple Way to Usenet