Created
October 14, 2009 06:28
-
-
Save ajshort/209835 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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 6 link(s) on 3 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'); | |
| $homeContent = sprintf ( | |
| '<a href="[sitetree_link id=%d]">About</a><a href="[sitetree_link id=%d]">Staff</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 | |
| ); | |
| $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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SiteTree: | |
| home: | |
| Title: Home Page | |
| URLSegment: home | |
| Content: <a href="about/">About</a><a href="staff">Staff</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 | |
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment