silverstripe-cms/code/Tasks/MigrateSiteTreeLinkingTask.php

56 lines
1.4 KiB
PHP
Raw Normal View History

<?php
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\DataObject;
/**
* Rewrites plain internal HTML links into shortcode form, using existing link tracking information.
*
* @package cms
* @subpackage tasks
*/
class MigrateSiteTreeLinkingTask extends BuildTask {
2016-03-08 21:50:55 +01:00
protected $title = 'Migrate SiteTree Linking Task';
2016-03-08 21:50:55 +01:00
protected $description = 'Rewrites plain internal HTML links into shortcode form, using existing link tracking information.';
2016-03-08 21:50:55 +01:00
public function run($request) {
$pages = 0;
$links = 0;
$linkedPages = new DataList('SiteTree');
$linkedPages = $linkedPages->innerJoin('SiteTree_LinkTracking', '"SiteTree_LinkTracking"."SiteTreeID" = "SiteTree"."ID"');
if($linkedPages) foreach($linkedPages as $page) {
$tracking = DB::prepared_query(
'SELECT "ChildID", "FieldName" FROM "SiteTree_LinkTracking" WHERE "SiteTreeID" = ?',
array($page->ID)
)->map();
foreach($tracking as $childID => $fieldName) {
$linked = DataObject::get_by_id('SiteTree', $childID);
2016-03-08 21:50:55 +01:00
// 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
);
2016-03-08 21:50:55 +01:00
if($replaced) {
$links += $replaced;
}
}
2016-03-08 21:50:55 +01:00
$page->write();
$pages++;
}
2016-03-08 21:50:55 +01:00
echo "Rewrote $links link(s) on $pages page(s) to use shortcodes.\n";
}
2016-03-08 21:50:55 +01:00
}