forked from ezsystems/ezpublish-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NameSchemaService.php
439 lines (394 loc) · 13.2 KB
/
NameSchemaService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
/**
* File containing the NameSchemaService class
*
* @copyright Copyright (C) 1999-2013 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version //autogentag//
*/
namespace eZ\Publish\Core\Repository;
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
/**
* NameSchemaService is internal service for resolving content name and url alias patterns.
* This code supports content name pattern groups.
*
* Syntax:
* <code>
* <attribute_identifier>
* <attribute_identifier> <2nd-identifier>
* User text <attribute_identifier>|(<2nd-identifier><3rd-identifier>)
* </code>
*
* Example:
* <code>
* <nickname|(<firstname> <lastname>)>
* </code>
*
* Tokens are looked up from left to right. If a match is found for the
* leftmost token, the 2nd token will not be used. Tokens are representations
* of fields. So a match means that that the current field has data.
*
* Tokens are the field definition identifiers which are used in the class edit-interface.
*
* @internal
*/
class NameSchemaService
{
/**
* The string to use to signify group tokens.
*
* @var string
*/
const META_STRING = 'EZMETAGROUP_';
/**
* @var \eZ\Publish\API\Repository\Repository
*/
protected $repository;
/**
* @var array
*/
protected $settings;
/**
* Constructs a object to resolve $nameSchema with $contentVersion fields values
*
* @param \eZ\Publish\API\Repository\Repository $repository
* @param array $settings
*
* @return \eZ\Publish\Core\Repository\NameSchemaService
*/
public function __construct( RepositoryInterface $repository, array $settings = array() )
{
$this->repository = $repository;
// Union makes sure default settings are ignored if provided in argument
$this->settings = $settings + array(
'limit' => 150,
'sequence' => '...',
);
}
/**
* Convenience method for resolving URL alias schema
*
* @param \eZ\Publish\API\Repository\Values\Content\Content $content
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentType|null $contentType
*
* @return array
*/
public function resolveUrlAliasSchema( Content $content, ContentType $contentType = null )
{
if ( $contentType === null )
{
$contentType = $this->repository->getContentTypeService()->loadContentType(
$content->contentInfo->contentTypeId
);
}
return $this->resolve(
strlen( $contentType->urlAliasSchema ) === 0 ?
$contentType->nameSchema :
$contentType->urlAliasSchema,
$contentType,
$content->fields,
$content->versionInfo->languageCodes
);
}
/**
* Convenience method for resolving name schema
*
* @param \eZ\Publish\API\Repository\Values\Content\Content $content
* @param array $fieldMap
* @param array $languageCodes
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentType|null $contentType
*
* @return array
*/
public function resolveNameSchema( Content $content, array $fieldMap = array(), array $languageCodes = array(), ContentType $contentType = null )
{
if ( $contentType === null )
{
$contentType = $this->repository->getContentTypeService()->loadContentType(
$content->contentInfo->contentTypeId
);
}
$languageCodes = $languageCodes ?: $content->versionInfo->languageCodes;
return $this->resolve(
$contentType->nameSchema,
$contentType,
$this->mergeFieldMap(
$content,
$fieldMap,
$languageCodes
),
$languageCodes
);
}
/**
* Convenience method for resolving name schema
*
* @param \eZ\Publish\API\Repository\Values\Content\Content $content
* @param array $fieldMap
* @param array $languageCodes
*
* @return array
*/
protected function mergeFieldMap( Content $content, array $fieldMap, array $languageCodes )
{
if ( empty( $fieldMap ) )
{
return $content->fields;
}
$mergedFieldMap = array();
foreach ( $content->fields as $fieldIdentifier => $fieldLanguageMap )
{
foreach ( $languageCodes as $languageCode )
{
$mergedFieldMap[$fieldIdentifier][$languageCode] = isset( $fieldMap[$fieldIdentifier][$languageCode] )
? $fieldMap[$fieldIdentifier][$languageCode]
: $fieldLanguageMap[$languageCode];
}
}
return $mergedFieldMap;
}
/**
* Returns the real name for a content name pattern
*
* @param string $nameSchema
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType
* @param array $fieldMap
* @param array $languageCodes
*
* @return string
*/
public function resolve( $nameSchema, ContentType $contentType, array $fieldMap, array $languageCodes )
{
list( $filteredNameSchema, $groupLookupTable ) = $this->filterNameSchema( $nameSchema );
$tokens = $this->extractTokens( $filteredNameSchema );
$schemaIdentifiers = $this->getIdentifiers( $nameSchema );
$names = array();
foreach ( $languageCodes as $languageCode )
{
// Fetch titles for language code
$titles = $this->getFieldTitles( $schemaIdentifiers, $contentType, $fieldMap, $languageCode );
$name = $filteredNameSchema;
// Replace tokens with real values
foreach ( $tokens as $token )
{
$string = $this->resolveToken( $token, $titles, $groupLookupTable );
$name = str_replace( $token, $string, $name );
}
// Make sure length is not longer then $limit unless it's 0
if ( $this->settings["limit"] && strlen( $name ) > $this->settings["limit"] )
{
$name = rtrim( substr( $name, 0, $this->settings["limit"] - strlen( $this->settings["sequence"] ) ) ) . $this->settings["sequence"];
}
$names[$languageCode] = $name;
}
return $names;
}
/**
* Fetches the list of available Field identifiers in the token and returns
* an array of their current title value.
*
* @see \eZ\Publish\Core\Repository\FieldType::getName()
*
* @param string[] $schemaIdentifiers
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType $fieldDefinitions
* @param array $fieldMap
* @param string $languageCode
*
* @return string[] Key is the field identifier, value is the title value
*/
protected function getFieldTitles( array $schemaIdentifiers, ContentType $contentType, array $fieldMap, $languageCode )
{
$fieldTitles = array();
foreach ( $schemaIdentifiers as $fieldDefinitionIdentifier )
{
if ( isset( $fieldMap[$fieldDefinitionIdentifier][$languageCode] ) )
{
$fieldDefinition = $contentType->getFieldDefinition( $fieldDefinitionIdentifier );
$fieldType = $this->repository->getFieldTypeService()->getFieldType(
$fieldDefinition->fieldTypeIdentifier
);
$fieldTitles[$fieldDefinitionIdentifier] = $fieldType->getName(
$fieldMap[$fieldDefinitionIdentifier][$languageCode]
);
}
}
return $fieldTitles;
}
/**
* Extract all tokens from $namePattern
*
* Example:
* <code>
* Text <token> more text ==> <token>
* </code>
*
* @param string $nameSchema
*
* @return array
*/
protected function extractTokens( $nameSchema )
{
preg_match_all(
"|<([^>]+)>|U",
$nameSchema,
$tokenArray
);
return $tokenArray[0];
}
/**
* Looks up the value $token should be replaced with and returns this as
* a string. Meta strings denoting token groups are automatically
* inferred.
*
* @param string $token
* @param array $titles
*
* @param array $groupLookupTable
*
* @return string
*/
protected function resolveToken( $token, $titles, $groupLookupTable )
{
$replaceString = "";
$tokenParts = $this->tokenParts( $token );
foreach ( $tokenParts as $tokenPart )
{
if ( $this->isTokenGroup( $tokenPart ) )
{
$replaceString = $groupLookupTable[$tokenPart];
$groupTokenArray = $this->extractTokens( $replaceString );
foreach ( $groupTokenArray as $groupToken )
{
$replaceString = str_replace(
$groupToken,
$this->resolveToken(
$groupToken,
$titles,
$groupLookupTable
),
$replaceString
);
}
// We want to stop after the first matching token part / identifier is found
// <id1|id2> if id1 has a value, id2 will not be used.
// In this case id1 or id1 is a token group.
break;
}
else
{
if ( array_key_exists( $tokenPart, $titles ) && $titles[$tokenPart] !== '' && $titles[$tokenPart] !== null )
{
$replaceString = $titles[$tokenPart];
// We want to stop after the first matching token part / identifier is found
// <id1|id2> if id1 has a value, id2 will not be used.
break;
}
}
}
return $replaceString;
}
/**
* Checks whether $identifier is a placeholder for a token group.
*
* @param string $identifier
*
* @return boolean
*/
protected function isTokenGroup( $identifier )
{
if ( strpos( $identifier, self::META_STRING ) === false )
{
return false;
}
return true;
}
/**
* Returns the different constituents of $token in an array.
* The normal case here is that the different identifiers within one token
* will be tokenized and returned.
*
* Example:
* <code>
* "<title|text>" ==> array( 'title', 'text' )
* </code>
*
* @param string $token
*
* @return array
*/
protected function tokenParts( $token )
{
return preg_split( '#\\W#', $token, -1, PREG_SPLIT_NO_EMPTY );
}
/**
* Builds a lookup / translation table for groups in the $namePattern.
* The groups are referenced with a generated meta-token in the original
* name pattern.
*
* Returns intermediate name pattern where groups are replaced with meta-
* tokens.
*
* @param string $nameSchema
*
* @return string
*/
protected function filterNameSchema( $nameSchema )
{
$retNamePattern = "";
$foundGroups = preg_match_all( "/[<|\\|](\\(.+\\))[\\||>]/U", $nameSchema, $groupArray );
$groupLookupTable = array();
if ( $foundGroups )
{
$i = 0;
foreach ( $groupArray[1] as $group )
{
// Create meta-token for group
$metaToken = self::META_STRING . $i;
// Insert the group with its placeholder token
$retNamePattern = str_replace( $group, $metaToken, $nameSchema );
// Remove the pattern "(" ")" from the tokens
$group = str_replace( array( '(', ')' ), '', $group );
$groupLookupTable[$metaToken] = $group;
++$i;
}
$nameSchema = $retNamePattern;
}
return array( $nameSchema, $groupLookupTable );
}
/**
* Returns all identifiers from all tokens in the name schema.
*
* @param string $schemaString
*
* @return array
*/
protected function getIdentifiers( $schemaString )
{
$allTokens = '#<(.*)>#U';
$identifiers = '#\\W#';
$tmpArray = array();
preg_match_all( $allTokens, $schemaString, $matches );
foreach ( $matches[1] as $match )
{
$tmpArray[] = preg_split( $identifiers, $match, -1, PREG_SPLIT_NO_EMPTY );
}
$retArray = array();
foreach ( $tmpArray as $matchGroup )
{
if ( is_array( $matchGroup ) )
{
foreach ( $matchGroup as $item )
{
$retArray[] = $item;
}
}
else
{
$retArray[] = $matchGroup;
}
}
return $retArray;
}
}