W3TPL/archive/code/smw-links.php

From Woozle Writes Code
< W3TPL‎ | archive‎ | code
Revision as of 19:35, 11 February 2013 by Woozle (talk | contribs) (Created page with "==History== * '''2013-02-11''' partially-working version, so I have a record of the commented-out code I'm about to hack off ==Code== <php> <?php /* PURPOSE: SMW listing man...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

History

  • 2013-02-11 partially-working version, so I have a record of the commented-out code I'm about to hack off

Code

<php> <?php /*

 PURPOSE: SMW listing management functions for w3tpl
 REQUIRES: filed-links.php
 HISTORY:
   2011-10-16 w3tpl code started to get too ugly, so pushing out some functionality into callable modules.
   2012-01-24 split off SMW stuff from filed-links.php to smw-links.php
  • /

//require(kfpMWLib.'/smw/smw-base.php'); if (!defined('WZL_SMW')) { die(__FILE__.' requires smw-base.php.'); }

new w3tpl_module_SMWLinks(); // class will self-register

class w3tpl_module_SMWLinks extends w3tpl_module {

   // TAG-CALLABLE FUNCTIONS
   /*----
     INPUT:

site: name of site whose users should be listed filt: additional SMW terms to include in the filter

     USAGE:

example: <exec module=smw-links func=SiteUserListing_with_xrefs site="Google+" />

   */
   public function w3f_SiteUserListing_with_xrefs(array $iArgs) {

$strSite = $iArgs['site']; $strFilt = NzArray($iArgs,'filt'); $smwFilt = "thing type::site-accountsite::$strSite$strFilt";

$dbr =& wfGetDB( DB_SLAVE ); $db = new clsSMWData($dbr);

$arFilt = array( 'PropName' => '="Thing type"', 'Value' => '="site-account"', ); $rs = $db->GetPages_forProps($arFilt);

$out = 'blah'; while ($rs->NextRow()) {

$out .= '

'.print_r($rs->Values(),TRUE).'

';

} echo 'got to here'; return $out;

   }
   /*----
     INPUT:

site: name of site whose users should be listed filt: additional SMW terms to include in the filter

     THIS IS THE OLD VERSION. Delete it when the new one is working.
   */
   public function w3f_SiteUserListing_with_xrefs_OLD(array $iArgs) {

$strSite = $iArgs['site']; $strFilt = NzArray($iArgs,'filt'); $smwFilt = "thing type::site-accountsite::$strSite$strFilt";

// there's probably a more direct way to pass the column names so they don't need "?"s... working on that. $arCols = array('?userid','?username','?employer','?job project','?job title','?legal name', '?note'); $smwRes = $this->SMW_Query($smwFilt,$arCols); // returns SMWQueryResult

$out = ;

$arPR = $smwRes->getPrintRequests();

foreach ($arPR as $smwReq) { $out .= '['.$smwReq->getLabel().']='.$smwReq->getHTMLText().' / ';

//$out .= '

'.print_r($smwReq,TRUE).'

';

} /* works as far as it goes, but no Page info while ( $row = $smwRes->getNext() ) { // returns array of SMWResultArray or false foreach ( $row as $field ) { // $field is SMWResultArray (see SMW_QueryResult.php) $objCol = $field->getPrintRequest(); $strKey = $objCol->getLabel(); $out .= '['.$strKey.']'; } }

  • /

/* TAKE 2 while ($smwRow = $smwRes->getNext()) { // $smwRow is SMWResultArray $out .= '*'; if (is_object($smwRow)) {

$out .= '

'.print_r($smwRow,TRUE).'

';

while ($smwItem = $smwRow->getNextDataItem()) { // $smwItem is SMWDataItem $objItem = new w3smwDataItem($smwItem);

$smwData = $objItem->getDataValue(); $htVal = $smwData-> getShortText(SMW_OUTPUT_HTML); $out .= '/'.$htVal;

} } }

  • /

/* TAKE 1 $arData = $this->SMW_Query_asArray($smwFilt,$arCols,'userid');

// set up for user xref queries $arRefCols = array('?name','?page','?URL');

$out = '

';

foreach ($arData as $idx => $row) {

echo '
'.print_r($arData,TRUE).'
'; die();

//$arPage = $row[NULL]; $arUser = $row['Username']; $arName = $row['Legal name']; $arEmployer = $row['Employer']; $arJobProj = $row['Job project']; $arJobTitle = $row['Job title']; $arNotes = $row['Note'];

$strUser = implode(',',array_values($arUser)); $strID = implode(',',$row['Userid']);

if (is_array($arName)) { $strName = implode(',',$arName); $htName = ' ('.$strName.')'; } else { $htName = NULL; }

$htEmployer = ArrayToString($arEmployer); $htJobProj = ArrayToString($arJobProj); $htJobTitle = ArrayToString($arJobTitle); $htJob = NULL; if ($htEmployer) { $htJob = $htEmployer; } if ($htJobProj) { if ($htJob) { $htJob .= ': '; } $htJob .= ''.$htJobProj.''; } if ($htJobTitle) { $htJob .= ' '.$htJobTitle.''; }

$out .= "\n"; $htRefs = NULL; if (is_array($arPage)) { $strPage = $arPage[0]; // we're going to blatantly assume there's only one title in the array // If I understand right, there's logically no way there *could* be multiple titles for this kind of query. $objTitle = Title::newFromText($strPage); $htLink = $objTitle->getFullURL(); $out .= ''; $out .= ''; } else { $out .= '';

}

// "About" column

$out .= ''; // "More info" column if (!is_null($htRefs)) { $out .= '';

}

$out .= ''; //$out .= $idx.'
'.print_r($arName,TRUE).'
';

}

$out .= "\n
NameAboutMore Information
<a href="'.$htLink.'" title="data for '.$strUser.'">#</a><a href="https://plus.google.com/'.$strID.'" title="'.$strUser."'s $strSite user page".'">'

.$strUser.'</a>'.$htName;

// get additional info for this user

$arData = $this->SMW_Query_asArray($smwFilt,$arCols,'userid');

// check for any user xref links $smwRefFilt = "page type::linkabout::$strPage"; $arRefData = $this->SMW_Query_asArray($smwRefFilt,$arRefCols,'userid');

//$out .= '
'.print_r($arRefData,TRUE).'
';

if (is_array($arRefData)) { foreach ($arRefData as $idx => $data) { $strLTitle = $data[NULL][0]; $strLName = $data['Name'][0]; $strLPage = $data['Page'][0]; $strLURL = $data['URL'][0]; $htLinkRef = Link_forURLorPage($strLURL,$strLPage); $htLTitle = Link_forPage($strLTitle);

$htRefs .= '<a href="'.$htLinkRef.'" title="'.$strLName.' link for '.$strUser.'">'.$strLName.'</a>' .'<a href="'.$htLTitle.'" title="local data for the reference">#</a>'; } }

$out .= '
Data unexpectedly missing...'.$htJob;

if (is_array($arNotes)) {

$out .= '
    '; foreach ($arNotes as $note) { $out .= '
  • '.$note.'
  • '; } $out .= '
';

}

$out .= '
'.$htRefs.'

"; //$out = '

'.print_r($arData,TRUE).'

';

  • /

return $out;

   }
   // SUB-FUNCTIONS
   /*----
     INPUT:m

filt: filter string formatted in {{#ask:}} syntax property::value, etc. cols: list of columns to retrieve in \prefix\demarcated\string format

     RETURNS: SMWResultArray
   */
   protected function SMW_Query($iFilt,array $arCols) {

/* $strAsk = $iArgs['filt']; // e.g. prop::value $strCols = $iArgs['cols']; // \list\of\column\names $xtCols = new xtString($strCols); $arCols = $xtCols->Xplode();

  • /

$strAsk = $iFilt;

// $out = NULL;

// $out = '

'.print_r($arCols,TRUE).'

';

$strQry = NULL; $arPrint = NULL; SMWQueryProcessor::processFunctionParams($arCols,$strQry,$arParams,$arPrint); // each PrintRequest is approximately equivalent to a column without data // OUTPUT: // $arParams -- not sure; may be incomplete // $arPrint contains an array of PrintRequest objects

// $out .= 'params:

'.print_r($arParams,TRUE).'

'; // $out .= 'print:

'.print_r($arPrint,TRUE).'

';

// $out .= 'strQry:['.$strQry.']';

/* if (!array_key_exists('format',$arParams)) {

echo 'cols:

'.print_r($arCols,TRUE).'

'; echo 'print:

'.print_r($arPrint,TRUE).'

'; echo 'params:

'.print_r($arParams,TRUE).'

';

   throw new exception('Got to here');

}

  • /

$arParams['order'] = array(); $arParams['sort'] = array(); $arParams['mainlabel'] = 'The Main Label'; $arParams['format'] = 'template';

$smwQry = SMWQueryProcessor::createQuery($strAsk,$arParams); // createQuery( $querystring, array $params, $context = SMWQueryProcessor::INLINE_QUERY, $format = , $extraprintouts = array() ) $smwQry->setExtraPrintouts( $arPrint ); // $objQry = SMWQueryProcessor::createQuery($strAsk,$arCols); $smwStore = smwfGetStore(); // default store (what are the options?) $res = $smwStore->getQueryResult($smwQry); // returns SMWQueryResult

//echo '

'.print_r($res,TRUE).'

';

return $res;

/* Leaving this here for now as sample code

// $out .= '

'; while ( $row = $res->getNext() ) { // returns array of SMWResultArray or false // $out .= "\n"; foreach ( $row as $field ) { // $field is SMWResultArray (see SMW_QueryResult.php) // $out .= ""; } // $out .= ''; } // $out .= '
";

$objCol = $field->getPrintRequest(); $out .= '
'.$objCol->getLabel().' =';

//$out .= '
'.print_r($objCol,TRUE).'
';

// a property can have multiple values for a given page -- usually just one, but there may be more while ( ( $obj = $field->getNextObject() ) !== false ) { // $obj is SMWWikiPageValue $htVal = $obj->getLongText(SMW_OUTPUT_HTML); $out .= ' ['.$htVal.'] ';

// $out .= '
'.print_r($obj,TRUE).'
';

}

// $out .= "

';

  • /
   }
   /*----
     RETURNS: same data as SMW_Query(), but formatted in an array[key]=array{values...}
   */
   protected function SMW_Query_asArray($iFilt,array $arCols,$iKeyCol) {

$objRes = $this->SMW_Query($iFilt,$arCols);

$arOut = NULL; while ( $row = $objRes->getNext() ) { // returns array of SMWResultArray or false $arRow = array(); $strIdx = NULL; foreach ( $row as $field ) { // $field is SMWResultArray (see SMW_QueryResult.php) $objCol = $field->getPrintRequest(); $strKey = $objCol->getLabel();

// a property can have multiple values for a given page -- usually just one, but there may be more $arVals = NULL; while ( ( $obj = $field->getNextObject() ) !== false ) { // $obj is SMWWikiPageValue $htVal = $obj->getLongText(SMW_OUTPUT_HTML); $arVals[] = $htVal; } $arRow[$strKey] = $arVals; if ($strKey == $iKeyCol) { $strIdx = $htVal; } } if (is_null($strIdx)) { $arOut[] = $arRow; } else { $arOut[$strIdx] = $arRow; } } return $arOut;

   }

}

// HELPER CLASSES

/*

 PURPOSE: extends functionality of smwDataItem
   ...which currently (no longer?) exists
  • /

class w3smwDataItem {

   protected $smwObj;
   public function __construct(smwDataItem $iItem) {

$this->smwObj = $iItem;

   }
   public function Object() {

return $this->smwObj;

   }
   /*----
     BASED ON: smwDataItem::getNextDataValue()
   */
   public function getDataValue() {

$obj = $this->Object(); if ( $obj === false ) { return false; } if ( $obj->mPrintRequest->getMode() == SMWPrintRequest::PRINT_PROP ) { $diProperty = $obj->mPrintRequest->getData()->getDataItem(); } else { $diProperty = null; } $dv = SMWDataValueFactory::newDataItemValue( $obj, $diProperty ); if ( $obj->mPrintRequest->getOutputFormat() ) { $dv->setOutputFormat( $obj->mPrintRequest->getOutputFormat() ); } return $dv;

   }

} </php>