mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
8b65a55b55
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@91418 467b73ca-7a2a-4603-9d3b-597d59a354a9
59 lines
1.5 KiB
PHP
Executable File
59 lines
1.5 KiB
PHP
Executable File
<?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"'
|
|
);
|
|
|
|
// Databases like MSSQL will give duplicate results - remove them
|
|
// This would normally be fixed by using SELECT DISTINCT, but DataObject::get() doesn't support it
|
|
$linkedPages->removeDuplicates();
|
|
|
|
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']);
|
|
|
|
// TOOD: Replace in all HTMLText fields
|
|
$page->Content = preg_replace (
|
|
"/href *= *([\"']?){$linked->URLSegment}\/?/i",
|
|
"href=$1[sitetree_link id={$linked->ID}]",
|
|
$page->Content,
|
|
-1,
|
|
$replaced
|
|
);
|
|
|
|
if($replaced) {
|
|
$page->write();
|
|
$links += $replaced;
|
|
}
|
|
}
|
|
|
|
$pages++;
|
|
}
|
|
|
|
echo "Rewrote $links link(s) on $pages page(s) to use shortcodes.\n";
|
|
}
|
|
|
|
} |