openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit b6183083ec8db226f266ef09678778b26b97d088
parent 10390f437985260b4c24c26aa05ab30febc02efa
Author: dankert <openrat@jandankert.de>
Date:   Sun,  5 Dec 2021 22:09:08 +0100

Fix: The Diff function was broken.

Diffstat:
Mmodules/cms/action/PageelementAction.class.php | 38++++++++++++++++++++++++--------------
Mmodules/cms/action/pageelement/PageelementDiffAction.class.php | 24++++++++++++++++--------
Mmodules/cms/model/BaseObject.class.php | 7+++----
Mmodules/cms/ui/themes/default/html/views/pageelement/diff.php | 20++++++--------------
Mmodules/cms/ui/themes/default/html/views/pageelement/diff.tpl.src.xml | 26+++++++++++++-------------
Mmodules/util/ArrayUtils.class.php | 20++++++++++++++++++++
Mmodules/util/Text.class.php | 37+------------------------------------
7 files changed, 83 insertions(+), 89 deletions(-)

diff --git a/modules/cms/action/PageelementAction.class.php b/modules/cms/action/PageelementAction.class.php @@ -24,6 +24,7 @@ use cms\model\User; use cms\model\Value; use language\Messages; use LogicException; +use util\ArrayUtils; use util\exception\ObjectNotFoundException; use util\exception\SecurityException; use util\exception\ValidationException; @@ -677,25 +678,28 @@ class PageelementAction extends BaseAction } - + /** + * Get page contents. + * + * @return PageContent[] array of pagecontents of the page + * @throws ObjectNotFoundException + */ protected function getContents() { $this->page->load(); $this->element->load(); - $contents = array(); - - foreach ($this->page->getProject()->getLanguages() as $languageId => $languageName) { - $pageContent = new PageContent(); - $pageContent->languageid = $languageId; - $pageContent->elementId = $this->element->elementid; - $pageContent->pageId = $this->page->pageid; - $pageContent->load(); - - $contents[] = new Content($pageContent->contentId); - } - - return $contents; + return ArrayUtils::mapToNewArray( + $this->page->getProject()->getLanguages(), + function ( $languageId, $languageName ) { + $pageContent = new PageContent(); + $pageContent->languageid = $languageId; + $pageContent->elementId = $this->element->elementid; + $pageContent->pageId = $this->page->pageid; + $pageContent->load(); + return [ $pageContent->contentId => new Content( $pageContent->contentId ) ]; + } + ); } @@ -711,4 +715,10 @@ class PageelementAction extends BaseAction throw new SecurityException('valueId is not valid in this context'); } + protected function ensureContentIdIsPartOfPage( $contentId ) + { + if ( ! in_array( $contentId, array_keys($this->getContents()) ) ) + throw new SecurityException('content '.$contentId.' is not part of page #'.$this->page->objectid.' with contents '.print_r($this->getContents(),true) ); + } + } \ No newline at end of file diff --git a/modules/cms/action/pageelement/PageelementDiffAction.class.php b/modules/cms/action/pageelement/PageelementDiffAction.class.php @@ -18,26 +18,32 @@ class PageelementDiffAction extends PageelementAction implements Method { if ( $value1id == $value2id ) throw new ValidationException('withid' ); - // Wenn Value1-Id groesser als Value2-Id, dann Variablen tauschen + // Value 2 must be greater than value 1. if ( $value1id > $value2id ) - list($value1id,$value2id) = array( $value2id,$value1id ); - + list($value1id,$value2id) = array( $value2id,$value1id ); $value1 = new Value( $value1id ); $value2 = new Value( $value2id ); - $value1->valueid = $value1id; - $value2->valueid = $value2id; - $value1->loadWithId(); - $value2->loadWithId(); + $value1->loadWithId( $value1id ); + $value2->loadWithId( $value2id ); + + // both values must be part of the same content. + if ( $value1->contentid != $value2->contentid ) + throw new ValidationException('compareid'); + + // Security-Check: + // Content must be a part of the page. + $this->ensureContentIdIsPartOfPage( $value1->contentid ); $this->setTemplateVar('date_left' ,$value1->lastchangeTimeStamp); $this->setTemplateVar('date_right',$value2->lastchangeTimeStamp); + // Split whole text into lines. $text1 = explode("\n",$value1->text); $text2 = explode("\n",$value2->text); - // Unterschiede feststellen. + // Make the diff $diffResult = Text::diff($text1,$text2); $outputResult = array_map( function( $left,$right) { @@ -49,6 +55,8 @@ class PageelementDiffAction extends PageelementAction implements Method { $this->setTemplateVar('diff',$outputResult ); } + + public function post() { } } diff --git a/modules/cms/model/BaseObject.class.php b/modules/cms/model/BaseObject.class.php @@ -1371,7 +1371,7 @@ SQL else $idCache[] = $row['id']; - $this->addParentfolder( $row['id'],$row['name'],$row['filename'] ); + $this->addParentfolder( $row['id'],$row['filename'] ); $foid = $row['parentid']; } @@ -1381,10 +1381,9 @@ SQL } - private function addParentFolder( $id,$name,$filename='' ) + private function addParentFolder( $id,$filename='' ) { - if ( empty($name) ) - $name = $filename; + $name = $filename; if ( empty($name) ) $name = "($id)"; diff --git a/modules/cms/ui/themes/default/html/views/pageelement/diff.php b/modules/cms/ui/themes/default/html/views/pageelement/diff.php @@ -6,49 +6,41 @@ <div class="<?php echo O::escapeHtml('or-table-area') ?>"><?php echo O::escapeHtml('') ?> <table class="<?php echo O::escapeHtml('or-table') ?>"><?php echo O::escapeHtml('') ?> <tr class="<?php echo O::escapeHtml('or-table-header') ?>"><?php echo O::escapeHtml('') ?> - <th class="<?php echo O::escapeHtml('or-table-column-action') ?>"><?php echo O::escapeHtml('') ?></th> <th class="<?php echo O::escapeHtml('or-table-column-auto') ?>"><?php echo O::escapeHtml('') ?> <em><?php echo O::escapeHtml(''.@O::lang('COMPARE').'') ?></em> <span><?php echo O::escapeHtml(' ') ?></span> <?php include_once( 'modules/template_engine/components/html/component_date/component-date.php'); { component_date($date_left); ?> <?php } ?> </th> - <th class="<?php echo O::escapeHtml('or-table-column-action') ?>"><?php echo O::escapeHtml('') ?></th> <th class="<?php echo O::escapeHtml('or-table-column-auto') ?>"><?php echo O::escapeHtml('') ?> - <em><?php echo O::escapeHtml(''.@O::lang('WITH').'') ?></em> + <em><?php echo O::escapeHtml(''.@O::lang('WITH').' ') ?></em> <span><?php echo O::escapeHtml(' ') ?></span> <?php include_once( 'modules/template_engine/components/html/component_date/component-date.php'); { component_date($date_right); ?> <?php } ?> </th> </tr> <tr><?php echo O::escapeHtml('') ?> - <td colspan="<?php echo O::escapeHtml('4') ?>"><?php echo O::escapeHtml('') ?></td> + <td colspan="<?php echo O::escapeHtml('2') ?>"><?php echo O::escapeHtml('') ?></td> </tr> <?php foreach((array)@$diff as $list_key=>$list_value) { extract($list_value); ?> <tr class="<?php echo O::escapeHtml('or-diff') ?>"><?php echo O::escapeHtml('') ?> <?php $if5=(isset($left)); if($if5) { ?> - <td class="<?php echo O::escapeHtml('or-diff-line') ?>"><?php echo O::escapeHtml('') ?> - <tt><?php echo O::escapeHtml(''.@$left['line'].'') ?></tt> - </td> - <td class="<?php echo O::escapeHtml('or-diff-text--'.@$left['type'].'') ?>"><?php echo O::escapeHtml('') ?> + <td title="<?php echo O::escapeHtml(''.@$left['text'].'') ?>" class="<?php echo O::escapeHtml('or-diff-text--'.@$left['type'].'') ?>"><?php echo O::escapeHtml('') ?> <span><?php echo O::escapeHtml(''.@$left['text'].'') ?></span> </td> <?php } ?> <?php if(!$if5) { ?> - <td colspan="<?php echo O::escapeHtml('2') ?>" class="<?php echo O::escapeHtml('or-help') ?>"><?php echo O::escapeHtml('') ?> + <td><?php echo O::escapeHtml('') ?> <span><?php echo O::escapeHtml(' ') ?></span> </td> <?php } ?> <?php $if5=(isset($right)); if($if5) { ?> - <td class="<?php echo O::escapeHtml('or-diff-line') ?>"><?php echo O::escapeHtml('') ?> - <tt><?php echo O::escapeHtml(''.@$right['line'].'') ?></tt> - </td> - <td class="<?php echo O::escapeHtml('or-diff-text--'.@$right['type'].'') ?>"><?php echo O::escapeHtml('') ?> + <td title="<?php echo O::escapeHtml(''.@$right['text'].'') ?>" class="<?php echo O::escapeHtml('or-diff-text--'.@$right['type'].'') ?>"><?php echo O::escapeHtml('') ?> <span><?php echo O::escapeHtml(''.@$right['text'].'') ?></span> </td> <?php } ?> <?php if(!$if5) { ?> - <td colspan="<?php echo O::escapeHtml('2') ?>" class="<?php echo O::escapeHtml('or-help') ?>"><?php echo O::escapeHtml('') ?> + <td><?php echo O::escapeHtml('') ?> <span><?php echo O::escapeHtml(' ') ?></span> </td> <?php } ?> diff --git a/modules/cms/ui/themes/default/html/views/pageelement/diff.tpl.src.xml b/modules/cms/ui/themes/default/html/views/pageelement/diff.tpl.src.xml @@ -2,47 +2,47 @@ xsi:schemaLocation="http://www.openrat.de/template ../../../../../../../template_engine/components/template.xsd"> <table> <row header="true"> - <column header="true" class="table-column-action"/> + <!-- <column header="true" class="table-column-action"/> --> <column header="true" class="table-column-auto"> <text type="emphatic" value="${message:COMPARE}"/> <text value=" "/> <date date="${date_left}"/> </column> - <column header="true" class="table-column-action"/> +<!-- <column header="true" class="table-column-action"/>--> <column header="true" class="table-column-auto"> - <text type="emphatic" value="${message:WITH}"/> + <text type="emphatic" value="${message:WITH} "/> <text value=" "/> <date date="${date_right}"/> </column> </row> <row> - <column colspan="4"/> + <column colspan="2"/> </row> <list list="${diff}" extract="true"> <row class="diff"> <if present="left"> - <column class="diff-line"> - <text value="${left.line}" type="tt"/> - </column> - <column class="diff-text--${left.type}"> +<!-- <column class="diff-line">--> +<!-- <text value="${left.line}" type="tt"/>--> +<!-- </column>--> + <column class="diff-text--${left.type}" title="${left.text}"> <text value="${left.text}"/> </column> </if> <else> - <column colspan="2" class="help" width="50%"> + <column> <text value=" "/> </column> </else> <if present="right"> - <column class="diff-line"> + <!--<column class="diff-line"> <text value="${right.line}" type="tt"/> - </column> - <column class="diff-text--${right.type}"> + </column>--> + <column class="diff-text--${right.type}" title="${right.text}"> <text value="${right.text}"/> </column> </if> <else> - <column colspan="2" class="help" width="50%"> + <column > <text value=" "/> </column> </else> diff --git a/modules/util/ArrayUtils.class.php b/modules/util/ArrayUtils.class.php @@ -28,6 +28,12 @@ class ArrayUtils } + /** + * Dives into an array and gets a value with a sub key. + * @param array $array + * @param array $keys + * @return mixed|null + */ public static function getSubValue($array, $keys) { @@ -43,6 +49,20 @@ class ArrayUtils } + /** + * Maps an array to a new array. + * @param $callback callable callback, it is called with 3 parameters: the new array, key, value. + * @param $array array array which should be mapped + * @return array the new array + */ + public static function mapToNewArray($array, $callback) { + $newArray = []; + foreach( $array as $key => $value ) { + $newArray += (array) call_user_func( $callback, $key, $value ); + } + return $newArray; + } + /** diff --git a/modules/util/Text.class.php b/modules/util/Text.class.php @@ -222,8 +222,7 @@ class Text $pos_to++; } break; - } elseif - (rtrim($from_text[$pos_from]) != rtrim($to_text[$pos_to])) { + } elseif( rtrim($from_text[$pos_from]) != rtrim($to_text[$pos_to]) ) { // Zeilen sind vorhanden, aber ungleich // 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); @@ -369,40 +368,6 @@ class Text } - public static function resolveVariables($value, $key, $resolver) - { - $begin = '${'; - $end = '}'; - $split = ':'; - $offset = 0; - - while (true) { - $pos = strpos($value, $begin . $key . $split, $offset); - - if ($pos === FALSE) - return $value; - - $offset = $pos + 1; - - $posEnd = strpos($value, $end, $offset); - - if ($posEnd === FALSE) - return $value; - - $name = substr($value, $pos + strlen($begin . $key . $split), $posEnd - strlen($begin . $key . $split) - $pos); - - $varValue = $resolver($name); - - $value = substr($value, 0, $pos) . $varValue . substr($value, $posEnd + strlen($end)); - } - - return $value; - } - - - - - // Source: http://de.php.net/manual/de/function.htmlentities.php#96648