User:Woozle/smw-base.php

Code
<?php /* PURPOSE: Semantic MediaWiki interface classes The existing class library is poorly documented, lacking a stable API, and difficult to use. This class set goes directly to the data structures -- which may change over time, but the changes should be easier to puzzle out than changes to the SMW class library. REQUIRES: data.php USAGE: set SMW_DATA_VERSION to 2 or 3 before including this file TODO: this goes directly to the DatabaseMysql class for some functions; needs to be generalized to use abstract wrappers. HISTORY: 2012-01-22 started 2012-09-17 useful pieces working 2013-01-24 moved clsMWData and clsDataResult_MW from to mw-base.php 2013-02-12 splitting some classes off into v2 and v3 versions in separate files, to handle different SMW data schemas define('WZL_SMW',TRUE);	// flag to let other libraries know these classes are available // is this actually needed?

clsLibMgr::AddClass('clsMWData','mw-base');	// 'mw-base' must be defined by caller

// include the schema-appropriate ancestor class if (SMW_DATA_VERSION > 2) { require('smw-base-v3.php'); } else { require('smw-base-v2.php'); }

class clsSMWData extends clsSMWData_version {

/*     ACTION: Convert title into normalized DB-key format */   static public function NormalizeTitle($iTitle,$iNameSpace) { $strTitle = Sanitizer::decodeCharReferencesAndNormalize($iTitle);	// convert HTML entities $strTitle = Title::capitalize($strTitle,$iNameSpace);			// initial caps, if needed $strTitle = str_replace( ' ', '_',$strTitle);				// convert spaces to underscores return $strTitle; }   /*      ACTION: convert DB-key formatted title into display format Basically, just convert underscores to spaces. */   static public function VisualizeTitle($iTitle) { $strTitle = str_replace('_',' ',$iTitle);				// convert spaces to underscores return $strTitle; }

}

// is this class necessary? class clsDataResult_SMW extends clsDataResult { public function is_okay { }   /*      ACTION: set the record pointer so the first row in the set will be read next */   public function do_rewind { }   /*      ACTION: Fetch the first/next row of data from a result set */   public function get_next { }   /*      ACTION: Return the number of rows in the result set */   public function get_count { }   /*      ACTION: Return whether row currently has data. */   public function is_filled { } }

class w3smwPage extends w3smwPage_version { private $objPageEnv;	// wiki page environment object (from w3tpl) private $mwoTitle;		// MediaWiki Title object, if set

public function __construct(w3tpl_module $iPageEnv) { $this->objPageEnv = $iPageEnv; }   /*      RETURNS: database engine from the wiki page environment */   protected function Engine { return $this->PageEnv->Engine; }   protected function PageEnv { return $this->objPageEnv; }

public function Use_TitleObject(Title $iTitle) { $this->mwoTitle = $iTitle; }   public function MW_Object { return $this->mwoTitle; }   public function Use_GlobalTitle { global $wgTitle; $this->Use_TitleObject($wgTitle); }   public function Use_Title_Named($iName) { $mwoTitle = Title::newFromDBkey($iName); $this->Use_TitleObject($mwoTitle); }   public function Use_Title_Keyed($iName,$iSpace=NS_MAIN) { $mwoTitle = Title::newFromText($iName,$iSpace); $this->Use_TitleObject($mwoTitle); }   public function TitleKey { return $this->mwoTitle->getDBkey; }   public function TitleShown { return $this->mwoTitle->getText; }   public function TitleFull { return $this->mwoTitle->getPrefixedText; }   public function Nspace { return $this->mwoTitle->getNamespace; }   public function PageLink($iText=NULL) { $mwo = $this->mwoTitle; $sTitle = is_null($iText)?$mwo->getFullText:$iText; $url = $mwo->getLinkURL; $out = ''.$sTitle.''; return $out; }   /*      RETURNS: array if multiple values found, otherwise just the value string ASSUMES: smw_sortkey is the non-underscored version of smw_title USAGE: when there's no reason to expect multiple values */   public function GetPropVal($iPropName) { $ar = $this->GetPropVals($iPropName); $cnt = count($ar); if ($cnt > 1) { return $ar; } elseif ($cnt == 1) { return array_shift($ar);	// return just the first element } else { return NULL;	// nothing found }   }    /*      RETURNS: nicely-formatted list of property values with links Sometimes it's just easier to use the existing code. This is just GetProperty_OLD with the no-link option removed. */   public function GetPropLinks($iPropName) { $strPgTitle = $this->TitleKey;

$arArgs = array($strPgTitle,'?'.$iPropName);

// get list of targets (usually just one, but could be more) $htVal = SMWQueryProcessor::getResultFromFunctionParams(	 $arArgs,	  SMW_OUTPUT_FILE,	  SMWQueryProcessor::INLINE_QUERY,	  TRUE);	// treat as if #show (rather than #ask) return $htVal; }   /*      NOTE: This is the old klugey version of GetPropLinks, which I'm keeping here (for now) for posterity. */   public function GetProperty_OLD($iPropName,array $iarOptions=NULL) { $strPgTitle = $this->TitleKey;

$arArgs = array($strPgTitle,'?'.$iPropName);

if (is_array($iarOptions)) { $doLink = NzArray($iarOptions,'link'); } else { $doLink = FALSE; }	if (!$doLink) { $arArgs[] = 'link=none'; // without this, SMW defaults to using links }

// get list of targets (usually just one, but could be more) $htVal = SMWQueryProcessor::getResultFromFunctionParams(	 $arArgs,	  SMW_OUTPUT_FILE,	  SMWQueryProcessor::INLINE_QUERY,	  TRUE);	// treat as if #show (rather than #ask) return $htVal; } }