mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
BUGFIX: Fixed image link rewriting and added a test. (from r89011)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@89208 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
e23023b8a9
commit
aaeaf6ccb2
@ -1469,6 +1469,54 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
|
||||
return self::get_by_link($link);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace a URL in html content with a new URL.
|
||||
* @param string $old The old URL
|
||||
* @param string $new The new URL
|
||||
*/
|
||||
function rewriteLink($old, $new) {
|
||||
$fields = $this->getCMSFields(null)->dataFields();
|
||||
foreach($fields as $field) {
|
||||
if(is_a($field, 'HtmlEditorField')) {
|
||||
$fieldName = $field->Name();
|
||||
$field->setValue($this->$fieldName);
|
||||
$field->rewriteLink($old, $new);
|
||||
$field->saveInto($this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewrite a file URL on this page, after its been renamed.
|
||||
* Triggers the onRenameLinkedAsset action on extensions.
|
||||
*/
|
||||
function rewriteFileURL($old, $new) {
|
||||
Debug::message("Rewriting $old to $new");
|
||||
|
||||
// TODO: Check all HTMLText fields
|
||||
$fieldName = "Content";
|
||||
if($fieldName) {
|
||||
$original = clone $this;
|
||||
|
||||
// Draft site
|
||||
$this->$fieldName = str_replace($old, $new, $this->$fieldName, $numReplaced);
|
||||
if($numReplaced) $this->write();
|
||||
|
||||
// Published site
|
||||
$published = DB::query("SELECT * FROM \"SiteTree_Live\" WHERE ID = $this->ID")->record();
|
||||
$origPublished = $published;
|
||||
$published[$fieldName] = str_replace($old, $new, $published[$fieldName], $numReplaced);
|
||||
if($numReplaced) {
|
||||
DB::query("UPDATE \"SiteTree_Live\" SET \"$fieldName\" = '"
|
||||
. Convert::raw2sql($published[$fieldName]) . "' WHERE ID = $this->ID");
|
||||
|
||||
$publishedClass = $origPublished['ClassName'];
|
||||
$origPublishedObj = new $publishedClass($origPublished);
|
||||
$this->extend('onRenameLinkedAsset', $origPublishedObj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a FieldSet with which to create the CMS editing form.
|
||||
*
|
||||
@ -1876,6 +1924,8 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
|
||||
|
||||
// Handle activities undertaken by decorators
|
||||
$this->extend('onAfterPublish', $original);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -380,15 +380,9 @@ class File extends DataObject {
|
||||
|
||||
$pages = $this->BackLinkTracking();
|
||||
|
||||
$summary = "";
|
||||
if($pages) {
|
||||
foreach($pages as $page) {
|
||||
$fieldName = $page->FieldName; // extracted from the many-many join
|
||||
if($fieldName) {
|
||||
$text = $page->$fieldName;
|
||||
$page->$fieldName = str_replace($old, $new, $page->$fieldName);
|
||||
$page->write();
|
||||
}
|
||||
}
|
||||
foreach($pages as $page) $page->rewriteFileURL($old,$new);
|
||||
}
|
||||
|
||||
if(class_exists('Subsite')) Subsite::disable_subsite_filter(false);
|
||||
|
40
tests/FileLinkTrackingTest.php
Normal file
40
tests/FileLinkTrackingTest.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Tests link tracking to files and images.
|
||||
*/
|
||||
class FileLinkTrackingTest extends SapphireTest {
|
||||
static $fixture_file = "sapphire/tests/FileLinkTrackingTest.yml";
|
||||
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
$this->logInWithPermssion('ADMIN');
|
||||
touch(Director::baseFolder() . '/assets/testscript-test-file.pdf');
|
||||
}
|
||||
function tearDown() {
|
||||
parent::tearDown();
|
||||
$testFiles = array(
|
||||
'/assets/testscript-test-file.pdf',
|
||||
'/assets/renamed-test-file.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('<img src="assets/testscript-test-file.pdf" />',
|
||||
DB::query("SELECT \"Content\" FROM \"SiteTree_Live\" WHERE \"ID\" = $page->ID")->value());
|
||||
|
||||
$file = $this->objFromFixture('File', 'file1');
|
||||
$file->Name = 'renamed-test-file.pdf';
|
||||
|
||||
$this->assertContains('<img src="assets/renamed-test-file.pdf" />',
|
||||
DB::query("SELECT \"Content\" FROM \"SiteTree\" WHERE \"ID\" = $page->ID")->value());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
10
tests/FileLinkTrackingTest.yml
Normal file
10
tests/FileLinkTrackingTest.yml
Normal file
@ -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: <p><img src="assets/testscript-test-file.pdf" /></p>
|
Loading…
x
Reference in New Issue
Block a user