2011-03-18 04:37:22 +01:00
|
|
|
<?php
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2016-08-10 06:08:39 +02:00
|
|
|
namespace SilverStripe\CMS\Tasks;
|
|
|
|
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Dev\BuildTask;
|
2016-06-16 06:57:19 +02:00
|
|
|
use SilverStripe\ORM\DataList;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2011-03-18 04:37:22 +01:00
|
|
|
/**
|
|
|
|
* Rewrites plain internal HTML links into shortcode form, using existing link tracking information.
|
|
|
|
*/
|
|
|
|
class MigrateSiteTreeLinkingTask extends BuildTask {
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2016-08-23 04:36:06 +02:00
|
|
|
private static $segment = 'MigrateSiteTreeLinkingTask';
|
|
|
|
|
2011-03-18 04:37:22 +01:00
|
|
|
protected $title = 'Migrate SiteTree Linking Task';
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:37:22 +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
|
|
|
|
2011-03-18 04:37:22 +01:00
|
|
|
public function run($request) {
|
|
|
|
$pages = 0;
|
|
|
|
$links = 0;
|
2011-10-28 02:34:55 +02:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$linkedPages = new DataList('SilverStripe\\CMS\\Model\\SiteTree');
|
2012-12-12 05:35:46 +01:00
|
|
|
$linkedPages = $linkedPages->innerJoin('SiteTree_LinkTracking', '"SiteTree_LinkTracking"."SiteTreeID" = "SiteTree"."ID"');
|
2011-03-18 04:37:22 +01:00
|
|
|
if($linkedPages) foreach($linkedPages as $page) {
|
2013-06-21 00:45:33 +02:00
|
|
|
$tracking = DB::prepared_query(
|
|
|
|
'SELECT "ChildID", "FieldName" FROM "SiteTree_LinkTracking" WHERE "SiteTreeID" = ?',
|
|
|
|
array($page->ID)
|
|
|
|
)->map();
|
2011-03-18 04:37:22 +01:00
|
|
|
|
|
|
|
foreach($tracking as $childID => $fieldName) {
|
2016-07-22 01:32:32 +02:00
|
|
|
$linked = DataObject::get_by_id('SilverStripe\\CMS\\Model\\SiteTree', $childID);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:37:22 +01:00
|
|
|
// TOOD: Replace in all HTMLText fields
|
|
|
|
$page->Content = preg_replace (
|
|
|
|
"/href *= *([\"']?){$linked->URLSegment}\/?/i",
|
2012-07-12 04:51:19 +02:00
|
|
|
"href=$1[sitetree_link,id={$linked->ID}]",
|
2011-03-18 04:37:22 +01:00
|
|
|
$page->Content,
|
|
|
|
-1,
|
|
|
|
$replaced
|
|
|
|
);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:37:22 +01:00
|
|
|
if($replaced) {
|
|
|
|
$links += $replaced;
|
|
|
|
}
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:37:22 +01:00
|
|
|
$page->write();
|
|
|
|
$pages++;
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:37:22 +01:00
|
|
|
echo "Rewrote $links link(s) on $pages page(s) to use shortcodes.\n";
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-04-12 09:23:20 +02:00
|
|
|
}
|