Mwjames has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/60420


Change subject: Follow-up, use set/getExtensionData() for MW 1.21+ in SMWFactbox
......................................................................

Follow-up, use set/getExtensionData() for MW 1.21+ in SMWFactbox

This follows [1], SMW\ParserTextProcessor uses set/getExtensionData()
for MW 1.21+ and for all others use mSMWMagicWords

[1] https://gerrit.wikimedia.org/r/#/c/60393

Change-Id: I4694e7b010e06e72dc53161cc9139f40e5bffd4f
---
M SemanticMediaWiki.hooks.php
M includes/SMW_Factbox.php
M includes/SMW_ParseData.php
A tests/phpunit/includes/FactboxTest.php
4 files changed, 235 insertions(+), 27 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SemanticMediaWiki 
refs/changes/20/60420/1

diff --git a/SemanticMediaWiki.hooks.php b/SemanticMediaWiki.hooks.php
index be0f613..e282a0e 100644
--- a/SemanticMediaWiki.hooks.php
+++ b/SemanticMediaWiki.hooks.php
@@ -241,6 +241,7 @@
                        'DataValueFactory',
                        'Settings',
                        'ParserTextProcessor',
+                       'Factbox',
 
                        'api/ApiSMWInfo',
 
diff --git a/includes/SMW_Factbox.php b/includes/SMW_Factbox.php
index caaabda..07f96e5 100644
--- a/includes/SMW_Factbox.php
+++ b/includes/SMW_Factbox.php
@@ -16,17 +16,17 @@
         * This function creates wiki text suitable for rendering a Factbox for 
a given
         * SMWSemanticData object that holds all relevant data. It also checks 
whether the
         * given setting of $showfactbox requires displaying the given data at 
all.
-        * 
+        *
         * @param SMWSemanticData $semdata
         * @param boolean $showfactbox
-        * 
+        *
         * @return string
         */
        static public function getFactboxText( SMWSemanticData $semdata, 
$showfactbox = SMW_FACTBOX_NONEMPTY ) {
                global $wgContLang;
-               
+
                wfProfileIn( 'SMWFactbox::printFactbox (SMW)' );
-               
+
                switch ( $showfactbox ) {
                        case SMW_FACTBOX_HIDDEN: // never show
                                wfProfileOut( 'SMWFactbox::printFactbox (SMW)' 
);
@@ -50,9 +50,9 @@
                $text = '';
                if ( wfRunHooks( 'smwShowFactbox', array( &$text, $semdata ) ) 
) {
                        $subjectDv = SMWDataValueFactory::newDataItemValue( 
$semdata->getSubject(), null );
-                       
+
                        SMWOutputs::requireResource( 'ext.smw.style' );
-                       
+
                        $rdflink = SMWInfolink::newInternalLink(
                                wfMessage( 'smw_viewasrdf' 
)->inContentLanguage()->text(),
                                $wgContLang->getNsText( NS_SPECIAL ) . 
':ExportRDF/' .
@@ -65,7 +65,7 @@
                                $subjectDv->getWikiValue(),
                                'swmfactboxheadbrowse'
                        );
-                       
+
                        $text .= '<div class="smwfact">' .
                                '<span class="smwfactboxhead">' .
                                wfMessage( 'smw_factbox_head', 
$browselink->getWikiText() )->inContentLanguage()->text() . '</span>' .
@@ -116,17 +116,23 @@
         * 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.
-        * 
+        *
         * @param ParserOutput $parseroutput
         * @param Title $title
-        * 
+        *
         * @return string
         */
        static public function getFactboxTextFromOutput( ParserOutput 
$parseroutput, Title $title ) {
                global $wgRequest, $smwgShowFactboxEdit, $smwgShowFactbox;
-               
-               $mws = isset( $parseroutput->mSMWMagicWords ) ? 
$parseroutput->mSMWMagicWords : array();
-               
+
+               // Prior MW 1.21 use mSMWMagicWords (see 
SMW\ParserTextProcessor)
+               if ( method_exists( $parseroutput, 'getExtensionData' ) ) {
+                       $mws = $parseroutput->getExtensionData( 'smwmagicwords' 
);
+                       $mws = isset( $mws ) ? $mws : array();
+               } else {
+                       $mws = isset( $parseroutput->mSMWMagicWords ) ? 
$parseroutput->mSMWMagicWords : array();
+               }
+
                if ( in_array( 'SMW_SHOWFACTBOX', $mws ) ) {
                        $showfactbox = SMW_FACTBOX_NONEMPTY;
                } elseif ( in_array( 'SMW_NOFACTBOX', $mws ) ) {
@@ -140,14 +146,14 @@
                if ( $showfactbox == SMW_FACTBOX_HIDDEN ) { // use shortcut
                        return '';
                }
-               
+
                // Deal with complete dataset only if needed:
                $smwData = SMWParseData::getSMWDataFromParserOutput( 
$parseroutput );
 
                if ( $smwData === null || $smwData->stubObject ) {
                        $smwData = smwfGetStore()->getSemanticData( 
SMWDIWikiPage::newFromTitle( $title ) );
                }
-               
+
                return SMWFactbox::getFactboxText( $smwData, $showfactbox );
        }
 
@@ -155,16 +161,16 @@
         * 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 Factbox.
-        * 
+        *
         * @param OutputPage $outputpage
         * @param ParserOutput $parseroutput
-        * 
+        *
         * @return true
         */
        static public function onOutputPageParserOutput( OutputPage 
$outputpage, ParserOutput $parseroutput ) {
                global $wgParser;
                $factbox = SMWFactbox::getFactboxTextFromOutput( $parseroutput, 
$outputpage->getTitle() );
-               
+
                if ( $factbox !== '' ) {
                        $popts = new ParserOptions();
                        $po = $wgParser->parse( $factbox, 
$outputpage->getTitle(), $popts );
@@ -173,16 +179,16 @@
                        SMWOutputs::requireFromParserOutput( $po );
                        SMWOutputs::commitToOutputPage( $outputpage );
                } // else: nothing shown, don't even set any text
-               
+
                return true;
        }
 
        /**
         * This hook is used for inserting the Factbox text directly after the 
wiki page.
-        * 
+        *
         * @param OutputPage $outputpage
         * @param string $text
-        * 
+        *
         * @return true
         */
        static public function onOutputPageBeforeHTML( OutputPage $outputpage, 
&$text ) {
@@ -195,10 +201,10 @@
        /**
         * This hook is used for inserting the Factbox text after the article 
contents (including
         * categories).
-        * 
+        *
         * @param string $data
         * @param Skin|null $skin
-        * 
+        *
         * @return true
         */
        static public function onSkinAfterContent( &$data, Skin $skin = null ) {
diff --git a/includes/SMW_ParseData.php b/includes/SMW_ParseData.php
index c35053f..3898cd6 100644
--- a/includes/SMW_ParseData.php
+++ b/includes/SMW_ParseData.php
@@ -41,7 +41,12 @@
                }
 
                $output = $parser->getOutput();
-               $output->mSMWMagicWords = $words;
+
+               if ( method_exists( $output, 'setExtensionData' ) ) {
+                       $output->setExtensionData( 'smwmagicwords', $words );
+               } else {
+                       $output->mSMWMagicWords = $words;
+               }
 
                return $words;
        }
@@ -77,8 +82,8 @@
         * @return SMWSemanticData|null
         */
        public static function getSMWDataFromParserOutput( ParserOutput 
$output, Title $title = null ) {
-               if ( method_exists( $output, 'getAdditionalData' ) ) {
-                       $smwData = $output->getAdditionalData( 'smwdata' );
+               if ( method_exists( $output, 'getExtensionData' ) ) {
+                       $smwData = $output->getExtensionData( 'smwdata' );
                } elseif ( isset( $output->mSMWData ) ) {
                        $smwData = $output->mSMWData;
                }
@@ -104,8 +109,8 @@
         * @param SMWSemanticData $smwData
         */
        public static function setSMWData( ParserOutput $output, 
SMWSemanticData $smwData ) {
-               if ( method_exists( $output, 'getAdditionalData' ) ) {
-                       $output->setAdditionalData( 'smwdata', $smwData );
+               if ( method_exists( $output, 'setExtensionData' ) ) {
+                       $output->setExtensionData( 'smwdata', $smwData );
                }
                else {
                        $output->mSMWData = $smwData;
diff --git a/tests/phpunit/includes/FactboxTest.php 
b/tests/phpunit/includes/FactboxTest.php
new file mode 100644
index 0000000..7856ddb
--- /dev/null
+++ b/tests/phpunit/includes/FactboxTest.php
@@ -0,0 +1,196 @@
+<?php
+
+namespace SMW\Test;
+
+use SMWFactbox;
+use SMW\ParserData;
+use SMW\Settings;
+use SMW\ParserTextProcessor;
+
+use Title;
+use ParserOutput;
+
+/**
+ * Tests for the SMWFactbox class
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 1.9
+ *
+ * @file
+ * @ingroup SMW
+ * @ingroup SMWParser
+ * @ingroup Test
+ *
+ * @group SMW
+ * @group SMWExtension
+ *
+ * @licence GNU GPL v2+
+ * @author mwjames
+ */
+
+/**
+ * Testing methods provided by the SMWFactbox class
+ *
+ * @ingroup SMW
+ */
+class FactboxTest extends \MediaWikiTestCase {
+
+       protected $className = 'SMWFactbox';
+
+       /**
+        * Provides magic words sample text
+        *
+        * @return array
+        */
+       public function getMagicWordDataProvider() {
+               return array(
+                       // #0 __NOFACTBOX__, no factbox output
+                       array(
+                               NS_MAIN,
+                               array(
+                                       'smwgNamespacesWithSemanticLinks' => 
array( NS_MAIN => true ),
+                                       'smwgLinksInValues' => false,
+                                       'smwgInlineErrors' => true,
+                               ),
+                               'Lorem ipsum dolor sit amet consectetuer auctor 
at quis' .
+                               ' [[Foo::dictumst cursus]]. Nisl sit 
condimentum Quisque facilisis' .
+                               ' Suspendisse [[Bar::tincidunt semper]] 
facilisi dolor Aenean. Ut' .
+                               ' __NOFACTBOX__ ',
+                               array(
+                                       'magicWords' => array( 'SMW_NOFACTBOX' 
),
+                                       'textOutput' => ''
+                               )
+                       ),
+
+                       // #1 __SHOWFACTBOX__, factbox output
+                       array(
+                               NS_HELP,
+                               array(
+                                       'smwgNamespacesWithSemanticLinks' => 
array( NS_HELP => true ),
+                                       'smwgLinksInValues' => false,
+                                       'smwgInlineErrors' => true,
+                               ),
+                               'Lorem ipsum dolor sit amet consectetuer auctor 
at quis' .
+                               ' [[Foo::dictumst cursus]]. Nisl sit 
condimentum Quisque facilisis' .
+                               ' Suspendisse [[Bar::tincidunt semper]] 
facilisi dolor Aenean. Ut' .
+                               ' __SHOWFACTBOX__',
+                               array(
+                                       'magicWords' => array( 
'SMW_SHOWFACTBOX' ),
+                                       'textOutput' => 'smwfactboxhead' // 
lazy check because we use assertContains
+                               )
+                       ),
+               );
+       }
+
+       /**
+        * Helper method that returns a random string
+        *
+        * @since 1.9
+        *
+        * @param $length
+        *
+        * @return string
+        */
+       private function getRandomString( $length = 10 ) {
+               return substr( str_shuffle( 
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ), 0, $length 
);
+       }
+
+       /**
+        * Helper method that returns a Title object
+        *
+        * @param $namespace
+        *
+        * @return Title
+        */
+       private function getTitle( $namespace = NS_MAIN ){
+               return Title::newFromText( $this->getRandomString(), $namespace 
);
+       }
+
+       /**
+        * Helper method that returns a ParserOutput object
+        *
+        * @return ParserOutput
+        */
+       private function getParserOutput(){
+               return new ParserOutput();
+       }
+
+       /**
+        * Helper method that returns a ParserData object
+        *
+        * @param $title
+        * @param $parserOutput
+        *
+        * @return ParserData
+        */
+       private function getParserData( Title $title, ParserOutput 
$parserOutput ){
+               return new ParserData( $title, $parserOutput );
+       }
+
+       /**
+        * Helper method that returns a ParserTextProcessor object
+        *
+        * @param $title
+        * @param $parserOutput
+        * @param $settings
+        *
+        * @return ParserTextProcessor
+        */
+       private function getInstance( Title $title, ParserOutput $parserOutput, 
array $settings = array() ) {
+               return new ParserTextProcessor(
+                       $this->getParserData( $title, $parserOutput ),
+                       Settings::newFromArray( $settings )
+               );
+       }
+
+       /**
+        * @test SMWFactbox::getFactboxTextFromOutput
+        * @dataProvider getMagicWordDataProvider
+        *
+        * @since 1.9
+        *
+        * @param $namespace
+        * @param $settings
+        * @param $text
+        * @param $expected
+        */
+       public function testGetFactboxTextFromOutput( $namespace, array 
$settings, $text, array $expected ) {
+               $parserOutput =  $this->getParserOutput();
+               $title = $this->getTitle( $namespace );
+               $textProcessor = $this->getInstance( $title, $parserOutput, 
$settings );
+
+               // Use the text processor to add text
+               $textProcessor->parse( $text );
+
+               // Check the magic words stripped and added by the text 
processor
+               if ( method_exists( $parserOutput, 'getExtensionData' ) ) {
+                       $this->assertEquals( $expected['magicWords'], 
$parserOutput->getExtensionData( 'smwmagicwords' ) );
+               } else {
+                       $this->assertEquals( $expected['magicWords'], 
$parserOutput->mSMWMagicWords );
+               }
+
+               $result = SMWFactbox::getFactboxTextFromOutput( $parserOutput, 
$title );
+               $this->assertInternalType( 'string', $result );
+
+               // Doing a lazy sanity check on the result
+               if ( $expected['textOutput'] !== '' ) {
+                       $this->assertContains( $expected['textOutput'], $result 
);
+               } else {
+                       $this->assertEmpty( $result );
+               }
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/60420
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4694e7b010e06e72dc53161cc9139f40e5bffd4f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SemanticMediaWiki
Gerrit-Branch: master
Gerrit-Owner: Mwjames <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to