2005-10-16 17:33:41 +00:00
|
|
|
<?php
|
2010-09-05 13:15:48 +00:00
|
|
|
/**
|
|
|
|
|
* Helper class for the --prefetch option of dumpTextPass.php
|
|
|
|
|
*
|
2010-12-16 19:15:12 +00:00
|
|
|
* Copyright © 2005 Brion Vibber <brion@pobox.com>
|
|
|
|
|
* http://www.mediawiki.org/
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2010-09-05 13:15:48 +00:00
|
|
|
* @file
|
2010-12-01 13:28:33 +00:00
|
|
|
* @ingroup Maintenance
|
2010-09-05 13:15:48 +00:00
|
|
|
*/
|
2005-10-16 17:33:41 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Readahead helper for making large MediaWiki data dumps;
|
|
|
|
|
* reads in a previous XML dump to sequentially prefetch text
|
|
|
|
|
* records already normalized and decompressed.
|
|
|
|
|
*
|
|
|
|
|
* This can save load on the external database servers, hopefully.
|
|
|
|
|
*
|
|
|
|
|
* Assumes that dumps will be recorded in the canonical order:
|
|
|
|
|
* - ascending by page_id
|
|
|
|
|
* - ascending by rev_id within each page
|
|
|
|
|
* - text contents are immutable and should not change once
|
|
|
|
|
* recorded, so the previous dump is a reliable source
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Maintenance
|
2005-10-16 17:33:41 +00:00
|
|
|
*/
|
|
|
|
|
class BaseDump {
|
|
|
|
|
var $reader = null;
|
|
|
|
|
var $atEnd = false;
|
2005-10-19 00:05:22 +00:00
|
|
|
var $atPageEnd = false;
|
2005-10-16 17:33:41 +00:00
|
|
|
var $lastPage = 0;
|
|
|
|
|
var $lastRev = 0;
|
2011-01-28 19:16:16 +00:00
|
|
|
var $infiles = null;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-10-16 17:33:41 +00:00
|
|
|
function BaseDump( $infile ) {
|
2011-01-28 19:16:16 +00:00
|
|
|
$this->infiles = explode(';',$infile);
|
2005-10-16 17:33:41 +00:00
|
|
|
$this->reader = new XMLReader();
|
2011-01-28 19:16:16 +00:00
|
|
|
$infile = array_shift($this->infiles);
|
2011-07-12 15:01:58 +00:00
|
|
|
if (defined( 'LIBXML_PARSEHUGE' ) ) {
|
|
|
|
|
$this->reader->open( $infile, null, LIBXML_PARSEHUGE );
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$this->reader->open( $infile );
|
|
|
|
|
}
|
2005-10-16 17:33:41 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-10-16 17:33:41 +00:00
|
|
|
/**
|
|
|
|
|
* Attempts to fetch the text of a particular page revision
|
|
|
|
|
* from the dump stream. May return null if the page is
|
|
|
|
|
* unavailable.
|
|
|
|
|
*
|
2010-04-26 19:31:55 +00:00
|
|
|
* @param $page Integer: ID number of page to read
|
|
|
|
|
* @param $rev Integer: ID number of revision to read
|
2005-10-16 17:33:41 +00:00
|
|
|
* @return string or null
|
|
|
|
|
*/
|
|
|
|
|
function prefetch( $page, $rev ) {
|
2005-10-19 00:05:22 +00:00
|
|
|
$page = intval( $page );
|
|
|
|
|
$rev = intval( $rev );
|
2010-05-22 16:50:39 +00:00
|
|
|
while ( $this->lastPage < $page && !$this->atEnd ) {
|
2005-10-19 00:05:22 +00:00
|
|
|
$this->debug( "BaseDump::prefetch at page $this->lastPage, looking for $page" );
|
2005-10-16 17:33:41 +00:00
|
|
|
$this->nextPage();
|
|
|
|
|
}
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $this->lastPage > $page || $this->atEnd ) {
|
2005-10-19 00:05:22 +00:00
|
|
|
$this->debug( "BaseDump::prefetch already past page $page looking for rev $rev [$this->lastPage, $this->lastRev]" );
|
2005-10-16 17:33:41 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2010-05-22 16:50:39 +00:00
|
|
|
while ( $this->lastRev < $rev && !$this->atEnd && !$this->atPageEnd ) {
|
2005-10-19 00:05:22 +00:00
|
|
|
$this->debug( "BaseDump::prefetch at page $this->lastPage, rev $this->lastRev, looking for $page, $rev" );
|
2005-10-16 17:33:41 +00:00
|
|
|
$this->nextRev();
|
|
|
|
|
}
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $this->lastRev == $rev && !$this->atEnd ) {
|
2005-10-19 00:05:22 +00:00
|
|
|
$this->debug( "BaseDump::prefetch hit on $page, $rev [$this->lastPage, $this->lastRev]" );
|
2005-10-16 17:33:41 +00:00
|
|
|
return $this->nextText();
|
|
|
|
|
} else {
|
2005-10-19 00:05:22 +00:00
|
|
|
$this->debug( "BaseDump::prefetch already past rev $rev on page $page [$this->lastPage, $this->lastRev]" );
|
2005-10-16 17:33:41 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-10-16 17:33:41 +00:00
|
|
|
function debug( $str ) {
|
2005-10-19 00:05:22 +00:00
|
|
|
wfDebug( $str . "\n" );
|
2010-05-22 16:50:39 +00:00
|
|
|
// global $dumper;
|
|
|
|
|
// $dumper->progress( $str );
|
2005-10-16 17:33:41 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-10-16 17:33:41 +00:00
|
|
|
/**
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function nextPage() {
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $this->skipTo( 'page', 'mediawiki' ) ) {
|
|
|
|
|
if ( $this->skipTo( 'id' ) ) {
|
2005-10-19 00:05:22 +00:00
|
|
|
$this->lastPage = intval( $this->nodeContents() );
|
|
|
|
|
$this->lastRev = 0;
|
|
|
|
|
$this->atPageEnd = false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2011-01-28 19:16:16 +00:00
|
|
|
$this->close();
|
|
|
|
|
if (count($this->infiles)) {
|
|
|
|
|
$infile = array_shift($this->infiles);
|
|
|
|
|
$this->reader->open( $infile );
|
|
|
|
|
$this->atEnd = false;
|
|
|
|
|
}
|
2005-10-19 00:05:22 +00:00
|
|
|
}
|
2005-10-16 17:33:41 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-10-16 17:33:41 +00:00
|
|
|
/**
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function nextRev() {
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $this->skipTo( 'revision' ) ) {
|
|
|
|
|
if ( $this->skipTo( 'id' ) ) {
|
2005-10-19 00:05:22 +00:00
|
|
|
$this->lastRev = intval( $this->nodeContents() );
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->atPageEnd = true;
|
|
|
|
|
}
|
2005-10-16 17:33:41 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-10-16 17:33:41 +00:00
|
|
|
/**
|
|
|
|
|
* @access private
|
2011-10-18 17:31:54 +00:00
|
|
|
* @return string
|
2005-10-16 17:33:41 +00:00
|
|
|
*/
|
|
|
|
|
function nextText() {
|
|
|
|
|
$this->skipTo( 'text' );
|
|
|
|
|
return strval( $this->nodeContents() );
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-10-16 17:33:41 +00:00
|
|
|
/**
|
|
|
|
|
* @access private
|
2011-10-18 17:31:54 +00:00
|
|
|
* @param $name string
|
|
|
|
|
* @param $parent string
|
|
|
|
|
* @return bool|null
|
2005-10-16 17:33:41 +00:00
|
|
|
*/
|
2010-05-22 16:50:39 +00:00
|
|
|
function skipTo( $name, $parent = 'page' ) {
|
|
|
|
|
if ( $this->atEnd ) {
|
2005-10-16 23:53:29 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2010-05-22 16:50:39 +00:00
|
|
|
while ( $this->reader->read() ) {
|
2010-10-03 08:14:38 +00:00
|
|
|
if ( $this->reader->nodeType == XMLReader::ELEMENT &&
|
2005-10-16 17:33:41 +00:00
|
|
|
$this->reader->name == $name ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2010-10-03 08:14:38 +00:00
|
|
|
if ( $this->reader->nodeType == XMLReader::END_ELEMENT &&
|
2005-10-19 00:05:22 +00:00
|
|
|
$this->reader->name == $parent ) {
|
|
|
|
|
$this->debug( "BaseDump::skipTo found </$parent> searching for <$name>" );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2005-10-16 17:33:41 +00:00
|
|
|
}
|
|
|
|
|
return $this->close();
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-10-16 17:33:41 +00:00
|
|
|
/**
|
|
|
|
|
* Shouldn't something like this be built-in to XMLReader?
|
|
|
|
|
* Fetches text contents of the current element, assuming
|
|
|
|
|
* no sub-elements or such scary things.
|
2010-04-26 19:31:55 +00:00
|
|
|
*
|
|
|
|
|
* @return String
|
2005-10-16 17:33:41 +00:00
|
|
|
* @access private
|
|
|
|
|
*/
|
|
|
|
|
function nodeContents() {
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $this->atEnd ) {
|
2005-10-19 00:05:22 +00:00
|
|
|
return null;
|
2005-10-16 23:53:29 +00:00
|
|
|
}
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $this->reader->isEmptyElement ) {
|
2005-10-16 17:33:41 +00:00
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
$buffer = "";
|
2010-05-22 16:50:39 +00:00
|
|
|
while ( $this->reader->read() ) {
|
2005-10-16 17:33:41 +00:00
|
|
|
switch( $this->reader->nodeType ) {
|
2010-10-03 08:14:38 +00:00
|
|
|
case XMLReader::TEXT:
|
|
|
|
|
// case XMLReader::WHITESPACE:
|
|
|
|
|
case XMLReader::SIGNIFICANT_WHITESPACE:
|
2005-10-16 17:33:41 +00:00
|
|
|
$buffer .= $this->reader->value;
|
|
|
|
|
break;
|
2010-10-03 08:14:38 +00:00
|
|
|
case XMLReader::END_ELEMENT:
|
2005-10-16 17:33:41 +00:00
|
|
|
return $buffer;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $this->close();
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-10-16 17:33:41 +00:00
|
|
|
/**
|
|
|
|
|
* @access private
|
2011-10-18 17:31:54 +00:00
|
|
|
* @return null
|
2005-10-16 17:33:41 +00:00
|
|
|
*/
|
|
|
|
|
function close() {
|
|
|
|
|
$this->reader->close();
|
|
|
|
|
$this->atEnd = true;
|
2005-10-19 00:05:22 +00:00
|
|
|
return null;
|
2005-10-16 17:33:41 +00:00
|
|
|
}
|
|
|
|
|
}
|