-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrichTextParser.class.php
More file actions
executable file
·170 lines (142 loc) · 5.71 KB
/
Copy pathrichTextParser.class.php
File metadata and controls
executable file
·170 lines (142 loc) · 5.71 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
//replaced urls have this form.
//.../nomplateforme/courses/codeCours/document/nomdoc.html
//.../nomplateforme/claroline/backends/download.php?url=LzZCJTJDNS5qcGc%3D&cidReset=true&cidReq=codeCours
//url are replaced by a link looking like that: [[uid=10]]
require_once get_path('incRepositorySys') . '/lib/file/downloader.lib.php';
class RichTextParser
{
private $data;
private $platformName;
private $course;
private $courseTmpDir;
private $downloader;
private $resManagerData;
public function __construct($data, $course, $courseTmpDir)
{
$this->data = $data;
foreach ($this->data['tools'] as $tool) {
if ($tool['tool']['type'] = 'resource_manager') {
$this->resManagerData = $tool['tool'];
}
}
$this->course = $course;
$this->courseTmpDir = $courseTmpDir;
$this->downloader = new Claro_PlatformDocumentsDownloader();
}
public function parseAndReplace()
{
if (!is_dir($this->courseTmpDir)) return;
//in this version of the exporter, all textes are located in ".txt" files.
//so we parse the tmp directory, find .txt files and replace everything we need out here.
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($this->courseTmpDir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($iterator as $el) {
if ($el->isFile() && pathinfo($el->getBaseName(), PATHINFO_EXTENSION) === 'txt') {
$content = file_get_contents($el->getPathName());
file_put_contents($el->getPathName(), $this->setPlaceholders($content));
}
}
}
private function setPlaceholders($text)
{
$matches = array();
//download url
preg_match_all(
'#/claroline/backends/download.php\?url=([^&]+)#',
$text,
$matches,
PREG_SET_ORDER
);
if (count($matches) > 0) {
foreach ($matches as $match) {
$matchReplaced = array();
$requestUrl = urldecode(strip_tags($match[1]));
if (is_download_url_encoded($requestUrl)) {
$requestUrl = download_url_decode($requestUrl);
}
$filepath = str_replace('//', '/', $requestUrl);
$path = realpath(rtrim(str_replace( '\\', '/', get_path('rootSys') ), '/')
. '/courses/' . $this->course . '/document' . $requestUrl);
$fileUid = $this->findFileInData($filepath);
preg_match(
'#<img([^>]+)claroline/backends/download.php([^>]+)' . $match[1] . '[^>]+>#',
$text,
$matchReplaced
);
if (count($matchReplaced) > 0) {
$text = str_replace($matchReplaced[0], "[[uid={$fileUid}]]", $text);
}
}
}
//the other url
$matches = array();
$regexp = '#<img src="(.*/' . $this->course . '/document)([^"]+)[^>]+>#';
preg_match_all(
$regexp,
$text,
$matches,
PREG_SET_ORDER
);
if (count($matches) > 1) {
foreach($matches as $match) {
$matchReplaced = array();
$fileUid = $this->findFileInData($match[2]);
preg_match($regexp, $text, $matchReplaced);
if (count($matchReplaced) > 0) {
$text = str_replace($matchReplaced[0], "[[uid={$fileUid}]]", $text);
}
}
}
return $text;
}
private function findFileInData($path)
{
$lastFoundDir = $this->findRecursiveLastDirInData($path);
$parentUid = $lastFoundDir['uid'];
preg_match('#^/(.+/)*(.+)$#', $path, $matches);
$name = array_pop($matches);
$item = $this->findFileByNameAndParentUid($name, $parentUid);
return $item['uid'];
}
private function findRecursiveLastDirInData($path, $parentUid = 0)
{
preg_match('#/([^/]+)/#', $path, $matches);
$isLastDir = substr_count($path, '/') === 2;
if (count($matches) === 0 && $parentUid === 0) {
return $this->resManagerData['data']['root'];
}
if ($isLastDir) {
$name = $matches[1];
return $this->findDirectoryByNameAndParentUid($name, $parentUid);
}
if (count($matches) === 2) {
$name = $matches[1];
$path = str_replace('/' . $name, '', $path);
$dirData = $this->findDirectoryByNameAndParentUid($name, $parentUid);
$parentUid = $dirData['uid'];
$lastFoundDir = $this->findRecursiveLastDirInData($path, $parentUid);
}
return $lastFoundDir;
}
private function findDirectoryByNameAndParentUid($name, $parentUid)
{
foreach ($this->resManagerData['data']['directories'] as $directory) {
if ($directory['directory']['parent'] === $parentUid &&
$directory['directory']['name'] === $name) {
return $directory['directory'];
}
}
}
private function findFileByNameAndParentUid($name, $parentUid)
{
foreach ($this->resManagerData['data']['items'] as $item) {
if ($item['item']['parent'] === $parentUid &&
$item['item']['name'] === $name) {
return $item['item'];
}
}
}
}