2011-03-18 04:37:22 +01:00
|
|
|
<?php
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2016-08-10 06:08:39 +02:00
|
|
|
namespace SilverStripe\CMS\Tasks;
|
|
|
|
|
2017-09-20 03:51:07 +02:00
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Dev\BuildTask;
|
2018-04-06 05:53:57 +02:00
|
|
|
use SilverStripe\Dev\Debug;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2018-04-06 05:53:57 +02:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2011-03-18 04:37:22 +01:00
|
|
|
/**
|
2018-04-06 05:53:57 +02:00
|
|
|
* Updates legacy SiteTree link tracking into new polymorphic many_many relation.
|
|
|
|
* This should be done for any site upgrading to 4.2.0
|
2011-03-18 04:37:22 +01:00
|
|
|
*/
|
2017-01-25 21:59:25 +01:00
|
|
|
class MigrateSiteTreeLinkingTask extends BuildTask
|
|
|
|
{
|
|
|
|
private static $segment = 'MigrateSiteTreeLinkingTask';
|
|
|
|
|
|
|
|
protected $title = 'Migrate SiteTree Linking Task';
|
|
|
|
|
2018-04-06 05:53:57 +02:00
|
|
|
protected $description = 'Updates legacy SiteTree link tracking into new polymorphic many_many relation';
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
public function run($request)
|
|
|
|
{
|
2018-04-06 05:53:57 +02:00
|
|
|
// Ensure legacy table exists
|
|
|
|
$exists = DB::get_conn()->getSchemaManager()->hasTable('SiteTree_LinkTracking');
|
|
|
|
if (!$exists) {
|
|
|
|
DB::alteration_message("Table SiteTree_LinkTracking has already been migrated, or doesn't exist");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
$pages = 0;
|
|
|
|
|
2018-04-06 05:53:57 +02:00
|
|
|
// Ensure sync occurs on draft
|
|
|
|
Versioned::withVersionedMode(function () use (&$pages) {
|
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
|
|
|
|
|
|
|
/** @var SiteTree[] $linkedPages */
|
|
|
|
$linkedPages = SiteTree::get()
|
|
|
|
->innerJoin(
|
|
|
|
'SiteTree_LinkTracking',
|
|
|
|
'"SiteTree_LinkTracking"."SiteTreeID" = "SiteTree"."ID"'
|
|
|
|
);
|
2017-01-25 21:59:25 +01:00
|
|
|
foreach ($linkedPages as $page) {
|
2018-04-06 05:53:57 +02:00
|
|
|
// Command page to update symlink tracking
|
|
|
|
$page->syncLinkTracking();
|
2017-09-20 03:51:07 +02:00
|
|
|
$pages++;
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
2018-04-06 05:53:57 +02:00
|
|
|
});
|
|
|
|
DB::alteration_message("Migrated page links on " . SiteTree::singleton()->i18n_pluralise($pages));
|
2017-01-25 21:59:25 +01:00
|
|
|
|
2018-04-06 05:53:57 +02:00
|
|
|
// Disable table to prevent double-migration
|
|
|
|
DB::dont_require_table('SiteTree_LinkTracking');
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
2012-04-12 09:23:20 +02:00
|
|
|
}
|