php8
[web.mtrack] / MTrackWeb / Wiki.php
index 5011940..5d18bd1 100644 (file)
@@ -1,7 +1,21 @@
 <?php # vim:ts=2:sw=2:et:
 /* For licensing and copyright terms, see the file named LICENSE */
 
-
+/**
+ *  This is alot simpler now...
+ *
+ *  In our horrifically simple version...
+ *
+ *  We could just use a database, and go from there.
+ *  ... that looses the diff feature... although we can use the mtrack_change stuff....
+ *
+ *  ... That sounds alot better in our context as we can show data on the changelog..
+ *  -----> It will mean that the change log will have to honour our privacy settings..
+ *  
+ *  
+ *  oK - uses new mtrack_wiki table..
+ *
+ */
 
 
 require_once 'MTrack/Attachment.php';
@@ -10,345 +24,112 @@ require_once 'MTrackWeb.php';
 class MTrackWeb_Wiki extends MTrackWeb
 {
     
-    var $template = 'wiki.html';
-    
     var $conflicted = 0;
     var $message = false;
     var $hasHistory = false;
+    var $title;
+    var $authUserArray;
     
     function getAuth()
     {
         return parent::getAuth();
     }
     
-    function get($pi)
+    function get($pi='')
     {
-        $this->pi = empty($pi) ? 'WikiStart' : ($pi . $this->bootLoader->ext);
+        // non 'json' request...
+      
         if (!isset($_REQUEST['ajax_body'])) {
-            $this->title = "Browse: " . $this->pi;
+            $this->title = "Browse: " . $pi;
             return;
         }
-        $this->masterTemplate = 'wiki.html';
         
-        
-        $this->edit = isset($_REQUEST['edit']) ? (int)$_REQUEST['edit'] : false;
-        
-        $this->hasHistory = false;
-        
-        
-        $this->doc = new MTrack_Wiki_Item($this->pi);
-        
-        $this->canEdit = $this->hasPerm('MTrack.wiki','E');
-        // we might add more perms based on project later..
-        
-         if (!$this->canEdit && $this->edit) {
-            return HTML_FlexyFramework::run('Noperm');
-        }
-        $this->hasHistory = $this->doc ? true: false;
-        
-        // blank doc.. on edit..
-        if ($this->doc->file === null && $this->edit) {
-            $this->doc = new MTrack_Wiki_Item($this->pi);
-            $this->doc->content = " = {$this->pi} =\n";
-        }
+        $p = DB_DataObject::Factory('Mtrack_wiki');
+        $p->project_id =  $this->currentProject();
          
 
-        /* now just render */
-
-        $this->title = $this->pi;
-        if ($this->edit) {
-          $this->title .= " (edit)";
-        }
-        
-        $this->canEdit = $this->edit ? false : $this->canEdit ; // if they are editing remove that permission..
+        $p->path = $pi;
        
-        if ($this->doc->file) {
-            $this->evt = $this->doc->file->getChangeEvent();
-            if (!strlen($this->evt->changelog)) {
-                $this->evt->changelog = 'Changed';
+        // we add stuff on the end..
+        if (empty($pi)) {
+            $px = DB_DataObject::Factory('Mtrack_wiki');
+            $px->project_id =  $this->currentProject();
+            $px->orderBy('path');
+            
+            $pr = DB_DataObject::factory('core_project');
+            $pr->get($px->project_id);
+            
+            $ar = $px->fetchAll('path');
+            $wikidata = "## Index of pages in Wiki - {$pr->name} \n\n";
+            foreach($ar as $pp) {
+                $wikidata .= " * [$pp](wiki:$pp)\n";
             }
+            $p->wikidata = $wikidata;
+            $this->jdata($p->toArray());
+            
+            
+            
+            
         }
         
-        if (!$this->edit  && !$this->hasHistory) {
-            if ($this->canEdit) {
-                $this->canCreate = 1;
-            } else {
-                $this->notExist = 1;
-            }
-        }
-        if ($this->edit) {
-            if (isset($_POST['preview'])) {
-                $this->showPreview = true;
-                $this->preview =  MTrackWiki::format_to_html($content);
-            }
-            $this->doc->contentb64= base64_encode($this->doc->content);
-            $this->doc->content = isset($_POST['content']) ? $_POST['content'] : $this->doc->content;
-            $this->comment = isset($_POST['comment']) ? $_POST['comment'] : '';
-
+        //var_dump($p);
+        if (!$p->find(true)) {
+            $p->id = 0;
+            $this->jdata($p->toArray());
         }
-   
-        $action = isset($_GET['action']) ? $_GET['action'] : 'view';
-
-        switch ($action) {
-            case 'view':
-                $this->actionView = 1;
-                break;
-
-            case 'list':
-                $this->actionList = 1;
-                
-                $htree = array();
-                $this->build_help_tree($htree, dirname(__FILE__) . '/../defaults/help');
-                $this->helptree = $htree;
-                
-  
-                /* get the page names into a tree format */
-                $tree = array();
-                $repo  = false; // 
-                $root = MTrack_Wiki_Item::getRepoAndRoot($repo);
-                $suf = MTrackConfig::get('core', 'wikifilenamesuffix'); // fixme!!!!
-                build_tree($tree, $repo, $root, $suf);
-                $this->tree = $tree;
-             
-                break;
-
-            case 'recent':
-                $this->actionRecent = 1;
-                $root = MTrack_Wiki_Item::getRepoAndRoot($repo);
-                $this->recent = $repo->history(null, 100) ;
-                foreach ($this->recent as $e) {
-                    list($e->page) = $e->files;
-                    if (strlen($root)) {
-                        $e->page = substr($e->page, strlen($root)+1);
-                    }
-                }
-                 
-                break;
-
-        }
-    }
-
-    function is_content_conflicted($content)
-    {
-        if (preg_match("/^(<+)\s+(mine|theirs|original)\s*$/m", $content)) {
-            return true;
-        }
-        return false;
+        $this->jdata($p->toArray());
+        //echo '<PRE>';print_r($this);echo '</PRE>';
     }
-    function normalize_text($text) {
-        return rtrim($text) . "\n";
+    function returnNotFound($p) {
+        
+        
+        
+        
+        
     }
-
-    function post()
+    function post($request)
     {
-        $this->get();
-        if (isset($_POST['cancel'])) {
-            header("Location: {$this->baseURL}/Wiki/{$this->pi}");
-            exit;
+        //DB_DataObject::debugLevel(1);
+  
+        $au = $this->getAuthUser();
+        if (!$au) {
+            // autherr?
+            $this->jerr("permission denied");
         }
         
+        $p = DB_DataObject::factory('mtrack_wiki');
+        $p->project_id =  $this->currentProject();
         
-        if (!MTrackCaptcha::check('wiki')) {
-            $this->message = 'CAPTCHA failed, please try again';
-            return;
-        }
-        
-          /* to avoid annoying "you lose because someone else edited" errors,
-           * we compute the diff from the original content we had, and apply
-           * that to the current content of the object */
-
-          $saved = false;
-
-          $orig = base64_decode($_POST['orig']);
-          $content = $_POST['content'];
-
-          /* for consistency, we always want a newline at the end, otherwise
-           * we can end up with some weird output from diff3 */
-          $orig = normalize_text($orig);
-          $content = normalize_text($content);
-          $this->doc->content = normalize_text($this->doc->content);
-          $this->conflicted = is_content_conflicted($content);
-          $tempdir = sys_get_temp_dir();
-
-          if (!$this->conflicted) {
-            $ofile = tempnam($tempdir, "mtrack");
-            $nfile = tempnam($tempdir, "mtrack");
-            $tfile = tempnam($tempdir, "mtrack");
-            $pfile = tempnam($tempdir, "mtrack");
+        $id = empty($_REQUEST['id']) ? 0 : $_REQUEST['id'] * 1;
+        if (!empty($id) && !$p->get($id)) {
             
-            require_once 'System.php';
-            $diff3 = System::which('diff3');
-            if (empty($diff3)) {
-              $diff3 = 'diff3';
-            }
-
-            file_put_contents($ofile, $orig);
-            file_put_contents($nfile, $content);
-            file_put_contents($tfile, $this->doc->content);
-
-            if (PHP_OS == 'SunOS') { // seriously does anyone use SunOS anymore???
-                exec("($diff3 -X $nfile $ofile $tfile ; echo '1,\$p') | ed - $nfile > $pfile",
-                    $output = array(), $retval = 0);
-            } else {
-                exec("$diff3 -mX --label mine $nfile --label original $ofile --label theirs $tfile > $pfile",
-                    $output = array(), $retval = 0);
-            }
-
-            if ($retval == 0) {
-              /* see if there were merge conflicts */
-              $content = '';
-              $mine = preg_quote($nfile, '/');
-              $theirs = preg_quote($tfile, '/');
-              $orig = preg_quote($ofile, '/');
-              $content = file_get_contents($pfile);
-
-              if (PHP_OS == 'SunOS') {
-                $content = str_replace($nfile, 'mine', $content);
-                $content = str_replace($ofile, 'original', $content);
-                $content = str_replace($tfile, 'theirs', $content);
-              }
-            }
-            unlink($ofile);
-            unlink($nfile);
-            unlink($tfile);
-            unlink($pfile);
-
-            $this->conflicted = is_content_conflicted($content);
-          }
-
-          /* keep the merged version for editing purposes */
-          $_POST['content'] = $content;
-          /* our concept of the the original content is now what
-           * is currently saved */
-          $_POST['orig'] = base64_encode($this->doc->content);
-
-          if ($this->conflicted) {
-            $this->message = "Conflicting edits were detected; please correct them before saving";
-          } else {
-            $this->doc->content = $content;
-            try {
-              $cs = MTrackChangeset::begin("wiki:{$this->pi}", $_POST['comment']);
-              $this->doc->save($cs);
-              if (is_array($_FILES['attachments'])) {
-                foreach ($_FILES['attachments']['name'] as $fileid => $name) {
-                  $do_attach = false;
-                  switch ($_FILES['attachments']['error'][$fileid]) {
-                    case UPLOAD_ERR_OK:
-                      $do_attach = true;
-                      break;
-                    case UPLOAD_ERR_NO_FILE:
-                      break;
-                    case UPLOAD_ERR_INI_SIZE:
-                    case UPLOAD_ERR_FORM_SIZE:
-                      $this->message = "Attachment(s) exceed the upload file size limit";
-                      break;
-                    case UPLOAD_ERR_PARTIAL:
-                    case UPLOAD_ERR_CANT_WRITE:
-                      $this->message = "Attachment file upload failed";
-                      break;
-                    case UPLOAD_ERR_NO_TMP_DIR:
-                      $this->message = "Server configuration prevents upload due to missing temporary dir";
-                      break;
-                    case UPLOAD_ERR_EXTENSION:
-                      $this->message = "An extension prevented an upload from running";
-                  }
-                  if ($this->message !== null) {
-                    throw new Exception($this->message);
-                  }
-                  if ($do_attach) {
-                    MTrackAttachment::add("wiki:{$this->pi}",
-                      $_FILES['attachments']['tmp_name'][$fileid],
-                      $_FILES['attachments']['name'][$fileid],
-                      $cs);
-                  }
-                }
-              }
-              MTrackAttachment::process_delete("wiki:{$this->pi}", $cs);
-              $cs->commit();
-              MTrack_Wiki_Item::commitNow();
-              $saved = true;
-            } catch (Exception $e) {
-              $this->message = $e->getMessage();
-            }
-          }
-
-          if ($saved) {
-                /* we're good; go back to view mode */
-                header("Location: {$this->baseURL}/Wiki/{$this->pi}");
-                exit;
-          }
-        }
+            $this->jerr('invalid id');
         
-        function build_help_tree(&$tree, $dir) {
-            foreach (scandir($dir) as $ent) {
-              if ($ent[0] == '.') {
-                continue;
-              }
-              $full = $dir . DIRECTORY_SEPARATOR . $ent;
-              if (is_dir($full)) {
-                $kid = array();
-                $this->build_help_tree($kid, $full);
-                $tree[$ent] = $kid;
-              } else {
-                $tree[$ent] = array();
-              }
-            }
         }
-        function build_tree(&$tree, $repo, $dir, $suf)
-        {
-            $items = $repo->readdir($dir);
-            foreach ($items as $file) {
-              $label = basename($file->name);
-              if ($file->is_dir) {
-                $kid = array();
-                $this->build_tree($kid, $repo, $file->name, $suf);
-                $tree[$label] = $kid;
-              } else {
-                if ($suf && substr($label, -strlen($suf)) == $suf) {
-                  $label = substr($label, 0, strlen($label) - strlen($suf));
-                }
-                $tree[$label] = array();
-              }
-            }
-        }   
-        function emit_tree($root,  $phppage,$parent='')
-        {
-            if (strlen($parent)) {
-              echo "<ul>\n";
-            } else {
-              echo "<ul class='wikitree'>\n";
-            }
-            $knames = array_keys($root);
-            usort($knames, 'strnatcasecmp');
-            foreach ($knames as $key) {
-              $kids = $root[$key];
-              $n = htmlentities($key, ENT_QUOTES, 'utf-8');
-              echo "<li>";
-              if (count($kids)) {
-                echo $n;
-                emit_tree($kids,  $phppage,"$parent$key/");
-              } else {
-                echo "<a href=\"{$this->baseURL}/$phppage/$parent$n\">$n</a>";
-              }
-              echo "</li>\n";
-            }
-            echo "</ul>\n";
+        $o = clone($p);
+        $p->setFrom($_POST);
+        if ($o->project_id != $p->project_id) {
+             $this->jerr("changing project id not permitted");
         }
-
         
-        function renderDeleteList() {
-            return MTrackAttachment::renderDeleteList("wiki:{$this->pi}");
-        }
-        function renderList() {
-            return MTrackAttachment::renderList("wiki:{$this->pi}");
-        }
-              
-
-        function captcha(){
-            return MTrackCaptcha::emit('wiki');
+        // check change of path...
+        if (!$o->id || ($o->path != $p->path)) {
+            $c = DB_DataObject::factory('mtrack_wiki');
+            $c->project_id = $p->project_id;
+            $c->path = $p->path;
+            if ($c->count()) {
+                $this->jerr("Path is not valid - that path already exists");
+            }
         }
-    }
\ No newline at end of file
+        
+        
+        call_user_func(array($p, $o->id ? 'update' : 'insert'),  $o);
+        
+        $this->jok($p->toArray());
+        
+    }
+}
\ No newline at end of file