SF.net SVN: phpwiki:[11116] trunk

carstenklapp--- via phpwiki-checkins <[email protected]> Sun, 16 Feb 2025 23:10:14 +0000
Newsgroups gmane.comp.web.wiki.phpwiki.checkins
Message-ID <[email protected]>
Revision: 11116
          http://sourceforge.net/p/phpwiki/code/11116
Author:   carstenklapp
Date:     2025-02-16 23:10:14 +0000 (Sun, 16 Feb 2025)
Log Message:
-----------
Fixed broken Merge edit functionality by moving some warning logic from editpage.tmpl into lib/editpage.php. Added Merge-edit diff preview. Prevent editing of nonexistent page revision unless creating a new page (anti-bot).

Modified Paths:
--------------
    trunk/lib/editpage.php
    trunk/pgsrc/ReleaseNotes
    trunk/themes/default/templates/editpage.tmpl

Modified: trunk/lib/editpage.php
===================================================================
--- trunk/lib/editpage.php	2025-02-16 22:30:49 UTC (rev 11115)
+++ trunk/lib/editpage.php	2025-02-16 23:10:14 UTC (rev 11116)
@@ -130,6 +130,7 @@
         $tokens['PAGE_LOCKED_MESSAGE'] = '';
         $tokens['LOCK_CHANGED_MSG'] = '';
         $tokens['CONCURRENT_UPDATE_MESSAGE'] = '';
+        $tokens['EDITING_OLD_REVISION_MESSAGE'] = '';
         $r =& $this->request;
 
         if (isset($r->args['pref']['editWidth'])
@@ -196,9 +197,9 @@
         }
 
         if ($saveFailed and $this->isConcurrentUpdate()) {
-             //get a diff preview before inserting merged content into editarea
-             $tokens['PREVIEW_CONTENT'] = $this->getDiff();
-             // Get the text of the original page, and the two conflicting edits
+            //get a diff preview before inserting merged content into editarea
+            $tokens['PREVIEW_CONTENT'] = $this->getDiff();
+            // Get the text of the original page, and the two conflicting edits
             // The diff3 class takes arrays as input.  So retrieve content as
             // an array, or convert it as necesary.
             $orig = $this->page->getRevision($this->_currentVersion);
@@ -222,7 +223,7 @@
                 HTML(
                     HTML::h2(_("Some internal editing error")),
                     HTML::p(_("Your are probably trying to edit/create an invalid version of this page.")),
-                    HTML::p(HTML::em(_("&version=-1 might help.")))
+                    HTML::p(HTML::em("&version=-1 might help."))
                 );
         }
 
@@ -236,7 +237,29 @@
             $tokens['PREVIEW_CONTENT'] = $this->getDiff();
         }
 
-        // FIXME: NOT_CURRENT_MESSAGE?
+        $selectedversionno = $this->selected->getVersion();
+        $currentversionno = $this->current->getVersion();
+        if (($selectedversionno > 0) && ($selectedversionno < $currentversionno)) {
+            $tokens['EDITING_OLD_REVISION_MESSAGE'] = $this->getEditOldmsg();
+        }
+        if (($currentversionno > 0) && ($selectedversionno < 1)) {
+            // version==0, editing noexistant version of page, possible manual url
+            // manipulation or bot, redirect to browsing
+            $this->_redirectToBrowsePage(); //todo: redirect to editing current version page instead
+            return false; //?
+        }
+        // This function needs more work so leaving the below in for future debugging.
+        // if (($currentversionno==0) && ($selectedversionno ==0)) {
+        //     // creating new page, do nothing
+        // }
+        // echo "<pre>\n";
+        // echo "\n\$this->editaction " . $this->editaction;
+        // echo "\n\$this->request->getArg('merge') " . $this->request->getArg('merge');
+        // echo "\n\$currentversionno " . $currentversionno;
+        // echo "\n\$selectedversionno " . $selectedversionno;
+        // echo "</pre>\n";
+        // die();
+
         $tokens = array_merge($tokens, $this->getFormElements());
 
         return $this->output('editpage', _("Edit: %s"));
@@ -570,8 +593,22 @@
         return new TransformedText($this->page, $this->_content, $this->meta);
     }
 
-    private function getDiff()
+    public function getEditOldmsg()
     {
+        $html = HTML();
+        $html->pushContent(
+            HTML::p(
+                array('class' => "warning_msg"),
+                        HTML::span(array('class' => "bold"), _("Warning:")),
+                    " " ._("You are editing an old version of this page.")
+                    ." ". _("Saving this page will overwrite the current version.")
+                ),
+        );
+        return $html;
+    }
+
+    public function getDiff()
+    {
         require_once 'lib/diff.php';
         $html = HTML();
 
@@ -1274,36 +1311,54 @@
         }
 
         if ($saveFailed || $this->isConcurrentUpdate()) {
+            //get a diff preview before inserting merged content into editarea
+            $tokens['PREVIEW_CONTENT'] = $this->getDiff();
             // Get the text of the original page, and the two conflicting edits
             // The diff class takes arrays as input.  So retrieve content as
             // an array, or convert it as necesary.
             $orig = $this->page->getRevision($this->_currentVersion);
             $this_content = explode("\n", $this->_content);
+            $orig_content = $orig->getContent();
             $other_content = $this->current->getContent();
-            require_once 'lib/diff.php';
-            $diff2 = new Diff($other_content, $this_content);
-            $context_lines = max(
-                4,
-                count($other_content) + 1,
-                count($this_content) + 1
-            );
-            $fmt = new BlockDiffFormatter($context_lines);
+            // require_once 'lib/diff.php';
+            // $diff2 = new Diff($other_content, $this_content);
+            require_once 'lib/diff3.php';
+            $diff2 = new diff3($orig_content, $this_content, $other_content);
+            $output = $diff2->merged_output(_("Loaded version"), _("Existing version"));
+            $this->_content = implode("\n", $output);
 
-            $this->_content = $fmt->format($diff2);
+            // $context_lines = max(
+            //     4,
+            //     count($other_content) + 1,
+            //     count($this_content) + 1
+            // );
+            // $fmt = new BlockDiffFormatter($context_lines);
+            // $this->_content = $fmt->format($diff2);
             // FIXME: integrate this into class BlockDiffFormatter
-            $this->_content = str_replace(
-                ">>>>>>>\n<<<<<<<\n",
-                "=======\n",
-                $this->_content
-            );
-            $this->_content = str_replace(
-                "<<<<<<<\n>>>>>>>\n",
-                "=======\n",
-                $this->_content
-            );
-
+            // $this->_content = str_replace(
+            //     ">>>>>>>\n<<<<<<<\n",
+            //     " ~=======\n",
+            //     $this->_content
+            // );
+            // $this->_content = str_replace(
+            //     "<<<<<<<\n>>>>>>>\n",
+            //     " ~=======\n",
+            //     $this->_content
+            // );
+            // // reformat diff markers to prevent from being interpreted as markup
+            // $this->_content = str_replace(
+            //     "<<<<<<<\n",
+            //     " ~<<<<<<<\n",
+            //     $this->_content
+            // );
+            // $this->_content = str_replace(
+            //     ">>>>>>>\n",
+            //     " ~>>>>>>>\n",
+            //     $this->_content
+            // );
+   
             $this->_currentVersion = $this->current->getVersion();
-            $this->version = $this->_currentVersion;
+            $this->version = $this->_currentVersion;//fixme: is this correct? apples to EDITING_OLD_REVISION_MESSAGE also. $this->selected->getVersion()
             $tokens['CONCURRENT_UPDATE_MESSAGE'] = $this->getConflictMessage();
         }
 
@@ -1314,7 +1369,11 @@
             $tokens['PREVIEW_CONTENT'] = $this->getPreview();
         } // FIXME: convert to _MESSAGE?
 
-        // FIXME: NOT_CURRENT_MESSAGE?
+        // FIXME: NOT_CURRENT_MESSAGE? ok trying
+        if (($this->version > 0) && ($this->version < $this->_currentVersion)) {
+            $tokens['EDITING_OLD_REVISION_MESSAGE'] = $this->getEditOldmsg();
+        }
+
         $tokens = array_merge($tokens, $this->getFormElements());
         // we need all GET params for loadfile overwrite
         if ($this->request->getArg('action') == 'loadfile') {

Modified: trunk/pgsrc/ReleaseNotes
===================================================================
--- trunk/pgsrc/ReleaseNotes	2025-02-16 22:30:49 UTC (rev 11115)
+++ trunk/pgsrc/ReleaseNotes	2025-02-16 23:10:14 UTC (rev 11116)
@@ -1,4 +1,4 @@
-Date: Sun, 16 Feb 2025 22:23:47 +0000
+Date: Sun, 16 Feb 2025 22:40:56 +0000
 Mime-Version: 1.0 (Produced by PhpWiki 1.6.5)
 Content-Type: application/x-phpwiki;
   pagename=ReleaseNotes;
@@ -52,9 +52,12 @@
   - Dumped pages now use unix line endings instead of windows. Import of files
     with windows line endings should still work.
 * configurator.php is now in a less-broken state. Useable but needs more work.
-* New: Show a nice diff preview when editing a page if there is an edit conflict.
+* New: Show a nice diff preview when editing a page if there is an edit
+  conflict.
 * New: Prevent diff markers from being rendered as markup when editing a
   conflict or merging a dumpload.
+* Fixed broken Merge edit functionality when loading dumps, also added diff
+  preview.
 
 == 1.6.4 2024-03-13 Marc-Etienne Vargenau, Christof Meerwald ==
 

Modified: trunk/themes/default/templates/editpage.tmpl
===================================================================
--- trunk/themes/default/templates/editpage.tmpl	2025-02-16 22:30:49 UTC (rev 11115)
+++ trunk/themes/default/templates/editpage.tmpl	2025-02-16 23:10:14 UTC (rev 11116)
@@ -9,13 +9,7 @@
 <?php } ?>
 <?php echo $PAGE_LOCKED_MESSAGE ?>
 <?php echo $CONCURRENT_UPDATE_MESSAGE ?>
-<?php if (!$revision->isCurrent() && !$revision->hasDefaultContents()) { ?>
-      <p class="warning_msg">
-      <strong><?php echo _("Warning")._(": ") ?></strong>
-      <?php echo _("You are editing an old version.") ?>
-      <?php echo _("Saving this page will overwrite the current version.") ?>
-      </p>
-<?php } ?>
+<?php echo $EDITING_OLD_REVISION_MESSAGE ?>
 <script type="text/javascript">
 <!--//
 $(document).ready(function() {

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.