Merge pull request #2362 from open-sausages/pulls/4/dont-assume-sitetree-table-name

MINOR Do not assume SiteTree's table will always be call SiteTree.
This commit is contained in:
Robbie Averill 2019-05-17 14:04:14 +12:00 committed by GitHub
commit 60b2947519
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 64 additions and 29 deletions

View File

@ -1854,8 +1854,9 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
// DataObject::fieldExists only checks the current class, not the hierarchy // DataObject::fieldExists only checks the current class, not the hierarchy
// This allows the CMS to set the correct sort value // This allows the CMS to set the correct sort value
if ($newItem->castingHelper('Sort')) { if ($newItem->castingHelper('Sort')) {
$table = DataObject::singleton(SiteTree::class)->baseTable();
$maxSort = DB::prepared_query( $maxSort = DB::prepared_query(
'SELECT MAX("Sort") FROM "SiteTree" WHERE "ParentID" = ?', "SELECT MAX(\"Sort\") FROM \"$table\" WHERE \"ParentID\" = ?",
array($parentID) array($parentID)
)->value(); )->value();
$newItem->Sort = (int)$maxSort + 1; $newItem->Sort = (int)$maxSort + 1;
@ -1911,8 +1912,10 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
} }
/** @var SiteTree $record */ /** @var SiteTree $record */
$table = DataObject::singleton(SiteTree::class)->baseTable();
$liveTable = DataObject::singleton(SiteTree::class)->stageTable($table, Versioned::LIVE);
$record = Versioned::get_one_by_stage(SiteTree::class, Versioned::LIVE, array( $record = Versioned::get_one_by_stage(SiteTree::class, Versioned::LIVE, array(
'"SiteTree_Live"."ID"' => $id "\"$liveTable\".\"ID\"" => $id
)); ));
// a user can restore a page without publication rights, as it just adds a new draft state // a user can restore a page without publication rights, as it just adds a new draft state

View File

@ -3,6 +3,7 @@
namespace SilverStripe\CMS\Controllers; namespace SilverStripe\CMS\Controllers;
use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned; use SilverStripe\Versioned\Versioned;
/** /**
@ -18,10 +19,12 @@ class CMSSiteTreeFilter_ChangedPages extends CMSSiteTreeFilter
public function getFilteredPages() public function getFilteredPages()
{ {
$table = DataObject::singleton(SiteTree::class)->baseTable();
$liveTable = DataObject::singleton(SiteTree::class)->stageTable($table, Versioned::LIVE);
$pages = Versioned::get_by_stage(SiteTree::class, Versioned::DRAFT); $pages = Versioned::get_by_stage(SiteTree::class, Versioned::DRAFT);
$pages = $this->applyDefaultFilters($pages) $pages = $this->applyDefaultFilters($pages)
->leftJoin('SiteTree_Live', '"SiteTree_Live"."ID" = "SiteTree"."ID"') ->leftJoin($liveTable, "\"$liveTable\".\"ID\" = \"$table\".\"ID\"")
->where('"SiteTree"."Version" <> "SiteTree_Live"."Version"'); ->where("\"$table\".\"Version\" <> \"$liveTable\".\"Version\"");
return $pages; return $pages;
} }
} }

View File

@ -135,9 +135,10 @@ class ModelAsController extends Controller implements NestedController
} }
// Select child page // Select child page
$conditions = array('"SiteTree"."URLSegment"' => $URLSegment); $tableName = DataObject::singleton(SiteTree::class)->baseTable();
$conditions = [sprintf('"%s"."URLSegment"', $tableName) => $URLSegment];
if (SiteTree::config()->get('nested_urls')) { if (SiteTree::config()->get('nested_urls')) {
$conditions[] = array('"SiteTree"."ParentID"' => 0); $conditions[] = [sprintf('"%s"."ParentID"', $tableName) => 0];
} }
/** @var SiteTree $sitetree */ /** @var SiteTree $sitetree */
$sitetree = DataObject::get_one(SiteTree::class, $conditions); $sitetree = DataObject::get_one(SiteTree::class, $conditions);

View File

@ -395,6 +395,11 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
*/ */
public static function get_by_link($link, $cache = true) public static function get_by_link($link, $cache = true)
{ {
// Compute the column names with dynamic a dynamic table name
$tableName = DataObject::singleton(self::class)->baseTable();
$urlSegmentExpr = sprintf('"%s"."URLSegment"', $tableName);
$parentIDExpr = sprintf('"%s"."ParentID"', $tableName);
if (trim($link, '/')) { if (trim($link, '/')) {
$link = trim(Director::makeRelative($link), '/'); $link = trim(Director::makeRelative($link), '/');
} else { } else {
@ -405,9 +410,9 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
// Grab the initial root level page to traverse down from. // Grab the initial root level page to traverse down from.
$URLSegment = array_shift($parts); $URLSegment = array_shift($parts);
$conditions = array('"SiteTree"."URLSegment"' => rawurlencode($URLSegment)); $conditions = array($urlSegmentExpr => rawurlencode($URLSegment));
if (self::config()->get('nested_urls')) { if (self::config()->get('nested_urls')) {
$conditions[] = array('"SiteTree"."ParentID"' => 0); $conditions[] = array($parentIDExpr => 0);
} }
/** @var SiteTree $sitetree */ /** @var SiteTree $sitetree */
$sitetree = DataObject::get_one(self::class, $conditions, $cache); $sitetree = DataObject::get_one(self::class, $conditions, $cache);
@ -416,7 +421,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
if (!$sitetree if (!$sitetree
&& self::config()->get('nested_urls') && self::config()->get('nested_urls')
&& $sitetree = DataObject::get_one(self::class, array( && $sitetree = DataObject::get_one(self::class, array(
'"SiteTree"."URLSegment"' => $URLSegment $urlSegmentExpr => $URLSegment
), $cache) ), $cache)
) { ) {
return $sitetree; return $sitetree;
@ -449,8 +454,8 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
$next = DataObject::get_one( $next = DataObject::get_one(
self::class, self::class,
array( array(
'"SiteTree"."URLSegment"' => $segment, $urlSegmentExpr => $segment,
'"SiteTree"."ParentID"' => $sitetree->ID $parentIDExpr => $sitetree->ID
), ),
$cache $cache
); );
@ -659,9 +664,10 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
{ {
$oldReadingMode = Versioned::get_reading_mode(); $oldReadingMode = Versioned::get_reading_mode();
Versioned::set_stage(Versioned::LIVE); Versioned::set_stage(Versioned::LIVE);
$tablename = $this->baseTable();
/** @var SiteTree $live */ /** @var SiteTree $live */
$live = Versioned::get_one_by_stage(self::class, Versioned::LIVE, array( $live = Versioned::get_one_by_stage(self::class, Versioned::LIVE, array(
'"SiteTree"."ID"' => $this->ID "\"$tablename\".\"ID\"" => $this->ID
)); ));
if ($live) { if ($live) {
$link = $live->AbsoluteLink(); $link = $live->AbsoluteLink();
@ -1500,7 +1506,8 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
DB::alteration_message('Home page created', 'created'); DB::alteration_message('Home page created', 'created');
} }
if (DB::query("SELECT COUNT(*) FROM \"SiteTree\"")->value() == 1) { $tablename = $this->baseTable();
if (DB::query("SELECT COUNT(*) FROM \"$tablename\"")->value() == 1) {
$aboutus = new Page(); $aboutus = new Page();
$aboutus->Title = _t(__CLASS__.'.DEFAULTABOUTTITLE', 'About Us'); $aboutus->Title = _t(__CLASS__.'.DEFAULTABOUTTITLE', 'About Us');
$aboutus->Content = _t( $aboutus->Content = _t(
@ -1535,8 +1542,9 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
// If Sort hasn't been set, make this page come after it's siblings // If Sort hasn't been set, make this page come after it's siblings
if (!$this->Sort) { if (!$this->Sort) {
$parentID = ($this->ParentID) ? $this->ParentID : 0; $parentID = ($this->ParentID) ? $this->ParentID : 0;
$tablename = $this->baseTable();
$this->Sort = DB::prepared_query( $this->Sort = DB::prepared_query(
"SELECT MAX(\"Sort\") + 1 FROM \"SiteTree\" WHERE \"ParentID\" = ?", "SELECT MAX(\"Sort\") + 1 FROM \"$tablename\" WHERE \"ParentID\" = ?",
array($parentID) array($parentID)
)->value(); )->value();
} }
@ -1768,9 +1776,10 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
*/ */
public function getStageURLSegment() public function getStageURLSegment()
{ {
$tablename = $this->baseTable();
/** @var SiteTree $stageRecord */ /** @var SiteTree $stageRecord */
$stageRecord = Versioned::get_one_by_stage(self::class, Versioned::DRAFT, [ $stageRecord = Versioned::get_one_by_stage(self::class, Versioned::DRAFT, [
'"SiteTree"."ID"' => $this->ID "\"$tablename\".\"ID\"" => $this->ID
]); ]);
return ($stageRecord) ? $stageRecord->URLSegment : null; return ($stageRecord) ? $stageRecord->URLSegment : null;
} }
@ -1782,9 +1791,10 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
*/ */
public function getLiveURLSegment() public function getLiveURLSegment()
{ {
$tablename = $this->baseTable();
/** @var SiteTree $liveRecord */ /** @var SiteTree $liveRecord */
$liveRecord = Versioned::get_one_by_stage(self::class, Versioned::LIVE, [ $liveRecord = Versioned::get_one_by_stage(self::class, Versioned::LIVE, [
'"SiteTree"."ID"' => $this->ID "\"$tablename\".\"ID\"" => $this->ID
]); ]);
return ($liveRecord) ? $liveRecord->URLSegment : null; return ($liveRecord) ? $liveRecord->URLSegment : null;
} }
@ -2459,12 +2469,15 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
public function onAfterPublish() public function onAfterPublish()
{ {
// Force live sort order to match stage sort order // Force live sort order to match stage sort order
DB::prepared_query( $sql = sprintf(
'UPDATE "SiteTree_Live" 'UPDATE "%2$s"
SET "Sort" = (SELECT "SiteTree"."Sort" FROM "SiteTree" WHERE "SiteTree_Live"."ID" = "SiteTree"."ID") SET "Sort" = (SELECT "%1$s"."Sort" FROM "%1$s" WHERE "%2$s"."ID" = "%1$s"."ID")
WHERE EXISTS (SELECT "SiteTree"."Sort" FROM "SiteTree" WHERE "SiteTree_Live"."ID" = "SiteTree"."ID") AND "ParentID" = ?', WHERE EXISTS (SELECT "%1$s"."Sort" FROM "%1$s" WHERE "%2$s"."ID" = "%1$s"."ID") AND "ParentID" = ?',
array($this->ParentID) $this->baseTable(),
$this->stageTable($this->baseTable(), Versioned::LIVE)
); );
DB::prepared_query($sql, [$this->ParentID]);
} }
/** /**

View File

@ -9,6 +9,7 @@ use SilverStripe\CMS\Model\VirtualPage;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FieldList;
use SilverStripe\Subsites\Model\Subsite;
use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\Reports\Report; use SilverStripe\Reports\Report;
@ -27,6 +28,8 @@ class BrokenLinksReport extends Report
public function sourceRecords($params, $sort, $limit) public function sourceRecords($params, $sort, $limit)
{ {
$sitetreeTbl = DataObject::singleton(SiteTree::class)->baseTable();
$join = ''; $join = '';
$sortBrokenReason = false; $sortBrokenReason = false;
if ($sort) { if ($sort) {
@ -36,15 +39,21 @@ class BrokenLinksReport extends Report
if ($field === 'AbsoluteLink') { if ($field === 'AbsoluteLink') {
$sort = 'URLSegment ' . $direction; $sort = 'URLSegment ' . $direction;
} elseif ($field === 'Subsite.Title') { } elseif ($field == 'Subsite.Title') {
$join = 'LEFT JOIN "Subsite" ON "Subsite"."ID" = "SiteTree"."SubsiteID"'; $subSiteTbl = DataObject::singleton(Subsite::class)->baseTable();
} elseif ($field === 'BrokenReason') { $join = sprintf(
'LEFT JOIN "%s" ON "%s"."ID" = "%s"."SubsiteID"',
$subSiteTbl,
$subSiteTbl,
$sitetreeTbl
);
} elseif ($field == 'BrokenReason') {
$sortBrokenReason = true; $sortBrokenReason = true;
$sort = ''; $sort = '';
} }
} }
$brokenFilter = [ $brokenFilter = [
'"SiteTree"."HasBrokenLink" = ? OR "SiteTree"."HasBrokenFile" = ?' => [true, true] sprintf('"%s"."HasBrokenLink" = ? OR "%s"."HasBrokenFile" = ?', $sitetreeTbl, $sitetreeTbl) => [true, true]
]; ];
$isLive = !isset($params['CheckSite']) || $params['CheckSite'] === 'Published'; $isLive = !isset($params['CheckSite']) || $params['CheckSite'] === 'Published';
if ($isLive) { if ($isLive) {

View File

@ -27,10 +27,11 @@ class RecentlyEditedReport extends Report
public function sourceRecords($params = null) public function sourceRecords($params = null)
{ {
$tableName = DataObject::singleton(SiteTree::class)->baseTable();
$threshold = strtotime('-14 days', DBDatetime::now()->getTimestamp()); $threshold = strtotime('-14 days', DBDatetime::now()->getTimestamp());
return SiteTree::get() return SiteTree::get()
->filter('LastEdited:GreaterThan', date("Y-m-d H:i:s", $threshold)) ->filter('LastEdited:GreaterThan', date("Y-m-d H:i:s", $threshold))
->sort("\"SiteTree\".\"LastEdited\" DESC"); ->sort("\"$tableName\".\"LastEdited\" DESC");
} }
public function columns() public function columns()

View File

@ -5,6 +5,7 @@ namespace SilverStripe\CMS\Tasks;
use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Dev\BuildTask; use SilverStripe\Dev\BuildTask;
use SilverStripe\Dev\Debug; use SilverStripe\Dev\Debug;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\Versioned\Versioned; use SilverStripe\Versioned\Versioned;
@ -35,11 +36,13 @@ class MigrateSiteTreeLinkingTask extends BuildTask
Versioned::withVersionedMode(function () use (&$pages) { Versioned::withVersionedMode(function () use (&$pages) {
Versioned::set_stage(Versioned::DRAFT); Versioned::set_stage(Versioned::DRAFT);
$sitetreeTbl = DataObject::singleton(SiteTree::class)->baseTable();
/** @var SiteTree[] $linkedPages */ /** @var SiteTree[] $linkedPages */
$linkedPages = SiteTree::get() $linkedPages = SiteTree::get()
->innerJoin( ->innerJoin(
'SiteTree_LinkTracking', 'SiteTree_LinkTracking',
'"SiteTree_LinkTracking"."SiteTreeID" = "SiteTree"."ID"' "\"SiteTree_LinkTracking\".\"SiteTreeID\" = \"$sitetreeTbl\".\"ID\""
); );
foreach ($linkedPages as $page) { foreach ($linkedPages as $page) {
// Command page to update symlink tracking // Command page to update symlink tracking

View File

@ -2,6 +2,7 @@
namespace SilverStripe\CMS\Tasks; namespace SilverStripe\CMS\Tasks;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
@ -14,8 +15,9 @@ class SiteTreeMaintenanceTask extends Controller
public function makelinksunique() public function makelinksunique()
{ {
$badURLs = "'" . implode("', '", DB::query("SELECT URLSegment, count(*) FROM SiteTree GROUP BY URLSegment HAVING count(*) > 1")->column()) . "'"; $table = DataObject::singleton(SiteTree::class)->baseTable();
$pages = DataObject::get("SilverStripe\\CMS\\Model\\SiteTree", "\"SiteTree\".\"URLSegment\" IN ($badURLs)"); $badURLs = "'" . implode("', '", DB::query("SELECT \"URLSegment\", count(*) FROM \"$table\" GROUP BY \"URLSegment\" HAVING count(*) > 1")->column()) . "'";
$pages = DataObject::get(SiteTree::class, "\"$table\".\"URLSegment\" IN ($badURLs)");
foreach ($pages as $page) { foreach ($pages as $page) {
echo "<li>$page->Title: "; echo "<li>$page->Title: ";