Passed
Pull Request — release-11.2.x (#3604)
by Markus
32:12 queued 27:55
created

Typo3PageContentExtractor::excludeContentByClass()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 36
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7.004

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 36
ccs 22
cts 23
cp 0.9565
rs 8.6346
c 0
b 0
f 0
cc 7
nc 6
nop 1
crap 7.004
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2010-2015 Ingo Renner <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
31
/**
32
 * Content extraction class for TYPO3 pages.
33
 *
34
 * @author Ingo Renner <[email protected]>
35
 */
36
class Typo3PageContentExtractor extends HtmlContentExtractor
37
{
38
39
    /**
40
     * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager
41
     */
42
    protected $logger;
43
44
    /**
45
     * Shortcut method to retrieve the raw content marked for indexing.
46
     *
47
     * @return string Content marked for indexing.
48
     */
49 37
    public function getContentMarkedForIndexing()
50
    {
51
        // @extensionScannerIgnoreLine
52 37
        return $this->extractContentMarkedForIndexing($this->content);
53
    }
54
55
    /**
56
     * Extracts the markup wrapped with TYPO3SEARCH_begin and TYPO3SEARCH_end
57
     * markers.
58
     *
59
     * @param string $html HTML markup with TYPO3SEARCH markers for content that should be indexed
60
     * @return string HTML markup found between TYPO3SEARCH markers
61
     */
62 46
    protected function extractContentMarkedForIndexing($html)
63
    {
64 46
        preg_match_all(
65 46
            '/<!--\s*?TYPO3SEARCH_begin\s*?-->.*?<!--\s*?TYPO3SEARCH_end\s*?-->/mis',
66
            $html,
67 46
            $indexableContents
68
        );
69 46
        $indexableContent = implode('', $indexableContents[0]);
70
71 46
        $indexableContent = $this->excludeContentByClass($indexableContent);
72 46
        if (empty($indexableContent) && $this->getConfiguration()->getLoggingIndexingMissingTypo3SearchMarkers()) {
73 17
            $this->logger = GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
74 17
            $this->logger->log(SolrLogManager::WARNING, 'No TYPO3SEARCH markers found.');
75
        }
76
77 46
        return $indexableContent;
78
    }
79
80
    /**
81
     * Exclude some html parts by class inside content wrapped with TYPO3SEARCH_begin and TYPO3SEARCH_end
82
     * markers.
83
     *
84
     * @param string $indexableContent HTML markup
85
     * @return string HTML
86
     */
87 49
    public function excludeContentByClass($indexableContent)
88
    {
89 49
        if (empty(trim($indexableContent))) {
90 17
            return $indexableContent;
91
        }
92
93 32
        $excludeClasses = $this->getConfiguration()->getIndexQueuePagesExcludeContentByClassArray();
94 32
        if (count($excludeClasses) === 0) {
95 13
            return $indexableContent;
96
        }
97
98 19
        $isInContent = Util::containsOneOfTheStrings($indexableContent, $excludeClasses);
99 19
        if (!$isInContent) {
100 16
            return $indexableContent;
101
        }
102
103 3
        $doc = new \DOMDocument('1.0', 'UTF-8');
104 3
        libxml_use_internal_errors(true);
105 3
        $doc->loadHTML('<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL . $indexableContent);
106 3
        $xpath = new \DOMXPath($doc);
107 3
        foreach ($excludeClasses as $excludePart) {
108 3
            $elements = $xpath->query("//*[contains(@class,'" . $excludePart . "')]");
109 3
            if (count($elements) == 0) {
110
                continue;
111
            }
112
113 3
            foreach ($elements as $element) {
114 3
                $element->parentNode->removeChild($element);
115
            }
116
        }
117 3
        $html = $doc->saveHTML($doc->documentElement->parentNode);
118
        // remove XML-Preamble, newlines and doctype
119 3
        $html = preg_replace('/(<\?xml[^>]+\?>|\r?\n|<!DOCTYPE.+?>)/imS', '', $html);
120 3
        $html = str_replace(['<html>', '</html>', '<body>', '</body>'], ['', '', '', ''], $html);
121
122 3
        return $html;
123
    }
124
125
    /**
126
     * Returns the cleaned indexable content from the page's HTML markup.
127
     *
128
     * The content is cleaned from HTML tags and control chars Solr could
129
     * stumble on.
130
     *
131
     * @return string Indexable, cleaned content ready for indexing.
132
     */
133 46
    public function getIndexableContent()
134
    {
135
        // @extensionScannerIgnoreLine
136 46
        $content = $this->extractContentMarkedForIndexing($this->content);
137
138
        // clean content
139 46
        $content = self::cleanContent($content);
140 46
        $content = trim($content);
141 46
        $content = preg_replace('!\s+!u', ' ', $content); // reduce multiple spaces to one space
142
143 46
        return $content;
144
    }
145
146
    /**
147
     * Retrieves the page's title by checking the indexedDocTitle, altPageTitle,
148
     * and regular page title - in that order.
149
     *
150
     * @return string the page's title
151
     */
152 37
    public function getPageTitle()
153
    {
154 37
        $page = $GLOBALS['TSFE'];
155
156 37
        if ($page->indexedDocTitle) {
157 20
            $pageTitle = $page->indexedDocTitle;
158 17
        } elseif ($page->altPageTitle) {
159
            $pageTitle = $page->altPageTitle;
160
        } else {
161 17
            $pageTitle = $page->page['title'];
162
        }
163
164 37
        return $pageTitle;
165
    }
166
167
    /**
168
     * Retrieves the page's body
169
     *
170
     * @return string the page's body
171
     */
172
    public function getPageBody()
173
    {
174
        // @extensionScannerIgnoreLine
175
        $pageContent = $this->content;
176
177
        return stristr($pageContent, '<body');
178
    }
179
}
180