From 45a9933524f9a31e0a104cccfd082217501017ba Mon Sep 17 00:00:00 2001
From: Adrian Lang <lang@cosmocode.de>
Date: Tue, 9 Mar 2010 11:42:34 +0100
Subject: [PATCH] Move data preprocessing out of html_edit

---
 doku.php         |  4 +++-
 inc/actions.php  | 37 +++++++++++++++++++++++++++++++--
 inc/html.php     | 54 ++++++++++++------------------------------------
 inc/template.php | 16 ++++++--------
 4 files changed, 57 insertions(+), 54 deletions(-)

diff --git a/doku.php b/doku.php
index 5f2d2c582..95d5c3101 100644
--- a/doku.php
+++ b/doku.php
@@ -39,7 +39,9 @@ $RANGE = $_REQUEST['range'];
 $HIGH  = $_REQUEST['s'];
 if(empty($HIGH)) $HIGH = getGoogleQuery();
 
-$TEXT  = cleanText($_POST['wikitext']);
+if (isset($_POST['wikitext'])) {
+    $TEXT  = cleanText($_POST['wikitext']);
+}
 $PRE   = cleanText($_POST['prefix']);
 $SUF   = cleanText($_POST['suffix']);
 $SUM   = $_REQUEST['summary'];
diff --git a/inc/actions.php b/inc/actions.php
index b7567bc20..5802bd6f5 100644
--- a/inc/actions.php
+++ b/inc/actions.php
@@ -111,7 +111,7 @@ function act_dispatch(){
             $ACT = act_draftsave($ACT);
 
         //edit
-        if(($ACT == 'edit' || $ACT == 'preview') && $INFO['editable']){
+        if(($ACT == 'edit' || $ACT == 'preview' || $ACT == 'recover') && $INFO['editable']){
             $ACT = act_edit($ACT);
         }else{
             unlock($ID); //try to unlock
@@ -442,7 +442,7 @@ function act_auth($act){
 }
 
 /**
- * Handle 'edit', 'preview'
+ * Handle 'edit', 'preview', 'recover'
  *
  * @author Andreas Gohr <andi@splitbrain.org>
  */
@@ -450,6 +450,39 @@ function act_edit($act){
     global $ID;
     global $INFO;
 
+    global $TEXT;
+    global $RANGE;
+    global $PRE;
+    global $SUF;
+    global $REV;
+    global $SUM;
+    global $lang;
+    global $DATE;
+
+    if (!isset($TEXT)) {
+        if ($INFO['exists']) {
+            if ($RANGE) {
+                list($PRE,$TEXT,$SUF) = rawWikiSlices($RANGE,$ID,$REV);
+            } else {
+                $TEXT = rawWiki($ID,$REV);
+            }
+        } else {
+            $data = array($ID);
+            $TEXT = trigger_event('HTML_PAGE_FROMTEMPLATE',$data,'pageTemplate',true);
+        }
+    }
+
+    //set summary default
+    if(!$SUM){
+        if($REV){
+            $SUM = $lang['restored'];
+        }elseif(!$INFO['exists']){
+            $SUM = $lang['created'];
+        }
+    }
+
+    if(!$DATE) $DATE = $INFO['lastmod'];
+
     //check if locked by anyone - if not lock for my self
     $lockedby = checklock($ID);
     if($lockedby) return 'locked';
diff --git a/inc/html.php b/inc/html.php
index d9766e543..ccab83a24 100644
--- a/inc/html.php
+++ b/inc/html.php
@@ -1134,54 +1134,26 @@ function html_edit($text=null,$include='edit'){ //FIXME: include needed?
     global $ID;
     global $REV;
     global $DATE;
-    global $RANGE;
     global $PRE;
     global $SUF;
     global $INFO;
     global $SUM;
     global $lang;
     global $conf;
+    global $TEXT;
 
-    //set summary default
-    if(!$SUM){
-        if($REV){
-            $SUM = $lang['restored'];
-        }elseif(!$INFO['exists']){
-            $SUM = $lang['created'];
-        }
-    }
-
-    //no text? Load it!
-    if(!isset($text)){
-        $pr = false; //no preview mode
-        if($INFO['exists']){
-            if($RANGE){
-                list($PRE,$text,$SUF) = rawWikiSlices($RANGE,$ID,$REV);
-            }else{
-                $text = rawWiki($ID,$REV);
-            }
-            $check = md5($text);
-            $mod = false;
-        }else{
-            //try to load a pagetemplate
-            $data = array($ID);
-            $text = trigger_event('HTML_PAGE_FROMTEMPLATE',$data,'pageTemplate',true);
-            $check = md5('');
-            $mod = $text!=='';
-        }
-    }else{
-        $pr = true; //preview mode
-        if (isset($_REQUEST['changecheck'])) {
-            $check = $_REQUEST['changecheck'];
-            $mod = md5($text)!==$check;
-        } else {
-            // Why? Assume default text is unmodified.
-            $check = md5($text);
-            $mod = false;
-        }
+    if (isset($_REQUEST['changecheck'])) {
+        $check = $_REQUEST['changecheck'];
+    } elseif(!$INFO['exists']){
+        // $TEXT has been loaded from page template
+        $check = md5('');
+    } else {
+        $check = md5($TEXT);
     }
+    $mod = md5($TEXT) !== $check;
 
     $wr = $INFO['writable'] && !$INFO['locked'];
+    $include = 'edit';
     if($wr){
         if ($REV) $include = 'editrev';
     }else{
@@ -1192,7 +1164,6 @@ function html_edit($text=null,$include='edit'){ //FIXME: include needed?
         }
         $include = 'read';
     }
-    if(!$DATE) $DATE = $INFO['lastmod'];
 
     global $license;
 
@@ -1204,7 +1175,7 @@ function html_edit($text=null,$include='edit'){ //FIXME: include needed?
     $form->addHidden('suffix', $SUF);
     $form->addHidden('changecheck', $check);
 
-    $data = compact('wr', 'text', 'form');
+    $data = compact('wr', 'form');
     $data['media_manager'] = true;
     $data['intro_locale'] = $include;
     trigger_event('HTML_EDIT_FORMSELECTION', $data, 'html_edit_form', true);
@@ -1266,10 +1237,11 @@ function html_edit($text=null,$include='edit'){ //FIXME: include needed?
  * @triggers HTML_EDITFORM_OUTPUT
  */
 function html_edit_form($param) {
+    global $TEXT;
     extract($param);
     $attr = array('tabindex'=>'1');
     if (!$wr) $attr['readonly'] = 'readonly';
-    $form->addElement(form_makeWikiText($text, $attr));
+    $form->addElement(form_makeWikiText($TEXT, $attr));
 }
 
 /**
diff --git a/inc/template.php b/inc/template.php
index f15b51694..371ef2a7b 100644
--- a/inc/template.php
+++ b/inc/template.php
@@ -60,15 +60,15 @@ function tpl_content_core(){
         case 'show':
             html_show();
             break;
-        case 'preview':
-            html_edit($TEXT);
-            html_show($TEXT);
-            break;
+        case 'locked':
+            html_locked();
+        case 'edit':
         case 'recover':
-            html_edit($TEXT);
+            html_edit();
             break;
-        case 'edit':
+        case 'preview':
             html_edit();
+            html_show($TEXT);
             break;
         case 'draft':
             html_draft();
@@ -104,10 +104,6 @@ function tpl_content_core(){
             html_conflict(con($PRE,$TEXT,$SUF),$SUM);
             html_diff(con($PRE,$TEXT,$SUF),false);
             break;
-        case 'locked':
-            html_locked();
-            html_edit();
-            break;
         case 'login':
             html_login();
             break;
-- 
GitLab