PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/SemanticMediaWiki/includes/SMW_Factbox.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 176 lines | 118 code | 20 blank | 38 comment | 18 complexity | a3b72cbae05cdd61bef6343dd14f042b MD5 | raw file
  1. <?php
  2. /**
  3. * The class in this file provides means of rendering a "Factbox" in articles.
  4. * @file
  5. * @ingroup SMW
  6. * @author Markus Krötzsch
  7. */
  8. /**
  9. * Static class for printing semantic data in a "Factbox".
  10. * @ingroup SMW
  11. */
  12. class SMWFactbox {
  13. /**
  14. * This function creates wiki text suitable for rendering a Factbox for a given
  15. * SMWSemanticData object that holds all relevant data. It also checks whether the
  16. * given setting of $showfactbox requires displaying the given data at all.
  17. */
  18. static public function getFactboxText( SMWSemanticData $semdata, $showfactbox = SMW_FACTBOX_NONEMPTY ) {
  19. global $wgContLang;
  20. wfProfileIn( 'SMWFactbox::printFactbox (SMW)' );
  21. switch ( $showfactbox ) {
  22. case SMW_FACTBOX_HIDDEN: // never show
  23. wfProfileOut( 'SMWFactbox::printFactbox (SMW)' );
  24. return '';
  25. case SMW_FACTBOX_SPECIAL: // show only if there are special properties
  26. if ( !$semdata->hasVisibleSpecialProperties() ) {
  27. wfProfileOut( 'SMWFactbox::printFactbox (SMW)' );
  28. return '';
  29. }
  30. break;
  31. case SMW_FACTBOX_NONEMPTY: // show only if non-empty
  32. if ( !$semdata->hasVisibleProperties() ) {
  33. wfProfileOut( 'SMWFactbox::printFactbox (SMW)' );
  34. return '';
  35. }
  36. break;
  37. // case SMW_FACTBOX_SHOWN: // just show ...
  38. }
  39. // actually build the Factbox text:
  40. $text = '';
  41. if ( wfRunHooks( 'smwShowFactbox', array( &$text, $semdata ) ) ) {
  42. smwfLoadExtensionMessages( 'SemanticMediaWiki' );
  43. $subjectDv = SMWDataValueFactory::newDataItemValue( $semdata->getSubject(), null );
  44. SMWOutputs::requireResource( 'ext.smw.style' );
  45. $rdflink = SMWInfolink::newInternalLink(
  46. wfMsgForContent( 'smw_viewasrdf' ),
  47. $wgContLang->getNsText( NS_SPECIAL ) . ':ExportRDF/' .
  48. $subjectDv->getWikiValue(),
  49. 'rdflink'
  50. );
  51. $browselink = SMWInfolink::newBrowsingLink(
  52. $subjectDv->getText(),
  53. $subjectDv->getWikiValue(),
  54. 'swmfactboxheadbrowse'
  55. );
  56. $text .= '<div class="smwfact">' .
  57. '<span class="smwfactboxhead">' . wfMsgForContent( 'smw_factbox_head', $browselink->getWikiText() ) . '</span>' .
  58. '<span class="smwrdflink">' . $rdflink->getWikiText() . '</span>' .
  59. '<table class="smwfacttable">' . "\n";
  60. foreach ( $semdata->getProperties() as $propertyDi ) {
  61. $propertyDv = SMWDataValueFactory::newDataItemValue( $propertyDi, null );
  62. if ( !$propertyDi->isShown() ) { // showing this is not desired, hide
  63. continue;
  64. } elseif ( $propertyDi->isUserDefined() ) { // user defined property
  65. $propertyDv->setCaption( preg_replace( '/[ ]/u', '&#160;', $propertyDv->getWikiValue(), 2 ) );
  66. /// NOTE: the preg_replace is a slight hack to ensure that the left column does not get too narrow
  67. $text .= '<tr><td class="smwpropname">' . $propertyDv->getLongWikiText( true ) . '</td><td class="smwprops">';
  68. } elseif ( $propertyDv->isVisible() ) { // predefined property
  69. $text .= '<tr><td class="smwspecname">' . $propertyDv->getLongWikiText( true ) . '</td><td class="smwspecs">';
  70. } else { // predefined, internal property
  71. continue;
  72. }
  73. $propvalues = $semdata->getPropertyValues( $propertyDi );
  74. $valuesHtml = array();
  75. foreach ( $propvalues as $dataItem ) {
  76. $dataValue = SMWDataValueFactory::newDataItemValue( $dataItem, $propertyDi );
  77. if ( $dataValue->isValid() ) {
  78. $valuesHtml[] = $dataValue->getLongWikiText( true ) . $dataValue->getInfolinkText( SMW_OUTPUT_WIKI );
  79. }
  80. }
  81. $text .= $GLOBALS['wgLang']->listToText( $valuesHtml );
  82. $text .= '</td></tr>';
  83. }
  84. $text .= '</table></div>';
  85. }
  86. wfProfileOut( 'SMWFactbox::printFactbox (SMW)' );
  87. return $text;
  88. }
  89. /**
  90. * This function creates wiki text suitable for rendering a Factbox based on the
  91. * information found in a given ParserOutput object. If the required custom data
  92. * is not found in the given ParserOutput, then semantic data for the provided Title
  93. * object is retreived from the store.
  94. */
  95. static public function getFactboxTextFromOutput( $parseroutput, $title ) {
  96. global $wgRequest, $smwgShowFactboxEdit, $smwgShowFactbox;
  97. $mws = ( isset( $parseroutput->mSMWMagicWords ) ) ? $parseroutput->mSMWMagicWords : array();
  98. if ( in_array( 'SMW_SHOWFACTBOX', $mws ) ) {
  99. $showfactbox = SMW_FACTBOX_NONEMPTY;
  100. } elseif ( in_array( 'SMW_NOFACTBOX', $mws ) ) {
  101. $showfactbox = SMW_FACTBOX_HIDDEN;
  102. } elseif ( $wgRequest->getCheck( 'wpPreview' ) ) {
  103. $showfactbox = $smwgShowFactboxEdit;
  104. } else {
  105. $showfactbox = $smwgShowFactbox;
  106. }
  107. if ( $showfactbox == SMW_FACTBOX_HIDDEN ) { // use shortcut
  108. return '';
  109. }
  110. // Deal with complete dataset only if needed:
  111. if ( !isset( $parseroutput->mSMWData ) || $parseroutput->mSMWData->stubObject ) {
  112. $semdata = smwfGetStore()->getSemanticData( SMWDIWikiPage::newFromTitle( $title ) );
  113. } else {
  114. $semdata = $parseroutput->mSMWData;
  115. }
  116. return SMWFactbox::getFactboxText( $semdata, $showfactbox );
  117. }
  118. /**
  119. * This hook copies SMW's custom data from the given ParserOutput object to
  120. * the given OutputPage object, since otherwise it is not possible to access
  121. * it later on to build a Factbox.
  122. */
  123. static public function onOutputPageParserOutput( $outputpage, $parseroutput ) {
  124. global $wgTitle, $wgParser;
  125. $factbox = SMWFactbox::getFactboxTextFromOutput( $parseroutput, $wgTitle );
  126. if ( $factbox != '' ) {
  127. $popts = new ParserOptions();
  128. $po = $wgParser->parse( $factbox, $wgTitle, $popts );
  129. $outputpage->mSMWFactboxText = $po->getText();
  130. // do not forget to grab the outputs header items
  131. SMWOutputs::requireFromParserOutput( $po );
  132. SMWOutputs::commitToOutputPage( $outputpage );
  133. } // else: nothing shown, don't even set any text
  134. return true;
  135. }
  136. /**
  137. * This hook is used for inserting the Factbox text directly after the wiki page.
  138. */
  139. static public function onOutputPageBeforeHTML( $outputpage, &$text ) {
  140. if ( isset( $outputpage->mSMWFactboxText ) ) {
  141. $text .= $outputpage->mSMWFactboxText;
  142. }
  143. return true;
  144. }
  145. /**
  146. * This hook is used for inserting the Factbox text after the article contents (including
  147. * categories).
  148. */
  149. static public function onSkinAfterContent( &$data, $skin = null ) {
  150. global $wgOut;
  151. if ( isset( $wgOut->mSMWFactboxText ) ) {
  152. $data .= $wgOut->mSMWFactboxText;
  153. }
  154. return true;
  155. }
  156. }