Changeset View
Changeset View
Standalone View
Standalone View
src/applications/phriction/query/PhrictionDocumentQuery.php
Show First 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | final class PhrictionDocumentQuery | ||||
} | } | ||||
public function setOrder($order) { | public function setOrder($order) { | ||||
$this->order = $order; | $this->order = $order; | ||||
return $this; | return $this; | ||||
} | } | ||||
protected function loadPage() { | protected function loadPage() { | ||||
$document = new PhrictionDocument(); | $table = new PhrictionDocument(); | ||||
$conn_r = $document->establishConnection('r'); | $conn_r = $table->establishConnection('r'); | ||||
$rows = queryfx_all( | $rows = queryfx_all( | ||||
$conn_r, | $conn_r, | ||||
'SELECT * FROM %T %Q %Q %Q', | 'SELECT * FROM %T %Q %Q %Q', | ||||
$document->getTableName(), | $table->getTableName(), | ||||
$this->buildWhereClause($conn_r), | $this->buildWhereClause($conn_r), | ||||
$this->buildOrderClause($conn_r), | $this->buildOrderClause($conn_r), | ||||
$this->buildLimitClause($conn_r)); | $this->buildLimitClause($conn_r)); | ||||
return $document->loadAllFromArray($rows); | $documents = $table->loadAllFromArray($rows); | ||||
if ($documents) { | |||||
$ancestor_slugs = array(); | |||||
foreach ($documents as $key => $document) { | |||||
$document_slug = $document->getSlug(); | |||||
foreach (PhabricatorSlug::getAncestry($document_slug) as $ancestor) { | |||||
$ancestor_slugs[$ancestor][] = $key; | |||||
} | |||||
} | |||||
if ($ancestor_slugs) { | |||||
$ancestors = queryfx_all( | |||||
$conn_r, | |||||
'SELECT * FROM %T WHERE slug IN (%Ls)', | |||||
$document->getTableName(), | |||||
array_keys($ancestor_slugs)); | |||||
$ancestors = $table->loadAllFromArray($ancestors); | |||||
$ancestors = mpull($ancestors, null, 'getSlug'); | |||||
foreach ($ancestor_slugs as $ancestor_slug => $document_keys) { | |||||
$ancestor = idx($ancestors, $ancestor_slug); | |||||
foreach ($document_keys as $document_key) { | |||||
$documents[$document_key]->attachAncestor( | |||||
$ancestor_slug, | |||||
$ancestor); | |||||
} | |||||
} | |||||
} | |||||
} | |||||
return $documents; | |||||
} | } | ||||
protected function willFilterPage(array $documents) { | protected function willFilterPage(array $documents) { | ||||
// To view a Phriction document, you must also be able to view all of the | |||||
// ancestor documents. Filter out documents which have ancestors that are | |||||
// not visible. | |||||
$document_map = array(); | |||||
foreach ($documents as $document) { | |||||
$document_map[$document->getSlug()] = $document; | |||||
foreach ($document->getAncestors() as $key => $ancestor) { | |||||
if ($ancestor) { | |||||
$document_map[$key] = $ancestor; | |||||
} | |||||
} | |||||
} | |||||
$filtered_map = $this->applyPolicyFilter( | |||||
$document_map, | |||||
array(PhabricatorPolicyCapability::CAN_VIEW)); | |||||
// Filter all of the documents where a parent is not visible. | |||||
foreach ($documents as $document_key => $document) { | |||||
// If the document itself is not visible, filter it. | |||||
if (!isset($filtered_map[$document->getSlug()])) { | |||||
$this->didRejectResult($documents[$document_key]); | |||||
unset($documents[$document_key]); | |||||
continue; | |||||
} | |||||
// If an ancestor exists but is not visible, filter the document. | |||||
foreach ($document->getAncestors() as $ancestor_key => $ancestor) { | |||||
if (!$ancestor) { | |||||
continue; | |||||
} | |||||
if (!isset($filtered_map[$ancestor_key])) { | |||||
$this->didRejectResult($documents[$document_key]); | |||||
unset($documents[$document_key]); | |||||
break; | |||||
} | |||||
} | |||||
} | |||||
if (!$documents) { | |||||
return $documents; | |||||
} | |||||
if ($this->needContent) { | if ($this->needContent) { | ||||
$contents = id(new PhrictionContent())->loadAllWhere( | $contents = id(new PhrictionContent())->loadAllWhere( | ||||
'id IN (%Ld)', | 'id IN (%Ld)', | ||||
mpull($documents, 'getContentID')); | mpull($documents, 'getContentID')); | ||||
foreach ($documents as $key => $document) { | foreach ($documents as $key => $document) { | ||||
$content_id = $document->getContentID(); | $content_id = $document->getContentID(); | ||||
if (empty($contents[$content_id])) { | if (empty($contents[$content_id])) { | ||||
▲ Show 20 Lines • Show All 125 Lines • Show Last 20 Lines |