mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
FEATURE: Added MigrateSiteTreeLinkingTask to allow plain HTML links to be migrated into shortcode links.
From: Andrew Short <andrewjshort@gmail.com> git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@88992 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
1ccdcbac5d
commit
86c81bc6b4
54
tasks/MigrateSiteTreeLinkingTask.php
Executable file
54
tasks/MigrateSiteTreeLinkingTask.php
Executable file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
/**
|
||||
* Rewrites plain internal HTML links into shortcode form, using existing link tracking information.
|
||||
*
|
||||
* @package sapphire
|
||||
* @subpackage tasks
|
||||
*/
|
||||
class MigrateSiteTreeLinkingTask extends BuildTask {
|
||||
|
||||
protected $title = 'Migrate SiteTree Linking Task';
|
||||
|
||||
protected $description = 'Rewrites plain internal HTML links into shortcode form, using existing link tracking information.';
|
||||
|
||||
public function run($request) {
|
||||
$pages = 0;
|
||||
$links = 0;
|
||||
|
||||
$linkedPages = DataObject::get (
|
||||
'SiteTree',
|
||||
null,
|
||||
null,
|
||||
'INNER JOIN "SiteTree_LinkTracking" ON "SiteTree_LinkTracking"."SiteTreeID" = "SiteTree"."ID"'
|
||||
);
|
||||
|
||||
if($linkedPages) foreach($linkedPages as $page) {
|
||||
$tracking = DB::query(sprintf (
|
||||
'SELECT "ChildID", "FieldName" FROM "SiteTree_LinkTracking" WHERE "SiteTreeID" = %d',
|
||||
$page->ID
|
||||
));
|
||||
|
||||
foreach($tracking as $link) {
|
||||
$linked = DataObject::get_by_id('SiteTree', $link['ChildID']);
|
||||
|
||||
$page->{$link['FieldName']} = preg_replace (
|
||||
"/href *= *([\"']?){$linked->URLSegment}\/?/i",
|
||||
"href=$1[sitetree_link id={$linked->ID}]",
|
||||
$page->{$link['FieldName']},
|
||||
-1,
|
||||
$replaced
|
||||
);
|
||||
|
||||
if($replaced) {
|
||||
$page->write();
|
||||
$links += $replaced;
|
||||
}
|
||||
}
|
||||
|
||||
$pages++;
|
||||
}
|
||||
|
||||
echo "Rewrote $links link(s) on $pages page(s) to use shortcodes.\n";
|
||||
}
|
||||
|
||||
}
|
80
tests/tasks/MigrateSiteTreeLinkingTaskTest.php
Executable file
80
tests/tasks/MigrateSiteTreeLinkingTaskTest.php
Executable file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* @package sapphire
|
||||
* @subpackage tests
|
||||
*/
|
||||
class MigrateSiteTreeLinkingTaskTest extends SapphireTest {
|
||||
|
||||
public static $fixture_file = 'sapphire/tests/tasks/MigrateSiteTreeLinkingTaskTest.yml';
|
||||
|
||||
public static $use_draft_site = true;
|
||||
|
||||
public function testLinkingMigration() {
|
||||
ob_start();
|
||||
|
||||
$task = new MigrateSiteTreeLinkingTask();
|
||||
$task->run(null);
|
||||
|
||||
$this->assertEquals (
|
||||
"Rewrote 9 link(s) on 5 page(s) to use shortcodes.\n",
|
||||
ob_get_contents(),
|
||||
'Rewritten links are correctly reported'
|
||||
);
|
||||
ob_end_clean();
|
||||
|
||||
$homeID = $this->idFromFixture('SiteTree', 'home');
|
||||
$aboutID = $this->idFromFixture('SiteTree', 'about');
|
||||
$staffID = $this->idFromFixture('SiteTree', 'staff');
|
||||
$actionID = $this->idFromFixture('SiteTree', 'action');
|
||||
$hashID = $this->idFromFixture('SiteTree', 'hash_link');
|
||||
|
||||
$homeContent = sprintf (
|
||||
'<a href="[sitetree_link id=%d]">About</a><a href="[sitetree_link id=%d]">Staff</a><a href="http://silverstripe.org/">External Link</a>',
|
||||
$aboutID,
|
||||
$staffID
|
||||
);
|
||||
$aboutContent = sprintf (
|
||||
'<a href="[sitetree_link id=%d]">Home</a><a href="[sitetree_link id=%d]">Staff</a>',
|
||||
$homeID,
|
||||
$staffID
|
||||
);
|
||||
$staffContent = sprintf (
|
||||
'<a href="[sitetree_link id=%d]">Home</a><a href="[sitetree_link id=%d]">About</a>',
|
||||
$homeID,
|
||||
$aboutID
|
||||
);
|
||||
$actionContent = sprintf (
|
||||
'<a href="[sitetree_link id=%d]SearchForm">Search Form</a>', $homeID
|
||||
);
|
||||
$hashLinkContent = sprintf (
|
||||
'<a href="[sitetree_link id=%d]#anchor">Home</a><a href="[sitetree_link id=%d]#second-anchor">About</a>',
|
||||
$homeID,
|
||||
$aboutID
|
||||
);
|
||||
|
||||
$this->assertEquals (
|
||||
$homeContent,
|
||||
DataObject::get_by_id('SiteTree', $homeID)->Content,
|
||||
'HTML URLSegment links are rewritten.'
|
||||
);
|
||||
$this->assertEquals (
|
||||
$aboutContent,
|
||||
DataObject::get_by_id('SiteTree', $aboutID)->Content
|
||||
);
|
||||
$this->assertEquals (
|
||||
$staffContent,
|
||||
DataObject::get_by_id('SiteTree', $staffID)->Content
|
||||
);
|
||||
$this->assertEquals (
|
||||
$actionContent,
|
||||
DataObject::get_by_id('SiteTree', $actionID)->Content,
|
||||
'Links to actions on pages are rewritten correctly.'
|
||||
);
|
||||
$this->assertEquals (
|
||||
$hashLinkContent,
|
||||
DataObject::get_by_id('SiteTree', $hashID)->Content,
|
||||
'Hash/anchor links are correctly handled.'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
67
tests/tasks/MigrateSiteTreeLinkingTaskTest.yml
Executable file
67
tests/tasks/MigrateSiteTreeLinkingTaskTest.yml
Executable file
@ -0,0 +1,67 @@
|
||||
SiteTree:
|
||||
home:
|
||||
Title: Home Page
|
||||
URLSegment: home
|
||||
Content: '<a href="about/">About</a><a href="staff">Staff</a><a href="http://silverstripe.org/">External Link</a>'
|
||||
about:
|
||||
Title: About Us
|
||||
URLSegment: about
|
||||
Content: <a href="home">Home</a><a href="staff/">Staff</a>
|
||||
staff:
|
||||
Title: Staff
|
||||
URLSegment: staff
|
||||
Content: <a href="home/">Home</a><a href="about">About</a>
|
||||
Parent: =>SiteTree.about
|
||||
action:
|
||||
Title: Action Link
|
||||
URLSegment: action
|
||||
Content: <a href="home/SearchForm">Search Form</a>
|
||||
hash_link:
|
||||
Title: Hash Link
|
||||
URLSegment: hash-link
|
||||
Content: '<a href="home/#anchor">Home</a><a href="about/#second-anchor">About</a>'
|
||||
admin_link:
|
||||
Title: Admin Link
|
||||
URLSegment: admin-link
|
||||
Content: <a href="admin">Admin</a>
|
||||
no_links:
|
||||
Title: No Links
|
||||
URLSemgment: No Links
|
||||
|
||||
SiteTree_LinkTracking:
|
||||
home_about:
|
||||
SiteTreeID: =>SiteTree.home
|
||||
ChildID: =>SiteTree.about
|
||||
FieldName: Content
|
||||
home_staff:
|
||||
SiteTreeID: =>SiteTree.home
|
||||
ChildID: =>SiteTree.staff
|
||||
FieldName: Content
|
||||
about_home:
|
||||
SiteTreeID: =>SiteTree.about
|
||||
ChildID: =>SiteTree.home
|
||||
FieldName: Content
|
||||
about_staff:
|
||||
SiteTreeID: =>SiteTree.about
|
||||
ChildID: =>SiteTree.staff
|
||||
FieldName: Content
|
||||
staff_home:
|
||||
SiteTreeID: =>SiteTree.staff
|
||||
ChildID: =>SiteTree.home
|
||||
FieldName: Content
|
||||
staff_about:
|
||||
SiteTreeID: =>SiteTree.staff
|
||||
ChildID: =>SiteTree.about
|
||||
FieldName: Content
|
||||
action_home:
|
||||
SiteTreeID: =>SiteTree.action
|
||||
ChildID: =>SiteTree.home
|
||||
FieldName: Content
|
||||
hash_link_home:
|
||||
SiteTreeID: =>SiteTree.hash_link
|
||||
ChildID: =>SiteTree.home
|
||||
FieldName: Content
|
||||
hash_link_about:
|
||||
SiteTreeID: =>SiteTree.hash_link
|
||||
ChildID: =>SiteTree.about
|
||||
FieldName: Content
|
Loading…
Reference in New Issue
Block a user