silverstripe-subsites/code/tasks/SubsiteCopyPagesTask.php

93 lines
3.2 KiB
PHP
Raw Normal View History

2012-08-10 11:13:43 +02:00
<?php
2016-09-22 16:38:29 +02:00
2017-05-24 12:32:05 +02:00
namespace SilverStripe\Subsites\Tasks;
use InvalidArgumentException;
2017-05-24 15:26:28 +02:00
use SilverStripe\Dev\BuildTask;
use SilverStripe\ORM\DataObject;
2017-05-24 12:32:05 +02:00
use SilverStripe\Subsites\Model\Subsite;
2017-05-24 13:36:04 +02:00
use SilverStripe\Subsites\Pages\SubsitesVirtualPage;
2017-05-24 15:26:28 +02:00
use SilverStripe\Versioned\Versioned;
2017-05-24 13:36:04 +02:00
2017-05-24 12:32:05 +02:00
2012-08-10 11:13:43 +02:00
/**
* Handy alternative to copying pages when creating a subsite through the UI.
2017-05-29 13:42:42 +02:00
*
* Can be used to batch-add new pages after subsite creation, or simply to
* process a large site outside of the UI.
2012-08-10 11:13:43 +02:00
*
* Example: sake dev/tasks/SubsiteCopyPagesTask from=<subsite-source> to=<subsite-target>
2017-05-29 13:42:42 +02:00
*
* @package subsites
2012-08-10 11:13:43 +02:00
*/
2017-05-24 15:26:28 +02:00
class SubsiteCopyPagesTask extends BuildTask
{
protected $title = 'Copy pages to different subsite';
protected $description = '';
2017-05-29 13:42:42 +02:00
public function run($request)
2017-05-24 15:26:28 +02:00
{
$subsiteFromId = $request->getVar('from');
if (!is_numeric($subsiteFromId)) {
throw new InvalidArgumentException('Missing "from" parameter');
}
2017-05-29 13:42:42 +02:00
$subsiteFrom = DataObject::get_by_id('Subsite', $subsiteFromId);
2017-05-24 15:26:28 +02:00
if (!$subsiteFrom) {
throw new InvalidArgumentException('Subsite not found');
}
$subsiteToId = $request->getVar('to');
if (!is_numeric($subsiteToId)) {
throw new InvalidArgumentException('Missing "to" parameter');
}
2017-05-29 13:42:42 +02:00
$subsiteTo = DataObject::get_by_id('Subsite', $subsiteToId);
2017-05-24 15:26:28 +02:00
if (!$subsiteTo) {
throw new InvalidArgumentException('Subsite not found');
}
$useVirtualPages = (bool)$request->getVar('virtual');
Subsite::changeSubsite($subsiteFrom);
// Copy data from this template to the given subsite. Does this using an iterative depth-first search.
// This will make sure that the new parents on the new subsite are correct, and there are no funny
// issues with having to check whether or not the new parents have been added to the site tree
// when a page, etc, is duplicated
2017-05-29 13:42:42 +02:00
$stack = array(array(0,0));
2017-05-24 15:26:28 +02:00
while (count($stack) > 0) {
list($sourceParentID, $destParentID) = array_pop($stack);
2017-05-29 13:42:42 +02:00
$children = Versioned::get_by_stage(SiteTree::class, 'Live', "\"ParentID\" = $sourceParentID", '');
2017-05-24 15:26:28 +02:00
if ($children) {
foreach ($children as $child) {
if ($useVirtualPages) {
$childClone = new SubsitesVirtualPage();
$childClone->writeToStage('Stage');
$childClone->CopyContentFromID = $child->ID;
$childClone->SubsiteID = $subsiteTo->ID;
} else {
$childClone = $child->duplicateToSubsite($subsiteTo->ID, true);
}
$childClone->ParentID = $destParentID;
$childClone->writeToStage('Stage');
$childClone->publish('Stage', 'Live');
2017-05-29 13:42:42 +02:00
array_push($stack, array($child->ID, $childClone->ID));
2017-05-24 15:26:28 +02:00
$this->log(sprintf('Copied "%s" (#%d, %s)', $child->Title, $child->ID, $child->Link()));
}
}
unset($children);
}
}
2017-05-29 13:42:42 +02:00
public function log($msg)
2017-05-24 15:26:28 +02:00
{
echo $msg . "\n";
}
2017-05-24 12:32:05 +02:00
}