Passed
Pull Request — master (#2964)
by Jens
36:36
created

Typo3PageContentExtractor::getIndexableContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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