Source of file MigrateSiteTreeOpenGraphTask.php
Size: 2,721 Bytes - Last Modified: 2021-12-23T10:52:50+00:00
/var/www/docs.ssmods.com/process/src/code/tasks/MigrateSiteTreeOpenGraphTask.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 | <?php /** * Class MigrateSiteTreeOpenGraphTask */ class MigrateSiteTreeOpenGraphTask extends BuildTask { protected $title = 'Migrate Open Graph metadata from Page to SiteTree'; protected $description = 'For versions < 0.1.0: Migrates existing data from the Page table to the SiteTree table. Pass the argument/GET var "overwrite=1" to replace existing data.'; protected $eol; protected $hr; protected $fields_to_update = array( 'OGTitle', 'OGContent', 'OGDescription', 'OGUrl', 'OGImageID' ); public function __construct() { if (!Director::is_cli()) { $this->eol = '<br>'; $this->hr = '<hr>'; } else { $this->eol = PHP_EOL; $this->hr = '--' . PHP_EOL; } parent::__construct(); } /** * @param SS_HTTPRequest $request */ public function run($request) { /** ========================================= * @var Page $page ===========================================*/ if (class_exists('Page')) { if (Page::has_extension('OpenGraphMeta')) { // Should we overwrite? $overwrite = $request->getVar('overwrite') ? true : false; echo sprintf('Overwrite is %s', $overwrite ? 'enabled' : 'disabled') . $this->eol . $this->eol; $pages = Page::get(); foreach ($pages as $page) { $id = $page->ID; echo $this->hr; echo 'Updating page: ' . $page->Title . $this->eol; foreach ($this->fields_to_update as $fieldName) { $oldData = DB::query("SELECT {$fieldName} FROM Page WHERE ID = {$id}")->column($fieldName); $newData = DB::query("SELECT {$fieldName} FROM SiteTree WHERE ID = {$id}")->column($fieldName); if (!empty($oldData)) { // If new data has been saved and we don't want to overwrite, exit the loop if (!empty($newData) && !$overwrite) { continue; } $data = Convert::raw2sql($oldData[0]); DB::query("UPDATE SiteTree SET {$fieldName} = '{$data}' WHERE ID = {$id}"); DB::query("UPDATE SiteTree_Live SET {$fieldName} = '{$data}' WHERE ID = {$id}"); } else { echo 'Field "' . $fieldName . '" empty.' . $this->eol; } } } } } } } |