N°1408 - DBSearch Serialize refactor (json format)

This commit is contained in:
Eric
2018-10-10 10:42:19 +02:00
parent 94b4f10277
commit bf000e6a89
7 changed files with 57 additions and 25 deletions

View File

@@ -710,9 +710,9 @@ class RuntimeDashboard extends Dashboard
*/
public function Render($oPage, $bEditMode = false, $aExtraParams = array())
{
if (!isset($aExtraParams['query_params']) && isset($aExtraParams['class']))
if (!isset($aExtraParams['query_params']) && isset($aExtraParams['this->class']))
{
$oObj = MetaModel::GetObject($aExtraParams['class'], $aExtraParams['id']);
$oObj = MetaModel::GetObject($aExtraParams['this->class'], $aExtraParams['this->id']);
$aRenderParams = array('query_params' => $oObj->ToArgsForQuery());
}
else
@@ -726,7 +726,7 @@ class RuntimeDashboard extends Dashboard
{
/** @var \DBObject $oObj */
$oObj = $aExtraParams['query_params']['this->object()'];
$aAjaxParams = array('class' => get_class($oObj), 'id' => $oObj->GetKey());
$aAjaxParams = array('this->class' => get_class($oObj), 'this->id' => $oObj->GetKey());
}
else
{
@@ -938,9 +938,9 @@ EOF
*/
public function RenderEditor($oPage, $aExtraParams = array())
{
if (isset($aExtraParams['class']))
if (isset($aExtraParams['this->class']))
{
$oObj = MetaModel::GetObject($aExtraParams['class'], $aExtraParams['id']);
$oObj = MetaModel::GetObject($aExtraParams['this->class'], $aExtraParams['this->id']);
$aRenderParams = array('query_params' => $oObj->ToArgsForQuery());
}
else

View File

@@ -781,9 +781,9 @@ class DashletObjectList extends Dashlet
{
$aQueryParams = $aExtraParams['query_params'];
}
elseif (isset($aExtraParams['class']))
elseif (isset($aExtraParams['this->class']))
{
$oObj = MetaModel::GetObject($aExtraParams['class'], $aExtraParams['id']);
$oObj = MetaModel::GetObject($aExtraParams['this->class'], $aExtraParams['this->id']);
$aQueryParams = $oObj->ToArgsForQuery();
}
else
@@ -1020,6 +1020,8 @@ abstract class DashletGroupBy extends Dashlet
* @param bool $bEditMode
* @param array $aExtraParams
*
* @throws \ArchivedObjectException
* @throws \CoreException
* @throws \OQLException
*/
public function Render($oPage, $bEditMode = false, $aExtraParams = array())
@@ -1033,9 +1035,9 @@ abstract class DashletGroupBy extends Dashlet
{
$aQueryParams = $aExtraParams['query_params'];
}
elseif (isset($aExtraParams['class']))
elseif (isset($aExtraParams['this->class']))
{
$oObj = MetaModel::GetObject($aExtraParams['class'], $aExtraParams['id']);
$oObj = MetaModel::GetObject($aExtraParams['this->class'], $aExtraParams['this->id']);
$aQueryParams = $oObj->ToArgsForQuery();
}
else
@@ -1837,9 +1839,9 @@ class DashletHeaderDynamic extends Dashlet
{
$aQueryParams = $aExtraParams['query_params'];
}
elseif (isset($aExtraParams['class']))
elseif (isset($aExtraParams['this->class']))
{
$oObj = MetaModel::GetObject($aExtraParams['class'], $aExtraParams['id']);
$oObj = MetaModel::GetObject($aExtraParams['this->class'], $aExtraParams['this->id']);
$aQueryParams = $oObj->ToArgsForQuery();
}
else

View File

@@ -472,7 +472,7 @@ class DisplayBlock
{
$aQueryParams = array();
}
$sFilter = urlencode($oSubsetSearch->serialize(false, $aQueryParams));
$sFilter = $oSubsetSearch->serialize(false, $aQueryParams);
$aData[] = array ('group' => $aLabels[$iRow],
'value' => "<a href=\"".utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=search&dosearch=1&$sParams&filter=$sFilter\">$iCount</a>"); // TO DO: add the context information
@@ -749,7 +749,7 @@ class DisplayBlock
$this->m_oSet->SetShowObsoleteData($this->m_bShowObsoleteData);
}
$iCount = $this->m_oSet->Count();
$sHyperlink = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=search&'.$oAppContext->GetForLink().'&filter='.urlencode($this->m_oFilter->serialize());
$sHyperlink = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=search&'.$oAppContext->GetForLink().'&filter='.$this->m_oFilter->serialize();
$sHtml .= '<p><a class="actions" href="'.$sHyperlink.'">';
// Note: border set to 0 due to various browser interpretations (IE9 adding a 2px border)
$sHtml .= MetaModel::GetClassIcon($sClass, true, 'float;left;margin-right:10px;border:0;');
@@ -838,7 +838,7 @@ class DisplayBlock
}
$sHyperlink = utils::GetAbsoluteUrlAppRoot()
.'pages/UI.php?operation=search&'.$oAppContext->GetForLink()
.'&filter='.urlencode($oSingleGroupByValueFilter->serialize());
.'&filter='.$oSingleGroupByValueFilter->serialize();
$aCounts[$sStateValue] = "<a href=\"$sHyperlink\">{$aCounts[$sStateValue]}</a>";
}
}
@@ -847,7 +847,7 @@ class DisplayBlock
$sHtml .= '<tr><td>'.implode('</td><td>', $aCounts).'</td></tr></table></div>';
// Title & summary
$iCount = $this->m_oSet->Count();
$sHyperlink = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=search&'.$oAppContext->GetForLink().'&filter='.urlencode($this->m_oFilter->serialize());
$sHyperlink = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=search&'.$oAppContext->GetForLink().'&filter='.$this->m_oFilter->serialize();
$sHtml .= '<h1>'.Dict::S(str_replace('_', ':', $sTitle)).'</h1>';
$sHtml .= '<a class="summary" href="'.$sHyperlink.'">'.Dict::Format(str_replace('_', ':', $sLabel), $iCount).'</a>';
$sHtml .= '<div style="clear:both;"></div>';
@@ -858,7 +858,7 @@ class DisplayBlock
$sCsvFile = strtolower($this->m_oFilter->GetClass()).'.csv';
$sDownloadLink = utils::GetAbsoluteUrlAppRoot().'webservices/export.php?expression='.urlencode($this->m_oFilter->ToOQL(true)).'&format=csv&filename='.urlencode($sCsvFile);
$sLinkToToggle = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=search&'.$oAppContext->GetForLink().'&filter='.urlencode($this->m_oFilter->serialize()).'&format=csv';
$sLinkToToggle = utils::GetAbsoluteUrlAppRoot().'pages/UI.php?operation=search&'.$oAppContext->GetForLink().'&filter='.$this->m_oFilter->serialize().'&format=csv';
// Pass the parameters via POST, since expression may be very long
$aParamsToPost = array(
'expression' => $this->m_oFilter->ToOQL(true),
@@ -986,7 +986,7 @@ EOF
$oSubsetSearch = $this->m_oFilter->DeepClone();
$oCondition = new BinaryExpression($oGroupByExp, '=', new ScalarExpression($sValue));
$oSubsetSearch->AddConditionExpression($oCondition);
$aURLs[] = utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=search&format=html&filter=".urlencode($oSubsetSearch->serialize()).'&'.$sContextParam;
$aURLs[] = utils::GetAbsoluteUrlAppRoot()."pages/UI.php?operation=search&format=html&filter=".$oSubsetSearch->serialize().'&'.$sContextParam;
}
$sJSURLs = json_encode($aURLs);
}

View File

@@ -332,7 +332,8 @@ EOF
if ( ($oCurrObject != null) && ($this->sAttCode != ''))
{
$oAttDef = MetaModel::GetAttributeDef(get_class($oCurrObject), $this->sAttCode);
$aArgs = array('this' => $oCurrObject);
/** @var \DBObject $oCurrObject */
$aArgs = $oCurrObject->ToArgsForQuery();
$aParams = array('query_params' => $aArgs);
$oSet = $oAttDef->GetAllowedValuesAsObjectSet($aArgs);
$oFilter = $oSet->GetFilter();

View File

@@ -9939,7 +9939,7 @@ class AttributeTagSet extends AttributeSet
$oAppContext = new ApplicationContext();
$sContext = $oAppContext->GetForLink();
$sUIPage = cmdbAbstractObject::ComputeStandardUIPage($oFilter->GetClass());
$sFilter = urlencode($oFilter->serialize());
$sFilter = $oFilter->serialize();
$sUrl = utils::GetAbsoluteUrlAppRoot()."pages/$sUIPage?operation=search&filter=".$sFilter."&{$sContext}";
$sHtml .= '<a href="'.$sUrl.'" class="attribute-set-item attribute-set-item-'.$sTagCode.'" data-code="'.$sTagCode.'" data-label="'.htmlentities($sTagLabel, ENT_QUOTES, 'UTF-8').'" data-description="'.htmlentities($sTagDescription, ENT_QUOTES, 'UTF-8').'">'.$sTagLabel.'</a>';

View File

@@ -262,34 +262,63 @@ abstract class DBSearch
if (isset($aContextParams[$sParamName.'->object()']))
{
$sAttCode = substr($sParam, $iPos + 2);
/** @var \DBObject $oObj */
$oObj = $aContextParams[$sParamName.'->object()'];
if ($sAttCode == 'id')
if ($oObj->IsModified())
{
$aQueryParams[$sParam] = $oObj->GetKey();
if ($sAttCode == 'id')
{
$aQueryParams[$sParam] = $oObj->GetKey();
}
else
{
$aQueryParams[$sParam] = $oObj->Get($sAttCode);
}
}
else
{
$aQueryParams[$sParam] = $oObj->Get($sAttCode);
unset($aQueryParams[$sParam]);
// For database objects, serialize only class, key
$aQueryParams[$sParamName.'->id'] = $oObj->GetKey();
$aQueryParams[$sParamName.'->class'] = get_class($oObj);
}
}
}
}
$sOql = $this->ToOql($bDevelopParams, $aContextParams);
return rawurlencode(base64_encode(serialize(array($sOql, $aQueryParams, $this->m_aModifierProperties))));
return rawurlencode(json_encode(array($sOql, $aQueryParams, $this->m_aModifierProperties)));
}
/**
* @param string $sValue Serialized OQL query
*
* @return \DBSearch
* @throws \ArchivedObjectException
* @throws \CoreException
* @throws \OQLException
*/
static public function unserialize($sValue)
{
$aData = unserialize(base64_decode(rawurldecode($sValue)));
$aData = json_decode(rawurldecode($sValue), true);
$sOql = $aData[0];
$aParams = $aData[1];
$aExtraParams = array();
foreach($aParams as $sParam => $sValue)
{
if (($iPos = strpos($sParam, '->class')) !== false)
{
$sParamName = substr($sParam, 0, $iPos);
if (isset($aParams[$sParamName.'->id']))
{
$sClass = $aParams[$sParamName.'->class'];
$iKey = $aParams[$sParamName.'->id'];
$oObj = MetaModel::GetObject($sClass, $iKey);
$aExtraParams[$sParamName.'->object()'] = $oObj;
}
}
}
$aParams = array_merge($aExtraParams, $aParams);
// We've tried to use gzcompress/gzuncompress, but for some specific queries
// it was not working at all (See Trac #193)
// gzuncompress was issuing a warning "data error" and the return object was null

View File

@@ -728,7 +728,7 @@ class TestQueriesOnFarm extends MyFarm
$oFilter2 = DBObjectSearch::unserialize($sSerialize);
try
{
$sQuery2 = $oMyFilter2->MakeSelectQuery();
$sQuery2 = $oFilter2->MakeSelectQuery();
}
catch (Exception $e)
{