forked from ezsystems/ezpublish-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocationHandler.php
719 lines (643 loc) · 23.5 KB
/
LocationHandler.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
<?php
/**
* File containing the LocationHandler implementation
*
* @copyright Copyright (C) 1999-2014 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\Persistence\InMemory;
use eZ\Publish\SPI\Persistence\Content\Location\Handler as LocationHandlerInterface;
use eZ\Publish\SPI\Persistence\Content\Location\CreateStruct;
use eZ\Publish\SPI\Persistence\Content\Location\UpdateStruct;
use eZ\Publish\SPI\Persistence\Content\Location as LocationValue;
use eZ\Publish\Core\Base\Exceptions\NotFoundException as NotFound;
use eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct;
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
class LocationHandler implements LocationHandlerInterface
{
const CHARS_ACCENT = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËéèêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ';
const CHARS_NOACCENT = 'AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn';
/**
* @var Handler
*/
protected $handler;
/**
* @var Backend
*/
protected $backend;
/**
* Setups current handler instance with reference to Handler object that created it.
*
* @param Handler $handler
* @param Backend $backend The storage engine backend
*/
public function __construct( Handler $handler, Backend $backend )
{
$this->handler = $handler;
$this->backend = $backend;
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
public function load( $locationId )
{
return $this->backend->load( 'Content\\Location', $locationId );
}
/**
* @see \eZ\Publish\SPI\Persistence\Content\Location\Handler::loadSubtreeIds
*/
public function loadSubtreeIds( $locationId )
{
$return = array();
foreach ( $this->getSubtreeLocations( $this->load( $locationId ) ) as $location )
{
$return[$location->id] = $location->contentId;
}
return $return;
}
/**
* Loads all locations for $contentId, optionally limited to a sub tree
* identified by $rootLocationId
*
* @param int $contentId
* @param int $rootLocationId
*
* @todo Add support for $rootLocationId when not child of node 1
*
* @return \eZ\Publish\SPI\Persistence\Content\Location[]
*/
public function loadLocationsByContent( $contentId, $rootLocationId = null )
{
if ( $rootLocationId )
return $this->backend->find(
'Content\\Location',
array( 'contentId' => $contentId, 'pathString' => "%/{$rootLocationId}/%" )
);
return $this->backend->find(
'Content\\Location',
array( 'contentId' => $contentId )
);
}
/**
* @see \eZ\Publish\SPI\Persistence\Content\Location\Handler::loadParentLocationsForDraftContent
*/
public function loadParentLocationsForDraftContent( $contentId )
{
$locations = $this->backend->find(
'Content\\Location',
array( 'contentId' => $contentId )
);
$parentLocations = array();
foreach ( $locations as $location )
{
$parentLocation = $this->backend->find(
"Content\\Location",
array( "id" => $location->parentId )
);
$contentInfo = $this->backend->load( 'Content\\ContentInfo', $location->contentId );
if ( $contentInfo->isPublished )
{
continue;
}
$parentLocations[] = $parentLocation[0];
}
return $parentLocations;
}
/**
* Loads the data for the location identified by $remoteId.
*
* @param string $remoteId
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
*
* @return \eZ\Publish\SPI\Persistence\Content\Location
*/
public function loadByRemoteId( $remoteId )
{
$locations = $this->backend->find(
'Content\\Location',
array( 'remoteId' => $remoteId )
);
if ( empty( $locations ) )
throw new NotFound( 'Location by remote id', $remoteId );
else if ( isset( $locations[1] ) )
throw new \RuntimeException( "Several Locations found with same remote id: {$remoteId}" );
return $locations[0];
}
/**
* Get all subtree locations for the given location (including), sorted by path string
*
* @param \eZ\Publish\SPI\Persistence\Content\Location $location
* @param array $locations
*
* @return array
*/
protected function getSubtreeLocations( $location, &$locations = array() )
{
if ( empty( $locations ) ) $locations[] = $location;
$subLocations = $this->backend->find( "Content\\Location", array( "parentId" => $location->id ) );
usort(
$subLocations,
function ( $subLocationA, $subLocationB )
{
return strnatcmp( $subLocationA->pathString, $subLocationB->pathString );
}
);
foreach ( $subLocations as $subLocation )
{
$locations[] = $subLocation;
$this->getSubtreeLocations( $subLocation, $locations );
}
return $locations;
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
public function copySubtree( $sourceId, $destinationParentId )
{
$sourceLocation = $this->load( $sourceId );
$children = $this->getSubtreeLocations( $sourceLocation );
$parentLocation = $this->load( $destinationParentId );
$contentMap = array();
$locationMap = array(
$children[0]->parentId => array(
"id" => $destinationParentId,
"hidden" => $parentLocation->hidden,
"invisible" => $parentLocation->invisible
)
);
$locations = array();
foreach ( $children as $child )
{
$locations[$child->contentId][] = $child->id;
}
$time = time();
$mainLocations = array();
$mainLocationsUpdate = array();
foreach ( $children as $index => $child )
{
$originalContentInfo = $this->handler->contentHandler()->loadContentInfo( $child->contentId );
if ( !isset( $contentMap[$child->contentId] ) )
{
$content = $this->handler->contentHandler()->copy(
$child->contentId,
$originalContentInfo->currentVersionNo
);
$newContentId = $content->versionInfo->contentInfo->id;
$content = $this->handler->contentHandler()->publish(
$newContentId,
$content->versionInfo->versionNo,
new MetadataUpdateStruct(
array(
"publicationDate" => $time,
"modificationDate" => $time
)
)
);
$contentMap[$child->contentId] = $newContentId;
}
$createStruct = new CreateStruct();
$createStruct->contentVersion = $originalContentInfo->currentVersionNo;
$createStruct->hidden = $child->hidden;
$createStruct->pathIdentificationString = $child->pathIdentificationString;
$createStruct->priority = $child->priority;
$createStruct->remoteId = md5( uniqid( get_class( $this ), true ) );
$createStruct->sortField = $child->sortField;
$createStruct->sortOrder = $child->sortOrder;
$createStruct->contentId = $contentMap[$child->contentId];
$createStruct->parentId = $locationMap[$child->parentId]["id"];
$createStruct->invisible = $createStruct->hidden ||
$locationMap[$child->parentId]["hidden"] ||
$locationMap[$child->parentId]["invisible"];
// Use content main node if already set, otherwise use this node as main
if ( isset( $mainLocations[$child->contentId] ) )
{
$createStruct->mainLocationId = $locationMap[$mainLocations[$child->contentId]]["id"];
}
else
{
$createStruct->mainLocationId = true;
$mainLocations[$child->contentId] = $child->id;
// If needed mark for later update
if (
in_array( $originalContentInfo->mainLocationId, $locations[$child->contentId] ) &&
count( $locations[$child->contentId] ) > 1 &&
$child->id !== $originalContentInfo->mainLocationId
)
{
$mainLocationsUpdate[$child->contentId] = $originalContentInfo->mainLocationId;
}
}
$newLocation = $this->create( $createStruct );
$locationMap[$child->id] = array(
"id" => $newLocation->id,
"hidden" => $newLocation->hidden,
"invisible" => $newLocation->invisible
);
if ( $index === 0 ) $copiedSubtreeRootLocation = $newLocation;
}
// Update main locations
foreach ( $mainLocationsUpdate as $contentId => $mainLocationId )
{
$this->changeMainLocation(
$contentMap[$contentId],
$locationMap[$mainLocationId]["id"]
);
}
// If subtree root is main location for its content, update subtree section to the one of the
// parent location content
$subtreeRootContentInfo = $this->handler->contentHandler()->loadContentInfo( $copiedSubtreeRootLocation->contentId );
if ( $subtreeRootContentInfo->mainLocationId === $copiedSubtreeRootLocation->id )
{
$this->setSectionForSubtree(
$copiedSubtreeRootLocation->id,
$this->handler->contentHandler()->loadContentInfo( $this->load( $destinationParentId )->contentId )->sectionId
);
}
return $copiedSubtreeRootLocation;
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
public function move( $sourceId, $destinationParentId )
{
$vo = $this->load( $sourceId );
$newParentVO = $this->load( $destinationParentId );
$oldPathString = $vo->pathString;
$newPathString = $newParentVO->pathString . $sourceId . '/';
$oldPathIdentificationString = $vo->pathIdentificationString;
$newPathIdentificationString = '';
if ( $newParentVO->parentId == 1 )
$newPathIdentificationString = $this->getStrippedContentName( $vo );
else
$newPathIdentificationString = $this->getPathIdentificationString( $newParentVO ) . '/' . $this->getStrippedContentName( $vo );
$this->backend->update(
'Content\\Location',
$sourceId,
array(
'parentId' => $destinationParentId,
'pathString' => $newPathString,
'pathIdentificationString' => $newPathIdentificationString
)
);
$children = $this->backend->find( 'Content\\Location', array( 'pathString' => "$vo->pathString%" ) );
foreach ( $children as $child )
{
$this->backend->update(
'Content\\Location',
$child->id,
array(
'pathString' => str_replace( $oldPathString, $newPathString, $child->pathString ),
'pathIdentificationString' => str_replace( $oldPathIdentificationString, $newPathIdentificationString, $child->pathIdentificationString )
)
);
}
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
public function markSubtreeModified( $locationId, $timestamp = null )
{
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
public function hide( $id )
{
$this->backend->update( 'Content\\Location', $id, array( 'hidden' => true, 'invisible' => true ) );
$locationVO = $this->backend->load( 'Content\\Location', $id );
$this->backend->updateByMatch(
"Content\\Location",
array( "pathString" => "{$locationVO->pathString}%" ),
array( "invisible" => true )
);
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
public function unHide( $id )
{
$this->backend->update( 'Content\\Location', $id, array( 'hidden' => false, 'invisible' => false ) );
$locationVO = $this->backend->load( 'Content\\Location', $id );
$hiddenLocations = $this->backend->find(
"Content\\Location",
array(
"pathString" => "{$locationVO->pathString}%",
"hidden" => true
)
);
$invisibleLocations = $this->backend->find(
"Content\\Location",
array(
"pathString" => "{$locationVO->pathString}%",
"invisible" => true,
"hidden" => false
)
);
$locationsToUnhide = array();
// Loop against all invisible locations and figure out
// if they are under a hidden one.
// If this is the case, the location won't be made visible
foreach ( $invisibleLocations as $loc )
{
foreach ( $hiddenLocations as $hiddenLoc )
{
if ( strpos( $loc->pathString, $hiddenLoc->pathString ) === 0 )
{
continue 2;
}
}
$locationsToUnhide[] = $loc->id;
}
$this->backend->updateByMatch( 'Content\\Location', array( 'id' => $locationsToUnhide ), array( 'invisible' => false ) );
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
public function swap( $locationId1, $locationId2 )
{
$location1 = $this->backend->load( 'Content\\Location', $locationId1 );
$content1 = $this->backend->load( 'Content', $location1->contentId );
$location2 = $this->backend->load( 'Content\\Location', $locationId2 );
$content2 = $this->backend->load( 'Content', $location2->contentId );
$this->backend->update(
'Content\\Location',
$locationId1,
array(
'contentId' => $location2->contentId,
)
);
$this->backend->update(
'Content\\Location',
$locationId2,
array(
'contentId' => $location1->contentId,
)
);
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
public function update( UpdateStruct $location, $locationId )
{
$this->backend->update(
'Content\\Location',
$locationId,
(array)$location
);
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
public function create( CreateStruct $locationStruct )
{
$parentId = $locationStruct->parentId;
$parent = $this->load( $parentId );
$params = (array)$locationStruct;
$params['parentId'] = $parentId;
$params['depth'] = $parent->depth + 1;
$params['hidden'] = (bool)$locationStruct->hidden;
if ( !isset( $params['remoteId'] ) )
{
$params['remoteId'] = md5( uniqid( 'Content\\Location', true ) );
}
// Creation, then update for pathString/pathIdentificationString/mainLocationId
$mainLocationId = null;
$otherLocationsForContent = $this->backend->find( 'Content\\Location', array( 'contentId' => $locationStruct->contentId ) );
if ( !empty( $otherLocationsForContent ) )
{
$mainLocationId = $otherLocationsForContent[0]->id;
}
$vo = $this->backend->create( 'Content\\Location', $params );
$pathString = $parent->pathString . $vo->id . '/';
$this->backend->update(
'Content\\Location',
$vo->id,
array(
'pathString' => $pathString,
'pathIdentificationString' => $this->getPathIdentificationString( $vo )
)
);
$this->backend->update(
'Content\\ContentInfo',
$vo->contentId,
array(
'mainLocationId' => isset( $mainLocationId ) ? $mainLocationId : $vo->id
)
);
return $this->load( $vo->id );
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
public function removeSubtree( $locationId )
{
$location = $this->load( $locationId );
// Begin recursive call on children, if any
$directChildren = $this->backend->find( 'Content\\Location', array( 'parentId' => $locationId ) );
if ( !empty( $directChildren ) )
{
foreach ( $directChildren as $child )
{
$this->removeSubtree( $child->id );
}
}
$this->delete( $locationId );
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
public function setSectionForSubtree( $locationId, $sectionId )
{
$location = $this->load( $locationId );
$aContentIds = array( $location->contentId => true );
$children = $this->backend->find( 'Content\\Location', array( 'pathString' => "$location->pathString%" ) );
foreach ( $children as $child )
{
$aContentIds[$child->contentId] = true;
}
$this->backend->updateByMatch(
'Content\\ContentInfo',
array( 'id' => array_keys( $aContentIds ) ),
array( 'sectionId' => $sectionId ),
true
);
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
public function storeUrlAliasPath( $path, $locationId, $languageCode = null, $alwaysAvailable = false )
{
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
public function createCustomUrlAlias( $alias, $locationId, $forwarding = false, $languageCode = null, $alwaysAvailable = false )
{
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
public function createUrlHistoryEntry( $historicUrl, $locationId )
{
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
public function listUrlsForLocation( $locationId, $urlType )
{
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
public function removeUrlsForLocation( $locationId, array $urlIdentifier )
{
}
/**
* @see eZ\Publish\SPI\Persistence\Content\Location\Handler
*/
public function getPath( $locationId, $language )
{
}
/**
* Removes a location from its $locationId (but not its descendants)
* Content which looses its main Location will get the first
* of its other Locations assigned as the new main Location.
* If content has no location left, it's removed from backend
*
* @param mixed $locationId
*/
public function delete( $locationId )
{
$location = $this->load( $locationId );
$this->backend->delete( 'Content\\Location', $locationId );
$remainingLocations = $this->backend->find( 'Content\\Location', array( 'contentId' => $location->contentId ) );
// If no remaining location for associated content, remove the content as well
// Else, update the mainLocationId if needed
if ( empty( $remainingLocations ) )
{
try
{
$this->handler->contentHandler()->deleteContent( $location->contentId );
}
// Ignoring a NotFound exception since the content handler also takes care of removing itself and locations
catch ( NotFound $e )
{
}
}
else
{
$this->backend->update(
'Content\\ContentInfo',
$location->contentId,
array(
'mainLocationId' => $remainingLocations[0]->id
)
);
}
}
/**
* Returns locations given a parent $locationId.
*
* @todo Requires approbation
* @param mixed $locationId
*
* @return \eZ\Publish\SPI\Persistence\Content\Location[]
*/
public function loadByParentId( $locationId )
{
$result = $this->backend->find( "Content\\Location", array( "parentId" => $locationId ) );
// If no result is found this might be caused by an unexisting location ID.
// We call load() to trigger a NotFound exception in such case for consistencies
// across the API.
if ( empty( $result ) )
{
$this->load( $locationId );
}
return $result;
}
/**
* Changes main location of content identified by given $contentId to location identified by given $locationId
*
* @param mixed $contentId
* @param mixed $locationId
*
* @return void
*/
public function changeMainLocation( $contentId, $locationId )
{
$location = $this->backend->load( "Content\\Location", $locationId );
$this->backend->update(
'Content\\ContentInfo',
$contentId,
array(
'mainLocationId' => $locationId
)
);
$parentLocation = $this->backend->load(
"Content\\Location",
$location->parentId
);
$parentContent = $this->backend->load(
"Content\\ContentInfo",
$parentLocation->contentId
);
$this->setSectionForSubtree( $locationId, $parentContent->sectionId );
}
/**
* Returns pathIdentificationString for provided location value object
* @param \eZ\Publish\SPI\Persistence\Content\Location $vo
*
* @return string
*/
private function getPathIdentificationString( LocationValue $vo )
{
$parent = $this->load( $vo->parentId );
if ( $vo->parentId == 1 )
{
return '';
}
if ( empty( $parent->pathIdentificationString ) )
{
return $this->getStrippedContentName( $vo );
}
return $parent->pathIdentificationString . '/' . $this->getStrippedContentName( $vo );
}
/**
* Returns stripped content name from location value
* All downcase, special chars to underscores
* e.g. my_content_name
* @param LocationValue $vo
*
* @return string
*/
private function getStrippedContentName( LocationValue $vo )
{
$version = $this->backend->find(
"Content\\VersionInfo",
array(
"contentId" => $vo->contentId,
"versionNo" => $this->backend->load( 'Content\\ContentInfo', $vo->contentId )->currentVersionNo
)
);
return isset( $version[0]->names["eng-GB"] )
? preg_replace(
'`[^a-z0-9_]`i',
'_',
strtolower(
trim(
strtr(
// @todo Remove hardcoding of eng-GB
$version[0]->names["eng-GB"],
self::CHARS_ACCENT,
self::CHARS_NOACCENT
)
)
)
)
: null;
}
}