Commit fad1cb52c319599b30d01ad36221f6a7676dc2c7

Authored by Conrad Vermeulen
1 parent 122d6a3f

KTS-2134

"Add @ in front of file functions such as unlink. There are cases that seem to break webservices because of warnings."
Fixed.

Reviewed By: Kevin Fourie

git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@6859 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/documentmanagement/documentutil.inc.php
... ... @@ -3,7 +3,7 @@
3 3 * $Id$
4 4 *
5 5 * Document-handling utility functions
6   - *
  6 + *
7 7 * Simplifies and canonicalises operations such as adding, updating, and
8 8 * deleting documents from the repository.
9 9 *
... ... @@ -11,7 +11,7 @@
11 11 * License Version 1.1.2 ("License"); You may not use this file except in
12 12 * compliance with the License. You may obtain a copy of the License at
13 13 * http://www.knowledgetree.com/KPL
14   - *
  14 + *
15 15 * Software distributed under the License is distributed on an "AS IS"
16 16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
17 17 * See the License for the specific language governing rights and
... ... @@ -22,9 +22,9 @@
22 22 * (ii) the KnowledgeTree copyright notice
23 23 * in the same form as they appear in the distribution. See the License for
24 24 * requirements.
25   - *
  25 + *
26 26 * The Original Code is: KnowledgeTree Open Source
27   - *
  27 + *
28 28 * The Initial Developer of the Original Code is The Jam Warehouse Software
29 29 * (Pty) Ltd, trading as KnowledgeTree.
30 30 * Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
... ... @@ -45,7 +45,7 @@ require_once(KT_LIB_DIR . '/storage/storagemanager.inc.php');
45 45 require_once(KT_LIB_DIR . '/filelike/filelikeutil.inc.php');
46 46 require_once(KT_LIB_DIR . '/metadata/metadatautil.inc.php');
47 47 require_once(KT_LIB_DIR . '/metadata/fieldset.inc.php');
48   -require_once(KT_LIB_DIR . '/subscriptions/subscriptions.inc.php');
  48 +require_once(KT_LIB_DIR . '/subscriptions/subscriptions.inc.php');
49 49 require_once(KT_LIB_DIR . '/triggers/triggerregistry.inc.php');
50 50 require_once(KT_LIB_DIR . '/foldermanagement/Folder.inc');
51 51  
... ... @@ -76,8 +76,8 @@ class KTDocumentUtil {
76 76 $oDocument->setCheckedOutUserID(-1);
77 77 if ($aOptions['major_update']) {
78 78 $oDocument->setMajorVersionNumber($oDocument->getMajorVersionNumber()+1);
79   - $oDocument->setMinorVersionNumber('0');
80   - } else {
  79 + $oDocument->setMinorVersionNumber('0');
  80 + } else {
81 81 $oDocument->setMinorVersionNumber($oDocument->getMinorVersionNumber()+1);
82 82 }
83 83 $oDocument->setFileSize($iFileSize);
... ... @@ -92,10 +92,10 @@ class KTDocumentUtil {
92 92 $default->log->info('renamed document ' . $oDocument->getId() . ' to ' . $sFilename);
93 93 }
94 94 }
95   -
  95 +
96 96 $sType = KTMime::getMimeTypeFromFile($sFilename);
97 97 $iMimeTypeId = KTMime::getMimeTypeID($sType, $oDocument->getFileName());
98   - $oDocument->setMimeTypeId($iMimeTypeId);
  98 + $oDocument->setMimeTypeId($iMimeTypeId);
99 99  
100 100 $bSuccess = $oDocument->update();
101 101 if ($bSuccess !== true) {
... ... @@ -108,7 +108,7 @@ class KTDocumentUtil {
108 108 // create the document transaction record
109 109 $oDocumentTransaction = & new DocumentTransaction($oDocument, $sCheckInComment, 'ktcore.transactions.check_in');
110 110 $oDocumentTransaction->create();
111   -
  111 +
112 112 $oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
113 113 $aTriggers = $oKTTriggerRegistry->getTriggers('content', 'scan');
114 114 foreach ($aTriggers as $aTrigger) {
... ... @@ -121,7 +121,7 @@ class KTDocumentUtil {
121 121 return $ret;
122 122 }
123 123 }
124   -
  124 +
125 125 $aTriggers = $oKTTriggerRegistry->getTriggers('content', 'transform');
126 126 foreach ($aTriggers as $aTrigger) {
127 127 $sTrigger = $aTrigger[0];
... ... @@ -132,14 +132,14 @@ class KTDocumentUtil {
132 132 $oTrigger->setDocument($oDocument);
133 133 $oTrigger->transform();
134 134 }
135   -
  135 +
136 136 // fire subscription alerts for the checked in document
137 137 $oSubscriptionEvent = new SubscriptionEvent();
138 138 $oFolder = Folder::get($oDocument->getFolderID());
139 139 $oSubscriptionEvent->CheckinDocument($oDocument, $oFolder);
140   -
  140 +
141 141 KTDocumentUtil::updateSearchableText($oDocument);
142   -
  142 +
143 143 return true;
144 144 }
145 145  
... ... @@ -147,9 +147,9 @@ class KTDocumentUtil {
147 147 if ($oDocument->getIsCheckedOut()) {
148 148 return PEAR::raiseError(_kt('Already checked out.'));
149 149 }
150   -
  150 +
151 151 // FIXME at the moment errors this _does not_ rollback.
152   -
  152 +
153 153 $oDocument->setIsCheckedOut(true);
154 154 $oDocument->setCheckedOutUserID($oUser->getId());
155 155 if (!$oDocument->update()) { return PEAR::raiseError(_kt('There was a problem checking out the document.')); }
... ... @@ -176,13 +176,13 @@ class KTDocumentUtil {
176 176 $oSubscriptionEvent = new SubscriptionEvent();
177 177 $oFolder = Folder::get($oDocument->getFolderID());
178 178 $oSubscriptionEvent->CheckOutDocument($oDocument, $oFolder);
179   -
  179 +
180 180 return true;
181 181 }
182 182  
183 183 function &_add($oFolder, $sFilename, $oUser, $aOptions) {
184 184 global $default;
185   -
  185 +
186 186 $oContents = KTUtil::arrayGet($aOptions, 'contents');
187 187 $aMetadata = KTUtil::arrayGet($aOptions, 'metadata', null, false);
188 188 $oDocumentType = KTUtil::arrayGet($aOptions, 'documenttype');
... ... @@ -274,17 +274,17 @@ class KTDocumentUtil {
274 274 $aFieldValues[$oField->getId()] = $v;
275 275 }
276 276 }
277   -
  277 +
278 278 if ($isRealConditional) {
279 279 $res = KTMetadataUtil::getNext($oFieldset, $aFieldValues);
280 280 if ($res) {
281 281 foreach ($res as $aMDSet) {
282 282 if ($aMDSet['field']->getIsMandatory()) {
283   - $aFailed['fieldset'][$oFieldset->getId()] = 1;
  283 + $aFailed['fieldset'][$oFieldset->getId()] = 1;
284 284 }
285 285 }
286   - }
287   - }
  286 + }
  287 + }
288 288 }
289 289 if (!empty($aFailed)) {
290 290 return new KTMetadataValidationError($aFailed);
... ... @@ -304,7 +304,7 @@ class KTDocumentUtil {
304 304 return $res;
305 305 }
306 306 $aMetadata = empty($res)?array():$res;
307   -
  307 +
308 308 $iMetadataVersionId = $oDocument->getMetadataVersionId();
309 309 $res = DBUtil::runQuery(array("DELETE FROM $table WHERE metadata_version_id = ?", array($iMetadataVersionId)));
310 310 if (PEAR::isError($res)) {
... ... @@ -341,7 +341,7 @@ class KTDocumentUtil {
341 341 $aRow['metadata_version_id'] = $iNewMetadataVersion;
342 342 DBUtil::autoInsert($sTable, $aRow);
343 343 }
344   -
  344 +
345 345 }
346 346  
347 347 // {{{ setIncomplete
... ... @@ -428,7 +428,7 @@ class KTDocumentUtil {
428 428 if (KTDocumentUtil::fileExists($oFolder, $sFilename)) {
429 429 $oDoc = Document::getByFilenameAndFolder($sFilename, $oFolder->getId());
430 430 if (PEAR::isError($oDoc)) {
431   - return PEAR::raiseError(_kt('Document with that filename already exists in this folder, and appears to be invalid. Please contact the system administrator.'));
  431 + return PEAR::raiseError(_kt('Document with that filename already exists in this folder, and appears to be invalid. Please contact the system administrator.'));
432 432 } else {
433 433 if ($oDoc->getStatusID() != LIVE) {
434 434 $sError = _kt('Document with that filename already exists in this folder, but it has been archived or deleted and is still available for restoration. To prevent it being overwritten, you are not allowed to add a document with the same title or filename.');
... ... @@ -444,7 +444,7 @@ class KTDocumentUtil {
444 444 if (KTDocumentUtil::nameExists($oFolder, $sName)) {
445 445 $oDoc = Document::getByNameAndFolder($sName, $oFolder->getId());
446 446 if (PEAR::isError($oDoc)) {
447   - return PEAR::raiseError(_kt('Document with that title already exists in this folder, and appears to be invalid. Please contact the system administrator.'));
  447 + return PEAR::raiseError(_kt('Document with that title already exists in this folder, and appears to be invalid. Please contact the system administrator.'));
448 448 } else {
449 449 if ($oDoc->getStatusID != LIVE) {
450 450 return PEAR::raiseError(_kt('Document with that title already exists in this folder, but it has been archived or deleted and is still available for restoration. To prevent it being overwritten, you are not allowed to add a document with the same title or filename.'));
... ... @@ -452,7 +452,7 @@ class KTDocumentUtil {
452 452 return PEAR::raiseError(_kt('Document with that title already exists in this folder.'));
453 453 }
454 454 }
455   -
  455 +
456 456 }
457 457  
458 458 $oUploadChannel =& KTUploadChannel::getSingleton();
... ... @@ -508,10 +508,10 @@ class KTDocumentUtil {
508 508 $oSubscriptionEvent = new SubscriptionEvent();
509 509 $oFolder = Folder::get($oDocument->getFolderID());
510 510 $oSubscriptionEvent->AddDocument($oDocument, $oFolder);
511   -
  511 +
512 512 $oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
513 513 $aTriggers = $oKTTriggerRegistry->getTriggers('add', 'postValidate');
514   -
  514 +
515 515 foreach ($aTriggers as $aTrigger) {
516 516 $sTrigger = $aTrigger[0];
517 517 $oTrigger = new $sTrigger;
... ... @@ -521,7 +521,7 @@ class KTDocumentUtil {
521 521 );
522 522 $oTrigger->setInfo($aInfo);
523 523 $ret = $oTrigger->postValidate();
524   -
  524 +
525 525 }
526 526 KTDocumentUtil::updateSearchableText($oDocument, true);
527 527  
... ... @@ -555,16 +555,16 @@ class KTDocumentUtil {
555 555 $oStorage =& KTStorageManagerUtil::getSingleton();
556 556  
557 557 $oKTConfig =& KTConfig::getSingleton();
558   - $sBasedir = $oKTConfig->get('urls/tmpDirectory');
559   -
  558 + $sBasedir = $oKTConfig->get('urls/tmpDirectory');
  559 +
560 560 $sFilename = tempnam($sBasedir, 'kt_storecontents');
561 561 $oOutputFile = new KTFSFileLike($sFilename);
562 562 $res = KTFileLikeUtil::copy_contents($oContents, $oOutputFile);
563   - if (($res === false)) {
  563 + if (($res === false)) {
564 564 return PEAR::raiseError(_kt("Couldn't store contents, and no reason given."));
565 565 } else if (PEAR::isError($res)) {
566 566 return PEAR::raiseError(sprintf(_kt("Couldn't store contents: %s"), $res->getMessage()));
567   - }
  567 + }
568 568 $sType = KTMime::getMimeTypeFromFile($sFilename);
569 569 $iMimeTypeId = KTMime::getMimeTypeID($sType, $oDocument->getFileName());
570 570 $oDocument->setMimeTypeId($iMimeTypeId);
... ... @@ -576,11 +576,11 @@ class KTDocumentUtil {
576 576 return PEAR::raiseError(sprintf(_kt("Couldn't store contents: %s"), $res->getMessage()));
577 577 }
578 578 KTDocumentUtil::setComplete($oDocument, 'contents');
579   -
  579 +
580 580 if ($aOptions['cleanup_initial_file']) {
581   - unlink($oContents->sFilename);
  581 + @unlink($oContents->sFilename);
582 582 }
583   -
  583 +
584 584 return true;
585 585 }
586 586 // }}}
... ... @@ -644,48 +644,48 @@ class KTDocumentUtil {
644 644 return DBUtil::autoInsert($sTable, $aInsert, array('noid' => true));
645 645 }
646 646 // }}}
647   -
  647 +
648 648 // {{{ delete
649 649 function delete($oDocument, $sReason, $iDestFolderId = null) {
650 650 $oDocument =& KTUtil::getObject('Document', $oDocument);
651   - if (is_null($iDestFolderId)) {
652   - $iDestFolderId = $oDocument->getFolderID();
  651 + if (is_null($iDestFolderId)) {
  652 + $iDestFolderId = $oDocument->getFolderID();
653 653 }
654 654 $oStorageManager =& KTStorageManagerUtil::getSingleton();
655   -
  655 +
656 656 global $default;
657   -
658   - if (count(trim($sReason)) == 0) {
659   - return PEAR::raiseError(_kt('Deletion requires a reason'));
  657 +
  658 + if (count(trim($sReason)) == 0) {
  659 + return PEAR::raiseError(_kt('Deletion requires a reason'));
660 660 }
661   -
662   - if (PEAR::isError($oDocument) || ($oDocument == false)) {
663   - return PEAR::raiseError(_kt('Invalid document object.'));
  661 +
  662 + if (PEAR::isError($oDocument) || ($oDocument == false)) {
  663 + return PEAR::raiseError(_kt('Invalid document object.'));
664 664 }
665   -
666   - if ($oDocument->getIsCheckedOut() == true) {
667   - return PEAR::raiseError(sprintf(_kt('The document is checked out and cannot be deleted: %s'), $oDocument->getName()));
  665 +
  666 + if ($oDocument->getIsCheckedOut() == true) {
  667 + return PEAR::raiseError(sprintf(_kt('The document is checked out and cannot be deleted: %s'), $oDocument->getName()));
668 668 }
669   -
  669 +
670 670 // IF we're deleted ...
671   - if ($oDocument->getStatusID() == DELETED) {
672   - return true;
  671 + if ($oDocument->getStatusID() == DELETED) {
  672 + return true;
673 673 }
674   -
  674 +
675 675 $oOrigFolder = Folder::get($oDocument->getFolderId());
676 676  
677 677 DBUtil::startTransaction();
678   -
  678 +
679 679 // flip the status id
680 680 $oDocument->setStatusID(DELETED);
681   -
  681 +
682 682 // $iDestFolderId is DEPRECATED.
683   - $oDocument->setFolderID(null);
  683 + $oDocument->setFolderID(null);
684 684 $oDocument->setRestoreFolderId($oOrigFolder->getId());
685 685 $oDocument->setRestoreFolderPath(Folder::generateFolderIDs($oOrigFolder->getId()));
686   -
  686 +
687 687 $res = $oDocument->update();
688   -
  688 +
689 689 if (PEAR::isError($res) || ($res == false)) {
690 690 DBUtil::rollback();
691 691 return PEAR::raiseError(_kt('There was a problem deleting the document from the database.'));
... ... @@ -699,35 +699,35 @@ class KTDocumentUtil {
699 699 $oDocument->getFileName() . ' from folder ' .
700 700 Folder::getFolderPath($oDocument->getFolderID()) .
701 701 ' id=' . $oDocument->getFolderID());
702   -
  702 +
703 703 // we use a _real_ transaction here ...
704   -
  704 +
705 705 DBUtil::rollback();
706   -
  706 +
707 707 /*
708 708 //reverse the document deletion
709 709 $oDocument->setStatusID(LIVE);
710 710 $oDocument->update();
711 711 */
712   -
  712 +
713 713 return PEAR::raiseError(_kt('There was a problem deleting the document from storage.'));
714 714 }
715   -
  715 +
716 716 $oDocumentTransaction = & new DocumentTransaction($oDocument, _kt('Document deleted: ') . $sReason, 'ktcore.transactions.delete');
717 717 $oDocumentTransaction->create();
718   -
  718 +
719 719 $oDocument->setFolderID(1);
720   -
  720 +
721 721 DBUtil::commit();
722 722  
723   -
  723 +
724 724 // we weren't doing notifications on this one
725 725 $oSubscriptionEvent = new SubscriptionEvent();
726 726 $oSubscriptionEvent->RemoveDocument($oDocument, $oOrigFolder);
727 727  
728   -
  728 +
729 729 // document is now deleted: triggers are best-effort.
730   -
  730 +
731 731 $oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
732 732 $aTriggers = $oKTTriggerRegistry->getTriggers('delete', 'postValidate');
733 733 foreach ($aTriggers as $aTrigger) {
... ... @@ -743,11 +743,11 @@ class KTDocumentUtil {
743 743 return $ret;
744 744 }
745 745 }
746   -
747   -
  746 +
  747 +
748 748 }
749 749 // }}}
750   -
  750 +
751 751 function reindexDocument($oDocument) {
752 752 $oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
753 753 $aTriggers = $oKTTriggerRegistry->getTriggers('content', 'transform');
... ... @@ -780,7 +780,7 @@ class KTDocumentUtil {
780 780 // 2. update the storage path.
781 781 //print '--------------------------------- BEFORE';
782 782 //print_r($oDocument);
783   -
  783 +
784 784 // grab the "source "data
785 785 $sTable = KTUtil::getTableName('documents');
786 786 $sQuery = 'SELECT * FROM ' . $sTable . ' WHERE id = ?';
... ... @@ -793,7 +793,7 @@ class KTDocumentUtil {
793 793 if (PEAR::isError($id)) { return $id; }
794 794 // we still have a bogus md_version, but integrity holds, so fix it now.
795 795 $oCore = KTDocumentCore::get($id);
796   -
  796 +
797 797 $sTable = KTUtil::getTableName('document_metadata_version');
798 798 $sQuery = 'SELECT * FROM ' . $sTable . ' WHERE id = ?';
799 799 $aParams = array($oDocument->getMetadataVersionId());
... ... @@ -804,7 +804,7 @@ class KTDocumentUtil {
804 804 if (PEAR::isError($id)) { return $id; }
805 805 $oCore->setMetadataVersionId($id);
806 806 $oMDV = KTDocumentMetadataVersion::get($id);
807   -
  807 +
808 808 $sTable = KTUtil::getTableName('document_content_version');
809 809 $sQuery = 'SELECT * FROM ' . $sTable . ' WHERE id = ?';
810 810 $aParams = array($oDocument->_oDocumentContentVersion->getId());
... ... @@ -814,24 +814,24 @@ class KTDocumentUtil {
814 814 $id = DBUtil::autoInsert($sTable, $aContentRow);
815 815 if (PEAR::isError($id)) { return $id; }
816 816 $oMDV->setContentVersionId($id);
817   -
  817 +
818 818 $res = $oCore->update();
819 819 if (PEAR::isError($res)) { return $res; }
820 820 $res = $oMDV->update();
821 821 if (PEAR::isError($res)) { return $res; }
822   -
  822 +
823 823 // now, we have a semi-sane document object. get it.
824 824 $oNewDocument = Document::get($oCore->getId());
825   -
  825 +
826 826 //print '--------------------------------- AFTER';
827 827 //print_r($oDocument);
828 828 //print '======';
829 829 //print_r($oNewDocument);
830   -
  830 +
831 831 // copy the metadata from old to new.
832 832 $res = KTDocumentUtil::copyMetadata($oNewDocument, $oDocument->getMetadataVersionId());
833 833 if (PEAR::isError($res)) { return $res; }
834   -
  834 +
835 835 // finally, copy the actual file.
836 836 $oStorage =& KTStorageManagerUtil::getSingleton();
837 837 $res = $oStorage->copy($oDocument, $oNewDocument);
... ... @@ -844,7 +844,7 @@ class KTDocumentUtil {
844 844 if ($iDocumentPermissionObjectId === $iOriginalFolderPermissionObjectId) {
845 845 $oNewDocument->setPermissionObjectId($oDestinationFolder->getPermissionObjectId());
846 846 }
847   -
  847 +
848 848 $res = $oNewDocument->update();
849 849 if (PEAR::isError($res)) { return $res; }
850 850  
... ... @@ -859,19 +859,19 @@ class KTDocumentUtil {
859 859 DBUtil::autoInsert($sTable, $aInsertValues, array('noid' => true));
860 860 KTDocumentUtil::updateSearchableText($oNewDocument);
861 861 KTPermissionUtil::updatePermissionLookup($oNewDocument);
862   -
  862 +
863 863 if (is_null($sReason)) {
864 864 $sReason = '';
865 865 }
866   -
867   -
  866 +
  867 +
868 868 $oDocumentTransaction = & new DocumentTransaction($oDocument, sprintf(_kt("Copied to folder \"%s\". %s"), $oDestinationFolder->getName(), $sReason), 'ktcore.transactions.copy');
869   - $oDocumentTransaction->create();
  869 + $oDocumentTransaction->create();
870 870  
871 871 $oSrcFolder = Folder::get($oDocument->getFolderID());
872 872 $oDocumentTransaction = & new DocumentTransaction($oNewDocument, sprintf(_kt("Copied from original in folder \"%s\". %s"), $oSrcFolder->getName(), $sReason), 'ktcore.transactions.copy');
873   - $oDocumentTransaction->create();
874   -
  873 + $oDocumentTransaction->create();
  874 +
875 875 return $oNewDocument;
876 876 }
877 877  
... ... @@ -906,19 +906,19 @@ class KTDocumentUtil {
906 906 // create the document transaction record
907 907 $oDocumentTransaction = & new DocumentTransaction($oDocument, _kt('Document renamed'), 'ktcore.transactions.update');
908 908 $oDocumentTransaction->create();
909   -
  909 +
910 910 // fire subscription alerts for the checked in document
911 911 $oSubscriptionEvent = new SubscriptionEvent();
912 912 $oFolder = Folder::get($oDocument->getFolderID());
913 913 $oSubscriptionEvent->ModifyDocument($oDocument, $oFolder);
914   -
915   - return true;
  914 +
  915 + return true;
916 916 }
917   -
  917 +
918 918 function move($oDocument, $oToFolder, $oUser = null, $sReason = null) {
919   -
920   - $oFolder = $oToFolder; // alias.
921   -
  919 +
  920 + $oFolder = $oToFolder; // alias.
  921 +
922 922 $oOriginalFolder = Folder::get($oDocument->getFolderId());
923 923 $iOriginalFolderPermissionObjectId = $oOriginalFolder->getPermissionObjectId();
924 924 $iDocumentPermissionObjectId = $oDocument->getPermissionObjectId();
... ... @@ -949,13 +949,13 @@ class KTDocumentUtil {
949 949  
950 950 $sMoveMessage = sprintf(_kt("Moved from %s/%s to %s/%s. %s"),
951 951 $oOriginalFolder->getFullPath(),
952   - $oOriginalFolder->getName(),
  952 + $oOriginalFolder->getName(),
953 953 $oFolder->getFullPath(),
954 954 $oFolder->getName(),
955 955 $sReason);
956 956  
957 957 // create the document transaction record
958   -
  958 +
959 959 $oDocumentTransaction = & new DocumentTransaction($oDocument, $sMoveMessage, 'ktcore.transactions.move');
960 960 $oDocumentTransaction->create();
961 961  
... ... @@ -975,8 +975,8 @@ class KTDocumentUtil {
975 975 if (PEAR::isError($ret)) {
976 976 return $ret;
977 977 }
978   - }
979   -
  978 + }
  979 +
980 980 return KTPermissionUtil::updatePermissionLookup($oDocument);
981 981 }
982 982  
... ...