forked from openslr-org/openslr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource.php
More file actions
123 lines (116 loc) · 5.06 KB
/
resource.php
File metadata and controls
123 lines (116 loc) · 5.06 KB
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
<?php
class Resource
{
public $dir = ''; // directory where data lives (should be absolute pathname)
public $id = ''; // Numeric code of resource, the same as basename of $dir.
public $summary = '';
public $name = '';
public $category = '';
public $license = '';
public $files = array(); // each element of file-list is an array containing
// the filename, then possibly an explanatory string.
public $alternate_urls = array(); // each element of alternate_urls is an array
// containing the alternate URL, then possibly an
// explanatory string.
// Resource describes
public function __construct($dir) {
$this->dir = $dir;
$this->id = basename($dir);
if ( !(intval($this->id) > 0) ) {
if ($this->id !== "." && $this->id !== "..") {
error_log("Base of directory name $dir is not numeric");
}
$this->dir = '';
return;
}
if ( !($handle = fopen($dir . "/info.txt", "r")) ) {
error_log("No such file $dir/info.txt");
$this->dir = '';
return;
}
while (($line = fgets($handle)) !== false) {
$this->parse_info_line($line);
}
}
public function get_about_html() {
$about_html = file_get_contents($this->dir . "/about.html");
return $about_html; // may be false if not found; calling script will deal with it.
}
public function ok () { return ($this->dir !== ''); }
public function get_file_size($file) {
// this function returns the fize of the file $file, as a human-readable string.
// We don't use PHP's filesize function because it gives the wrong answer if
// the size doesn't fit in 32 bits.
// $file should be a member of the @files data-member, but we don't check this.
$full_filename = $this->dir . "/" . $file;
$size = trim(`stat -L -c '%s' $full_filename`);
if ($size === false || preg_match('/^[0-9]+$/', $size) != 1) {
error_log("Error getting size of file $full_filename, size is '$size'");
return "error getting size";
}
$len = strlen($size);
if ($len > 10) {
return substr($size, 0, $len - 9) . "G";
} elseif ($len == 10) {
return $size[0] . "." . $size[1] . "G";
} elseif ($len > 7) {
return substr($size, 0, $len - 6) . "M";
} elseif ($len == 7) {
return $size[0] . "." . $size[1] . "M";
} elseif ($len > 4) {
return substr($size, 0, $len - 3) . "K";
} elseif ($len == 4) {
return $size[0] . "." . $size[1] . "K";
} else {
return $size . " bytes";
}
}
private function parse_info_line($line) {
// This doesn't make sure that $line is suitable to be parsed in this way.
// If it has multiple spaces between fields we'll get the wrong answer.
// We'll write a separate script to statically check the correctness of the
// info.txt files
$strings = explode(' ', $line);
if ($strings[0] === 'summary:') {
$this->summary = implode(' ', array_slice($strings, 1));
} elseif ($strings[0] === 'name:') {
$this->name = implode(' ', array_slice($strings, 1));
} elseif ($strings[0] === 'category:') {
$this->category = ucfirst(implode(' ', array_slice($strings, 1)));
} elseif ($strings[0] === 'license:') {
$this->license = implode(' ', array_slice($strings, 1));
} elseif ($strings[0] === 'file:') {
if (count($strings) == 2) { // just the filename.
array_push($this->files, array($strings[1]));
} elseif (count($strings) > 2) { // filename with explanation.
array_push($this->files, array($strings[1], implode(' ', array_slice($strings, 2))));
}
} elseif ($strings[0] == 'alternate_url:') {
if (count($strings) == 2) { // just the alternate url name.
array_push($this->alternate_urls, array($strings[1]));
} elseif (count($strings) > 2) { // alternate url name with explanation.
array_push($this->alternate_urls, array($strings[1], implode(' ', array_slice($strings, 2))));
}
}
}
}
function get_resource_id_list($root) {
// This function, given a root directory (e.g. /var/www/openslr/resources/,
// returns a list of resource-ids, e.g. ('1', '2', '3', ... ), derived
// from iterating over the directory, finding numeric subdirectories,
// and sorting them.
$dir = new DirectoryIterator($root);
$id_list = array();
foreach ($dir as $fileinfo) {
$filename = $fileinfo->getFilename();
if ($filename === strval(intval($filename))) { // filename is an integer
array_push($id_list, intval($filename)); // push onto array as integer.
}
}
sort($id_list, SORT_NUMERIC); // sort $id_list by numeric value.
for ($n = 0; $n < count($id_list); $n++) { // convert back into strings.
$id_list[$n] = strval($id_list[$n]);
}
return $id_list;
}
?>