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
|
68 |
|
public function getContentMarkedForIndexing() |
49
|
|
|
{ |
50
|
68 |
|
return $this->extractContentMarkedForIndexing($this->content); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Extracts the markup wrapped with TYPO3SEARCH_begin and TYPO3SEARCH_end |
55
|
|
|
* markers. |
56
|
|
|
* |
57
|
|
|
* @param string $html HTML markup with TYPO3SEARCH markers for content that should be indexed |
58
|
|
|
* @return string HTML markup found between TYPO3SEARCH markers |
59
|
|
|
*/ |
60
|
76 |
|
protected function extractContentMarkedForIndexing($html) |
61
|
|
|
{ |
62
|
76 |
|
preg_match_all('/<!--\s*?TYPO3SEARCH_begin\s*?-->.*?<!--\s*?TYPO3SEARCH_end\s*?-->/mis', |
63
|
76 |
|
$html, $indexableContents); |
64
|
76 |
|
$indexableContent = implode($indexableContents[0], ''); |
|
|
|
|
65
|
|
|
|
66
|
76 |
|
$indexableContent = $this->excludeContentByClass($indexableContent); |
67
|
76 |
|
if (empty($indexableContent) && $this->getConfiguration()->getLoggingIndexingMissingTypo3SearchMarkers()) { |
68
|
17 |
|
$this->logger = GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__); |
69
|
17 |
|
$this->logger->log(SolrLogManager::WARNING, 'No TYPO3SEARCH markers found.'); |
70
|
|
|
} |
71
|
|
|
|
72
|
76 |
|
return $indexableContent; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Exclude some html parts by class inside content wrapped with TYPO3SEARCH_begin and TYPO3SEARCH_end |
77
|
|
|
* markers. |
78
|
|
|
* |
79
|
|
|
* @param string $indexableContent HTML markup |
80
|
|
|
* @return string HTML |
81
|
|
|
*/ |
82
|
79 |
|
public function excludeContentByClass($indexableContent) |
83
|
|
|
{ |
84
|
79 |
|
if (empty(trim($indexableContent))) { |
85
|
17 |
|
return $indexableContent; |
86
|
|
|
} |
87
|
|
|
|
88
|
62 |
|
$excludeClasses = $this->getConfiguration()->getIndexQueuePagesExcludeContentByClassArray(); |
89
|
62 |
|
if (count($excludeClasses) === 0) { |
90
|
7 |
|
return $indexableContent; |
91
|
|
|
} |
92
|
|
|
|
93
|
55 |
|
$isInContent = Util::containsOneOfTheStrings($indexableContent, $excludeClasses); |
94
|
55 |
|
if (!$isInContent) { |
95
|
52 |
|
return $indexableContent; |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
$doc = new \DOMDocument('1.0', 'UTF-8'); |
99
|
3 |
|
libxml_use_internal_errors(true); |
100
|
3 |
|
$doc->loadHTML('<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL . $indexableContent); |
101
|
3 |
|
$xpath = new \DOMXPath($doc); |
102
|
3 |
|
foreach ($excludeClasses as $excludePart) { |
103
|
3 |
|
$elements = $xpath->query("//*[contains(@class,'" . $excludePart . "')]"); |
104
|
3 |
|
if (count($elements) == 0) { |
|
|
|
|
105
|
|
|
continue; |
106
|
|
|
} |
107
|
|
|
|
108
|
3 |
|
foreach ($elements as $element) { |
109
|
3 |
|
$element->parentNode->removeChild($element); |
110
|
|
|
} |
111
|
|
|
} |
112
|
3 |
|
$html = $doc->saveHTML($doc->documentElement->parentNode); |
113
|
|
|
// remove XML-Preamble, newlines and doctype |
114
|
3 |
|
$html = preg_replace('/(<\?xml[^>]+\?>|\r?\n|<!DOCTYPE.+?>)/imS', '', $html); |
115
|
3 |
|
$html = str_replace(['<html>', '</html>', '<body>', '</body>'], ['', '', '', ''], $html); |
116
|
|
|
|
117
|
3 |
|
return $html; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns the cleaned indexable content from the page's HTML markup. |
122
|
|
|
* |
123
|
|
|
* The content is cleaned from HTML tags and control chars Solr could |
124
|
|
|
* stumble on. |
125
|
|
|
* |
126
|
|
|
* @return string Indexable, cleaned content ready for indexing. |
127
|
|
|
*/ |
128
|
76 |
|
public function getIndexableContent() |
129
|
|
|
{ |
130
|
76 |
|
$content = $this->extractContentMarkedForIndexing($this->content); |
131
|
|
|
|
132
|
|
|
// clean content |
133
|
76 |
|
$content = self::cleanContent($content); |
134
|
76 |
|
$content = trim($content); |
135
|
76 |
|
$content = preg_replace('!\s+!', ' ', $content); // reduce multiple spaces to one space |
136
|
|
|
|
137
|
76 |
|
return $content; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Retrieves the page's title by checking the indexedDocTitle, altPageTitle, |
142
|
|
|
* and regular page title - in that order. |
143
|
|
|
* |
144
|
|
|
* @return string the page's title |
145
|
|
|
*/ |
146
|
68 |
|
public function getPageTitle() |
147
|
|
|
{ |
148
|
68 |
|
$page = $GLOBALS['TSFE']; |
149
|
|
|
|
150
|
68 |
|
if ($page->indexedDocTitle) { |
151
|
51 |
|
$pageTitle = $page->indexedDocTitle; |
152
|
17 |
|
} elseif ($page->altPageTitle) { |
153
|
|
|
$pageTitle = $page->altPageTitle; |
154
|
|
|
} else { |
155
|
17 |
|
$pageTitle = $page->page['title']; |
156
|
|
|
} |
157
|
|
|
|
158
|
68 |
|
return $pageTitle; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Retrieves the page's body |
163
|
|
|
* |
164
|
|
|
* @return string the page's body |
165
|
|
|
*/ |
166
|
|
|
public function getPageBody() |
167
|
|
|
{ |
168
|
|
|
$pageContent = $this->content; |
169
|
|
|
|
170
|
|
|
return stristr($pageContent, '<body'); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|