Completed
Pull Request — master (#1112)
by Timo
31:50
created

Builder::getPageDocumentId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\ApacheSolrDocument;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2017 Timo Hund <[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 2 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 Apache_Solr_Document;
28
use ApacheSolrForTypo3\Solr\Access\Rootline;
29
use ApacheSolrForTypo3\Solr\Domain\Variants\IdBuilder;
30
use ApacheSolrForTypo3\Solr\Site;
31
use ApacheSolrForTypo3\Solr\Typo3PageContentExtractor;
32
use ApacheSolrForTypo3\Solr\Util;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
35
36
/**
37
 * Builder class to build an ApacheSolrDocument
38
 *
39
 * Responsible to build Apache_Solr_Documents
40
 *
41
 * @author Timo Hund <[email protected]>
42
 */
43
class Builder
44
{
45
    /**
46
     * @var IdBuilder
47
     */
48
    protected $variantIdBuilder;
49
50
    /**
51
     * Builder constructor.
52
     * @param IdBuilder|null $variantIdBuilder
53
     */
54
    public function __construct(IdBuilder $variantIdBuilder = null)
55
    {
56
        $this->variantIdBuilder = is_null($variantIdBuilder) ? GeneralUtility::makeInstance(IdBuilder::class) : $variantIdBuilder;
57
    }
58
59
    /**
60
     * This method can be used to build an Apache_Solr_Document from a TYPO3 page.
61
     *
62
     * @param TypoScriptFrontendController $page
63
     * @param string $url
64
     * @param Rootline $pageAccessRootline
65
     * @param string $mountPointParameter
66
     * @return Apache_Solr_Document|object
67
     */
68
    public function fromPage(TypoScriptFrontendController $page, $url, Rootline $pageAccessRootline, $mountPointParameter)
69
    {
70
        /* @var $document \Apache_Solr_Document */
71
        $document = GeneralUtility::makeInstance(Apache_Solr_Document::class);
72
        $site = $this->getSiteByPageId($page->id);
73
        $pageRecord = $page->page;
74
75
        $accessGroups = $this->getDocumentIdGroups($pageAccessRootline);
76
        $documentId = $this->getPageDocumentId($page, $accessGroups, $mountPointParameter);
77
78
        $document->setField('id', $documentId);
79
        $document->setField('site', $site->getDomain());
80
        $document->setField('siteHash', $site->getSiteHash());
81
        $document->setField('appKey', 'EXT:solr');
82
        $document->setField('type', 'pages');
83
84
        // system fields
85
        $document->setField('uid', $page->id);
86
        $document->setField('pid', $pageRecord['pid']);
87
88
        // variantId
89
        $variantId = $this->variantIdBuilder->buildFromTypeAndUid('pages', $page->id);
90
        $document->setField('variantId', $variantId);
91
92
        $document->setField('typeNum', $page->type);
93
        $document->setField('created', $pageRecord['crdate']);
94
        $document->setField('changed', $pageRecord['SYS_LASTCHANGED']);
95
96
        $rootline = $this->getRootLineFieldValue($page->id, $mountPointParameter);
97
        $document->setField('rootline', $rootline);
98
99
        // access
100
        $this->addAccessField($document, $pageAccessRootline);
101
        $this->addEndtimeField($document, $pageRecord);
102
103
        // content
104
        $contentExtractor = $this->getExtractorForPageContent($page->content);
105
        $document->setField('title', $contentExtractor->getPageTitle());
106
        $document->setField('subTitle', $pageRecord['subtitle']);
107
        $document->setField('navTitle', $pageRecord['nav_title']);
108
        $document->setField('author', $pageRecord['author']);
109
        $document->setField('description', $pageRecord['description']);
110
        $document->setField('abstract', $pageRecord['abstract']);
111
        $document->setField('content', $contentExtractor->getIndexableContent());
112
        $document->setField('url', $url);
113
114
        $this->addKeywordsField($document, $pageRecord);
115
        $this->addTagContentFields($document, $contentExtractor->getTagContent());
116
117
        return $document;
118
    }
119
120
    /**
121
     * @param TypoScriptFrontendController  $page
122
     * @param string $accessGroups
123
     * @param string $mountPointParameter
124
     * @return string
125
     */
126
    protected function getPageDocumentId($page, $accessGroups, $mountPointParameter)
127
    {
128
        return Util::getPageDocumentId($page->id, $page->type, $page->sys_language_uid, $accessGroups, $mountPointParameter);
129
    }
130
131
    /**
132
     * @param integer $pageId
133
     * @return Site
134
     */
135
    protected function getSiteByPageId($pageId)
136
    {
137
        return Site::getSiteByPageId($pageId);
138
    }
139
140
    /**
141
     * @param string $pageContent
142
     * @return Typo3PageContentExtractor
143
     */
144
    protected function getExtractorForPageContent($pageContent)
145
    {
146
        return GeneralUtility::makeInstance(Typo3PageContentExtractor::class, $pageContent);
147
    }
148
149
    /**
150
     * Builds the content for the rootline field.
151
     *
152
     * @param int $pageId
153
     * @param string $mountPointParameter
154
     * @return string
155
     */
156
    protected function getRootLineFieldValue($pageId, $mountPointParameter)
157
    {
158
        $rootline = $pageId;
159
        if ($mountPointParameter !== '') {
160
            $rootline .= ',' . $mountPointParameter;
161
        }
162
        return $rootline;
163
    }
164
165
    /**
166
     * Gets a comma separated list of frontend user groups to use for the
167
     * document ID.
168
     *
169
     * @param Rootline $pageAccessRootline
170
     * @return string A comma separated list of frontend user groups.
171
     */
172
    protected function getDocumentIdGroups(Rootline $pageAccessRootline)
173
    {
174
        $groups = $pageAccessRootline->getGroups();
175
        $groups = Rootline::cleanGroupArray($groups);
176
177
        if (empty($groups)) {
178
            $groups[] = 0;
179
        }
180
181
        $groups = implode(',', $groups);
182
183
        return $groups;
184
    }
185
186
    /**
187
     * Adds the access field to the document if needed.
188
     *
189
     * @param \Apache_Solr_Document $document
190
     * @param Rootline $pageAccessRootline
191
     */
192
    protected function addAccessField(\Apache_Solr_Document $document, Rootline $pageAccessRootline)
193
    {
194
        $access = (string)$pageAccessRootline;
195
        if (trim($access) !== '') {
196
            $document->setField('access', $access);
197
        }
198
    }
199
200
    /**
201
     * Adds the endtime field value to the Apache_Solr_Document.
202
     *
203
     * @param \Apache_Solr_Document $document
204
     * @param array $pageRecord
205
     */
206
    protected function addEndtimeField(\Apache_Solr_Document  $document, $pageRecord)
207
    {
208
        if ($pageRecord['endtime']) {
209
            $document->setField('endtime', $pageRecord['endtime']);
210
        }
211
    }
212
213
    /**
214
     * Adds keywords, multi valued.
215
     *
216
     * @param \Apache_Solr_Document $document
217
     * @param array $pageRecord
218
     */
219
    protected function addKeywordsField(\Apache_Solr_Document $document, $pageRecord)
220
    {
221
        if (!isset($pageRecord['keywords'])) {
222
            return;
223
        }
224
225
        $keywords = array_unique(GeneralUtility::trimExplode(',', $pageRecord['keywords'], true));
226
        foreach ($keywords as $keyword) {
227
            $document->addField('keywords', $keyword);
228
        }
229
    }
230
231
    /**
232
     * Add content from several tags like headers, anchors, ...
233
     *
234
     * @param \Apache_Solr_Document $document
235
     * @param array $tagContent
236
     */
237
    protected function addTagContentFields(\Apache_Solr_Document  $document, $tagContent = [])
238
    {
239
        foreach ($tagContent as $fieldName => $fieldValue) {
240
            $document->setField($fieldName, $fieldValue);
241
        }
242
    }
243
}
244