[TASK] Move prefixLocalAnchors to compatibility6 35/38035/3
authorBenjamin Mack <benni@typo3.org>
Mon, 23 Mar 2015 13:01:33 +0000 (14:01 +0100)
committerBenjamin Mack <benni@typo3.org>
Tue, 24 Mar 2015 15:30:33 +0000 (16:30 +0100)
Resolves: #65934
Releases: master
Change-Id: I0e3a56dc569d19fbce46708656f7a342d2f698e5
Reviewed-on: http://review.typo3.org/38035
Reviewed-by: Andreas Fernandez <andreas.fernandez@aspedia.de>
Tested-by: Andreas Fernandez <andreas.fernandez@aspedia.de>
Reviewed-by: Jan Helke <typo3@helke.de>
Tested-by: Jan Helke <typo3@helke.de>
Reviewed-by: Benjamin Mack <benni@typo3.org>
Tested-by: Benjamin Mack <benni@typo3.org>
typo3/sysext/compatibility6/Classes/Hooks/TypoScriptFrontendController/ContentPostProcHook.php
typo3/sysext/compatibility6/ext_localconf.php
typo3/sysext/core/Documentation/Changelog/master/Deprecation-65934-PrefixLocalAnchorsMovedToLegacyExtension.rst [new file with mode: 0644]
typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php

index d730e5e..e6c93e0 100644 (file)
@@ -18,7 +18,7 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Html\HtmlParser;
 
 /**
- * Class that hooks into TypoScriptFrontendController to do XHTML cleaning
+ * Class that hooks into TypoScriptFrontendController to do XHTML cleaning and prefixLocalAnchors functionality
  */
 class ContentPostProcHook {
 
@@ -29,13 +29,19 @@ class ContentPostProcHook {
 
        /**
         * XHTML-clean the code, if flag config.xhtml_cleaning is set
-        * to "all"
+        * to "all", same goes for config.prefixLocalAnchors
         *
         * @param array $parameters
         * @param \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController $parentObject
         */
        public function contentPostProcAll(&$parameters, $parentObject) {
                $this->pObj = $parentObject;
+               // Fix local anchors in links, if flag set
+               if ($this->doLocalAnchorFix() === 'all') {
+                       $GLOBALS['TT']->push('Local anchor fix, all', '');
+                       $this->prefixLocalAnchorsWithScript();
+                       $GLOBALS['TT']->pull();
+               }
                // XHTML-clean the code, if flag set
                if ($this->doXHTML_cleaning() === 'all') {
                        $GLOBALS['TT']->push('XHTML clean, all', '');
@@ -47,13 +53,19 @@ class ContentPostProcHook {
 
        /**
         * XHTML-clean the code, if flag config.xhtml_cleaning is set
-        * to "cached"
+        * to "cached", same goes for config.prefixLocalAnchors
         *
         * @param array $parameters
         * @param \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController $parentObject
         */
        public function contentPostProcCached(&$parameters, $parentObject) {
                $this->pObj = $parentObject;
+               // Fix local anchors in links, if flag set
+               if ($this->doLocalAnchorFix() === 'cached') {
+                       $GLOBALS['TT']->push('Local anchor fix, cached', '');
+                       $this->prefixLocalAnchorsWithScript();
+                       $GLOBALS['TT']->pull();
+               }
                // XHTML-clean the code, if flag set
                if ($this->doXHTML_cleaning() === 'cached') {
                        $GLOBALS['TT']->push('XHTML clean, cached', '');
@@ -65,13 +77,19 @@ class ContentPostProcHook {
 
        /**
         * XHTML-clean the code, if flag config.xhtml_cleaning is set
-        * to "output"
+        * to "output", same goes for config.prefixLocalAnchors
         *
         * @param array $parameters
         * @param \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController $parentObject
         */
        public function contentPostProcOutput(&$parameters, $parentObject) {
                $this->pObj = $parentObject;
+               // Fix local anchors in links, if flag set
+               if ($this->doLocalAnchorFix() === 'output') {
+                       $GLOBALS['TT']->push('Local anchor fix, output', '');
+                       $this->prefixLocalAnchorsWithScript();
+                       $GLOBALS['TT']->pull();
+               }
                // XHTML-clean the code, if flag set
                if ($this->doXHTML_cleaning() === 'output') {
                        $GLOBALS['TT']->push('XHTML clean, output', '');
@@ -92,4 +110,40 @@ class ContentPostProcHook {
                }
                return $this->pObj->config['config']['xhtml_cleaning'];
        }
+
+
+       /**
+        * Returns the mode of Local Anchor prefixing
+        *
+        * @return string Keyword: "all", "cached" or "output"
+        */
+       public function doLocalAnchorFix() {
+               return isset($this->pObj->config['config']['prefixLocalAnchors']) ? $this->pObj->config['config']['prefixLocalAnchors'] : NULL;
+       }
+
+       /**
+        * Substitutes all occurrences of <a href="#"... in $this->content with <a href="[path-to-url]#"...
+        *
+        * @return void Works directly on $this->content
+        */
+       protected function prefixLocalAnchorsWithScript() {
+               if (!$this->pObj->beUserLogin) {
+                       if (!is_object($this->pObj->cObj)) {
+                               $this->pObj->newCObj();
+                       }
+                       $scriptPath = $this->pObj->cObj->getUrlToCurrentLocation();
+               } else {
+                       // To break less existing sites, we allow the REQUEST_URI to be used for the prefix
+                       $scriptPath = GeneralUtility::getIndpEnv('REQUEST_URI');
+                       // Disable the cache so that these URI will not be the ones to be cached
+                       $this->pObj->no_cache = TRUE;
+               }
+               $originalContent = $this->pObj->content;
+               $this->pObj->content = preg_replace('/(<(?:a|area).*?href=")(#[^"]*")/i', '${1}' . htmlspecialchars($scriptPath) . '${2}', $originalContent);
+               // There was an error in the call to preg_replace, so keep the original content (behavior prior to PHP 5.2)
+               if (preg_last_error() > 0) {
+                       GeneralUtility::sysLog('preg_replace returned error-code: ' . preg_last_error() . ' in function prefixLocalAnchorsWithScript. Replacement not done!', 'cms', GeneralUtility::SYSLOG_SEVERITY_FATAL);
+                       $this->pObj->content = $originalContent;
+               }
+       }
 }
index 42b147c..4a03e5b 100644 (file)
@@ -19,7 +19,7 @@ if (TYPO3_MODE === 'FE') {
        // Register a hook for data submission
        $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['checkDataSubmission']['mailform'] = \TYPO3\CMS\Compatibility6\Controller\FormDataSubmissionController::class;
 
-       // Register hooks for xhtml_cleaning
+       // Register hooks for xhtml_cleaning and prefixLocalAnchors
        $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-all'][] = \TYPO3\CMS\Compatibility6\Hooks\TypoScriptFrontendController\ContentPostProcHook::class . '->contentPostProcAll';
        $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-cached'][] = \TYPO3\CMS\Compatibility6\Hooks\TypoScriptFrontendController\ContentPostProcHook::class . '->contentPostProcCached';
        $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-output'][] = \TYPO3\CMS\Compatibility6\Hooks\TypoScriptFrontendController\ContentPostProcHook::class . '->contentPostProcOutput';
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Deprecation-65934-PrefixLocalAnchorsMovedToLegacyExtension.rst b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-65934-PrefixLocalAnchorsMovedToLegacyExtension.rst
new file mode 100644 (file)
index 0000000..9d52a09
--- /dev/null
@@ -0,0 +1,42 @@
+====================================================================================
+Deprecation: #65934 - "Prefix Local Anchors" functionality moved to legacy extension
+====================================================================================
+
+Description
+===========
+
+Prefixing local anchors is not considered best practice in web sites aynmore as the same is achieved with
+absolute prefixes for links (see ``config.absRefPrefix``). Therefore the according functionality has been moved to
+the legacy extension EXT:compatibility6.
+
+The following TypoScript option has been marked for deprecation:
+
+.. code-block:: ts
+
+       config.prefixLocalAnchors
+
+The following PHP methods have been marked for deprecation:
+
+.. code-block:: php
+
+       TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::prefixLocalAnchorsWithScript()
+       TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::doLocalAnchorFix()
+
+
+Impact
+======
+
+The content output of the TYPO3 frontend is not prefixed with local anchors anymore unless EXT:compatibility6 is loaded.
+
+
+Affected installations
+======================
+
+Any installation having the TypoScript option ``config.prefixLocalAnchors`` set will have different behaviour in the
+frontend rendering.
+
+
+Migration
+=========
+
+For TYPO3 CMS 7, installing EXT:compatibility6 brings back the existing functionality.
index 91ae203..c93ca9f 100644 (file)
@@ -3273,12 +3273,6 @@ class TypoScriptFrontendController {
                if ($this->no_cacheBeforePageGen) {
                        $this->set_no_cache('no_cache has been set before the page was generated - safety check', TRUE);
                }
-               // Fix local anchors in links, if flag set
-               if ($this->doLocalAnchorFix() == 'all') {
-                       $GLOBALS['TT']->push('Local anchor fix, all', '');
-                       $this->prefixLocalAnchorsWithScript();
-                       $GLOBALS['TT']->pull();
-               }
                // Hook for post-processing of page content cached/non-cached:
                if (is_array($this->TYPO3_CONF_VARS['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-all'])) {
                        $_params = array('pObj' => &$this);
@@ -3288,12 +3282,6 @@ class TypoScriptFrontendController {
                }
                // Processing if caching is enabled:
                if (!$this->no_cache) {
-                       // Fix local anchors in links, if flag set
-                       if ($this->doLocalAnchorFix() == 'cached') {
-                               $GLOBALS['TT']->push('Local anchor fix, cached', '');
-                               $this->prefixLocalAnchorsWithScript();
-                               $GLOBALS['TT']->pull();
-                       }
                        // Hook for post-processing of page content before being cached:
                        if (is_array($this->TYPO3_CONF_VARS['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-cached'])) {
                                $_params = array('pObj' => &$this);
@@ -3511,8 +3499,10 @@ class TypoScriptFrontendController {
         * Returns the mode of Local Anchor prefixing
         *
         * @return string Keyword: "all", "cached" or "output
+        * @deprecated The TypoScript option "config.prefixLocalAnchors" and the according method in TSFE have been deprecated with TYPO3 CMS 7 and will be removed with TYPO3 CMS 8.
         */
        public function doLocalAnchorFix() {
+               GeneralUtility::logDeprecatedFunction();
                return isset($this->config['config']['prefixLocalAnchors']) ? $this->config['config']['prefixLocalAnchors'] : NULL;
        }
 
@@ -3585,12 +3575,6 @@ class TypoScriptFrontendController {
                if (!$this->isClientCachable) {
                        $this->contentStrReplace();
                }
-               // Fix local anchors in links, if flag set
-               if ($this->doLocalAnchorFix() == 'output') {
-                       $GLOBALS['TT']->push('Local anchor fix, output', '');
-                       $this->prefixLocalAnchorsWithScript();
-                       $GLOBALS['TT']->pull();
-               }
                // Hook for post-processing of page content before output:
                if (isset($this->TYPO3_CONF_VARS['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-output']) && is_array($this->TYPO3_CONF_VARS['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-output'])) {
                        $_params = array('pObj' => &$this);
@@ -3983,8 +3967,10 @@ class TypoScriptFrontendController {
         * Substitutes all occurencies of <a href="#"... in $this->content with <a href="[path-to-url]#"...
         *
         * @return void Works directly on $this->content
+        * @deprecated since TYPO3 CMS 7, will be removed with TYPO3 CMS 8
         */
        public function prefixLocalAnchorsWithScript() {
+               GeneralUtility::logDeprecatedFunction();
                if (!$this->beUserLogin) {
                        if (!is_object($this->cObj)) {
                                $this->newCObj();