1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\Importer\ContentCreators; |
4
|
|
|
|
5
|
|
|
use Onoi\MessageReporter\MessageReporter; |
6
|
|
|
use SMW\MediaWiki\PageCreator; |
7
|
|
|
use SMW\Importer\ImportContents; |
8
|
|
|
use SMW\Importer\ContentCreator; |
9
|
|
|
use SMW\MediaWiki\Database; |
10
|
|
|
use ContentHandler; |
11
|
|
|
use Title; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @license GNU GPL v2+ |
15
|
|
|
* @since 2.5 |
16
|
|
|
* |
17
|
|
|
* @author mwjames |
18
|
|
|
*/ |
19
|
|
|
class TextContentCreator implements ContentCreator { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var MessageReporter |
23
|
|
|
*/ |
24
|
|
|
private $messageReporter; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var PageCreator |
28
|
|
|
*/ |
29
|
|
|
private $pageCreator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Database |
33
|
|
|
*/ |
34
|
|
|
private $connection; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @since 2.5 |
38
|
|
|
* |
39
|
|
|
* @param PageCreator $pageCreator |
40
|
|
|
* @param Database $connection |
41
|
|
|
*/ |
42
|
|
|
public function __construct( PageCreator $pageCreator, Database $connection ) { |
43
|
|
|
$this->pageCreator = $pageCreator; |
44
|
|
|
$this->connection = $connection; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @see MessageReporterAware::setMessageReporter |
49
|
|
|
* |
50
|
|
|
* @since 2.5 |
51
|
|
|
* |
52
|
|
|
* @param MessageReporter $messageReporter |
53
|
|
|
*/ |
54
|
|
|
public function setMessageReporter( MessageReporter $messageReporter ) { |
55
|
|
|
$this->messageReporter = $messageReporter; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @since 2.5 |
60
|
|
|
* |
61
|
|
|
* @param ImportContents $importContents |
62
|
|
|
*/ |
63
|
|
|
public function canCreateContentsFor( ImportContents $importContents ) { |
64
|
|
|
return $importContents->getContentType() === ImportContents::CONTENT_TEXT; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @since 2.5 |
69
|
|
|
* |
70
|
|
|
* @param ImportContents $importContents |
71
|
|
|
*/ |
72
|
|
|
public function doCreateFrom( ImportContents $importContents ) { |
73
|
|
|
|
74
|
|
|
if ( !class_exists( 'ContentHandler' ) ) { |
75
|
|
|
return $this->messageReporter->reportMessage( "\nContentHandler doesn't exist therefore importing is not possible.\n" ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$indent = ' ...'; |
79
|
|
|
$name = $importContents->getName(); |
80
|
|
|
|
81
|
|
|
if ( $name === '' ) { |
82
|
|
|
return $this->messageReporter->reportMessage( "$indent no valid page name, abort import." ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$title = Title::newFromText( |
86
|
|
|
$name, |
87
|
|
|
$importContents->getNamespace() |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
if ( $title === null ) { |
91
|
|
|
return $this->messageReporter->reportMessage( "$indent $name returned with a null title, abort import." ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$prefixedText = $title->getPrefixedText(); |
95
|
|
|
|
96
|
|
|
if ( $title->exists() && !$importContents->getOption( 'canReplace' ) && !$importContents->getOption( 'replaceable' ) ) { |
97
|
|
|
return $this->messageReporter->reportMessage( "$indent skipping $prefixedText, already exists ...\n" ); |
98
|
|
|
} elseif( $title->exists() ) { |
99
|
|
|
$this->messageReporter->reportMessage( "$indent replacing $prefixedText contents ...\n" ); |
100
|
|
|
} else { |
101
|
|
|
$this->messageReporter->reportMessage( "$indent creating $prefixedText contents ...\n" ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Avoid a possible "Notice: WikiPage::doEditContent: Transaction already |
105
|
|
|
// in progress (from DatabaseUpdater::doUpdates), performing implicit |
106
|
|
|
// commit ..." |
107
|
|
|
$this->connection->onTransactionIdle( function() use ( $title, $importContents ) { |
108
|
|
|
$this->doCreateContent( $title, $importContents ); |
109
|
|
|
} ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function doCreateContent( $title, $importContents ) { |
113
|
|
|
|
114
|
|
|
$page = $this->pageCreator->createPage( $title ); |
115
|
|
|
|
116
|
|
|
$content = ContentHandler::makeContent( |
117
|
|
|
$this->fetchContents( $importContents ), |
118
|
|
|
$title |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
$page->doEditContent( |
122
|
|
|
$content, |
123
|
|
|
$importContents->getDescription(), |
124
|
|
|
EDIT_FORCE_BOT |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$title->invalidateCache(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private function fetchContents( $importContents ) { |
131
|
|
|
|
132
|
|
|
if ( $importContents->getContentsFile() === '' ) { |
133
|
|
|
return $importContents->getContents(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$contents = file_get_contents( $importContents->getContentsFile() ); |
137
|
|
|
|
138
|
|
|
// http://php.net/manual/en/function.file-get-contents.php |
139
|
|
|
return mb_convert_encoding( |
140
|
|
|
$contents, |
141
|
|
|
'UTF-8', |
142
|
|
|
mb_detect_encoding( |
143
|
|
|
$contents, |
144
|
|
|
'UTF-8, ISO-8859-1, ISO-8859-2', |
145
|
|
|
true |
146
|
|
|
) |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
|