00001 <?php
00013 class SMWFactbox {
00014
00025 static public function getFactboxText( SMWSemanticData $semdata, $showfactbox = SMW_FACTBOX_NONEMPTY ) {
00026 global $wgContLang;
00027
00028 wfProfileIn( 'SMWFactbox::printFactbox (SMW)' );
00029
00030 switch ( $showfactbox ) {
00031 case SMW_FACTBOX_HIDDEN:
00032 wfProfileOut( 'SMWFactbox::printFactbox (SMW)' );
00033 return '';
00034 case SMW_FACTBOX_SPECIAL:
00035 if ( !$semdata->hasVisibleSpecialProperties() ) {
00036 wfProfileOut( 'SMWFactbox::printFactbox (SMW)' );
00037 return '';
00038 }
00039 break;
00040 case SMW_FACTBOX_NONEMPTY:
00041 if ( !$semdata->hasVisibleProperties() ) {
00042 wfProfileOut( 'SMWFactbox::printFactbox (SMW)' );
00043 return '';
00044 }
00045 break;
00046
00047 }
00048
00049
00050 $text = '';
00051 if ( wfRunHooks( 'smwShowFactbox', array( &$text, $semdata ) ) ) {
00052 $subjectDv = SMWDataValueFactory::newDataItemValue( $semdata->getSubject(), null );
00053
00054 SMWOutputs::requireResource( 'ext.smw.style' );
00055
00056 $rdflink = SMWInfolink::newInternalLink(
00057 wfMsgForContent( 'smw_viewasrdf' ),
00058 $wgContLang->getNsText( NS_SPECIAL ) . ':ExportRDF/' .
00059 $subjectDv->getWikiValue(),
00060 'rdflink'
00061 );
00062
00063 $browselink = SMWInfolink::newBrowsingLink(
00064 $subjectDv->getText(),
00065 $subjectDv->getWikiValue(),
00066 'swmfactboxheadbrowse'
00067 );
00068
00069 $text .= '<div class="smwfact">' .
00070 '<span class="smwfactboxhead">' .
00071 wfMsgForContent( 'smw_factbox_head', $browselink->getWikiText() ) . '</span>' .
00072 '<span class="smwrdflink">' . $rdflink->getWikiText() . '</span>' .
00073 '<table class="smwfacttable">' . "\n";
00074
00075 foreach ( $semdata->getProperties() as $propertyDi ) {
00076 $propertyDv = SMWDataValueFactory::newDataItemValue( $propertyDi, null );
00077 if ( !$propertyDi->isShown() ) {
00078 continue;
00079 } elseif ( $propertyDi->isUserDefined() ) {
00080 $propertyDv->setCaption( preg_replace( '/[ ]/u', ' ', $propertyDv->getWikiValue(), 2 ) );
00082 $text .= '<tr><td class="smwpropname">' . $propertyDv->getShortWikiText( true ) . '</td><td class="smwprops">';
00083 } elseif ( $propertyDv->isVisible() ) {
00084 $text .= '<tr><td class="smwspecname">' . $propertyDv->getShortWikiText( true ) . '</td><td class="smwspecs">';
00085 } else {
00086 continue;
00087 }
00088
00089 $propvalues = $semdata->getPropertyValues( $propertyDi );
00090
00091 $valuesHtml = array();
00092
00093 foreach ( $propvalues as $dataItem ) {
00094 $dataValue = SMWDataValueFactory::newDataItemValue( $dataItem, $propertyDi );
00095
00096 if ( $dataValue->isValid() ) {
00097 $valuesHtml[] = $dataValue->getLongWikiText( true ) .
00098 $dataValue->getInfolinkText( SMW_OUTPUT_WIKI );
00099 }
00100 }
00101
00102 $text .= $GLOBALS['wgLang']->listToText( $valuesHtml );
00103
00104 $text .= '</td></tr>';
00105 }
00106
00107 $text .= '</table></div>';
00108 }
00109
00110 wfProfileOut( 'SMWFactbox::printFactbox (SMW)' );
00111 return $text;
00112 }
00113
00125 static public function getFactboxTextFromOutput( ParserOutput $parseroutput, Title $title ) {
00126 global $wgRequest, $smwgShowFactboxEdit, $smwgShowFactbox;
00127
00128 $mws = isset( $parseroutput->mSMWMagicWords ) ? $parseroutput->mSMWMagicWords : array();
00129
00130 if ( in_array( 'SMW_SHOWFACTBOX', $mws ) ) {
00131 $showfactbox = SMW_FACTBOX_NONEMPTY;
00132 } elseif ( in_array( 'SMW_NOFACTBOX', $mws ) ) {
00133 $showfactbox = SMW_FACTBOX_HIDDEN;
00134 } elseif ( $wgRequest->getCheck( 'wpPreview' ) ) {
00135 $showfactbox = $smwgShowFactboxEdit;
00136 } else {
00137 $showfactbox = $smwgShowFactbox;
00138 }
00139
00140 if ( $showfactbox == SMW_FACTBOX_HIDDEN ) {
00141 return '';
00142 }
00143
00144
00145 if ( !isset( $parseroutput->mSMWData ) || $parseroutput->mSMWData->stubObject ) {
00146 $semdata = smwfGetStore()->getSemanticData( SMWDIWikiPage::newFromTitle( $title ) );
00147 } else {
00148 $semdata = $parseroutput->mSMWData;
00149 }
00150
00151 return SMWFactbox::getFactboxText( $semdata, $showfactbox );
00152 }
00153
00164 static public function onOutputPageParserOutput( OutputPage $outputpage, ParserOutput $parseroutput ) {
00165 global $wgTitle, $wgParser;
00166 $factbox = SMWFactbox::getFactboxTextFromOutput( $parseroutput, $wgTitle );
00167
00168 if ( $factbox !== '' ) {
00169 $popts = new ParserOptions();
00170 $po = $wgParser->parse( $factbox, $wgTitle, $popts );
00171 $outputpage->mSMWFactboxText = $po->getText();
00172
00173 SMWOutputs::requireFromParserOutput( $po );
00174 SMWOutputs::commitToOutputPage( $outputpage );
00175 }
00176
00177 return true;
00178 }
00179
00188 static public function onOutputPageBeforeHTML( OutputPage $outputpage, &$text ) {
00189 if ( isset( $outputpage->mSMWFactboxText ) ) {
00190 $text .= $outputpage->mSMWFactboxText;
00191 }
00192 return true;
00193 }
00194
00204 static public function onSkinAfterContent( &$data, Skin $skin = null ) {
00205 global $wgOut;
00206 if ( isset( $wgOut->mSMWFactboxText ) ) {
00207 $data .= $wgOut->mSMWFactboxText;
00208 }
00209 return true;
00210 }
00211
00212 }