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...") |
m (5 revisions imported: moving this project here) |
||
(4 intermediate revisions by one other user not shown) | |||
Line 7: | Line 7: | ||
/*==== | /*==== | ||
PURPOSE: version-3-specific class methods for clsSMWData | PURPOSE: version-3-specific class methods for clsSMWData | ||
HISTORY: | |||
2013-02-14 basic functions working | |||
*/ | */ | ||
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 { | |||
public function SMW_Object() { | |||
return SMWDIWikiPage::newFromTitle($this->MW_Object()); | |||
} | |||
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 => $osmItem) { | |||
// $osmItem is an SMWDataItem of appopriate type, e.g. SMWDIBlob | |||
if (!is_null($out)) { | |||
$out .= $iSep; | |||
} | |||
//$sSer = $osmVal->getSerialization(); // not all types support getString() | |||
$osmVal = SMWDataValueFactory::newDataItemValue($osmItem); // $osmVal is a SMWDataValue | |||
//$out .= $osmItem->getSortKey(); // THIS IS A KLUGE | |||
//$out .= $osmVal->getLongHTMLText(); // works, but shortens long text with ellipses | |||
$out .= $osmVal->getWikiValue(); // this might be wikitext; needs testing | |||
} | |||
return $out; | |||
} else { | |||
return NULL; // property not found on page | |||
} | |||
} | |||
public function GetPropVals($iPropName) { | |||
die('Not yet implemented'); | |||
} | } | ||
} | } | ||
</php> | </php> |
Latest revision as of 16:42, 22 May 2022
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 HISTORY: 2013-02-14 basic functions working
- /
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 {
public function SMW_Object() {
return SMWDIWikiPage::newFromTitle($this->MW_Object());
}
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 => $osmItem) { // $osmItem is an SMWDataItem of appopriate type, e.g. SMWDIBlob if (!is_null($out)) { $out .= $iSep; } //$sSer = $osmVal->getSerialization(); // not all types support getString() $osmVal = SMWDataValueFactory::newDataItemValue($osmItem); // $osmVal is a SMWDataValue //$out .= $osmItem->getSortKey(); // THIS IS A KLUGE //$out .= $osmVal->getLongHTMLText(); // works, but shortens long text with ellipses $out .= $osmVal->getWikiValue(); // this might be wikitext; needs testing } return $out; } else { return NULL; // property not found on page }
} public function GetPropVals($iPropName) {
die('Not yet implemented');
}
} </php>