DumpXmlReader::nextEntityPage()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
nc 2
nop 0
1
<?php
2
3
namespace Queryr\DumpReader\XmlReader;
4
5
use Queryr\DumpReader\DumpReader;
6
use Queryr\DumpReader\DumpReaderException;
7
use Queryr\DumpReader\Page;
8
use XMLReader;
9
10
/**
11
 * @licence GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 */
14
class DumpXmlReader extends DumpReader {
15
16
	/**
17
	 * @var XMLReader
18
	 */
19
	private $xmlReader;
20
21
	/**
22
	 * @var string
23
	 */
24
	private $dumpFile;
25
26
	public function __construct( $dumpFile ) {
27
		$this->dumpFile = $dumpFile;
28
29
		$this->initReader();
30
	}
31
32
	private function initReader() {
33
		$this->xmlReader = new XMLReader();
34
		$this->xmlReader->open( $this->dumpFile );
35
	}
36
37
	public function __destruct() {
38
		$this->closeReader();
39
	}
40
41
	private function closeReader() {
42
		$this->xmlReader->close();
43
	}
44
45
	/**
46
	 * @see DumpReader::rewind
47
	 */
48
	public function rewind() {
49
		$this->closeReader();
50
		$this->initReader();
51
	}
52
53
	/**
54
	 * @see DumpReader::nextEntityPage
55
	 *
56
	 * @return Page|null
57
	 * @throws DumpReaderException
58
	 */
59
	public function nextEntityPage() {
60
		do {
61
			$page = $this->nextPage();
62
63
			if ( $page === null ) {
64
				return null;
65
			}
66
		} while ( !$page->getRevision()->hasEntityModel() );
67
68
		return $page;
69
	}
70
71
	/**
72
	 * @return Page|null
73
	 */
74
	private function nextPage() {
75
		while ( !$this->isPageNode() ) {
76
			$this->xmlReader->read();
77
78
			if ( $this->xmlReader->nodeType === XMLReader::NONE ) {
79
				return null;
80
			}
81
		}
82
83
		$pageNode = new PageNode( $this->xmlReader->expand() );
84
85
		$page = $pageNode->asPage();
86
		$this->xmlReader->next();
87
		return $page;
88
	}
89
90
	private function isPageNode() {
91
		return $this->xmlReader->nodeType === XMLReader::ELEMENT && $this->xmlReader->name === 'page';
92
	}
93
94
}
95