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
// This allows the CMS to set the correct sort value
if ($newItem->castingHelper('Sort')) {
$table = DataObject::singleton(SiteTree::class)->baseTable();
$maxSort = DB::prepared_query(
'SELECT MAX("Sort") FROM "SiteTree" WHERE "ParentID" = ?',
"SELECT MAX(\"Sort\") FROM \"$table\" WHERE \"ParentID\" = ?",
array($parentID)
)->value();
$newItem->Sort = (int)$maxSort + 1;
@ -1911,8 +1912,10 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
}
/** @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(
'"SiteTree_Live"."ID"' => $id
"\"$liveTable\".\"ID\"" => $id
));
// 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;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned;
/**
@ -18,10 +19,12 @@ class CMSSiteTreeFilter_ChangedPages extends CMSSiteTreeFilter
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 = $this->applyDefaultFilters($pages)
->leftJoin('SiteTree_Live', '"SiteTree_Live"."ID" = "SiteTree"."ID"')
->where('"SiteTree"."Version" <> "SiteTree_Live"."Version"');
->leftJoin($liveTable, "\"$liveTable\".\"ID\" = \"$table\".\"ID\"")
->where("\"$table\".\"Version\" <> \"$liveTable\".\"Version\"");
return $pages;
}
}

View File

@ -135,9 +135,10 @@ class ModelAsController extends Controller implements NestedController
}
// 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')) {
$conditions[] = array('"SiteTree"."ParentID"' => 0);
$conditions[] = [sprintf('"%s"."ParentID"', $tableName) => 0];
}
/** @var SiteTree $sitetree */
$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)
{
// 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, '/')) {
$link = trim(Director::makeRelative($link), '/');
} else {
@ -405,9 +410,9 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
// Grab the initial root level page to traverse down from.
$URLSegment = array_shift($parts);
$conditions = array('"SiteTree"."URLSegment"' => rawurlencode($URLSegment));
$conditions = array($urlSegmentExpr => rawurlencode($URLSegment));
if (self::config()->get('nested_urls')) {
$conditions[] = array('"SiteTree"."ParentID"' => 0);
$conditions[] = array($parentIDExpr => 0);
}
/** @var SiteTree $sitetree */
$sitetree = DataObject::get_one(self::class, $conditions, $cache);
@ -416,7 +421,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
if (!$sitetree
&& self::config()->get('nested_urls')
&& $sitetree = DataObject::get_one(self::class, array(
'"SiteTree"."URLSegment"' => $URLSegment
$urlSegmentExpr => $URLSegment
), $cache)
) {
return $sitetree;
@ -449,8 +454,8 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
$next = DataObject::get_one(
self::class,
array(
'"SiteTree"."URLSegment"' => $segment,
'"SiteTree"."ParentID"' => $sitetree->ID
$urlSegmentExpr => $segment,
$parentIDExpr => $sitetree->ID
),
$cache
);
@ -659,9 +664,10 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
{
$oldReadingMode = Versioned::get_reading_mode();
Versioned::set_stage(Versioned::LIVE);
$tablename = $this->baseTable();
/** @var SiteTree $live */
$live = Versioned::get_one_by_stage(self::class, Versioned::LIVE, array(
'"SiteTree"."ID"' => $this->ID
"\"$tablename\".\"ID\"" => $this->ID
));
if ($live) {
$link = $live->AbsoluteLink();
@ -1500,7 +1506,8 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
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->Title = _t(__CLASS__.'.DEFAULTABOUTTITLE', 'About Us');
$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 (!$this->Sort) {
$parentID = ($this->ParentID) ? $this->ParentID : 0;
$tablename = $this->baseTable();
$this->Sort = DB::prepared_query(
"SELECT MAX(\"Sort\") + 1 FROM \"SiteTree\" WHERE \"ParentID\" = ?",
"SELECT MAX(\"Sort\") + 1 FROM \"$tablename\" WHERE \"ParentID\" = ?",
array($parentID)
)->value();
}
@ -1768,9 +1776,10 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
*/
public function getStageURLSegment()
{
$tablename = $this->baseTable();
/** @var SiteTree $stageRecord */
$stageRecord = Versioned::get_one_by_stage(self::class, Versioned::DRAFT, [
'"SiteTree"."ID"' => $this->ID
"\"$tablename\".\"ID\"" => $this->ID
]);
return ($stageRecord) ? $stageRecord->URLSegment : null;
}
@ -1782,9 +1791,10 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
*/
public function getLiveURLSegment()
{
$tablename = $this->baseTable();
/** @var SiteTree $liveRecord */
$liveRecord = Versioned::get_one_by_stage(self::class, Versioned::LIVE, [
'"SiteTree"."ID"' => $this->ID
"\"$tablename\".\"ID\"" => $this->ID
]);
return ($liveRecord) ? $liveRecord->URLSegment : null;
}
@ -2459,12 +2469,15 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
public function onAfterPublish()
{
// Force live sort order to match stage sort order
DB::prepared_query(
'UPDATE "SiteTree_Live"
SET "Sort" = (SELECT "SiteTree"."Sort" FROM "SiteTree" WHERE "SiteTree_Live"."ID" = "SiteTree"."ID")
WHERE EXISTS (SELECT "SiteTree"."Sort" FROM "SiteTree" WHERE "SiteTree_Live"."ID" = "SiteTree"."ID") AND "ParentID" = ?',
array($this->ParentID)
$sql = sprintf(
'UPDATE "%2$s"
SET "Sort" = (SELECT "%1$s"."Sort" FROM "%1$s" WHERE "%2$s"."ID" = "%1$s"."ID")
WHERE EXISTS (SELECT "%1$s"."Sort" FROM "%1$s" WHERE "%2$s"."ID" = "%1$s"."ID") AND "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\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Subsites\Model\Subsite;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\Reports\Report;
@ -27,6 +28,8 @@ class BrokenLinksReport extends Report
public function sourceRecords($params, $sort, $limit)
{
$sitetreeTbl = DataObject::singleton(SiteTree::class)->baseTable();
$join = '';
$sortBrokenReason = false;
if ($sort) {
@ -36,15 +39,21 @@ class BrokenLinksReport extends Report
if ($field === 'AbsoluteLink') {
$sort = 'URLSegment ' . $direction;
} elseif ($field === 'Subsite.Title') {
$join = 'LEFT JOIN "Subsite" ON "Subsite"."ID" = "SiteTree"."SubsiteID"';
} elseif ($field === 'BrokenReason') {
} elseif ($field == 'Subsite.Title') {
$subSiteTbl = DataObject::singleton(Subsite::class)->baseTable();
$join = sprintf(
'LEFT JOIN "%s" ON "%s"."ID" = "%s"."SubsiteID"',
$subSiteTbl,
$subSiteTbl,
$sitetreeTbl
);
} elseif ($field == 'BrokenReason') {
$sortBrokenReason = true;
$sort = '';
}
}
$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';
if ($isLive) {

View File

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

View File

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

View File

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