MediaWiki REL1_40
BaseDump.php
Go to the documentation of this file.
1<?php
28
44class BaseDump {
46 protected $reader = null;
48 protected $atEnd = false;
50 protected $atPageEnd = false;
52 protected $lastPage = 0;
54 protected $lastRev = 0;
56 protected $infiles = null;
57
61 public function __construct( $infile ) {
62 $this->infiles = explode( ';', $infile );
63 $this->reader = new XMLReader();
64 $infile = array_shift( $this->infiles );
65 if ( !$this->reader->open( $infile, null, LIBXML_PARSEHUGE ) ) {
66 $this->debug( __METHOD__ . ' was unable to open xml' );
67 $this->atEnd = true;
68 }
69 }
70
81 public function prefetch( $page, $rev, $slot = SlotRecord::MAIN ) {
82 $page = intval( $page );
83 $rev = intval( $rev );
84 while ( $this->lastPage < $page && !$this->atEnd ) {
85 $this->debug( "BaseDump::prefetch at page $this->lastPage, looking for $page" );
86 $this->nextPage();
87 }
88 if ( $this->lastPage > $page || $this->atEnd ) {
89 $this->debug( "BaseDump::prefetch already past page $page or failed to open/read input file, "
90 . "looking for rev $rev [$this->lastPage, $this->lastRev]" );
91
92 return null;
93 }
94 while ( $this->lastRev < $rev && !$this->atEnd && !$this->atPageEnd ) {
95 $this->debug( "BaseDump::prefetch at page $this->lastPage, rev $this->lastRev, "
96 . "looking for $page, $rev" );
97 $this->nextRev();
98 }
99 if ( $this->lastRev == $rev && !$this->atEnd ) {
100 $this->debug( "BaseDump::prefetch hit on $page, $rev [$this->lastPage, $this->lastRev]" );
101
102 if ( $slot !== SlotRecord::MAIN ) {
103 $lastSlot = SlotRecord::MAIN;
104 while ( $lastSlot !== $slot ) {
105 if ( !$this->skipTo( 'content', 'revision' ) ) {
106 return null;
107 }
108 if ( !$this->skipTo( 'role', 'revision' ) ) {
109 return null;
110 }
111 $lastSlot = $this->nodeContents();
112 }
113 }
114
115 return $this->nextText();
116 } else {
117 $this->debug( "BaseDump::prefetch already past rev $rev on page $page "
118 . "[$this->lastPage, $this->lastRev]" );
119
120 return null;
121 }
122 }
123
127 protected function debug( $str ) {
128 wfDebug( $str );
129 // global $dumper;
130 // $dumper->progress( $str );
131 }
132
133 private function nextPage() {
134 if ( $this->skipTo( 'page', 'mediawiki' ) ) {
135 if ( $this->skipTo( 'id' ) ) {
136 $this->lastPage = intval( $this->nodeContents() );
137 $this->lastRev = 0;
138 $this->atPageEnd = false;
139 }
140 } else {
141 $this->close();
142 if ( count( $this->infiles ) ) {
143 $infile = array_shift( $this->infiles );
144 if ( !$this->reader->open( $infile, null, LIBXML_PARSEHUGE ) ) {
145 $this->debug( __METHOD__ . ' was unable to open xml' );
146 $this->atEnd = true;
147 } else {
148 $this->atEnd = false;
149 }
150 }
151 }
152 }
153
154 private function nextRev() {
155 if ( $this->skipTo( 'revision' ) ) {
156 if ( $this->skipTo( 'id' ) ) {
157 $this->lastRev = intval( $this->nodeContents() );
158 }
159 } else {
160 $this->atPageEnd = true;
161 }
162 }
163
167 private function nextText() {
168 if ( !$this->skipTo( 'text', 'revision' ) ) {
169 return null;
170 }
171
172 return strval( $this->nodeContents() );
173 }
174
180 private function skipTo( $name, $parent = 'page' ) {
181 if ( $this->atEnd ) {
182 return false;
183 }
184 while ( $this->reader->read() ) {
185 if ( $this->reader->nodeType == XMLReader::ELEMENT
186 && $this->reader->name == $name
187 ) {
188 return true;
189 }
190 if ( $this->reader->nodeType == XMLReader::END_ELEMENT
191 && $this->reader->name == $parent
192 ) {
193 $this->debug( "BaseDump::skipTo found </$parent> searching for <$name>" );
194
195 return false;
196 }
197 }
198
199 return $this->close();
200 }
201
209 private function nodeContents() {
210 if ( $this->atEnd ) {
211 return null;
212 }
213 if ( $this->reader->isEmptyElement ) {
214 return "";
215 }
216 $buffer = "";
217 while ( $this->reader->read() ) {
218 switch ( $this->reader->nodeType ) {
219 case XMLReader::TEXT:
220 // case XMLReader::WHITESPACE:
221 case XMLReader::SIGNIFICANT_WHITESPACE:
222 $buffer .= $this->reader->value;
223 break;
224 case XMLReader::END_ELEMENT:
225 return $buffer;
226 }
227 }
228
229 return $this->close();
230 }
231
235 public function close() {
236 $this->reader->close();
237 $this->atEnd = true;
238
239 return null;
240 }
241}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Readahead helper for making large MediaWiki data dumps; reads in a previous XML dump to sequentially ...
Definition BaseDump.php:44
bool $atPageEnd
Definition BaseDump.php:50
__construct( $infile)
Definition BaseDump.php:61
XMLReader null $reader
Definition BaseDump.php:46
string[] null $infiles
Definition BaseDump.php:56
debug( $str)
Definition BaseDump.php:127
int $lastRev
Definition BaseDump.php:54
int $lastPage
Definition BaseDump.php:52
prefetch( $page, $rev, $slot=SlotRecord::MAIN)
Attempts to fetch the text of a particular page revision from the dump stream.
Definition BaseDump.php:81
bool $atEnd
Definition BaseDump.php:48
Value object representing a content slot associated with a page revision.