Commit 6ff10a4fdae5472f092a75ba52ee593cd6c1b8f9

Authored by kevin_fourie
1 parent a8c5ce54

Merged in from DEV trunk...

KTS-2810
"RSS plugin slows down the dashboard"
Fixed. Cut down the number of times the database has to be accessed. Optimised the code.

Committed by: Megan Watson
Reviewed by: Conrad Vermeulen


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/STABLE/branches/3.4.6-Release-Branch@7903 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing 1 changed file with 140 additions and 81 deletions
plugins/rssplugin/KTrss.inc.php
@@ -64,18 +64,42 @@ class KTrss{ @@ -64,18 +64,42 @@ class KTrss{
64 64
65 // Gets full listing of data of documents and folders subscribed to 65 // Gets full listing of data of documents and folders subscribed to
66 function getInternalFeed($iUserId){ 66 function getInternalFeed($iUserId){
67 - $documents=KTrss::getDocuments($iUserId);  
68 - $folders=KTrss::getFolders($iUserId); 67 + $documents = KTrss::getDocuments($iUserId);
  68 + $folders = KTrss::getFolders($iUserId);
  69 +
69 if (is_null($documents)) $documents=array(); 70 if (is_null($documents)) $documents=array();
70 if (is_null($folders)) $folders=array(); 71 if (is_null($folders)) $folders=array();
71 - $aFullList = array_merge($documents,$folders );  
72 - if($aFullList){ 72 +
  73 + $response = '';
  74 + $aFullList = kt_array_merge($documents,$folders );
  75 + if(!empty($aFullList)){
73 $internalFeed = KTrss::arrayToXML($aFullList); 76 $internalFeed = KTrss::arrayToXML($aFullList);
74 $response = rss2arrayBlock($internalFeed); 77 $response = rss2arrayBlock($internalFeed);
75 } 78 }
76 return $response; 79 return $response;
77 } 80 }
78 81
  82 + // Get the data for the document or folder
  83 + function getExternalInternalFeed($sFeed, $iUserId){
  84 + $aRss = array();
  85 + $pos = strpos($sFeed, 'docId');
  86 +
  87 + if($pos === false){
  88 + $pos = strpos($sFeed, 'folderId');
  89 + $folderId = substr($sFeed, $pos+9);
  90 + $aRss[] = KTrss::getOneFolder($folderId);
  91 + }else{
  92 + $docId = substr($sFeed, $pos+6);
  93 + $aRss[] = KTrss::getOneDocument($docId, $iUserId);
  94 + }
  95 +
  96 + if($aRss){
  97 + $internalFeed = KTrss::arrayToXML($aRss);
  98 + $response = rss2arrayBlock($internalFeed);
  99 + }
  100 + return $response;
  101 + }
  102 +
79 // Get list of document subscriptions 103 // Get list of document subscriptions
80 function getDocumentList($iUserId){ 104 function getDocumentList($iUserId){
81 $sQuery = "SELECT document_id as id FROM document_subscriptions WHERE user_id = ?"; 105 $sQuery = "SELECT document_id as id FROM document_subscriptions WHERE user_id = ?";
@@ -95,7 +119,7 @@ class KTrss{ @@ -95,7 +119,7 @@ class KTrss{
95 function getFolderList($iUserId){ 119 function getFolderList($iUserId){
96 $sQuery = "SELECT folder_id as id FROM folder_subscriptions WHERE user_id = ?"; 120 $sQuery = "SELECT folder_id as id FROM folder_subscriptions WHERE user_id = ?";
97 $aParams = array($iUserId); 121 $aParams = array($iUserId);
98 - $aFolderList = DBUtil::getResultArrayKey(array($sQuery, $aParams), 'id'); 122 + $aFolderList = DBUtil::getResultArrayKey(array($sQuery, $aParams), 'id');
99 123
100 if (PEAR::isError($aFolderList)) { 124 if (PEAR::isError($aFolderList)) {
101 // XXX: log error 125 // XXX: log error
@@ -131,9 +155,8 @@ class KTrss{ @@ -131,9 +155,8 @@ class KTrss{
131 $aFList = KTrss::getFolderList($iUserId); 155 $aFList = KTrss::getFolderList($iUserId);
132 156
133 if($aFList){ 157 if($aFList){
134 - foreach($aFList as $folderElement){  
135 - $folder_id = $folderElement['id'];  
136 - $folder = KTrss::getOneFolder($folder_id, $iUserId); 158 + foreach($aFList as $folder_id){
  159 + $folder = KTrss::getOneFolder($folder_id);
137 if($folder){ 160 if($folder){
138 $aFolders[] = $folder; 161 $aFolders[] = $folder;
139 } 162 }
@@ -150,44 +173,53 @@ class KTrss{ @@ -150,44 +173,53 @@ class KTrss{
150 } 173 }
151 174
152 function getChildrenFolderTransactions($iParentFolderId, $depth = '1'){ 175 function getChildrenFolderTransactions($iParentFolderId, $depth = '1'){
153 - if($depth == '1'){  
154 - $sQuery = "SELECT id from folders WHERE parent_folder_ids LIKE ?";  
155 - $aParams = array('%'.$iParentFolderId);  
156 - }//else 176 + $aParams = array($iParentFolderId);
  177 +
  178 + if($depth == '1'){
  179 + // Get direct child folder id's
  180 + $sQuery = "SELECT id FROM folders WHERE parent_id = ?";
  181 + }else{
  182 + // Get all child folders
  183 + if($iParentFolderId == 1){
  184 + $sQuery = "SELECT id FROM folders WHERE parent_folder_ids LIKE '?' OR parent_folder_ids LIKE '?,%'";
  185 + }
  186 + $sQuery = "SELECT id FROM folders WHERE parent_folder_ids LIKE '%,?' OR parent_folder_ids LIKE '%,?,%'";
  187 + $aParams[] = $iParentFolderId;
  188 + }
  189 +
  190 + $aFolderList = DBUtil::getResultArrayKey(array($sQuery, $aParams), 'id');
157 191
158 - $aFolderList = DBUtil::getResultArray(array($sQuery, $aParams));  
159 if (PEAR::isError($aFolderList)) { 192 if (PEAR::isError($aFolderList)) {
160 // XXX: log error 193 // XXX: log error
161 return false; 194 return false;
162 } 195 }
163 - if ($aFolderList) {  
164 - foreach($aFolderList as $folderElement){  
165 - $folder_id = $folderElement['id'];  
166 - $aFolderTransactions = array_merge($aFolderTransactions, KTrss::getFolderTransactions($folder_id));  
167 - }  
168 - }  
169 - if ($aFolderTransactions){  
170 - return $aFolderTransactions;  
171 - } 196 + return $aFolderList;
172 } 197 }
173 198
174 function getChildrenDocumentTransactions($iParentFolderId, $depth = '1'){ 199 function getChildrenDocumentTransactions($iParentFolderId, $depth = '1'){
175 - if($depth == '1'){  
176 - $sQuery = "SELECT id from documents WHERE parent_folder_ids LIKE ? ";  
177 - $aParams = array('%'.$iParentFolderId);  
178 - }//else 200 + $aParams = array($iParentFolderId);
  201 +
  202 + if($depth == '1'){
  203 + // Get direct child document id's
  204 + $sQuery = "SELECT id FROM documents WHERE folder_id = ?";
  205 + }else{
  206 + // Get all documents in child folders
  207 + if($iParentFolderId == 1){
  208 + $sQuery = "SELECT id FROM documents WHERE parent_folder_ids LIKE '?' OR parent_folder_ids LIKE '?,%'";
  209 + }
  210 + $sQuery = "SELECT id FROM documents WHERE parent_folder_ids LIKE '%,?' OR parent_folder_ids LIKE '%,?,%'";
  211 + $aParams[] = $iParentFolderId;
  212 + }
179 213
180 - $aDocumentList = DBUtil::getResultArray(array($sQuery, $aParams)); 214 + $aDocumentList = DBUtil::getResultArrayKey(array($sQuery, $aParams), 'id');
181 215
182 if (PEAR::isError($aDocumentList)) { 216 if (PEAR::isError($aDocumentList)) {
183 // XXX: log error 217 // XXX: log error
184 return false; 218 return false;
185 } 219 }
  220 +
186 if ($aDocumentList) { 221 if ($aDocumentList) {
187 - foreach($aDocumentList as $documentElement){  
188 - $document_id = $documentElement['id'];  
189 - $aDocumentTransactions = kt_array_merge($aDocumentTransactions, KTrss::getDocumentTransactions($document_id));  
190 - } 222 + $aDocumentTransactions = KTrss::getDocumentTransactions($aDocumentList);
191 } 223 }
192 if ($aDocumentTransactions){ 224 if ($aDocumentTransactions){
193 return $aDocumentTransactions; 225 return $aDocumentTransactions;
@@ -197,7 +229,8 @@ class KTrss{ @@ -197,7 +229,8 @@ class KTrss{
197 // get information on document 229 // get information on document
198 function getOneDocument($iDocumentId, $iUserId){ 230 function getOneDocument($iDocumentId, $iUserId){
199 $aDData = KTrss::getDocumentData($iUserId, $iDocumentId); 231 $aDData = KTrss::getDocumentData($iUserId, $iDocumentId);
200 - $aDTransactions = KTrss::getDocumentTransactions($iDocumentId); 232 + $aDTransactions = KTrss::getDocumentTransactions(array($iDocumentId));
  233 +
201 if($aDData){ 234 if($aDData){
202 $aDData['itemType'] = 'document'; 235 $aDData['itemType'] = 'document';
203 236
@@ -219,22 +252,41 @@ class KTrss{ @@ -219,22 +252,41 @@ class KTrss{
219 252
220 // get information for folder 253 // get information for folder
221 function getOneFolder($iFolderId){ 254 function getOneFolder($iFolderId){
  255 + $aFolder = array();
222 $aFData = KTrss::getFolderData($iFolderId); 256 $aFData = KTrss::getFolderData($iFolderId);
223 - $aFTransactions = kt_array_merge(KTrss::getChildrenFolderTransactions($iFolderId), KTrss::getFolderTransactions($iFolderId));  
224 - $aFTransactions = kt_array_merge($aFTransactions, KTrss::getChildrenDocumentTransactions($iFolderId));  
225 257
226 - $code = 'if (strtotime($a[datetime]) == strtotime($b[datetime])){  
227 - return 0;  
228 - }  
229 - return (strtotime($a[datetime]) > strtotime($b[datetime])) ? -1 : 1;'; 258 + if (PEAR::isError($aFData)) {
  259 + return false;
  260 + }
230 261
231 - $compare = create_function('$a,$b', $code); 262 + // Get child folder ids
  263 + $aFolderIds = KTrss::getChildrenFolderTransactions($iFolderId);
232 264
233 - usort($aFTransactions, $compare);  
234 - for($i=0; $i<4; $i++){  
235 - $aFTransactions_new[] = $aFTransactions[$i];  
236 - }  
237 - $aFTransactions = $aFTransactions_new; 265 + // Get folder transactions
  266 + $aFolderIds[] = $iFolderId;
  267 + $aFTransactions = KTrss::getFolderTransactions($aFolderIds);
  268 +
  269 + if(PEAR::isError($aFTransactions)){
  270 + return false;
  271 + }
  272 +
  273 + // Get child document transactions
  274 + $aDocTransactions = KTrss::getChildrenDocumentTransactions($iFolderId);
  275 +
  276 + if(!empty($aDocTransactions)){
  277 + $aFTransactions = array_merge($aFTransactions, $aDocTransactions);
  278 +
  279 + // Sort the child folder and document transactions by date and reduce to 4
  280 + $code = 'if (strtotime($a[datetime]) == strtotime($b[datetime])){
  281 + return 0;
  282 + }
  283 + return (strtotime($a[datetime]) > strtotime($b[datetime])) ? -1 : 1;';
  284 +
  285 + $compare = create_function('$a,$b', $code);
  286 +
  287 + usort($aFTransactions, $compare);
  288 + $aFTransactions = array_slice($aFTransactions, 0, 4);
  289 + }
238 290
239 if($aFData){ 291 if($aFData){
240 $aFData['itemType'] = 'folder'; 292 $aFData['itemType'] = 'folder';
@@ -245,10 +297,6 @@ class KTrss{ @@ -245,10 +297,6 @@ class KTrss{
245 297
246 $aFolder[] = $aFData; 298 $aFolder[] = $aFData;
247 $aFolder[] = $aFTransactions; 299 $aFolder[] = $aFTransactions;
248 - $aFolderBox[] = $aFolder;  
249 - }  
250 - if (PEAR::isError($aFData)) {  
251 - return false;  
252 } 300 }
253 if ($aFolder){ 301 if ($aFolder){
254 return $aFolder; 302 return $aFolder;
@@ -271,7 +319,15 @@ class KTrss{ @@ -271,7 +319,15 @@ class KTrss{
271 // Build path to host 319 // Build path to host
272 $aPath = explode('/', trim($_SERVER['PHP_SELF'])); 320 $aPath = explode('/', trim($_SERVER['PHP_SELF']));
273 global $default; 321 global $default;
274 - $hostPath = "http" . ($default->sslEnabled ? "s" : "") . "://".$_SERVER['HTTP_HOST']."/".$aPath[1]."/"; 322 + if(count($aPath) > 2){
  323 + for($i = 0; $i < count($aPath)-1; $i++){
  324 + $sSuffix .= $aPath[$i];
  325 + }
  326 + $sSuffix = $aPath[1]."/";
  327 + }else{
  328 + $sSuffix = '';
  329 + }
  330 + $hostPath = "http" . ($default->sslEnabled ? "s" : "") . "://".$_SERVER['HTTP_HOST']."/".$sSuffix;
275 $feed = "<?xml version=\"1.0\"?>\n"; 331 $feed = "<?xml version=\"1.0\"?>\n";
276 $feed .= "<rss version=\"2.0\">\n". 332 $feed .= "<rss version=\"2.0\">\n".
277 "<channel>\n" . 333 "<channel>\n" .
@@ -331,7 +387,7 @@ class KTrss{ @@ -331,7 +387,7 @@ class KTrss{
331 "&lt;td&gt;&lt;/td&gt;\n". 387 "&lt;td&gt;&lt;/td&gt;\n".
332 "&lt;/tr&gt;\n". 388 "&lt;/tr&gt;\n".
333 "&lt;/table&gt;&lt;br&gt;\n". 389 "&lt;/table&gt;&lt;br&gt;\n".
334 - "Transaction Summary (Last 3)\n". 390 + "Transaction Summary (Last 4)\n".
335 "&lt;hr&gt;\n". 391 "&lt;hr&gt;\n".
336 "&lt;table width='100%'&gt;\n"; 392 "&lt;table width='100%'&gt;\n";
337 foreach($aItems[1] as $item){ 393 foreach($aItems[1] as $item){
@@ -618,38 +674,41 @@ class KTrss{ @@ -618,38 +674,41 @@ class KTrss{
618 } 674 }
619 675
620 // get a listing of the latest 3 transactions for a document 676 // get a listing of the latest 3 transactions for a document
621 - function getDocumentTransactions($iDocumentId){  
622 - $sQuery = "SELECT DT.datetime AS datetime, 'Document' AS type, DMV.name, D.full_path AS fullpath, DTT.name AS transaction_name, U.name AS user_name, DT.version AS version, DT.comment AS comment " .  
623 - "FROM document_transactions AS DT INNER JOIN users AS U ON DT.user_id = U.id " .  
624 - "INNER JOIN document_transaction_types_lookup AS DTT ON DTT.namespace = DT.transaction_namespace " .  
625 - "LEFT JOIN document_metadata_version AS DMV ON DT.document_id = DMV.document_id " .  
626 - "LEFT JOIN documents AS D ON DT.document_id = D.id " .  
627 - "WHERE DT.document_id = ? " .  
628 - "ORDER BY DT.datetime DESC " .  
629 - "LIMIT 4";  
630 -  
631 - $aParams = array($iDocumentId);  
632 - $aDocumentTransactions = DBUtil::getResultArray(array($sQuery, $aParams));  
633 - if($aDocumentTransactions){  
634 - return $aDocumentTransactions;  
635 - } 677 + function getDocumentTransactions($aDocumentIds){
  678 + $sDocumentIds = implode(', ', $aDocumentIds);
  679 +
  680 + $sQuery = "SELECT DT.datetime AS datetime, 'Document' AS type, DMV.name, D.full_path AS fullpath,
  681 + DTT.name AS transaction_name, U.name AS user_name, DT.version AS version, DT.comment AS comment
  682 + FROM document_transactions AS DT
  683 + INNER JOIN users AS U ON DT.user_id = U.id
  684 + INNER JOIN document_transaction_types_lookup AS DTT ON DTT.namespace = DT.transaction_namespace
  685 + LEFT JOIN documents AS D ON DT.document_id = D.id
  686 + LEFT JOIN document_metadata_version AS DMV ON D.metadata_version_id = DMV.id
  687 + WHERE DT.document_id IN ($sDocumentIds)
  688 + ORDER BY DT.datetime DESC
  689 + LIMIT 4";
  690 +
  691 + $aDocumentTransactions = DBUtil::getResultArray($sQuery);
  692 + if(!PEAR::isError($aDocumentTransactions)){
  693 + return $aDocumentTransactions;
  694 + }
636 } 695 }
637 696
638 - // Get a listing of the latest 3 transactions for a folder  
639 - function getFolderTransactions($iFolderId){  
640 - $sQuery = "SELECT FT.datetime AS datetime, 'Folder' AS type, F.name, F.full_path AS fullpath, DTT.name AS transaction_name, U.name AS user_name, FT.comment AS comment " .  
641 - "FROM folder_transactions AS FT LEFT JOIN users AS U ON FT.user_id = U.id " .  
642 - "LEFT JOIN document_transaction_types_lookup AS DTT ON DTT.namespace = FT.transaction_namespace " .  
643 - "LEFT JOIN folders AS F ON FT.folder_id = F.id " .  
644 - "WHERE FT.folder_id = ? " .  
645 - "ORDER BY FT.datetime DESC " .  
646 - "LIMIT 4";  
647 -  
648 - $aParams = array($iFolderId);  
649 - $aFolderTransactions = DBUtil::getResultArray(array($sQuery, $aParams));  
650 - if($iFolderId){  
651 - return $aFolderTransactions;  
652 - } 697 + // Get a listing of the latest transactions for a folder and its child folders
  698 + function getFolderTransactions($aFolderIds){
  699 + $sFolderIds = implode(', ', $aFolderIds);
  700 +
  701 + $sQuery = "SELECT FT.datetime AS datetime, 'Folder' AS type, F.name, F.full_path AS fullpath,
  702 + DTT.name AS transaction_name, U.name AS user_name, FT.comment AS comment
  703 + FROM folder_transactions AS FT LEFT JOIN users AS U ON FT.user_id = U.id
  704 + LEFT JOIN document_transaction_types_lookup AS DTT ON DTT.namespace = FT.transaction_namespace
  705 + LEFT JOIN folders AS F ON FT.folder_id = F.id
  706 + WHERE FT.folder_id IN ($sFolderIds)
  707 + ORDER BY FT.datetime DESC
  708 + LIMIT 4";
  709 +
  710 + $aFolderTransactions = DBUtil::getResultArray($sQuery);
  711 + return $aFolderTransactions;
653 } 712 }
654 } 713 }
655 -?> 714 -?>
  715 +?>
656 \ No newline at end of file 716 \ No newline at end of file