-define( 'MW_EXPORT_BUFFER', 0 );
-define( 'MW_EXPORT_STREAM', 1 );
-
-/**
- * @package MediaWiki
- * @subpackage SpecialPage
- */
-class WikiExporter {
- var $pageCallback = null;
- var $revCallback = null;
-
- /**
- * If using MW_EXPORT_STREAM to stream a large amount of data,
- * provide a database connection which is not managed by
- * LoadBalancer to read from: some history blob types will
- * make additional queries to pull source data while the
- * main query is still running.
- *
- * @param Database $db
- * @param int $history one of MW_EXPORT_FULL or MW_EXPORT_CURRENT
- * @param int $buffer one of MW_EXPORT_BUFFER or MW_EXPORT_STREAM
- */
- function WikiExporter( &$db, $history = MW_EXPORT_CURRENT,
- $buffer = MW_EXPORT_BUFFER ) {
- $this->db =& $db;
- $this->history = $history;
- $this->buffer = $buffer;
- }
-
- /**
- * Set a callback to be called after each page in the output
- * stream is closed. The callback will be passed a database row
- * object with the last revision output.
- *
- * A set callback can be removed by passing null here.
- *
- * @param mixed $callback
- */
- function setPageCallback( $callback ) {
- $this->pageCallback = $callback;
- }
-
- /**
- * Set a callback to be called after each revision in the output
- * stream is closed. The callback will be passed a database row
- * object with the revision data.
- *
- * A set callback can be removed by passing null here.
- *
- * @param mixed $callback
- */
- function setRevisionCallback( $callback ) {
- $this->revCallback = $callback;
- }
-
- /**
- * Returns the export schema version.
- * @return string
- */
- function schemaVersion() {
- return "0.3";
- }
-
- /**
- * Opens the XML output stream's root <mediawiki> element.
- * This does not include an xml directive, so is safe to include
- * as a subelement in a larger XML stream. Namespace and XML Schema
- * references are included.
- *
- * To capture the stream to a string, use PHP's output buffering
- * functions. Output will be encoded in UTF-8.
- */
- function openStream() {
- global $wgContLanguageCode;
- $ver = $this->schemaVersion();
- print wfElement( 'mediawiki', array(
- 'xmlns' => "http://www.mediawiki.org/xml/export-$ver/",
- 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
- 'xsi:schemaLocation' => "http://www.mediawiki.org/xml/export-$ver/ " .
- "http://www.mediawiki.org/xml/export-$ver.xsd",
- 'version' => $ver,
- 'xml:lang' => $wgContLanguageCode ),
- null ) . "\n";
- $this->siteInfo();
- }
-
- function siteInfo() {
- $info = array(
- $this->sitename(),
- $this->homelink(),
- $this->generator(),
- $this->caseSetting(),
- $this->namespaces() );
- print "<siteinfo>\n";
- foreach( $info as $item ) {
- print " $item\n";
- }
- print "</siteinfo>\n";
- }
-
- function sitename() {
- global $wgSitename;
- return wfElement( 'sitename', array(), $wgSitename );
- }
-
- function generator() {
- global $wgVersion;
- return wfElement( 'generator', array(), "MediaWiki $wgVersion" );
- }
-
- function homelink() {
- $page = Title::newFromText( wfMsgForContent( 'mainpage' ) );
- return wfElement( 'base', array(), $page->getFullUrl() );
- }
-
- function caseSetting() {
- global $wgCapitalLinks;
- // "case-insensitive" option is reserved for future
- $sensitivity = $wgCapitalLinks ? 'first-letter' : 'case-sensitive';
- return wfElement( 'case', array(), $sensitivity );
- }
-
- function namespaces() {
- global $wgContLang;
- $spaces = "<namespaces>\n";
- foreach( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
- $spaces .= ' ' . wfElement( 'namespace', array( 'key' => $ns ), $title ) . "\n";
- }
- $spaces .= " </namespaces>";
- return $spaces;
- }
-
- /**
- * Closes the output stream with the closing root element.
- * Call when finished dumping things.
- */
- function closeStream() {
- print "</mediawiki>\n";
- }
-
- /**
- * Dumps a series of page and revision records for all pages
- * in the database, either including complete history or only
- * the most recent version.
- *
- *
- * @param Database $db
- */
- function allPages() {
- return $this->dumpFrom( '' );
- }
-
- /**
- * @param Title $title
- */
- function pageByTitle( $title ) {
- return $this->dumpFrom(
- 'page_namespace=' . $title->getNamespace() .
- ' AND page_title=' . $this->db->addQuotes( $title->getDbKey() ) );
- }
-
- function pageByName( $name ) {
- $title = Title::newFromText( $name );
- if( is_null( $title ) ) {
- return new WikiError( "Can't export invalid title" );
- } else {
- return $this->pageByTitle( $title );
- }
- }
-
- function pagesByName( $names ) {
- foreach( $names as $name ) {
- $this->pageByName( $name );