3 require_once(dirname(__FILE__
) . "/ORDataObject.class.php");
4 require_once(dirname(__FILE__
) . "/CouchDB.class.php");
8 * This class is the logical representation of a physical file on some system somewhere that can be referenced with a URL
9 * of some type. This URL is not necessarily a web url, it could be a file URL or reference to a BLOB in a db.
10 * It is implicit that a document can have other related tables to it at least a one document to many notes which join on a documents
11 * id and categories which do the same.
14 class Document
extends ORDataObject
{
17 * Database unique identifier
23 * DB unique identifier reference to some other table, this is not unique in the document table
29 * Enumerated DB field which is met information about how to use the URL
30 * @var int can also be a the properly enumerated string
35 * Array mapping of possible for values for the type variable
36 * mapping is array text name to index
39 var $type_array = array();
42 * Size of the document in bytes if that is available
48 * Date the document was first persisted
54 * URL which point to the document, may be a file URL, a web URL, a db BLOB URL, or others
60 * Mimetype of the document if available
66 * If the document is a multi-page format like tiff and has at least 1 page this will be 1 or greater, if a non-multi-page format this should be null or empty
72 * Foreign key identifier of who initially persisited the document,
73 * potentially ownership could be changed but that would be up to an external non-document object process
79 * Timestamp of the last time the document was changed and persisted, auto maintained by DB, manually change at your own peril
85 * Date (YYYY-MM-DD) logically associated with the document, e.g. when a picture was taken.
91 * 40-character sha1 hash key of the document from when it was uploaded.
97 * DB identifier reference to the lists table (the related issue), 0 if none.
103 * Constructor sets all Document attributes to their default value
104 * @param int $id optional existing id of a specific document, if omitted a "blank" document is created
106 function Document($id = "") {
107 //call the parent constructor so we have a _db to work with
108 parent
::ORDataObject();
110 //shore up the most basic ORDataObject bits
112 $this->_table
= "documents";
114 //load the enum type from the db using the parent helper function, this uses psuedo-class variables so it is really cheap
115 $this->type_array
= $this->_load_enum("type");
117 $this->type
= $this->type_array
[0];
119 $this->date
= date("Y-m-d H:i:s");
121 $this->mimetype
= "";
122 $this->docdate
= date("Y-m-d");
132 * Convenience function to get an array of many document objects
133 * For really large numbers of documents there is a way more efficient way to do this by overwriting the populate method
134 * @param int $foreign_id optional id use to limit array on to a specific relation, otherwise every document object is returned
136 function documents_factory($foreign_id = "") {
137 $documents = array();
139 if (empty($foreign_id)) {
140 $foreign_id= "like '%'";
143 $foreign_id= " = '" . mysql_real_escape_string(strval($foreign_id)) . "'";
147 $sql = "SELECT id FROM " . $d->_table
. " WHERE foreign_id " .$foreign_id ;
148 $result = $d->_db
->Execute($sql);
150 while ($result && !$result->EOF
) {
151 $documents[] = new Document($result->fields
['id']);
159 * Convenience function to get a document object from a url
160 * Checks to see if there is an existing document with that URL and if so returns that object, otherwise
161 * creates a new one, persists it and returns it
163 * @return object new or existing document object with the specified URL
165 function document_factory_url($url) {
167 //strip url handler, for now we always assume file://
168 $filename = preg_replace("|^(.*)://|","",$url);
170 if (!file_exists($filename)) {
171 die("An invalid URL was specified to crete a new document, this would only be caused if files are being deleted as you are working through the queue. '$filename'\n");
174 $sql = "SELECT id FROM " . $d->_table
. " WHERE url= '" . mysql_real_escape_string($url) ."'" ;
175 $result = $d->_db
->Execute($sql);
177 if ($result && !$result->EOF
) {
178 if (file_exists($filename)) {
179 $d = new Document($result->fields
['id']);
182 $sql = "DELETE FROM " . $d->_table
. " WHERE id= '" . $result->fields
['id'] ."'";
183 $result = $d->_db
->Execute($sql);
184 echo("There is a database for the file but it no longer exists on the file system. Its document entry has been deleted. '$filename'\n");
188 $file_command = $GLOBALS['oer_config']['document']['file_command_path'] ;
189 $cmd_args = "-i ".escapeshellarg($new_path.$fname);
191 $command = $file_command." ".$cmd_args;
192 $mimetype = exec($command);
193 $mime_array = split(":", $mimetype);
194 $mimetype = $mime_array[1];
195 $d->set_mimetype($mimetype);
197 $d->size
= filesize($filename);
198 $d->type
= $d->type_array
['file_url'];
207 * Convenience function to generate string debug data about the object
209 function toString($html = false) {
211 . "ID: " . $this->id
."\n"
212 . "FID: " . $this->foreign_id
."\n"
213 . "type: " . $this->type
. "\n"
214 . "type_array: " . print_r($this->type_array
,true) . "\n"
215 . "size: " . $this->size
. "\n"
216 . "date: " . $this->date
. "\n"
217 . "url: " . $this->url
. "\n"
218 . "mimetype: " . $this->mimetype
. "\n"
219 . "pages: " . $this->pages
. "\n"
220 . "owner: " . $this->owner
. "\n"
221 . "revision: " . $this->revision
. "\n"
222 . "docdate: " . $this->docdate
. "\n"
223 . "hash: " . $this->hash
. "\n"
224 . "list_id: " . $this->list_id
. "\n";
227 return nl2br($string);
235 * Getter/Setter methods used by reflection to affect object in persist/poulate operations
236 * @param mixed new value for given attribute
238 function set_id($id) {
244 function set_foreign_id($fid) {
245 $this->foreign_id
= $fid;
247 function get_foreign_id() {
248 return $this->foreign_id
;
250 function set_type($type) {
253 function get_type() {
256 function set_size($size) {
259 function get_size() {
262 function set_date($date) {
265 function get_date() {
268 function set_hash($hash) {
271 function get_hash() {
274 function set_url($url) {
281 * this returns the url stripped down to basename
283 function get_url_web() {
284 return basename($this->url
);
287 * get the url without the protocol handler
289 function get_url_filepath() {
290 return preg_replace("|^(.*)://|","",$this->url
);
293 * get the url filename only
295 function get_url_file() {
296 return basename(preg_replace("|^(.*)://|","",$this->url
));
299 * get the url path only
301 function get_url_path() {
302 return dirname(preg_replace("|^(.*)://|","",$this->url
)) ."/";
304 function set_mimetype($mimetype) {
305 $this->mimetype
= $mimetype;
307 function get_mimetype() {
308 return $this->mimetype
;
310 function set_pages($pages) {
311 $this->pages
= $pages;
313 function get_pages() {
316 function set_owner($owner) {
317 $this->owner
= $owner;
319 function get_owner() {
323 * No getter for revision because it is updated automatically by the DB.
325 function set_revision($revision) {
326 $this->revision
= $revision;
328 function set_docdate($docdate) {
329 $this->docdate
= $docdate;
331 function get_docdate() {
332 return $this->docdate
;
334 function set_list_id($list_id) {
335 $this->list_id
= $list_id;
337 function get_list_id() {
338 return $this->list_id
;
340 function get_ccr_type($doc_id){
341 $type = sqlQuery("SELECT c.name FROM categories AS c LEFT JOIN categories_to_documents AS ctd ON c.id = ctd.category_id WHERE ctd.document_id = ?",array($doc_id));
342 return $type['name'];
345 * Overridden function to stor current object state in the db.
346 * current overide is to allow for a just in time foreign id, often this is needed
347 * when the object is never directly exposed and is handled as part of a larger
349 * @param int $fid foreign id that should be used so that this document can be related (joined) on it later
352 function persist($fid ="") {
354 $this->foreign_id
= $fid;
359 function set_storagemethod($str) {
360 $this->storagemethod
= $str;
363 function get_storagemethod() {
364 return $this->storagemethod
;
367 function set_couch_docid($str) {
368 $this->couch_docid
= $str;
371 function get_couch_docid() {
372 return $this->couch_docid
;
375 function set_couch_revid($str) {
376 $this->couch_revid
= $str;
379 function get_couch_revid() {
380 return $this->couch_revid
;
383 function get_couch_url($pid,$encounter){
384 $couch_docid = $this->get_couch_docid();
385 $couch_url = $this->get_url();
386 $couch = new CouchDB();
387 $data = array($GLOBALS['couchdb_dbase'],$couch_docid,$pid,$encounter);
388 $resp = $couch->retrieve_doc($data);
389 $content = $resp->data
;
390 $temp_url=$couch_url;
391 $temp_url = $GLOBALS['OE_SITE_DIR'] . '/documents/temp/' . $pid . '_' . $couch_url;
392 $f_CDB = fopen($temp_url,'w');
393 fwrite($f_CDB,base64_decode($content));
401 $d = new Document(3);
402 $d->type = $d->type_array[1];
403 $d->url = "file:///tmp/test.gif";
411 echo $d->toString(true);*/