Newer
Older
Mark Theunissen
committed
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
170
171
172
173
174
175
176
<?php
/**
* @file
* Support for migration from files sources.
*/
/**
* Implementation of MigrateList, for retrieving a list of IDs to be migrated
* from a directory listing. Each item is a file, it's ID is the path.
*/
class MigrateListFiles extends MigrateList {
protected $listDirs;
protected $baseDir;
protected $fileMask;
protected $directoryOptions;
/**
* Constructor.
*
* @param $list_dirs
* Array of directory paths that will be scanned for files. No trailing
* slash. For example:
* array(
* '/var/html_source/en/news',
* '/var/html_source/fr/news',
* '/var/html_source/zh/news',
* );
* @param $base_dir
* The base dir is the part of the path that will be excluded when making
* an ID for each file. To continue the example from above, you want base_dir
* to be = '/var/html_source', so that the files will have IDs in the format
* '/en/news/news_2011_03_4.html'.
* @param $file_mask
* Passed on and used to filter for certain types of files. Use a regular
* expression, for example '/(.*\.htm$|.*\.html$)/i' to match all .htm and
* .html files, case insensitive.
* @param $options
* Options that will be passed on to file_scan_directory(). See docs of that
* core Drupal function for more information.
*/
public function __construct($list_dirs, $base_dir, $file_mask = NULL, $options = array()) {
parent::__construct();
$this->listDirs = $list_dirs;
$this->baseDir = $base_dir;
$this->fileMask = $file_mask;
$this->directoryOptions = $options;
}
/**
* Our public face is the directories we're getting items from.
*/
public function __toString() {
if (is_array($this->listDirs)) {
return implode(',', $this->listDirs);
}
else {
return $this->listDirs;
}
}
/**
* Retrieve a list of files based on parameters passed for the migration.
*/
public function getIdList() {
$files = array();
foreach ($this->listDirs as $dir) {
migrate_instrument_start("Retrieve $dir");
$files = array_merge(file_scan_directory($dir, $this->fileMask, $this->directoryOptions), $files);
migrate_instrument_stop("Retrieve $dir");
}
if (isset($files)) {
return $this->getIDsFromFiles($files);
}
$migration = Migration::currentMigration();
$migration->showMessage(t('Loading of !listuri failed:', array('!listuri' => $this->listUri)));
return NULL;
}
/**
* Given an array generated from file_scan_directory(), parse out the IDs for
* processing and return them as an array.
*/
protected function getIDsFromFiles(array $files) {
$ids = array();
foreach ($files as $file) {
$ids[] = str_replace($this->baseDir, '', (string) $file->uri);
}
return array_unique($ids);
}
/**
* Return a count of all available IDs from the source listing.
*/
public function computeCount() {
$count = 0;
$files = $this->getIdList();
if ($files) {
$count = count($files);
}
return $count;
}
}
/**
* Implementation of MigrateItem, for retrieving a file from the file system
* based on source directory and an ID provided by a MigrateList class.
*/
class MigrateItemFile extends MigrateItem {
protected $baseDir;
protected $getContents;
/**
* Constructor.
*
* @param $base_dir
* The base directory from which all file paths are calculated.
* @param $get_contents
* TRUE if we should try load the contents of each file (in the case
* of a text file), or FALSE if we just want to confirm it exists (binary).
*/
public function __construct($base_dir, $get_contents = TRUE) {
parent::__construct();
$this->baseDir = $base_dir;
$this->getContents = $get_contents;
}
/**
* Return an object representing a file.
*
* @param $id
* The file id, which is the file URI.
*
* @return object
* The item object for migration.
*/
public function getItem($id) {
$item_uri = $this->baseDir . $id;
// Get the file data at the specified URI
$data = $this->loadFile($item_uri);
if (is_string($data)) {
$return = new stdClass;
$return->filedata = $data;
return $return;
}
else if ($data === TRUE) {
$return = new stdClass;
return $return;
}
else {
$migration = Migration::currentMigration();
$message = t('Loading of !objecturi failed:', array('!objecturi' => $item_uri));
$migration->getMap()->saveMessage(
array($id), $message, MigrationBase::MESSAGE_ERROR);
return NULL;
}
}
/**
* Default file loader.
*/
protected function loadFile($item_uri) {
// Only try load the contents if we have this flag set.
if ($this->getContents) {
$data = file_get_contents($item_uri);
}
else {
$data = file_exists($item_uri);
}
return $data;
}
}