diff --git a/_config.php b/_config.php
index 42038793..7a7f03db 100644
--- a/_config.php
+++ b/_config.php
@@ -58,4 +58,6 @@ else SS_Report::register('ReportAdmin', 'BrokenLinksReport',-20);
/**
* Register the default internal shortcodes.
*/
-ShortcodeParser::get('default')->register('sitetree_link', array('SiteTree', 'link_shortcode_handler'));
\ No newline at end of file
+ShortcodeParser::get('default')->register('sitetree_link', array('SiteTree', 'link_shortcode_handler'));
+
+Object::add_extension('File', 'SiteTreeFileDecorator');
\ No newline at end of file
diff --git a/code/SiteTreeFileDecorator.php b/code/SiteTreeFileDecorator.php
new file mode 100644
index 00000000..3df79ef1
--- /dev/null
+++ b/code/SiteTreeFileDecorator.php
@@ -0,0 +1,71 @@
+ array(
+ "BackLinkTracking" => "SiteTree",
+ )
+ );
+ }
+
+ /**
+ * @todo Unnecessary shortcut for AssetTableField, coupled with cms module.
+ *
+ * @return Integer
+ */
+ function BackLinkTrackingCount() {
+ $pages = $this->owner->BackLinkTracking();
+ if($pages) {
+ return $pages->Count();
+ } else {
+ return 0;
+ }
+ }
+
+ /**
+ * Updates link tracking.
+ */
+ function onAfterDelete() {
+ $brokenPages = $this->owner->BackLinkTracking();
+ if($brokenPages) {
+ $origStage = Versioned::current_stage();
+
+ // This will syncLinkTracking on draft
+ Versioned::reading_stage('Stage');
+ foreach($brokenPages as $brokenPage) $brokenPage->write();
+
+ // This will syncLinkTracking on published
+ Versioned::reading_stage('Live');
+ foreach($brokenPages as $brokenPage) $brokenPage->write();
+
+ Versioned::reading_stage($origStage);
+ }
+ }
+
+ /**
+ * Rewrite links to the $old file to now point to the $new file.
+ *
+ * @uses SiteTree->rewriteFileURL()
+ *
+ * @param String $old File path relative to the webroot
+ * @param String $new File path relative to the webroot
+ */
+ function updateLinks($old, $new) {
+ if(class_exists('Subsite')) Subsite::disable_subsite_filter(true);
+
+ $pages = $this->owner->BackLinkTracking();
+
+ $summary = "";
+ if($pages) {
+ foreach($pages as $page) $page->rewriteFileURL($old,$new);
+ }
+
+ if(class_exists('Subsite')) Subsite::disable_subsite_filter(false);
+ }
+
+}
\ No newline at end of file
diff --git a/tests/FileLinkTrackingTest.php b/tests/FileLinkTrackingTest.php
new file mode 100644
index 00000000..b847b786
--- /dev/null
+++ b/tests/FileLinkTrackingTest.php
@@ -0,0 +1,112 @@
+logInWithPermission('ADMIN');
+
+ $fh = fopen(Director::baseFolder() . '/assets/testscript-test-file.pdf', "w");
+ fwrite($fh, str_repeat('x',1000000));
+ fclose($fh);
+ }
+ function tearDown() {
+ parent::tearDown();
+ $testFiles = array(
+ '/assets/testscript-test-file.pdf',
+ '/assets/renamed-test-file.pdf',
+ '/assets/renamed-test-file-second-time.pdf',
+ );
+ foreach($testFiles as $file) {
+ if(file_exists(Director::baseFolder().$file)) unlink(Director::baseFolder().$file);
+ }
+ }
+
+ function testFileRenameUpdatesDraftAndPublishedPages() {
+ $page = $this->objFromFixture('Page', 'page1');
+ $this->assertTrue($page->doPublish());
+ $this->assertContains('ID")->value());
+
+ $file = $this->objFromFixture('File', 'file1');
+ $file->Name = 'renamed-test-file.pdf';
+ $file->write();
+
+ $this->assertContains('
ID")->value());
+ $this->assertContains('
ID")->value());
+ }
+
+ function testFileLinkRewritingOnVirtualPages() {
+ // Publish the source page
+ $page = $this->objFromFixture('Page', 'page1');
+ $this->assertTrue($page->doPublish());
+
+ // Create a virtual page from it, and publish that
+ $svp = new VirtualPage();
+ $svp->CopyContentFromID = $page->ID;
+ $svp->write();
+ $svp->doPublish();
+
+ // Rename the file
+ $file = $this->objFromFixture('File', 'file1');
+ $file->Name = 'renamed-test-file.pdf';
+ $file->write();
+
+ // Verify that the draft and publish virtual pages both have the corrected link
+ $this->assertContains('
ID")->value());
+ $this->assertContains('
ID")->value());
+ }
+
+ function testLinkRewritingOnAPublishedPageDoesntMakeItEditedOnDraft() {
+ // Publish the source page
+ $page = $this->objFromFixture('Page', 'page1');
+ $this->assertTrue($page->doPublish());
+ $this->assertFalse($page->IsModifiedOnStage);
+
+ // Rename the file
+ $file = $this->objFromFixture('File', 'file1');
+ $file->Name = 'renamed-test-file.pdf';
+ $file->write();
+
+ // Caching hack
+ Versioned::prepopulate_versionnumber_cache('SiteTree', 'Stage', array($page->ID));
+ Versioned::prepopulate_versionnumber_cache('SiteTree', 'Live', array($page->ID));
+
+ // Confirm that the page hasn't gone green.
+ $this->assertFalse($page->IsModifiedOnStage);
+ }
+
+ function testTwoFileRenamesInARowWork() {
+ $page = $this->objFromFixture('Page', 'page1');
+ $this->assertTrue($page->doPublish());
+ $this->assertContains('
ID")->value());
+
+ // Rename the file twice
+ $file = $this->objFromFixture('File', 'file1');
+ $file->Name = 'renamed-test-file.pdf';
+ $file->write();
+
+ // TODO Workaround for bug in DataObject->getChangedFields(), which returns stale data,
+ // and influences File->updateFilesystem()
+ $file = DataObject::get_by_id('File', $file->ID);
+ $file->Name = 'renamed-test-file-second-time.pdf';
+ $file->write();
+
+ // Confirm that the correct image is shown in both the draft and live site
+ $this->assertContains('
ID")->value());
+ $this->assertContains('
ID")->value());
+ }
+}
+
+?>
diff --git a/tests/FileLinkTrackingTest.yml b/tests/FileLinkTrackingTest.yml
new file mode 100644
index 00000000..45906aea
--- /dev/null
+++ b/tests/FileLinkTrackingTest.yml
@@ -0,0 +1,10 @@
+# These need to come first so that SiteTree has the link meta-data written.
+File:
+ file1:
+ Filename: assets/testscript-test-file.pdf
+
+Page:
+ page1:
+ Title: page1
+ URLSegment: page1
+ Content: