root/gs2-extensions/afrepo/trunk/src/src/AFRepo.GSDLEXT.class.php.in @ 27317

Revision 27317, 6.2 KB (checked in by davidb, 7 years ago)

Changes after testing on the jamendo set

Line 
1<?php
2
3/**
4 * Bart Nagel <bjn@ecs.soton.ac.uk>
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
13class 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    }
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
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    /**
88     * filePathToId
89     * Return the ID of the audiofile with the given path (which can be
90     * canonical or a symlink)
91     */
92    public function filePathToId($full_filepath) {
93      if (!$this->fileInRepo($full_filepath)) {
94        throw new Exception("file with path '$filepath' is not in the repository");
95      }
96
97      $hash_filepath = "salami:/" . $this->remove_audio_files_prefix($full_filepath);
98
99##    echo "Hashing on: " . $hash_filepath . "\n";
100
101      $id = md5($hash_filepath);
102      return $id;
103    }
104
105
106
107    // recursive method to delete things from the links directory according to the
108    // options
109    function getAllCollectionFilesRec($collection, $path) {
110
111      echo "Processing directory: " . $path . "\n";
112
113      $dir = dir($path);
114
115      while (($entry = $dir->read()) !== false) {
116
117        if ($entry == "." || $entry == "..") {
118          continue;
119        }
120
121        $fullpath = $path . "/" . $entry;
122
123        $isdir = false;
124        if (is_link($fullpath)) {
125
126          $realpath = readlink($fullpath);
127
128          // follow potential chain of sym-links
129          while (is_link($realpath)) {
130        $realpath = readlink($realpath);
131          }
132
133          $isdir = is_dir($realpath);
134        }
135        else {
136          $isdir = is_dir($fullpath);
137        }
138
139
140        if ($isdir) {
141
142          $this->getAllCollectionFilesRec($collection,$fullpath);
143        }
144        else {
145          // assume it is a file
146##        echo "Adding file: " . $fullpath . "\n";
147          $this->allfiles[$fullpath] = true;
148        }
149      }
150
151      $dir->close();
152     
153    }
154   
155   
156    public function getAllFiles()
157    {
158      if (!is_null($this->allfiles)) {
159        return $this->allfiles;
160      }
161     
162      $this->allfiles = array();
163
164##    $path = realpath($this->audio_files_dir);   
165      $path = $this->af_prefix;
166      $dir  = dir($path);
167     
168      while (($file = $dir->read()) !== false) {
169
170        if ($file[0] == ".") {
171          // skip all dot files and dirs
172          continue;
173        }
174
175        $fullpath = $path . "/" . $file;
176
177        $isdir = false;
178        if (is_link($fullpath)) {
179
180          $realpath = readlink($fullpath);
181
182          // follow potential chain of sym-links
183          while (is_link($realpath)) {
184        $realpath = readlink($realpath);
185          }
186
187          $isdir = is_dir($realpath);
188        }
189        else {
190          $isdir = is_dir($fullpath);
191        }
192
193        if ($isdir) {
194
195          // recursively work through each collection directory
196          $this->getAllCollectionFilesRec($file, $fullpath);
197        }
198      }
199
200      $dir->close();
201     
202      return $this->allfiles;
203    }
204
205
206    public function getAllFilesOLD() {
207        if (!is_null($this->allfiles))
208            return $this->allfiles;
209
210        $this->allfiles = array();
211        $path = realpath("audio-files");
212        $dir = dir($path);
213        while (($file = $dir->read()) !== false) {
214            if ($file[0] != "." && is_dir($path . "/" . $file)) {
215                $subdir = dir($path . "/" . $file);
216                while (($subfile = $subdir->read()) !== false) {
217                    if ($subfile[0] != "." && is_file($path . "/" . $file . "/" . $subfile))
218                        $this->allfiles[$path . "/" . $file . "/" . $subfile] = true;
219                }
220                $subdir->close();
221            }
222        }
223        $dir->close();
224
225        return $this->allfiles;
226    }
227
228    public function getSongFiles($id) {
229        $filepath = $this->idToLinkPath($id);
230        $origfilepath = realpath($filepath);
231
232        if ($origfilepath === false)
233            return array();
234
235        // is it a clip?
236        if (preg_match('%\.clip\..{1,4}$%', $origfilepath)) {
237            // does full version exist?
238            $fullpath = preg_replace('%\.clip%', "", $origfilepath);
239            if (file_exists($fullpath))
240                return array($fullpath, $origfilepath);
241            return array($origfilepath);
242        }
243
244        // it's a full song. does clip exist?
245        $clippath = preg_replace('%(\..{1,4})$%', '.clip\1', $origfilepath);
246        if (file_exists($clippath))
247            return array($origfilepath, $clippath);
248        return array($origfilepath);
249    }
250
251    public function haveMetadataPermission() {
252        return true;
253    }
254
255    public function haveAudioPermission() {
256        return ipInRange($_SERVER["REMOTE_ADDR"], "127.0.0.0/8");
257    }
258
259    public function getMBID($id) {
260        $classifiers = array(
261            new TagClassifier(),
262            new EchonestClassifier(),
263            new PathClassifier(),
264        );
265        foreach ($classifiers as $classifier)
266            if ($classifier->available() && $classifier->hasMBID($id))
267                return $classifier->getMBID($id);
268        return null;
269    }
270}
271
272?>
Note: See TracBrowser for help on using the browser.