source: gs2-extensions/afrepo/trunk/src/src/AFRepo.GSDLEXT.class.php.in@ 27339

Last change on this file since 27339 was 27339, checked in by davidb, 11 years ago

AFRepo PHP script changed so the computation of the md5 hash is relative to the 'audio-files' folder rather than being dependent on the absoulte path name, withl symbolic links resolved

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