openrat-cms

OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs | README

commit 8bdb3c1fc6d5a6caef1112b1d4b600f26854654e
parent 684e26d41446e2649c18b49c0498f8ecac9dd32c
Author: Jan Dankert <develop@jandankert.de>
Date:   Fri, 13 Nov 2020 21:27:57 +0100

Clearer code for text diff, cleaned up the view.

Diffstat:
Mmodules/cms/action/PageelementAction.class.php | 26++++++++------------------
Mmodules/cms/ui/themes/default/html/views/pageelement/history.php | 18+++++++++---------
Mmodules/cms/ui/themes/default/html/views/pageelement/history.tpl.src.xml | 18+++++++++---------
Mmodules/util/Text.class.php | 84++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
4 files changed, 77 insertions(+), 69 deletions(-)

diff --git a/modules/cms/action/PageelementAction.class.php b/modules/cms/action/PageelementAction.class.php @@ -744,26 +744,16 @@ class PageelementAction extends BaseAction $text2 = explode("\n",$value2->text); // Unterschiede feststellen. - $res_diff = Text::diff($text1,$text2); + $diffResult = Text::diff($text1,$text2); - list( $text1,$text2 ) = $res_diff; - - $diff = array(); - $i = 0; - while( isset($text1[$i]) || isset($text2[$i]) ) - { - $line = array(); - - if ( isset($text1[$i]['text']) ) - $line['left'] = $text1[$i]; - - if ( isset($text2[$i]['text']) ) - $line['right'] = $text2[$i]; + $outputResult = array_map( function( $left,$right) { + return [ + 'left' => $left, + 'right'=> $right + ]; + },$diffResult[0],$diffResult[1] ); - $i++; - $diff[] = $line; - } - $this->setTemplateVar('diff',$diff ); + $this->setTemplateVar('diff',$outputResult ); } diff --git a/modules/cms/ui/themes/default/html/views/pageelement/history.php b/modules/cms/ui/themes/default/html/views/pageelement/history.php @@ -30,15 +30,15 @@ </span> </td> <td class="<?php echo O::escapeHtml('or-help') ?>"><?php echo O::escapeHtml('') ?> - <span><?php echo O::escapeHtml(''.@O::lang('DATE').'') ?> + <span><?php echo O::escapeHtml(''.@O::lang('VALUE').'') ?> </span> </td> <td class="<?php echo O::escapeHtml('or-help') ?>"><?php echo O::escapeHtml('') ?> - <span><?php echo O::escapeHtml(''.@O::lang('USER').'') ?> + <span><?php echo O::escapeHtml(''.@O::lang('DATE').'') ?> </span> </td> <td class="<?php echo O::escapeHtml('or-help') ?>"><?php echo O::escapeHtml('') ?> - <span><?php echo O::escapeHtml(''.@O::lang('VALUE').'') ?> + <span><?php echo O::escapeHtml(''.@O::lang('USER').'') ?> </span> </td> <td class="<?php echo O::escapeHtml('or-help') ?>"><?php echo O::escapeHtml('') ?> @@ -62,7 +62,7 @@ <?php } ?> <?php foreach((array)$values as $list_key=>$list_value) { extract($list_value); ?> <tr class="<?php echo O::escapeHtml('or-data') ?>"><?php echo O::escapeHtml('') ?> - <td><?php echo O::escapeHtml('') ?> + <td width="<?php echo O::escapeHtml('5%') ?>"><?php echo O::escapeHtml('') ?> <?php $if9=($comparable); if($if9) { ?> <input type="<?php echo O::escapeHtml('radio') ?>" name="<?php echo O::escapeHtml('compareid') ?>" value="<?php echo O::escapeHtml(''.@$id.'') ?>" <?php if(@$compareid=='${id}'){ ?>checked="<?php echo O::escapeHtml('checked') ?>"<?php } ?> class="<?php echo O::escapeHtml('or-form-radio') ?>" /><?php echo O::escapeHtml('') ?> <?php } ?> @@ -71,7 +71,7 @@ </span> <?php } ?> </td> - <td><?php echo O::escapeHtml('') ?> + <td width="<?php echo O::escapeHtml('5%') ?>"><?php echo O::escapeHtml('') ?> <?php $if9=($comparable); if($if9) { ?> <input type="<?php echo O::escapeHtml('radio') ?>" name="<?php echo O::escapeHtml('withid') ?>" value="<?php echo O::escapeHtml(''.@$id.'') ?>" <?php if(@$withid=='${id}'){ ?>checked="<?php echo O::escapeHtml('checked') ?>"<?php } ?> class="<?php echo O::escapeHtml('or-form-radio') ?>" /><?php echo O::escapeHtml('') ?> <?php } ?> @@ -80,6 +80,10 @@ </span> <?php } ?> </td> + <td width="<?php echo O::escapeHtml('50%') ?>"><?php echo O::escapeHtml('') ?> + <span title="<?php echo O::escapeHtml(''.@$text.'') ?>"><?php echo O::escapeHtml(''.@$text.'') ?> + </span> + </td> <td><?php echo O::escapeHtml('') ?> <?php include_once( 'modules/template_engine/components/html/component_date/component-date.php'); { component_date($date); ?> <?php } ?> @@ -88,10 +92,6 @@ <span><?php echo O::escapeHtml(''.@$user.'') ?> </span> </td> - <td><?php echo O::escapeHtml('') ?> - <span title="<?php echo O::escapeHtml(''.@$text.'') ?>"><?php echo O::escapeHtml(''.@$text.'') ?> - </span> - </td> <?php $if8=($publish); if($if8) { ?> <td><?php echo O::escapeHtml('') ?> <strong><?php echo O::escapeHtml(''.@O::lang('VALUE_PUBLIC').'') ?> diff --git a/modules/cms/ui/themes/default/html/views/pageelement/history.tpl.src.xml b/modules/cms/ui/themes/default/html/views/pageelement/history.tpl.src.xml @@ -5,7 +5,7 @@ <group collapsible="true" title="${message:language}: ${language.name}"> - <table widths="5%,5%,15%,15%,35%,10%,10%"> + <table> <row class="headline"> <!-- @@ -16,13 +16,13 @@ <text value="${message:COMPARE}"/> </column> <column class="help"> - <text value="${message:DATE}"/> + <text value="${message:VALUE}"/> </column> <column class="help"> - <text value="${message:USER}"/> + <text value="${message:DATE}"/> </column> <column class="help"> - <text value="${message:VALUE}"/> + <text value="${message:USER}"/> </column> <column class="help"> <text value="${message:STATE}"/> @@ -46,7 +46,7 @@ <column> <text value="${lfd_nr}"/> </column>--> - <column> + <column width="5%"> <if true="${comparable}"> <radio name="compareid" value="${id}"/> </if> @@ -54,7 +54,7 @@ <text value=" "/> </else> </column> - <column> + <column width="5%"> <if true="${comparable}"> <radio name="withid" value="${id}"/> </if> @@ -62,15 +62,15 @@ <text value=" "/> </else> </column> + <column width="50%"> + <text value="${text}" title="${text}"/> + </column> <column> <date date="${date}"/> </column> <column> <text value="${user}"/> </column> - <column> - <text value="${text}" title="${text}"/> - </column> <if true="${publish}"> <column> <text value="${message:VALUE_PUBLIC}" type="strong"/> diff --git a/modules/util/Text.class.php b/modules/util/Text.class.php @@ -156,19 +156,43 @@ class Text } + const DIFF_NEW = 'new'; + const DIFF_OLD = 'old'; + const DIFF_EQUAL = 'equal'; + const DIFF_CHANGED = 'notequal'; + const DIFF_EMPTY = 'empty'; + /** * Vergleicht 2 Text-Arrays und ermittelt eine Darstellung der Unterschiede - * + * @param $from_text array text lines + * @param $to_text array text lines + * @return array[] an array containing 2 arrays with the same length */ public static function diff($from_text, $to_text) { + /** + * Creating a diff entry + * @param $text + * @param $line + * @param $type + * @return array + */ + $createEntry = function($text, $line, $type) { + return [ + 'text' => $text, + 'line' => $line, + 'type' => $type, + ]; + }; + $emptyEntry = $createEntry(null,null,self::DIFF_EMPTY); + // Zaehler pro Textarray $pos_from = -1; - $pos_to = -1; + $pos_to = -1; // Ergebnis-Arrays - $from_out = array(); - $to_out = array(); + $from_out = []; + $to_out = []; while (true) { $pos_from++; @@ -183,8 +207,8 @@ class Text !isset($to_text [$pos_to])) { // Text in 'neu' ist zuende, die Restzeilen von 'alt' werden ausgegeben while (isset($from_text[$pos_from])) { - $from_out[] = array('text' => $from_text[$pos_from], 'line' => $pos_from + 1, 'type' => 'old'); - $to_out [] = array(); + $from_out[] = $createEntry( $from_text[$pos_from],$pos_from + 1, self::DIFF_OLD); + $to_out [] = $emptyEntry; $pos_from++; } break; @@ -193,8 +217,8 @@ class Text isset($to_text [$pos_to])) { // Umgekehrter Fall: Text in 'alt' ist zuende, Restzeilen aus 'neu' werden ausgegeben while (isset($to_text[$pos_to])) { - $from_out[] = array(); - $to_out [] = array('text' => $to_text[$pos_to], 'line' => $pos_to + 1, 'type' => 'new'); + $from_out[] = $emptyEntry; + $to_out [] = $createEntry($to_text[$pos_to], $pos_to + 1, self::DIFF_NEW); $pos_to++; } break; @@ -204,12 +228,8 @@ class Text // Wir suchen jetzt die naechsten beiden Zeilen, die gleich sind. $max_entf = min(count($from_text) - $pos_from - 1, count($to_text) - $pos_to - 1); - #echo "suche start, max_entf=$max_entf, pos_from=$pos_from, pos_to=$pos_to<br/>"; - for ($a = 0; $a <= $max_entf; $a++) { - #echo "a ist $a<br/>"; for ($b = 0; $b <= $max_entf; $b++) { - #echo "b ist $b<br/>"; if (trim($from_text[$pos_from + $b]) != '' && $from_text[$pos_from + $b] == $to_text[$pos_to + $a]) { $pos_gef_from = $pos_from + $b; @@ -232,59 +252,57 @@ class Text if ($a <= $max_entf) { // Gleiche Zeile gefunden - #echo "gefunden, pos_gef_from=$pos_gef_from, pos_gef_to=$pos_gef_to<br/>"; if ($pos_gef_from - $pos_from == 0) - $type = 'new'; + $type = self::DIFF_NEW; elseif ($pos_gef_to - $pos_to == 0) - $type = 'old'; + $type = self::DIFF_OLD; else - $type = 'notequal'; + $type = self::DIFF_CHANGED; while ($pos_gef_from - $pos_from > 0 && $pos_gef_to - $pos_to > 0) { - $from_out[] = array('text' => $from_text[$pos_from], 'line' => $pos_from + 1, 'type' => $type); - $to_out [] = array('text' => $to_text [$pos_to], 'line' => $pos_to + 1, 'type' => $type); + $from_out[] = $createEntry($from_text[$pos_from], $pos_from + 1, $type); + $to_out [] = $createEntry($to_text [$pos_to ], $pos_to + 1, $type); $pos_from++; $pos_to++; } while ($pos_gef_from - $pos_from > 0) { - $from_out[] = array('text' => $from_text[$pos_from], 'line' => $pos_from + 1, 'type' => $type); - $to_out [] = array(); + $from_out[] = $createEntry($from_text[$pos_from], $pos_from + 1, $type); + $to_out [] = $emptyEntry; $pos_from++; } while ($pos_gef_to - $pos_to > 0) { - $from_out[] = array(); - $to_out [] = array('text' => $to_text [$pos_to], 'line' => $pos_to + 1, 'type' => $type); + $from_out[] = $emptyEntry; + $to_out [] = $createEntry($to_text [$pos_to], $pos_to + 1, $type); $pos_to++; } $pos_from--; $pos_to--; } else { // Keine gleichen Zeilen gefunden - #echo "nicht gefunden, i=$i, j=$j, pos_from war $pos_from, pos_to war $pos_to<br/>"; while (true) { if (!isset($from_text[$pos_from]) && - !isset($to_text [$pos_to])) { + !isset($to_text [$pos_to ])) { break; } elseif (isset($from_text[$pos_from]) && !isset($to_text [$pos_to])) { - $from_out[] = array('text' => $from_text[$pos_from], 'line' => $pos_from + 1, 'type' => 'notequal'); - $to_out [] = array(); + $from_out[] = array($from_text[$pos_from], $pos_from + 1, self::DIFF_CHANGED); + $to_out [] = $emptyEntry; } elseif (!isset($from_text[$pos_from]) && isset($to_text [$pos_to])) { - $from_out[] = array(); - $to_out [] = array('text' => $to_text [$pos_to], 'line' => $pos_to + 1, 'type' => 'notequal'); + $from_out[] = $emptyEntry; + $to_out [] = $createEntry($to_text [$pos_to] , $pos_to + 1, self::DIFF_CHANGED); } else { - $from_out[] = array('text' => $from_text[$pos_from], 'line' => $pos_from + 1, 'type' => 'notequal'); - $to_out [] = array('text' => $to_text [$pos_to], 'line' => $pos_to + 1, 'type' => 'notequal'); + $from_out[] = $createEntry($from_text[$pos_from], $pos_from + 1, self::DIFF_CHANGED); + $to_out [] = $createEntry($to_text [$pos_to] , $pos_to + 1, self::DIFF_CHANGED); } $pos_from++; $pos_to++; @@ -292,12 +310,12 @@ class Text } } else { // Zeilen sind gleich - $from_out[] = array('text' => $from_text[$pos_from], 'line' => $pos_from + 1, 'type' => 'equal'); - $to_out [] = array('text' => $to_text [$pos_to], 'line' => $pos_to + 1, 'type' => 'equal'); + $from_out[] = $createEntry($from_text[$pos_from], $pos_from + 1, self::DIFF_EQUAL); + $to_out [] = $createEntry($to_text [$pos_to] , $pos_to + 1, self::DIFF_EQUAL); } } - return (array($from_out, $to_out)); + return ( [$from_out, $to_out] ); }