2009-10-15 02:02:06 +02:00
|
|
|
<?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;
|
|
|
|
|
2009-11-12 22:46:14 +01:00
|
|
|
$linkedPages = DataObject::get(
|
2009-10-15 02:02:06 +02:00
|
|
|
'SiteTree',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
'INNER JOIN "SiteTree_LinkTracking" ON "SiteTree_LinkTracking"."SiteTreeID" = "SiteTree"."ID"'
|
|
|
|
);
|
|
|
|
|
2009-11-12 22:46:14 +01:00
|
|
|
// 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
|
2010-10-15 02:30:10 +02:00
|
|
|
if($linkedPages) $linkedPages->removeDuplicates();
|
2009-11-12 22:46:14 +01:00
|
|
|
|
2009-10-15 02:02:06 +02:00
|
|
|
if($linkedPages) foreach($linkedPages as $page) {
|
2010-10-19 03:28:13 +02:00
|
|
|
$tracking = DB::query(sprintf('SELECT "ChildID", "FieldName" FROM "SiteTree_LinkTracking" WHERE "SiteTreeID" = %d', $page->ID))->map();
|
|
|
|
|
|
|
|
foreach($tracking as $childID => $fieldName) {
|
|
|
|
$linked = DataObject::get_by_id('SiteTree', $childID);
|
2009-10-15 02:02:06 +02:00
|
|
|
|
2009-10-19 07:29:46 +02:00
|
|
|
// TOOD: Replace in all HTMLText fields
|
|
|
|
$page->Content = preg_replace (
|
2009-10-15 02:02:06 +02:00
|
|
|
"/href *= *([\"']?){$linked->URLSegment}\/?/i",
|
|
|
|
"href=$1[sitetree_link id={$linked->ID}]",
|
2009-10-19 07:29:46 +02:00
|
|
|
$page->Content,
|
2009-10-15 02:02:06 +02:00
|
|
|
-1,
|
|
|
|
$replaced
|
|
|
|
);
|
|
|
|
|
|
|
|
if($replaced) {
|
|
|
|
$links += $replaced;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-19 03:28:13 +02:00
|
|
|
$page->write();
|
2009-10-15 02:02:06 +02:00
|
|
|
$pages++;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "Rewrote $links link(s) on $pages page(s) to use shortcodes.\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|