Ferreteria/archive/smw-base-v3.php: Difference between revisions
(Created page with "==Notes== This is barely even started; just creating the page for easier updating later. ==Code== <php> <?php /*==== PURPOSE: version-3-specific class methods for clsSMWDat...") |
(works now) |
||
Line 9: | Line 9: | ||
*/ | */ | ||
class clsSMWData_version extends clsMWData { | class clsSMWData_version extends clsMWData { | ||
public function GetObjectID($iName) { | |||
$sqlKey = SQLValue(static::NormalizeTitle($iName,SMW_NS_PROPERTY)); | |||
$sql = "SELECT smw_id FROM smw_object_ids WHERE (smw_title=$sqlKey) LIMIT 1;"; | |||
$rs = $this->DataSet($sql); | |||
if ($rs->HasRows()) { | |||
$rs->NextRow(); // should be only one row -- get it. | |||
$idObj = $rs->Value('smw_id'); | |||
} else { | |||
$idObj = NULL; | |||
} | |||
return $idObj; | |||
} | |||
public function GetPages_forPropVal($iPropName,$iPropValue) { | public function GetPages_forPropVal($iPropName,$iPropValue) { | ||
// look up property's SMW ID | $idProp = $this->GetObjectID($iPropName); // look up property's SMW ID | ||
/* | |||
$sqlKey = SQLValue(static::NormalizeTitle($iPropName,SMW_NS_PROPERTY)); | |||
$sql = "SELECT smw_id FROM smw_object_ids WHERE (smw_title=$sqlKey) LIMIT 1;"; | |||
$rs = $this->DataSet($sql); | |||
if ($rs->HasRows()) { | |||
$rs->NextRow(); // should be only one row -- get it. | |||
$idProp = $rs->Value('smw_id'); | |||
} else { | |||
// TODO: some kind of indication that the property does not seem to even exist | |||
} | |||
*/ | |||
$idVal = $this->GetObjectID($iPropValue); // look up value's SMW ID | |||
/* | |||
$sqlKey = SQLValue(static::NormalizeTitle($iPropValue,SMW_NS_PROPERTY)); | |||
$sql = "SELECT smw_id FROM smw_object_ids WHERE (smw_title=$sqlKey) LIMIT 1;"; | |||
$rs = $this->DataSet($sql); | |||
if ($rs->HasRows()) { | |||
$rs->NextRow(); // should be only one row -- get it. | |||
$idVal = $rs->Value('smw_id'); | |||
} else { | |||
// TODO: some kind of indication that the property does not seem to even exist | |||
} | |||
*/ | |||
// find all pages where that property is set | |||
// start with smw_di_wikipage, which is properties that are page titles (the default kind of property) | |||
// there are probably other tables we need to check, but this will do for immediate needs. | |||
$sql = 'SELECT s_id,' | |||
.' s.smw_namespace AS s_namespace' | |||
.', CAST(s.smw_title AS char) AS s_title' | |||
//.', CAST(p.smw_title AS char) AS p_title' | |||
//.', CAST(o.smw_title AS char) AS o_title' | |||
.' FROM' | |||
.' (smw_di_wikipage AS r' | |||
.' LEFT JOIN smw_object_ids AS s ON r.s_id=s.smw_id)' | |||
//.' LEFT JOIN smw_ids AS p ON r.p_id=p.smw_id)' | |||
.' LEFT JOIN smw_object_ids AS o ON r.o_id=o.smw_id' | |||
." WHERE (o_id=$idVal) AND (p_id=$idProp);"; | |||
$rs = $this->DataSet($sql); | |||
if ($rs->HasRows()) { | |||
$arOut = array(); | |||
while ($rs->NextRow()) { | |||
$idPage = $rs->Value('s_id'); | |||
$arOut[$idPage] = $rs->Values(); | |||
} | |||
return $arOut; | |||
} else { | |||
// TODO: some kind of indication that the property was found but no pages matched | |||
return NULL; // nothing found | |||
} | |||
} | |||
// NOT TESTED | |||
/* | |||
public function GetPropData($iPropName) { | |||
$strPageKey = $this->TitleKey(); | |||
$strPropKey = Title::capitalize($iPropName,SMW_NS_PROPERTY); | |||
$sqlPageKey = $this->Engine()->SafeParam($strPageKey); | |||
$sqlPropKey = $this->Engine()->SafeParam($strPropKey); | |||
$intNSpace = (int)$this->Nspace(); | |||
$arTypes = SMWSQLStore3::$di_type_tables; | |||
$ | foreach ($arTypes as $idType => $sTable) { | ||
$sql = 'SELECT' | |||
.' r.*,' | |||
.' CAST(s.smw_title AS char) AS s_title,' | |||
.' CAST(p.smw_title AS char) AS p_title,' | |||
.' CAST(o.smw_title AS char) AS o_title' | |||
.' FROM' | |||
." (($sTable AS r" | |||
.' LEFT JOIN smw_object_ids AS s ON r.s_id=s.smw_id)' | |||
.' LEFT JOIN smw_object_ids AS p ON r.p_id=p.smw_id)' | |||
.' LEFT JOIN smw_object_ids AS o ON r.o_id=o.smw_id' | |||
.' WHERE' | |||
." (p.smw_title = '$sqlPropKey') AND" | |||
." (s.smw_title = '$sqlPageKey') AND" | |||
." (s.smw_namespace = $intNSpace);"; | |||
$rs = $this->Engine()->DataSet($sql); | |||
$arOut[$idType] = $rs; | |||
} | |||
return $arOut; | |||
} | } | ||
*/ | |||
} | |||
class w3smwPage_version { | |||
protected $osmTitle; | |||
public function SMW_Object() { | |||
//if (!isset($this->osmTitle)) { | |||
$this->osmTitle = SMWDIWikiPage::newFromTitle($this->MW_Object()); | |||
//} | |||
return $this->osmTitle; | |||
} | |||
public function GetPropData($iPropName,$iSep=NULL) { | |||
//$arTypes = SMWSQLStore3::$di_type_tables; | |||
$arTypes = SMWSQLStore3::getPropertyTables(); | |||
$osmStore = new SMWSQLStore3(); | |||
$osmItem = $osmStore->getSemanticData($this->SMW_Object()); // $osmItem is SMWSemanticData | |||
$arProps = $osmItem->getProperties(); | |||
if (array_key_exists($iPropName,$arProps)) { | |||
$osmProp = $arProps[$iPropName]; // $osmProp is a SMWDIProperty | |||
$arVals = $osmItem->getPropertyValues($osmProp); | |||
$out = NULL; | |||
foreach ($arVals as $key => $osmVal) { | |||
// $osmVal is an SMWDataItem of appopriate type, e.g. SMWDIBlob | |||
if (!is_null($out)) { | |||
$out .= $iSep; | |||
} | |||
//$sSer = $osmVal->getSerialization(); // not all types support getString() | |||
$out .= $osmVal->getSortKey(); // THIS IS A KLUGE | |||
} | |||
return $out; | |||
} else { | |||
return NULL; // property not found on page | |||
} | |||
/* | |||
foreach ($arTypes as $idType => $osmTable) { | |||
$osmStore->getDataItemHandlerForDIType($idType); | |||
/* | |||
$sTable = $osmTable->getName(); | |||
if (!$osmTable->isFixedPropertyTable()) { | |||
// I'm not sure what "fixed property" means, but those seem to be the tables I don't want | |||
$sql = "SELECT * FROM $sTable WHERE s_id=$idPage"; | |||
$rs = $this->Engine()->DataSet($sql); | |||
if ($rs->HasRows()) { | |||
echo 'FOUND ONE!'; | |||
return $rs; | |||
} | |||
} | |||
} | |||
*/ | |||
return NULL; | |||
/* | |||
$ar = $this->GetPropData_raw($iPropName); | |||
$arOut = NULL; | |||
foreach ($ar as $type => $arProps) { | |||
ArrayMerge($arOut,$arProps); | |||
} | |||
return $arOut; | |||
*/ | |||
} | |||
public function GetPropVals($iPropName) { | |||
die('Not yet implemented'); | |||
} | |||
/* | |||
protected function GetPropData_raw($iPropName) { | |||
$strPageKey = $this->TitleKey(); | |||
$strPropKey = Title::capitalize($iPropName,SMW_NS_PROPERTY); | |||
$sqlPageKey = $this->Engine()->SafeParam($strPageKey); | |||
$sqlPropKey = $this->Engine()->SafeParam($strPropKey); | |||
$intNSpace = (int)$this->Nspace(); | |||
$arTypes = SMWSQLStore3::$di_type_tables; | |||
foreach ($arTypes as $idType => $sTable) { | |||
$sql = 'SELECT' | |||
.' r.*,' | |||
.' CAST(s.smw_title AS char) AS s_title,' | |||
.' CAST(p.smw_title AS char) AS p_title,' | |||
.' CAST(o.smw_title AS char) AS o_title' | |||
.' FROM' | |||
." (($sTable AS r" | |||
.' LEFT JOIN smw_object_ids AS s ON r.s_id=s.smw_id)' | |||
.' LEFT JOIN smw_object_ids AS p ON r.p_id=p.smw_id)' | |||
.' LEFT JOIN smw_object_ids AS o ON r.o_id=o.smw_id' | |||
.' WHERE' | |||
." (p.smw_title = '$sqlPropKey') AND" | |||
." (s.smw_title = '$sqlPageKey') AND" | |||
." (s.smw_namespace = $intNSpace);"; | |||
$rs = $this->Engine()->DataSet($sql); | |||
$arOut[$idType] = $rs; | |||
} | |||
return $arOut; | |||
} | |||
*/ | |||
} | } | ||
</php> | </php> |
Revision as of 19:17, 14 February 2013
Notes
This is barely even started; just creating the page for easier updating later.
Code
<php> <?php
/*====
PURPOSE: version-3-specific class methods for clsSMWData
- /
class clsSMWData_version extends clsMWData {
public function GetObjectID($iName) {
$sqlKey = SQLValue(static::NormalizeTitle($iName,SMW_NS_PROPERTY)); $sql = "SELECT smw_id FROM smw_object_ids WHERE (smw_title=$sqlKey) LIMIT 1;"; $rs = $this->DataSet($sql); if ($rs->HasRows()) { $rs->NextRow(); // should be only one row -- get it. $idObj = $rs->Value('smw_id'); } else { $idObj = NULL; } return $idObj;
}
public function GetPages_forPropVal($iPropName,$iPropValue) {
$idProp = $this->GetObjectID($iPropName); // look up property's SMW ID
/* $sqlKey = SQLValue(static::NormalizeTitle($iPropName,SMW_NS_PROPERTY)); $sql = "SELECT smw_id FROM smw_object_ids WHERE (smw_title=$sqlKey) LIMIT 1;"; $rs = $this->DataSet($sql); if ($rs->HasRows()) { $rs->NextRow(); // should be only one row -- get it. $idProp = $rs->Value('smw_id'); } else { // TODO: some kind of indication that the property does not seem to even exist }
- /
$idVal = $this->GetObjectID($iPropValue); // look up value's SMW ID /* $sqlKey = SQLValue(static::NormalizeTitle($iPropValue,SMW_NS_PROPERTY)); $sql = "SELECT smw_id FROM smw_object_ids WHERE (smw_title=$sqlKey) LIMIT 1;"; $rs = $this->DataSet($sql); if ($rs->HasRows()) { $rs->NextRow(); // should be only one row -- get it. $idVal = $rs->Value('smw_id'); } else { // TODO: some kind of indication that the property does not seem to even exist }
- /
// find all pages where that property is set
// start with smw_di_wikipage, which is properties that are page titles (the default kind of property) // there are probably other tables we need to check, but this will do for immediate needs.
$sql = 'SELECT s_id,' .' s.smw_namespace AS s_namespace' .', CAST(s.smw_title AS char) AS s_title' //.', CAST(p.smw_title AS char) AS p_title' //.', CAST(o.smw_title AS char) AS o_title' .' FROM' .' (smw_di_wikipage AS r' .' LEFT JOIN smw_object_ids AS s ON r.s_id=s.smw_id)' //.' LEFT JOIN smw_ids AS p ON r.p_id=p.smw_id)' .' LEFT JOIN smw_object_ids AS o ON r.o_id=o.smw_id' ." WHERE (o_id=$idVal) AND (p_id=$idProp);"; $rs = $this->DataSet($sql); if ($rs->HasRows()) { $arOut = array(); while ($rs->NextRow()) { $idPage = $rs->Value('s_id'); $arOut[$idPage] = $rs->Values(); } return $arOut; } else { // TODO: some kind of indication that the property was found but no pages matched return NULL; // nothing found }
}
// NOT TESTED /*
public function GetPropData($iPropName) {
$strPageKey = $this->TitleKey(); $strPropKey = Title::capitalize($iPropName,SMW_NS_PROPERTY); $sqlPageKey = $this->Engine()->SafeParam($strPageKey); $sqlPropKey = $this->Engine()->SafeParam($strPropKey);
$intNSpace = (int)$this->Nspace();
$arTypes = SMWSQLStore3::$di_type_tables;
foreach ($arTypes as $idType => $sTable) { $sql = 'SELECT' .' r.*,' .' CAST(s.smw_title AS char) AS s_title,' .' CAST(p.smw_title AS char) AS p_title,' .' CAST(o.smw_title AS char) AS o_title' .' FROM' ." (($sTable AS r" .' LEFT JOIN smw_object_ids AS s ON r.s_id=s.smw_id)' .' LEFT JOIN smw_object_ids AS p ON r.p_id=p.smw_id)' .' LEFT JOIN smw_object_ids AS o ON r.o_id=o.smw_id' .' WHERE' ." (p.smw_title = '$sqlPropKey') AND" ." (s.smw_title = '$sqlPageKey') AND" ." (s.smw_namespace = $intNSpace);"; $rs = $this->Engine()->DataSet($sql); $arOut[$idType] = $rs; } return $arOut;
}
- /
}
class w3smwPage_version {
protected $osmTitle;
public function SMW_Object() {
//if (!isset($this->osmTitle)) { $this->osmTitle = SMWDIWikiPage::newFromTitle($this->MW_Object()); //} return $this->osmTitle;
}
public function GetPropData($iPropName,$iSep=NULL) {
//$arTypes = SMWSQLStore3::$di_type_tables; $arTypes = SMWSQLStore3::getPropertyTables(); $osmStore = new SMWSQLStore3(); $osmItem = $osmStore->getSemanticData($this->SMW_Object()); // $osmItem is SMWSemanticData $arProps = $osmItem->getProperties();
if (array_key_exists($iPropName,$arProps)) { $osmProp = $arProps[$iPropName]; // $osmProp is a SMWDIProperty $arVals = $osmItem->getPropertyValues($osmProp); $out = NULL; foreach ($arVals as $key => $osmVal) { // $osmVal is an SMWDataItem of appopriate type, e.g. SMWDIBlob if (!is_null($out)) { $out .= $iSep; } //$sSer = $osmVal->getSerialization(); // not all types support getString() $out .= $osmVal->getSortKey(); // THIS IS A KLUGE } return $out; } else { return NULL; // property not found on page } /* foreach ($arTypes as $idType => $osmTable) {
$osmStore->getDataItemHandlerForDIType($idType); /* $sTable = $osmTable->getName(); if (!$osmTable->isFixedPropertyTable()) { // I'm not sure what "fixed property" means, but those seem to be the tables I don't want $sql = "SELECT * FROM $sTable WHERE s_id=$idPage"; $rs = $this->Engine()->DataSet($sql); if ($rs->HasRows()) { echo 'FOUND ONE!'; return $rs; } } }
- /
return NULL; /* $ar = $this->GetPropData_raw($iPropName); $arOut = NULL; foreach ($ar as $type => $arProps) { ArrayMerge($arOut,$arProps); } return $arOut;
- /
} public function GetPropVals($iPropName) {
die('Not yet implemented');
}
/*
protected function GetPropData_raw($iPropName) {
$strPageKey = $this->TitleKey(); $strPropKey = Title::capitalize($iPropName,SMW_NS_PROPERTY); $sqlPageKey = $this->Engine()->SafeParam($strPageKey); $sqlPropKey = $this->Engine()->SafeParam($strPropKey);
$intNSpace = (int)$this->Nspace();
$arTypes = SMWSQLStore3::$di_type_tables;
foreach ($arTypes as $idType => $sTable) { $sql = 'SELECT' .' r.*,' .' CAST(s.smw_title AS char) AS s_title,' .' CAST(p.smw_title AS char) AS p_title,' .' CAST(o.smw_title AS char) AS o_title' .' FROM' ." (($sTable AS r" .' LEFT JOIN smw_object_ids AS s ON r.s_id=s.smw_id)' .' LEFT JOIN smw_object_ids AS p ON r.p_id=p.smw_id)' .' LEFT JOIN smw_object_ids AS o ON r.o_id=o.smw_id' .' WHERE' ." (p.smw_title = '$sqlPropKey') AND" ." (s.smw_title = '$sqlPageKey') AND" ." (s.smw_namespace = $intNSpace);"; $rs = $this->Engine()->DataSet($sql); $arOut[$idType] = $rs; } return $arOut;
}
- /
} </php>