Extension:Factscript

This extension works much like the SMW Factbox extension, except the output is all javascript.

When triggered, the javascript object "smw_properties" is created. The properties of the object match the SMW property names, and the values match the values. When a property contains more than one value, the object is an array:

 var smw_properties = { "Book" : "Shades of Hope", "Chapter" : "unknown", "Hero" : [ "Abigail", "Thorak", "Inteus" ],   "Location" : "Farlourn", "Title" : "A Helping Hand" };

todo
Better type awareness
 * Numbers without quotes
 * dates as "new Date(...)"

Better installation instructions

Translations from "factbox" to "factscript"

installation
1. Create a new file "$IP/extensions/SemanticMediaWiki/includes/Factscript.php" by copy-and-paste from the "source", listed below

2. wire reference to the new code
 * Do a file-content search in $IP/extensions/SemanticMeidaWiki for "factbox"
 * duplicate each line with "factbox" in it, and change "factbox" to "factscript" (honor case conventions: eg use __NOFACTSCRIPT__ instead of __NOFACTscript__)

3. Test it to see if you found all the factbox references

source
<?php /** * The class in this file provides means of rendering a "Factscript" in articles. * @file * @ingroup SMW * @author Earl Brown, adapted from Factbox by Markus Krötzsch */

/** * Static class for printing semantic data in a "Factscript". * @ingroup SMW */ class SMWFactscript { /**  * This function creates wiki text suitable for rendering a Factscript for a given * SMWSemanticData object that holds all relevant data. It also checks whether the * given setting of $showfactscript requires displaying the given data at all. */ static public function getFactscriptText( SMWSemanticData $semdata, $showfactscript = SMW_FACTSCRIPT_SHOWN ) { global $wgContLang; wfProfileIn( "SMWFactscript::printFactscript (SMW)" ); $text = ''; switch ( $showfactscript ) { case SMW_FACTSCRIPT_HIDDEN: // show never wfProfileOut( "SMWFactscript::printFactscript (SMW)" ); return $text; case SMW_FACTSCRIPT_SPECIAL: // show only if there are special properties if ( !$semdata->hasVisibleSpecialProperties ) { wfProfileOut( "SMWFactscript::printFactscript (SMW)" ); return ''; }     break; case SMW_FACTSCRIPT_NONEMPTY: // show only if non-empty if ( !$semdata->hasVisibleProperties ) { wfProfileOut( "SMWFactscript::printFactscript (SMW)" ); return ''; }     break; case SMW_FACTSCRIPT_SHOWN: // just show ... }

// actually build the Factscript text: if ( wfRunHooks( 'smwShowFactscript', array( &$text, $semdata ) ) ) { smwfLoadExtensionMessages( 'SemanticMediaWiki' );

$props = $semdata->getProperties; $lprops = count($props); if ($lprops < 1) return $text; $text .= '  ';     $text .= 'var smw_properties = {'; $iprops = 0; foreach ( $props as $property ) { $iprops += 1; if ( !$property->isShown ) { // showing this is not desired, hide continue; } elseif (( $property->isUserDefined ) || ($property->isVisible)) { // user defined property $text .= ' "' . $property->getWikiValue . '" : '; } else { // predefined, internal property continue; }

$propvalues = $semdata->getPropertyValues( $property ); $l = count( $propvalues ); if ($l < 1) continue; if ($l > 1) $text .= '[ ';       $i = 0; foreach ( $propvalues as $propvalue ) { $i += 1; $text .= '"' . $propvalue->getWikiValue . '"'; if ( $i < $l ) { $text .= ', ';         }        }        if ($l > 1) $text .= ' ]';       if ($iprops < $lprops) $text .= ','; }     $text .= ' }; ';   }    wfProfileOut( "SMWFactscript::printFactscript (SMW)" ); return $text; }

/**  * This function creates wiki text suitable for rendering a Factscript based on the * information found in a given ParserOutput object. If the required custom data * is not found in the given ParserOutput, then semantic data for the provided Title * object is retreived from the store. */ static public function getFactscriptTextFromOutput( $parseroutput, $title ) { global $wgRequest, $smwgShowFactscriptEdit, $smwgShowFactscript; $mws = ( isset( $parseroutput->mSMWMagicWords ) ) ? $parseroutput->mSMWMagicWords:array; if ( in_array( 'SMW_SHOWFACTSCRIPT', $mws ) ) { $showfactscript = SMW_FACTSCRIPT_NONEMPTY; } elseif ( in_array( 'SMW_NOFACTSCRIPT', $mws ) ) { $showfactscript = SMW_FACTSCRIPT_HIDDEN; } elseif ( $wgRequest->getCheck( 'wpPreview' ) ) { $showfactscript = $smwgShowFactscriptEdit; } else { $showfactscript = $smwgShowFactscript; }   if ( $showfactscript == SMW_FACTSCRIPT_HIDDEN ) { // use shortcut return ''; }   // Deal with complete dataset only if needed: if ( !isset( $parseroutput->mSMWData ) || $parseroutput->mSMWData->stubObject ) { $semdata = smwfGetStore->getSemanticData( $title ); } else { $semdata = $parseroutput->mSMWData; }   return SMWFactscript::getFactscriptText( $semdata, $showfactscript ); }

/**  * This hook copies SMW's custom data from the given ParserOutput object to   * the given OutputPage object, since otherwise it is not possible to access * it later on to build a Factscript. */ static public function onOutputPageParserOutput( $outputpage, $parseroutput ) { global $wgTitle, $wgParser; $factscript = SMWFactscript::getFactscriptTextFromOutput( $parseroutput, $wgTitle ); if ( $factscript != '' ) { $outputpage->mSMWFactscriptText = $factscript;		// $po->getText; SMWOutputs::commitToOutputPage( $outputpage ); } // else: nothing shown, don't even set any text return true; }

/**  * This hook is used for inserting the Factscript text directly after the wiki page. */ static public function onOutputPageBeforeHTML( $outputpage, &$text ) { if ( isset( $outputpage->mSMWFactscriptText ) ) { $text .= $outputpage->mSMWFactscriptText; }   return true; }

/**  * This hook is used for inserting the Factscript text after the article contents (including   * categories). */ static public function onSkinAfterContent( &$data ) { global $wgOut; if ( isset( $wgOut->mSMWFactscriptText ) ) { $data .= $wgOut->mSMWFactscriptText; }   return true; } }