summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Importer/ContentCreators/XmlContentCreator.php
blob: 5785acc563a3646676e4e0492fa568fda2cc8c33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php

namespace SMW\Importer\ContentCreators;

use Onoi\MessageReporter\MessageReporter;
use SMW\Importer\ContentCreator;
use SMW\Importer\ImportContents;
use SMW\Services\ImporterServiceFactory;

/**
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class XmlContentCreator implements ContentCreator {

	/**
	 * @var ImportContentsIterator
	 */
	private $importerServiceFactory;

	/**
	 * @var MessageReporter
	 */
	private $messageReporter;

	/**
	 * @since 3.0
	 *
	 * @param ImporterServiceFactory $importerServiceFactory
	 */
	public function __construct( ImporterServiceFactory $importerServiceFactory ) {
		$this->importerServiceFactory = $importerServiceFactory;
	}

	/**
	 * @see MessageReporterAware::setMessageReporter
	 *
	 * @since 3.0
	 *
	 * @param MessageReporter $messageReporter
	 */
	public function setMessageReporter( MessageReporter $messageReporter ) {
		$this->messageReporter = $messageReporter;
	}

	/**
	 * @since 3.0
	 *
	 * @param ImportContents $importContents
	 */
	public function canCreateContentsFor( ImportContents $importContents ) {
		return $importContents->getContentType() === ImportContents::CONTENT_XML;
	}

	/**
	 * @since 3.0
	 *
	 * @param ImportContents $importContents
	 */
	public function create( ImportContents $importContents ) {

		$indent = '   ...';

		if ( $importContents->getOption( 'skip' ) === true || $importContents->getContentsFile() === '' ) {
			return $this->messageReporter->reportMessage( "\n   " . $importContents->getDescription() . " was skipped.\n" );
		}

		$importSource = $this->importerServiceFactory->newImportStreamSource(
			@fopen( $importContents->getContentsFile(), 'rt' )
		);

		$importer = $this->importerServiceFactory->newWikiImporter(
			$importSource
		);

		$importer->setDebug( false );
		$importer->setPageOutCallback( [ $this, 'reportPage' ] );

		if ( $importContents->getDescription() !== '' ) {
			$this->messageReporter->reportMessage( "\n   " . $importContents->getDescription() . "\n" );
		}

		try {
			$importer->doImport();
		} catch ( \Exception $e ) {
			$this->messageReporter->reportMessage( "Failed with " . $e->getMessage() );
		}

		$this->messageReporter->reportMessage( "$indent done.\n" );
	}

	/**
	 * @see WikiImporter::handlePage
	 *
	 * @param Title $title
	 * @param ForeignTitle $foreignTitle
	 * @param int $revisionCount
	 * @param int $successCount
	 * @param array $pageInfo
	 */
	public function reportPage( $title, $foreignTitle, $revisionCount, $successCount, $pageInfo ) {

		$indent = '   ...';

		// Invalid or non-importable title
		if ( $title === null ) {
			return;
		}

		$title->invalidateCache();

		if ( $successCount > 0 ) {
			$this->messageReporter->reportMessage( "$indent importing " . $title->getPrefixedText() . "\n" );
		} else {
			$this->messageReporter->reportMessage( "$indent skipping " . $title->getPrefixedText() . ", no new revision\n" );
		}
	}

}