Ignore:
Timestamp:
05/08/13 15:27:52 (8 years ago)
Author:
davidb
Message:

Changes after testing on the jamendo set

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gs2-extensions/afrepo/trunk/src/src/AFRepo.GSDLEXT.class.php.in

    r27290 r27317  
    66 * AFRepo top-level class to slot in with the Greenstone extension framework
    77 *
    8  * The audio is expected to be in subdirectories of the 'import-mp3' directory, and
     8 * The audio is expected to be in subdirectories of the 'audio-files' directory, and
    99 * for example purposes can be named in such a way that the example
    1010 * PathClassifier classifier can get meanining from it.
     
    1313class AFRepo extends AFRepoBase {
    1414    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    }
    1521
    1622    public function getName() {
     
    3642    }
    3743
    38     public function getAllFiles() {
     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() {
    39207        if (!is_null($this->allfiles))
    40208            return $this->allfiles;
    41209
    42210        $this->allfiles = array();
    43         $path = realpath("import-mp3");
     211        $path = realpath("audio-files");
    44212        $dir = dir($path);
    45213        while (($file = $dir->read()) !== false) {
Note: See TracChangeset for help on using the changeset viewer.