[BUGFIX] lineNumber count in Template Analyzer 47/26747/8
authorPeter Niederlag <netservice@niekom.de>
Fri, 10 Jan 2014 17:31:08 +0000 (18:31 +0100)
committerHelmut Hummel <helmut.hummel@typo3.org>
Tue, 21 Jan 2014 16:57:10 +0000 (17:57 +0100)
ExtendedTemplateService
=======================
* fix/improve ExtentedTemplateService->lineNumberToScript
* don't prepend additional LF in ExtentedTemplateService->ext_outputTS
* remove obscure empty line removals in ext_outputTS and ext_formatTS

TemplateAnalyzerModuleFunctionController
========================================
* use local $hierarchyInfo to fetch template titles

Resolves: #54897
Releases: 6.2
Change-Id: Ia01a89af49d72ed47bef6430042e260af0de3907
Reviewed-on: https://review.typo3.org/26747
Reviewed-by: Helmut Hummel
Tested-by: Helmut Hummel
typo3/sysext/core/Classes/TypoScript/ExtendedTemplateService.php
typo3/sysext/core/Classes/TypoScript/TemplateService.php
typo3/sysext/tstemplate/Classes/Controller/TemplateAnalyzerModuleFunctionController.php

index a4fd645..b4efd75 100644 (file)
@@ -209,6 +209,11 @@ class ExtendedTemplateService extends \TYPO3\CMS\Core\TypoScript\TemplateService
        public $templateTitles = array();
 
        /**
+        * @var array|NULL
+        */
+       protected $lnToScript = NULL;
+
+       /**
         * This flattens a hierarchical setuparray to $this->flatSetup
         * The original function fetched the resource-file if any ('file.'). This functions doesn't.
         *
@@ -498,23 +503,33 @@ class ExtendedTemplateService extends \TYPO3\CMS\Core\TypoScript\TemplateService
        }
 
        /**
-        * [Describe function...]
+        * Find the originating template name for an array of line numbers (TypoScript setup only!)
+        * Given an array of linenumbers the method will try to find the corresponding template where this line originated
+        * The linenumber indicates the *last* lineNumber that is part of the template
         *
-        * @param       [type]          $lnArr: ...
-        * @return      [type]          ...
+        * lineNumbers are in sync with the calculated lineNumbers '.ln..' in TypoScriptParser
+        *
+        * @param array $lnArr Array with linenumbers (might have some extra symbols, for example for unsetting) to be processed
+        * @return array The same array where each entry has been prepended by the template title if available
         * @todo Define visibility
         */
-       public function lineNumberToScript($lnArr) {
+       public function lineNumberToScript(array $lnArr) {
+               // On the first call, construct the lnToScript array.
                if (!is_array($this->lnToScript)) {
                        $this->lnToScript = array();
-                       $c = 1;
-                       $c += substr_count($GLOBALS['TYPO3_CONF_VARS']['FE']['defaultTypoScript_setup'], LF) + 2;
-                       $this->lnToScript[$c] = '[Default]';
-                       foreach ($this->hierarchyInfoToRoot as $info) {
-                               $c += $info['configLines'] + 1;
+
+                       // aggregatedTotalLineCount
+                       $c = 0;
+                       foreach ($this->hierarchyInfo as $templateNumber => $info) {
+                               // hierarchyInfo has the number of lines in configLines, but unfortunatly this value
+                               // was calculated *before* processing of any INCLUDE instructions
+                               // for some yet unknown reason we have to add an extra +2 offset
+                               $linecountAfterIncludeProcessing = substr_count($this->config[$templateNumber], LF) + 2;
+                               $c += $linecountAfterIncludeProcessing;
                                $this->lnToScript[$c] = $info['title'];
                        }
                }
+
                foreach ($lnArr as $k => $ln) {
                        foreach ($this->lnToScript as $endLn => $title) {
                                if ($endLn >= intval($ln)) {
@@ -523,6 +538,7 @@ class ExtendedTemplateService extends \TYPO3\CMS\Core\TypoScript\TemplateService
                                }
                        }
                }
+
                return implode('; ', $lnArr);
        }
 
@@ -686,14 +702,15 @@ class ExtendedTemplateService extends \TYPO3\CMS\Core\TypoScript\TemplateService
        }
 
        /**
-        * [Describe function...]
+        * Processes the flat array from TemplateService->hierarchyInfo
+        * and turns it into a hierachical array to show dependencies (used by TemplateAnalyzer)
         *
-        * @param       [type]          $depthDataArr: ...
-        * @param       [type]          $pointer: ...
-        * @return      [type]          ...
-        * @todo Define visibility
+        * @param array $depthDataArr (empty array on external call)
+        * @param integer &$pointer Element number (1! to count()) of $this->hierarchyInfo that should be processed.
+        *
+        * @return array Processed hierachyInfo.
         */
-       public function ext_process_hierarchyInfo($depthDataArr, &$pointer) {
+       public function ext_process_hierarchyInfo(array $depthDataArr, &$pointer) {
                $parent = $this->hierarchyInfo[$pointer - 1]['templateParent'];
                while ($pointer > 0 && $this->hierarchyInfo[$pointer - 1]['templateParent'] == $parent) {
                        $pointer--;
@@ -712,25 +729,27 @@ class ExtendedTemplateService extends \TYPO3\CMS\Core\TypoScript\TemplateService
        }
 
        /**
-        * [Describe function...]
+        * Get formatted HTML output for TypoScript either with Syntaxhiglighting or in plain mode
+        *
+        * @param array $config Array with simple strings of typoscript code.
+        * @param boolean $lineNumbers Prepend linNumbers to each line.
+        * @param boolean $comments Enable including comments in output.
+        * @param boolean $crop Enable cropping of long lines.
+        * @param boolean $syntaxHL Enrich output with syntaxhighlighting.
+        * @param integer $syntaxHLBlockmode
+        *
+        * @return string
         *
-        * @param       [type]          $config: ...
-        * @param       [type]          $lineNumbers: ...
-        * @param       [type]          $comments: ...
-        * @param       [type]          $crop: ...
-        * @param       [type]          $syntaxHL: ...
-        * @param       [type]          $syntaxHLBlockmode: ...
-        * @return      [type]          ...
         * @todo Define visibility
         */
-       public function ext_outputTS($config, $lineNumbers = 0, $comments = 0, $crop = 0, $syntaxHL = 0, $syntaxHLBlockmode = 0) {
+       public function ext_outputTS(
+               array $config, $lineNumbers = FALSE, $comments = FALSE, $crop = FALSE, $syntaxHL = FALSE, $syntaxHLBlockmode = 0
+       ) {
                $all = '';
                foreach ($config as $str) {
-                       $all .= LF . '[GLOBAL]' . LF . $str;
+                       $all .= '[GLOBAL]' . LF . $str;
                }
                if ($syntaxHL) {
-                       $all = preg_replace('/^[^' . LF . ']*./', '', $all);
-                       $all = chop($all);
                        $tsparser = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\Parser\\TypoScriptParser');
                        $tsparser->lineNumberOffset = $this->ext_lineNumberOffset + 1;
                        $tsparser->parentObject = $this;
@@ -786,8 +805,6 @@ class ExtendedTemplateService extends \TYPO3\CMS\Core\TypoScript\TemplateService
         * @todo Define visibility
         */
        public function ext_formatTS($input, $ln, $comments = 1, $crop = 0) {
-               $input = preg_replace('/^[^' . LF . ']*./', '', $input);
-               $input = chop($input);
                $cArr = explode(LF, $input);
                $n = ceil(log10(count($cArr) + $this->ext_lineNumberOffset));
                $lineNum = '';
index f9820bb..5144ed1 100644 (file)
@@ -251,13 +251,16 @@ class TemplateService {
         */
        public $sectionsMatch;
 
-       // Backend: ts_analyzer
        /**
+        * Used by Backend only (Typoscript Template Analyzer)
+        *
         * @todo Define visibility
         */
        public $clearList_const = array();
 
        /**
+        * Used by Backend only (Typoscript Template Analyzer)
+        *
         * @todo Define visibility
         */
        public $clearList_setup = array();
index 3726d19..6b957e8 100644 (file)
@@ -125,8 +125,7 @@ class TemplateAnalyzerModuleFunctionController extends \TYPO3\CMS\Backend\Module
                $GLOBALS['tmpl']->clearList_const_temp = array_flip($GLOBALS['tmpl']->clearList_const);
                $GLOBALS['tmpl']->clearList_setup_temp = array_flip($GLOBALS['tmpl']->clearList_setup);
                $pointer = count($GLOBALS['tmpl']->hierarchyInfo);
-               $GLOBALS['tmpl']->hierarchyInfoArr = $GLOBALS['tmpl']->ext_process_hierarchyInfo(array(), $pointer);
-               $hierarArr = array();
+               $hierarchyInfo = $GLOBALS['tmpl']->ext_process_hierarchyInfo(array(), $pointer);
                $head = '<thead><tr>';
                $head .= '<th>' . $GLOBALS['LANG']->getLL('title', TRUE) . '</th>';
                $head .= '<th>' . $GLOBALS['LANG']->getLL('rootlevel', TRUE) . '</th>';
@@ -136,7 +135,7 @@ class TemplateAnalyzerModuleFunctionController extends \TYPO3\CMS\Backend\Module
                $head .= '<th>' . $GLOBALS['LANG']->getLL('rootline', TRUE) . '</th>';
                $head .= '<th>' . $GLOBALS['LANG']->getLL('nextLevel', TRUE) . '</th>';
                $head .= '</tr></thead>';
-               $hierar = implode(array_reverse($GLOBALS['tmpl']->ext_getTemplateHierarchyArr($GLOBALS['tmpl']->hierarchyInfoArr, '', array(), 1)), '');
+               $hierar = implode(array_reverse($GLOBALS['tmpl']->ext_getTemplateHierarchyArr($hierarchyInfo, '', array(), 1)), '');
                $hierar = '<table class="t3-table" id="ts-analyzer">' . $head . $hierar . '</table>';
                $theOutput .= $this->pObj->doc->spacer(5);
                $theOutput .= $this->pObj->doc->section($GLOBALS['LANG']->getLL('templateHierarchy', TRUE), $hierar, 0, 1);
@@ -162,16 +161,14 @@ class TemplateAnalyzerModuleFunctionController extends \TYPO3\CMS\Backend\Module
                                <table class="t3-table ts-typoscript">
                        ';
                        // Don't know why -2 and not 0... :-) But works.
-                       $GLOBALS['tmpl']->ext_lineNumberOffset = -2;
+                       $GLOBALS['tmpl']->ext_lineNumberOffset = 0;
                        $GLOBALS['tmpl']->ext_lineNumberOffset_mode = 'const';
-                       $GLOBALS['tmpl']->ext_lineNumberOffset += count(explode(LF, \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser::checkIncludeLines(('' . $GLOBALS['TYPO3_CONF_VARS']['FE']['defaultTypoScript_constants'])))) + 1;
-                       reset($GLOBALS['tmpl']->clearList_const);
                        foreach ($GLOBALS['tmpl']->constants as $key => $val) {
-                               $cVal = current($GLOBALS['tmpl']->clearList_const);
-                               if ($cVal == \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('template') || \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('template') == 'all') {
+                               $currentTemplateId = $GLOBALS['tmpl']->hierarchyInfo[$key]['templateID'];
+                               if ($currentTemplateId == \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('template') || \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('template') == 'all') {
                                        $theOutput .= '
                                                <tr>
-                                                       <td><strong>' . htmlspecialchars($GLOBALS['tmpl']->templateTitles[$cVal]) . '</strong></td>
+                                                       <td><strong>' . htmlspecialchars($GLOBALS['tmpl']->hierarchyInfo[$key]['title']) . '</strong></td>
                                                </tr>
                                                <tr>
                                                        <td>
@@ -184,7 +181,6 @@ class TemplateAnalyzerModuleFunctionController extends \TYPO3\CMS\Backend\Module
                                        }
                                }
                                $GLOBALS['tmpl']->ext_lineNumberOffset += count(explode(LF, $val)) + 1;
-                               next($GLOBALS['tmpl']->clearList_const);
                        }
                        $theOutput .= '
                                </table>
@@ -200,13 +196,12 @@ class TemplateAnalyzerModuleFunctionController extends \TYPO3\CMS\Backend\Module
                        ';
                        $GLOBALS['tmpl']->ext_lineNumberOffset = 0;
                        $GLOBALS['tmpl']->ext_lineNumberOffset_mode = 'setup';
-                       $GLOBALS['tmpl']->ext_lineNumberOffset += count(explode(LF, \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser::checkIncludeLines(('' . $GLOBALS['TYPO3_CONF_VARS']['FE']['defaultTypoScript_setup'])))) + 1;
-                       reset($GLOBALS['tmpl']->clearList_setup);
                        foreach ($GLOBALS['tmpl']->config as $key => $val) {
-                               if (current($GLOBALS['tmpl']->clearList_setup) == \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('template') || \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('template') == 'all') {
+                               $currentTemplateId = $GLOBALS['tmpl']->hierarchyInfo[$key]['templateID'];
+                               if ($currentTemplateId == \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('template') || \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('template') == 'all') {
                                        $theOutput .= '
                                                <tr>
-                                                       <td><strong>' . htmlspecialchars($GLOBALS['tmpl']->templateTitles[current($GLOBALS['tmpl']->clearList_setup)]) . '</strong></td></tr>
+                                                       <td><strong>' . htmlspecialchars($GLOBALS['tmpl']->hierarchyInfo[$key]['title']) . '</strong></td></tr>
                                                <tr>
                                                        <td><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td nowrap="nowrap">' . $GLOBALS['tmpl']->ext_outputTS(array($val), $this->pObj->MOD_SETTINGS['ts_analyzer_checkLinenum'], $this->pObj->MOD_SETTINGS['ts_analyzer_checkComments'], $this->pObj->MOD_SETTINGS['ts_analyzer_checkCrop'], $this->pObj->MOD_SETTINGS['ts_analyzer_checkSyntax'], 0) . '</td></tr></table>
                                                        </td>
@@ -217,7 +212,6 @@ class TemplateAnalyzerModuleFunctionController extends \TYPO3\CMS\Backend\Module
                                        }
                                }
                                $GLOBALS['tmpl']->ext_lineNumberOffset += count(explode(LF, $val)) + 1;
-                               next($GLOBALS['tmpl']->clearList_setup);
                        }
                        $theOutput .= '
                                </table>