check object exists
[web.mtrack] / MTrackWeb / Browse.php
index e37839b..fe85912 100644 (file)
@@ -68,6 +68,11 @@ class MTrackWeb_Browse extends MTrackWeb
         if (!$this->repo->id) {
             $this->repo = false;
         }
+        // if we have an active project.. enforce it..
+        if ($this->currentProject() && $this->repo &&  $this->repo->project_id != $this->currentProject()) {
+            $this->repo =  false; // no repo..
+        }
+        
         
         $this->object = null;
         $this->ident = null;
@@ -253,6 +258,10 @@ class MTrackWeb_Browse extends MTrackWeb
         } else {
             $do->orderBy("shortname ASC");
         }
+        
+        if ($this->currentProject()) {
+            $do->project_id = $this->currentProject();
+        }
 // FIXME -> permissions on repositories goes here.. 
         //$do->ensurePerm($this->authUser); 
         //$do->fetchAll();
@@ -276,7 +285,7 @@ class MTrackWeb_Browse extends MTrackWeb
 
 
 // static..?
-    function getBrowseData($repo, $pi, $object, $ident)
+    function getBrowseData($repo, $pi, $object, $ident, $hashes = false)
     {
          $data = new StdClass;
         $data->dirs = array();
@@ -304,18 +313,24 @@ class MTrackWeb_Browse extends MTrackWeb
         }
         $files = array();
         $dirs = array();
-       
+        
+        $needLog = array();
         if ($repo) {
             //try {
             $ents = $repo->readdir($pi, $object, $ident);
-            $this->cachedChangeEvent($ents);
-            //} catch (Exception $e) {
-                  // Typically a freshly created repo
-              //    $ents = array();
-                //  $data->err = $e->getMessage();
-            //}
-           // echo '<PRE>' ; var_dump($ents); echo '</PRE>' ;
+            if ($hashes === false) { 
+                $ents = $this->cachedChangeEvent($ents);
+            } else {
+                // we are filling the cache..
+                return $this->cachedChangeEventFill($ents, $hashes);
+                 
+            }
+            
             foreach ($ents as $file) {
+                if (isset($file->hash)) {
+                    $needLog[] = $file->hash;
+                }
+                
                 $basename = basename($file->name);
                 if ($file->is_dir) {
                     $dirs[$basename] = $file;
@@ -327,38 +342,19 @@ class MTrackWeb_Browse extends MTrackWeb
         uksort($files, 'strnatcmp');
         uksort($dirs, 'strnatcmp');
 
-        $data->files = array();
-        $data->dirs = array();
-
-        //$urlbase    = $this->baseURL . '/browse.php';
-        //$pathbase   = '/' . $repo->getBrowseRootName();
+         
+        $data->dirs  = array_values($dirs);
+        $data->files = array_values($files);
         
-        foreach ($dirs as $basename => $file) {
-            
-            $ent = $file->getChangeEvent(); //MTrackSCMEvent
-            // let's copy extra stuff into the event..
-            //$ent->url = $urlbase . $pathbase . '/' . $file->name;
-            $ent->basename = $basename;
-            $ent->changelogOne = $ent->changelogOneToHtml();
-            
-            $data->dirs[] = $ent;
-        }
+        $this->repopath = $this->repo->displayName();
+        $this->needLog = $needLog;
+        // gather list 
         
-        foreach ($files as $basename => $file) {
-            $ent = $file->getChangeEvent();
-            if (!$ent) { // skips broken files..
-                continue;
-            }
-            // needed?
-            $ent->basename = $basename;
-            
-            $data->files[] = $ent;
-        }
-
         return $data;
     }
     /**
      * New version of Directory listing
+     * Basically fetching the last change is very slow...
      *  - 1st run through -> fetch all the change events that we have
      *  for ones we do not have, fetch them later..
      *
@@ -369,43 +365,127 @@ class MTrackWeb_Browse extends MTrackWeb
         // build a map...
         $map = array();
         foreach($ar as $e) {
+            $e->basename = basename($e->name);
+            // remove e->rev as it's not valid..
+            $e->rev = false;
             $map[$e->hash] = $e;
+            
         }
         
         $q = DB_DataObject::factory('mtrack_clcache');
         $q->whereAddIn('rev', array_keys($map), 'string');
         $q->repo_id = $this->repo->id;
         $revs = $q->fetchAll('rev', 'sobject');
+        $impl = $this->repo->impl();
+        
+        foreach($revs as $hash => $sobject) {
+            $event = $impl->commitLogToEvent($sobject);
+            // add something??? 
+            if (isset($map[$hash])) {
+                $event->is_dir = $map[$hash]->is_dir;
+                $event->name = $map[$hash]->name;
+                $event->basename = $map[$hash]->basename;
+            }
+            $map[$hash] =  $event; // this was previous only done for directories??? why???
+        }
+        return array_values($map);
+    }
+    
+    function cachedChangeEventFill($ar,$hashes)
+    {
+        $map = array();
+        $impl = $this->repo->impl();
+                
+        foreach($ar as $e) {
+            if (!in_array($e->hash,$hashes)) {
+                continue;
+            }
+            
+            // there is a small chance that concurrent users are looking for the same info..
+            $q = DB_DataObject::factory('mtrack_clcache');
+            $q->rev = $e->hash;
+            $q->repo_id = $this->repo->id;
+            
+            if ($q->find(true)) {
+                $event = $impl->commitLogToEvent($q->sobject);
+            } else { 
+                $ent = $this->repo->history($e->name, 1, 'rev', $e->rev);
+                
+                
+                if (!$ent) {
+                    continue;
+                }
+                $event = $ent[0];
+                // cache it..
+                $q = DB_DataObject::factory('mtrack_clcache');
+                $q->rev = $e->hash;
+                $q->repo_id = $this->repo->id;
+                $q->sobject = $event->commit;
+                $q->insert();
+            }
+            
+            // only copy a few essentials from ent, as we will send it back via json.
+            // these all need escaoing..
+            $add = new stdClass;
+            $add->changelog = $event->changelogOneToHtml($this->link);
+            $add->age = $event->ctimeToHtml($this->link);
+            $add->basename = basename($e->name);
+            $add->changeby = htmlspecialchars($event->changeby); 
+            $add->rev = '<a class="changesetlink browse-link"  href="'. 
+                        htmlspecialchars($this->baseURL) .
+                        '/Changeset/' .
+                        htmlspecialchars($this->repo->displayName()). 
+                        '/'.
+                        htmlspecialchars($event->rev) .
+                        '">'.
+                        htmlspecialchars($event->rev) .
+                        '</a>';
+            
+            $map[$e->hash] = $add;
+            
+            
+        }
+        return $map;
         
         
-        /*
+    
+    }
+    function post($pi)
+    {
+        $this->pi =  $pi . (strlen($pi) ? $this->bootLoader->ext : '');
+       
          
-         need to work out a 'sweet caching method'...
          
-           // var_Dump($this->id);
-        $q = MTrackDB::q('SELECT * FROM clcache where 
-            id = ? AND rev = ?' , $this->repo->id, $file->hash );
-            
-        $ar = $q->fetchAll(PDO::FETCH_ASSOC);
-        if (!empty($ar)) {
-            require_once 'MTrack/SCM/Git/Event.php';
-            $ro = MTrack_SCM_Git_Event::newFromCommit($ar[0]['sobject'], $this->repo);
-          //  var_dump("RETURNING FROM DB");
-            return $ro;
-        }                                        
-        */
+        $this->repo = DB_DataObject::factory('mtrack_repos');
+        $file = $this->repo->loadFromPath($this->pi);
+        if (!$this->repo->id) {
+            $this->jerr("INVALID URL");
+        }
         
-        $ent = $this->repo->history($this->name, 1, 'rev', $this->rev);
+        $this->object = null;
+        $this->ident = null;
         
-        return $ent[0];
-    
-        if ($ent) {
-            MTrackDB::q('INSERT INTO mtrack_clcache (id, rev, sobject) VALUES ( ? ,? ,? )',
-                    $this->repo->id, $this->hash , $ent[0]->commit
-            );
-         
+        if (isset($_GET['jump']) && strlen($_GET['jump'])) {
+            list($this->object, $this->ident) = explode(':', $_GET['jump'], 2);
         }
         
+        if (!$this->repo) {
+            $this->jerr("INVALID URL");
+        }
+        if (!$this->projectPerm($this->repo->project_id, 'MTrack.Repos', 'S')) {
+            $this->jerr("INVALID URL");
+        }
+        if (empty($_POST['hashes'])) {
+            $this->jerr("INVALID URL");
+        }
+        
+        $ar =  $this->getBrowseData($this->repo, $file, $this->object, $this->ident, explode(',', $_POST['hashes']));
+        $this->jdata($ar);
+        exit;
+        
+        
+    }
+    
     
 }