forked from jyobb/islandoraScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addPolicy.php
executable file
·169 lines (139 loc) · 5.51 KB
/
addPolicy.php
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/php -q
<?php
/**
* This script is used to bulk add a XACML policy to a collection and water mark
* the JPEG derivative.
* The XCAML policy needs to be given as an arguement
* Please excuse the crap coding it was meant to be a one off for my eyes only :)
*
* The code needs to be modified to add the path to the watermark
* The code needs to be modified to add the collection
*/
require_once './connection.php';
/**
*This section is where I grab the list of pids from a collection using itql query
*/
$query = 'select $object from <#ri> where $object <info:fedora/fedora-system:def/relations-external#isMemberOfCollection> <info:fedora/SPLLHR:00001>';
$objects = $repository->ri->itqlQuery($query, 'unlimited', '0'); // for itql
$to_mark = 0;
$already_marked = 0;
$cnt = 0;
$file = $argv[1];
$start = date("Y-m-d H:i:s");
if(file_exists($file)){
foreach ($objects as $k => $v) {
$cnt = $cnt+1;
$pid = $v['object']['value'];
$fedora_object = $repository->getObject($pid);
//if($fedora_object['JPG'] && !$fedora_object['JPGRAW'] && !$fedora_object['POLICY']){
//Comment out the statement above if you are only adding the policy
//and uncomment the statement below
if(!$fedora_object['POLICY']){
//addWatermark($fedora_object);
addPolicy($fedora_object, $file);
$to_mark = $to_mark +1;
print "processed". $pid ."\n";
} else {
$already_marked = $already_marked +1;
print "done: ". $pid ."\n";
}
print "found $cnt\n";
print "$to_mark to mark and $already_marked did not require marking\n";
}
}
print "Start time: ". $start ." End time". date("Y-m-d H:i:s") ."\n";
// This function takes as arguements the file that was passed on the command
// line and the fedora object found in the search
function addPolicy($fedora_object, $file){
$policy_file = file_get_contents($file);
$log_message = "Xacml derivative created using FITS with SUCCESS";
addDerivative($fedora_object,'X', 'POLICY', 'Xacml Policy Stream', $file, 'text/xml', $log_message, FALSE);
}
function addWatermark($fedora_object){
//Get the existing datastream
$temp_file = saveDatastream($fedora_object, 'JPG', 'JPG');
//Delete the current jpg
$fedora_object->purgeDatastream('JPG');
//Logo to overlay and tempfile
$logo = "/usr/fedora/local_scripts/localHistoryRoomMark.png";
$output_file = $temp_file .'_WM';
//do processing
$command = "composite -gravity north -geometry +10+10 $logo $temp_file $output_file";
//$command = "composite -dissolve 5 -tile $logo $temp_file $output_file";
exec($command, $jpg_output, $return);
//Add new derivitives
$log_message = "JPG derivative created using FITS with command - $command || SUCCESS";
addDerivative($fedora_object, 'M', 'JPG', 'JPG', $output_file, 'image/jpeg', $log_message);
$log_message = "JPGRAW derivative created using FITS with command - $command || SUCCESS";
addDerivative($fedora_object, 'M', 'JPGRAW', 'JPGRAW', $temp_file, 'image/jpeg', $log_message);
}
function addDerivative($fedora_object, $ctrlgrp, $dsid, $label, $output_file, $mimetype, $log_message = NULL, $delete = TRUE) {
$datastream = new NewFedoraDatastream($dsid, $ctrlgrp, $fedora_object, $fedora_object->repository);
$datastream->setContentFromFile($output_file);
$datastream->label = $label;
$datastream->mimetype = $mimetype;
$datastream->state = 'A';
$datastream->checksum = TRUE;
$datastream->checksumType = 'MD5';
if ($log_message) {
$datastream->logMessage = $log_message;
}
$return = $fedora_object->ingestDatastream($datastream);
if ($delete) {
unlink($output_file);
}
//$this->log->lwrite('Finished processing', 'COMPLETE_DATASTREAM', $this->pid, $dsid);
return $return;
}
function watermarkJPG($dsid = 'JPEG', $label = 'JPEG image', $resize = '800') {
$this->log->lwrite('Starting processing', 'PROCESS_DATASTREAM', $this->pid, $dsid);
try {
$output_file = $this->temp_file . '_JPG.jpg';
$command = "convert -resize $resize $this->temp_file $output_file";
exec($command, $jpg_output, $return);
$log_message = "$dsid derivative created using ImageMagick with command - $command || SUCCESS";
$this->add_derivative($dsid, $label, $output_file, 'image/jpeg', $log_message);
} catch (Exception $e) {
$this->log->lwrite("Could not create the $dsid derivative!", 'FAIL_DATASTREAM', $this->pid, $dsid, NULL, 'ERROR');
unlink($output_file);
}
return $return;
}
function saveDatastream($fedora_object = NULL, $dsid = NULL, $extension = NULL) {
if (!isset($dsid)) {
return;
}
$datastream_array = array();
foreach ($fedora_object as $datastream) {
$datastream_array[] = $datastream->id;
}
if (!in_array($dsid, $datastream_array)) {
print "Could not find the $dsid datastream!";
}
try {
$datastream = $fedora_object->getDatastream($dsid);
$mime_type = $datastream->mimetype;
if (!$extension) {
$extension = system_mime_type_extension($mime_type);
}
$tempfile = temp_filename($extension);
$file_handle = fopen($tempfile, 'w');
fwrite($file_handle, $datastream->content);
fclose($file_handle);
//The tempfile needs to be removed because I am lazy I just deleted from the tmp directory
//I think its just unlink($tempfile)
} catch (Exception $e) {
print "Could not save datastream - $e";
}
return $tempfile;
}
function temp_filename($extension = NULL) {
while (true) {
$filename = sys_get_temp_dir() . '/' . uniqid(rand()) . '.' . $extension;
print $filename . "\n";
if (!file_exists($filename))
break;
}
return $filename;
}
?>