Make sure the TOC is there when toggleToc is called. Although the function calling...
[lhc/web/wiklou.git] / maintenance / backupPrefetch.inc
1 <?php
2 /**
3 * Helper class for the --prefetch option of dumpTextPass.php
4 *
5 * Copyright © 2005 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup Maintenance
25 */
26
27 /**
28 * Readahead helper for making large MediaWiki data dumps;
29 * reads in a previous XML dump to sequentially prefetch text
30 * records already normalized and decompressed.
31 *
32 * This can save load on the external database servers, hopefully.
33 *
34 * Assumes that dumps will be recorded in the canonical order:
35 * - ascending by page_id
36 * - ascending by rev_id within each page
37 * - text contents are immutable and should not change once
38 * recorded, so the previous dump is a reliable source
39 *
40 * @ingroup Maintenance
41 */
42 class BaseDump {
43 var $reader = null;
44 var $atEnd = false;
45 var $atPageEnd = false;
46 var $lastPage = 0;
47 var $lastRev = 0;
48
49 function BaseDump( $infile ) {
50 $this->reader = new XMLReader();
51 $this->reader->open( $infile );
52 }
53
54 /**
55 * Attempts to fetch the text of a particular page revision
56 * from the dump stream. May return null if the page is
57 * unavailable.
58 *
59 * @param $page Integer: ID number of page to read
60 * @param $rev Integer: ID number of revision to read
61 * @return string or null
62 */
63 function prefetch( $page, $rev ) {
64 $page = intval( $page );
65 $rev = intval( $rev );
66 while ( $this->lastPage < $page && !$this->atEnd ) {
67 $this->debug( "BaseDump::prefetch at page $this->lastPage, looking for $page" );
68 $this->nextPage();
69 }
70 if ( $this->lastPage > $page || $this->atEnd ) {
71 $this->debug( "BaseDump::prefetch already past page $page looking for rev $rev [$this->lastPage, $this->lastRev]" );
72 return null;
73 }
74 while ( $this->lastRev < $rev && !$this->atEnd && !$this->atPageEnd ) {
75 $this->debug( "BaseDump::prefetch at page $this->lastPage, rev $this->lastRev, looking for $page, $rev" );
76 $this->nextRev();
77 }
78 if ( $this->lastRev == $rev && !$this->atEnd ) {
79 $this->debug( "BaseDump::prefetch hit on $page, $rev [$this->lastPage, $this->lastRev]" );
80 return $this->nextText();
81 } else {
82 $this->debug( "BaseDump::prefetch already past rev $rev on page $page [$this->lastPage, $this->lastRev]" );
83 return null;
84 }
85 }
86
87 function debug( $str ) {
88 wfDebug( $str . "\n" );
89 // global $dumper;
90 // $dumper->progress( $str );
91 }
92
93 /**
94 * @access private
95 */
96 function nextPage() {
97 if ( $this->skipTo( 'page', 'mediawiki' ) ) {
98 if ( $this->skipTo( 'id' ) ) {
99 $this->lastPage = intval( $this->nodeContents() );
100 $this->lastRev = 0;
101 $this->atPageEnd = false;
102 }
103 } else {
104 $this->atEnd = true;
105 }
106 }
107
108 /**
109 * @access private
110 */
111 function nextRev() {
112 if ( $this->skipTo( 'revision' ) ) {
113 if ( $this->skipTo( 'id' ) ) {
114 $this->lastRev = intval( $this->nodeContents() );
115 }
116 } else {
117 $this->atPageEnd = true;
118 }
119 }
120
121 /**
122 * @access private
123 */
124 function nextText() {
125 $this->skipTo( 'text' );
126 return strval( $this->nodeContents() );
127 }
128
129 /**
130 * @access private
131 */
132 function skipTo( $name, $parent = 'page' ) {
133 if ( $this->atEnd ) {
134 return false;
135 }
136 while ( $this->reader->read() ) {
137 if ( $this->reader->nodeType == XMLReader::ELEMENT &&
138 $this->reader->name == $name ) {
139 return true;
140 }
141 if ( $this->reader->nodeType == XMLReader::END_ELEMENT &&
142 $this->reader->name == $parent ) {
143 $this->debug( "BaseDump::skipTo found </$parent> searching for <$name>" );
144 return false;
145 }
146 }
147 return $this->close();
148 }
149
150 /**
151 * Shouldn't something like this be built-in to XMLReader?
152 * Fetches text contents of the current element, assuming
153 * no sub-elements or such scary things.
154 *
155 * @return String
156 * @access private
157 */
158 function nodeContents() {
159 if ( $this->atEnd ) {
160 return null;
161 }
162 if ( $this->reader->isEmptyElement ) {
163 return "";
164 }
165 $buffer = "";
166 while ( $this->reader->read() ) {
167 switch( $this->reader->nodeType ) {
168 case XMLReader::TEXT:
169 // case XMLReader::WHITESPACE:
170 case XMLReader::SIGNIFICANT_WHITESPACE:
171 $buffer .= $this->reader->value;
172 break;
173 case XMLReader::END_ELEMENT:
174 return $buffer;
175 }
176 }
177 return $this->close();
178 }
179
180 /**
181 * @access private
182 */
183 function close() {
184 $this->reader->close();
185 $this->atEnd = true;
186 return null;
187 }
188 }