function run($request)
 {
     $subsiteFromId = $request->getVar('from');
     if (!is_numeric($subsiteFromId)) {
         throw new InvalidArgumentException('Missing "from" parameter');
     }
     $subsiteFrom = DataObject::get_by_id('Subsite', $subsiteFromId);
     if (!$subsiteFrom) {
         throw new InvalidArgumentException('Subsite not found');
     }
     $subsiteToId = $request->getVar('to');
     if (!is_numeric($subsiteToId)) {
         throw new InvalidArgumentException('Missing "to" parameter');
     }
     $subsiteTo = DataObject::get_by_id('Subsite', $subsiteToId);
     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
     $stack = array(array(0, 0));
     while (count($stack) > 0) {
         list($sourceParentID, $destParentID) = array_pop($stack);
         $children = Versioned::get_by_stage('SiteTree', 'Live', "\"ParentID\" = {$sourceParentID}", '');
         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');
                 array_push($stack, array($child->ID, $childClone->ID));
                 $this->log(sprintf('Copied "%s" (#%d, %s)', $child->Title, $child->ID, $child->Link()));
             }
         }
         unset($children);
     }
 }
 /**
  * This test ensures published Subsites Virtual Pages immediately reflect updates
  * to their published target pages. Note - this has to happen when the virtual page
  * is in a different subsite to the page you are editing and republishing,
  * otherwise the test will pass falsely due to current subsite ID being the same.
  */
 function testPublishedSubsiteVirtualPagesUpdateIfTargetPageUpdates()
 {
     // create page
     $p = new Page();
     $p->Content = 'Content';
     $p->Title = 'Title';
     $p->writeToStage('Stage');
     $p->publish('Stage', 'Live');
     $this->assertTrue($p->ExistsOnLive);
     // change to subsite
     $subsite = $this->objFromFixture('Subsite', 'subsite2');
     Subsite::changeSubsite($subsite->ID);
     Subsite::$disable_subsite_filter = false;
     // create svp in subsite
     $svp = new SubsitesVirtualPage();
     $svp->CopyContentFromID = $p->ID;
     $svp->write();
     $svp->writeToStage('Stage');
     $svp->publish('Stage', 'Live');
     $this->assertEquals($svp->SubsiteID, $subsite->ID);
     $this->assertTrue($svp->ExistsOnLive);
     // change back to original subsite ("Main site")
     Subsite::changeSubsite(0);
     // update original page
     $p->Title = 'New Title';
     // "save & publish"
     $p->writeToStage('Stage');
     $p->publish('Stage', 'Live');
     $this->assertNotEquals($p->SubsiteID, $subsite->ID);
     // reload SVP from database
     // can't use DO::get by id because caches.
     $svpdb = $svp->get()->byID($svp->ID);
     // ensure title changed
     $this->assertEquals($svpdb->Title, $p->Title);
 }