1 | <?php
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * Bart Nagel <[email protected]>
|
---|
5 | *
|
---|
6 | * AFRepo top-level class to slot in with the Greenstone extension framework
|
---|
7 | *
|
---|
8 | * The audio is expected to be in subdirectories of the 'audio-files' directory, and
|
---|
9 | * for example purposes can be named in such a way that the example
|
---|
10 | * PathClassifier classifier can get meanining from it.
|
---|
11 | */
|
---|
12 |
|
---|
13 | class AFRepo extends AFRepoBase {
|
---|
14 | private $allfiles;
|
---|
15 |
|
---|
16 | public function AFRepo()
|
---|
17 | {
|
---|
18 | $this->audio_files_dir = "audio-files";
|
---|
19 | $this->af_prefix = getcwd() . "/" . $this->audio_files_dir;
|
---|
20 | }
|
---|
21 |
|
---|
22 | public function getName() {
|
---|
23 | return "Salami 4Store audio repository";
|
---|
24 | }
|
---|
25 |
|
---|
26 | public function getURIPrefix() {
|
---|
27 | #return "@afrepo-http-prefix@/afrepo/";
|
---|
28 | return "@public-facing-afrepo-http-prefix@";
|
---|
29 | }
|
---|
30 |
|
---|
31 | /* Done this way, can talk to the 4store server directly
|
---|
32 | rather than relying on proxying through the PHP-based 'afrepo' server */
|
---|
33 | public function getTrippleStoreLocalURIPrefix() {
|
---|
34 | //return "@afrepo-http-prefix@/4store/";
|
---|
35 | return "@4store-http-prefix@/";
|
---|
36 | }
|
---|
37 |
|
---|
38 | public function getTrippleStoreURIPrefix() {
|
---|
39 | return "@public-facing-4store-http-prefix@";
|
---|
40 | }
|
---|
41 |
|
---|
42 | public function getSparqlEndpoint() {
|
---|
43 | return $this->getTrippleStoreURIPrefix() . "sparql/";
|
---|
44 | }
|
---|
45 |
|
---|
46 | public function getDataEndpoint() {
|
---|
47 | return $this->getTrippleStoreLocalURIPrefix() . "data/";
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * getAudioPath
|
---|
52 | * Return the path to the audio links in this repository (without a trailing
|
---|
53 | * slash)
|
---|
54 | */
|
---|
55 | public function getAudioPath() {
|
---|
56 | return realpath("audio-ids");
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | protected function remove_audio_files_prefix($full_filepath) {
|
---|
61 | # echo "full filepath = " . $full_filepath . "\n";
|
---|
62 | # echo "af prefix = " . $this->af_prefix . "\n";
|
---|
63 |
|
---|
64 | $af_prefix_len = strlen($this->af_prefix);
|
---|
65 | $filepath = $full_filepath;
|
---|
66 |
|
---|
67 | if (substr($filepath, 0, $af_prefix_len) == $this->af_prefix) {
|
---|
68 | $filepath = substr($filepath, $af_prefix_len);
|
---|
69 | }
|
---|
70 |
|
---|
71 | # echo "** filepath = " . $filepath . "\n";
|
---|
72 |
|
---|
73 | return $filepath;
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * fileInRepo
|
---|
79 | * Return true if the audiofile with the given path (canonical or symlink)
|
---|
80 | * is in the repository or false if not
|
---|
81 | */
|
---|
82 | public function fileInRepo($full_filepath) {
|
---|
83 | $realpath = realpath($full_filepath);
|
---|
84 | if ($realpath === false) {
|
---|
85 | trigger_error("file '$filepath' does not exist on disk or is a broken symlink", E_USER_WARNING);
|
---|
86 | return false;
|
---|
87 | }
|
---|
88 |
|
---|
89 | return array_key_exists($full_filepath, $this->getAllFiles());
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * idToCanonicalPath
|
---|
95 | * Return the path to the canonical file with the given ID
|
---|
96 | */
|
---|
97 | public function idToCanonicalPath($id) {
|
---|
98 | return readlink($this->idToLinkPath($id));
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * filePathToId
|
---|
104 | * Return the ID of the audiofile with the given path (which can be
|
---|
105 | * canonical or a symlink)
|
---|
106 | */
|
---|
107 | public function filePathToId($full_filepath) {
|
---|
108 | if (!$this->fileInRepo($full_filepath)) {
|
---|
109 | throw new Exception("file with path '$filepath' is not in the repository");
|
---|
110 | }
|
---|
111 |
|
---|
112 | $hash_filepath = "salami:/" . $this->remove_audio_files_prefix($full_filepath);
|
---|
113 |
|
---|
114 | ## echo "Hashing on: " . $hash_filepath . "\n";
|
---|
115 |
|
---|
116 | $id = md5($hash_filepath);
|
---|
117 | return $id;
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 |
|
---|
122 | // recursive method to delete things from the links directory according to the
|
---|
123 | // options
|
---|
124 | function getAllCollectionFilesRec($collection, $path) {
|
---|
125 |
|
---|
126 | echo "Processing directory: " . $path . "\n";
|
---|
127 |
|
---|
128 | $dir = dir($path);
|
---|
129 |
|
---|
130 | while (($entry = $dir->read()) !== false) {
|
---|
131 |
|
---|
132 | if ($entry == "." || $entry == "..") {
|
---|
133 | continue;
|
---|
134 | }
|
---|
135 |
|
---|
136 | $fullpath = $path . "/" . $entry;
|
---|
137 |
|
---|
138 | $isdir = false;
|
---|
139 | if (is_link($fullpath)) {
|
---|
140 |
|
---|
141 | $realpath = readlink($fullpath);
|
---|
142 |
|
---|
143 | // follow potential chain of sym-links
|
---|
144 | while (is_link($realpath)) {
|
---|
145 | $realpath = readlink($realpath);
|
---|
146 | }
|
---|
147 |
|
---|
148 | $isdir = is_dir($realpath);
|
---|
149 | }
|
---|
150 | else {
|
---|
151 | $isdir = is_dir($fullpath);
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | if ($isdir) {
|
---|
156 |
|
---|
157 | $this->getAllCollectionFilesRec($collection,$fullpath);
|
---|
158 | }
|
---|
159 | else {
|
---|
160 | // assume it is a file
|
---|
161 | ## echo "Adding file: " . $fullpath . "\n";
|
---|
162 | $this->allfiles[$fullpath] = true;
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | $dir->close();
|
---|
167 |
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | public function getAllFiles()
|
---|
172 | {
|
---|
173 | if (!is_null($this->allfiles)) {
|
---|
174 | return $this->allfiles;
|
---|
175 | }
|
---|
176 |
|
---|
177 | $this->allfiles = array();
|
---|
178 |
|
---|
179 | ## $path = realpath($this->audio_files_dir);
|
---|
180 | $path = $this->af_prefix;
|
---|
181 | $dir = dir($path);
|
---|
182 |
|
---|
183 | while (($file = $dir->read()) !== false) {
|
---|
184 |
|
---|
185 | if ($file[0] == ".") {
|
---|
186 | // skip all dot files and dirs
|
---|
187 | continue;
|
---|
188 | }
|
---|
189 |
|
---|
190 | $fullpath = $path . "/" . $file;
|
---|
191 |
|
---|
192 | $isdir = false;
|
---|
193 | if (is_link($fullpath)) {
|
---|
194 |
|
---|
195 | $realpath = readlink($fullpath);
|
---|
196 |
|
---|
197 | // follow potential chain of sym-links
|
---|
198 | while (is_link($realpath)) {
|
---|
199 | $realpath = readlink($realpath);
|
---|
200 | }
|
---|
201 |
|
---|
202 | $isdir = is_dir($realpath);
|
---|
203 | }
|
---|
204 | else {
|
---|
205 | $isdir = is_dir($fullpath);
|
---|
206 | }
|
---|
207 |
|
---|
208 | if ($isdir) {
|
---|
209 |
|
---|
210 | // recursively work through each collection directory
|
---|
211 | $this->getAllCollectionFilesRec($file, $fullpath);
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | $dir->close();
|
---|
216 |
|
---|
217 | return $this->allfiles;
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | public function getAllFilesOLD() {
|
---|
222 | if (!is_null($this->allfiles))
|
---|
223 | return $this->allfiles;
|
---|
224 |
|
---|
225 | $this->allfiles = array();
|
---|
226 | $path = realpath("audio-files");
|
---|
227 | $dir = dir($path);
|
---|
228 | while (($file = $dir->read()) !== false) {
|
---|
229 | if ($file[0] != "." && is_dir($path . "/" . $file)) {
|
---|
230 | $subdir = dir($path . "/" . $file);
|
---|
231 | while (($subfile = $subdir->read()) !== false) {
|
---|
232 | if ($subfile[0] != "." && is_file($path . "/" . $file . "/" . $subfile))
|
---|
233 | $this->allfiles[$path . "/" . $file . "/" . $subfile] = true;
|
---|
234 | }
|
---|
235 | $subdir->close();
|
---|
236 | }
|
---|
237 | }
|
---|
238 | $dir->close();
|
---|
239 |
|
---|
240 | return $this->allfiles;
|
---|
241 | }
|
---|
242 |
|
---|
243 | public function getSongFilesOLD($id) {
|
---|
244 | $filepath = $this->idToLinkPath($id);
|
---|
245 | $origfilepath = realpath($filepath);
|
---|
246 |
|
---|
247 | if ($origfilepath === false)
|
---|
248 | return array();
|
---|
249 |
|
---|
250 | // is it a clip?
|
---|
251 | if (preg_match('%\.clip\..{1,4}$%', $origfilepath)) {
|
---|
252 | // does full version exist?
|
---|
253 | $fullpath = preg_replace('%\.clip%', "", $origfilepath);
|
---|
254 | if (file_exists($fullpath))
|
---|
255 | return array($fullpath, $origfilepath);
|
---|
256 | return array($origfilepath);
|
---|
257 | }
|
---|
258 |
|
---|
259 | // it's a full song. does clip exist?
|
---|
260 | $clippath = preg_replace('%(\..{1,4})$%', '.clip\1', $origfilepath);
|
---|
261 | if (file_exists($clippath))
|
---|
262 | return array($origfilepath, $clippath);
|
---|
263 | return array($origfilepath);
|
---|
264 | }
|
---|
265 |
|
---|
266 | public function haveMetadataPermission() {
|
---|
267 | return true;
|
---|
268 | }
|
---|
269 |
|
---|
270 | public function haveAudioPermission() {
|
---|
271 | return ipInRange($_SERVER["REMOTE_ADDR"], "127.0.0.0/8");
|
---|
272 | }
|
---|
273 |
|
---|
274 | public function getMBID($id) {
|
---|
275 | $classifiers = array(
|
---|
276 | new TagClassifier(),
|
---|
277 | new EchonestClassifier(),
|
---|
278 | new PathClassifier(),
|
---|
279 | );
|
---|
280 |
|
---|
281 | foreach ($classifiers as $classifier)
|
---|
282 | if ($classifier->available() && $classifier->hasMBID($id))
|
---|
283 | return $classifier->getMBID($id);
|
---|
284 | return null;
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | ?>
|
---|