From bfdd2f1022634904eddfcb685069e2f0394ee242 Mon Sep 17 00:00:00 2001 From: mwjames Date: Sat, 15 Apr 2017 22:56:40 +0000 Subject: [PATCH] Add HtmlTable, refs 2387 (#2409) --- .../Specials/Browse/ContentsBuilder.php | 110 +++++++++---- src/MediaWiki/Specials/Browse/FormHelper.php | 2 +- src/Utils/HtmlTable.php | 145 ++++++++++++++++++ .../Specials/Browse/FormHelperTest.php | 2 +- tests/phpunit/Unit/Utils/HtmlTableTest.php | 71 +++++++++ 5 files changed, 296 insertions(+), 34 deletions(-) create mode 100644 src/Utils/HtmlTable.php create mode 100644 tests/phpunit/Unit/Utils/HtmlTableTest.php diff --git a/src/MediaWiki/Specials/Browse/ContentsBuilder.php b/src/MediaWiki/Specials/Browse/ContentsBuilder.php index c21d38594f..f90ceeb9db 100644 --- a/src/MediaWiki/Specials/Browse/ContentsBuilder.php +++ b/src/MediaWiki/Specials/Browse/ContentsBuilder.php @@ -14,6 +14,7 @@ use SMWDataValue as DataValue; use SMW\DataValues\ValueFormatters\DataValueFormatter; use SMW\RequestOptions; +use SMW\Utils\HtmlTable; /** * @license GNU GPL v2+ @@ -252,9 +253,9 @@ private function doGenerateHtml() { private function displayData( SemanticData $data, $left = true, $incoming = false, $isLoading = false ) { // Some of the CSS classes are different for the left or the right side. // In this case, there is an "i" after the "smwb-". This is set here. - $ccsPrefix = $left ? 'smwb-' : 'smwb-i'; + $dirPrefix = $left ? 'smwb-' : 'smwb-i'; - $html = "
"; + $html = HtmlTable::open( array( 'class' => "{$dirPrefix}factbox smwb-bottom" ) ); $noresult = true; $contextPage = $data->getSubject(); @@ -282,8 +283,8 @@ private function displayData( SemanticData $data, $left = true, $incoming = fals continue; } - $head = "
" . $propertyLabel . "
\n"; - $body = "
\n"; + $head = HtmlTable::cell( $propertyLabel, array( "class" => 'smwb-cell smwb-prophead' ) ); + $propertyValue = ''; $values = $data->getPropertyValues( $diProperty ); @@ -299,7 +300,7 @@ private function displayData( SemanticData $data, $left = true, $incoming = fals if ( $first ) { $first = false; } else { - $body .= ', '; + $propertyValue .= ', '; } if ( $incoming ) { @@ -308,14 +309,14 @@ private function displayData( SemanticData $data, $left = true, $incoming = fals $dv = DataValueFactory::getInstance()->newDataValueByItem( $di, $diProperty ); } - $body .= "" . + $propertyValue .= "" . $this->displayValue( $dvProperty, $dv, $incoming ) . "\n"; } // Added in 2.3 // link to the remaining incoming pages - if ( $moreIncoming && \Hooks::run( 'SMW::Browse::BeforeIncomingPropertyValuesFurtherLinkCreate', array( $diProperty, $this->subject->getDataItem(), &$body ) ) ) { - $body .= \Html::element( + if ( $moreIncoming && \Hooks::run( 'SMW::Browse::BeforeIncomingPropertyValuesFurtherLinkCreate', array( $diProperty, $this->subject->getDataItem(), &$propertyValue ) ) ) { + $propertyValue .= \Html::element( 'a', array( 'href' => \SpecialPage::getSafeTitleFor( 'SearchByProperty' )->getLocalURL( array( @@ -328,22 +329,37 @@ private function displayData( SemanticData $data, $left = true, $incoming = fals } - $body .= "
\n"; + $body = HtmlTable::cell( $propertyValue, array( "class" => 'smwb-cell smwb-propval' ) ); // display row - $html .= "
\n" . - ( $left ? ( $head . $body ):( $body . $head ) ) . "
\n"; + $html .= HtmlTable::row( + ( $left ? ( $head . $body ):( $body . $head ) ), + array( + "class" => "{$dirPrefix}propvalue" + ) + ); $noresult = false; } // end foreach properties if ( $noresult ) { $noMsgKey = $incoming ? 'smw_browse_no_incoming':'smw_browse_no_outgoing'; - $rColumn = "
"; - $lColumn = "
" . wfMessage( $isLoading ? 'smw-browse-from-backend' : $noMsgKey )->escaped() . "
"; - $html .= "
" . ( $left ? ( $rColumn . $lColumn ):( $lColumn . $rColumn ) ) . "
"; + $rColumn = HtmlTable::cell( '', array( "class" => 'smwb-cell smwb-prophead' ) ); + $lColumn = HtmlTable::cell( + wfMessage( $isLoading ? 'smw-browse-from-backend' : $noMsgKey )->escaped(), + array( + "class" => 'smwb-cell smwb-propval' + ) + ); + + $html .= HtmlTable::row( + ( $left ? ( $rColumn . $lColumn ):( $lColumn . $rColumn ) ), + array( + "class" => "{$dirPrefix}propvalue" + ) + ); } - $html .= "
\n"; + $html .= HtmlTable::close(); return $html; } @@ -375,12 +391,17 @@ private function displayHead() { $label = ValueFormatter::getFormattedSubject( $this->subject ); - $html = "
" . - "
" . - $label . "\n" . - "
"; - - return $html; + return HtmlTable::table( + HtmlTable::row( + $label, + array( + 'class' => 'smwb-title' + ) + ), + array( + 'class' => 'smwb-factbox' + ) + ); } /** @@ -408,12 +429,19 @@ private function displayCenter( $article ) { $linkMsg = 'smw_browse_show_incoming'; } - $html = FormHelper::createLink( $linkMsg, $parameters ); + $html = FormHelper::createLinkFromMessage( $linkMsg, $parameters ); - return "\n" . - "
" . - "
" . $html . - " \n" . "
\n" . "
\n"; + return "" . HtmlTable::table( + HtmlTable::row( + $html . " \n", + array( + 'class' => 'smwb-center' + ) + ), + array( + 'class' => 'smwb-factbox' + ) + ); } /** @@ -429,11 +457,23 @@ private function displayBottom( $more ) { $open = "
" . "
"; - $close = " \n" . "
\n" . "
\n"; - $html = ''; + $open = HtmlTable::open( + array( + 'class' => 'smwb-factbox' + ) + ); + + $html = HtmlTable::row( + ' ', + array( + 'class' => 'smwb-center' + ) + ); + + $close = HtmlTable::close(); if ( $this->getOption( 'showAll' ) ) { - return $open . $close; + return $open . $html . $close; } if ( ( $this->offset > 0 ) || $more ) { @@ -447,7 +487,7 @@ private function displayBottom( $more ) { $linkMsg = 'smw_result_prev'; - $html .= ( $this->offset == 0 ) ? wfMessage( $linkMsg )->escaped() : FormHelper::createLink( $linkMsg, $parameters ); + $html .= ( $this->offset == 0 ) ? wfMessage( $linkMsg )->escaped() : FormHelper::createLinkFromMessage( $linkMsg, $parameters ); $offset = $this->offset + $this->incomingPropertiesCount - 1; @@ -459,10 +499,16 @@ private function displayBottom( $more ) { $linkMsg = 'smw_result_next'; - // @todo FIXME: i18n patchwork. $html .= "     " . wfMessage( 'smw_result_results' )->escaped() . " " . ( $this->offset + 1 ) . " – " . ( $offset ) . "     "; - $html .= $more ? FormHelper::createLink( $linkMsg, $parameters ) : wfMessage( $linkMsg )->escaped(); + $html .= $more ? FormHelper::createLinkFromMessage( $linkMsg, $parameters ) : wfMessage( $linkMsg )->escaped(); + + $html = HtmlTable::row( + $html, + array( + 'class' => 'smwb-center' + ) + ); } return $open . $html . $close; diff --git a/src/MediaWiki/Specials/Browse/FormHelper.php b/src/MediaWiki/Specials/Browse/FormHelper.php index d5a8a9c923..63c6e99b0b 100644 --- a/src/MediaWiki/Specials/Browse/FormHelper.php +++ b/src/MediaWiki/Specials/Browse/FormHelper.php @@ -107,7 +107,7 @@ public static function getQueryForm( $articletext = '' ) { * * @return string */ - public static function createLink( $linkMsg, array $parameters ) { + public static function createLinkFromMessage( $linkMsg, array $parameters ) { $title = SpecialPage::getSafeTitleFor( 'Browse' ); $fragment = $linkMsg === 'smw_browse_show_incoming' ? '#smw_browse_incoming' : ''; diff --git a/src/Utils/HtmlTable.php b/src/Utils/HtmlTable.php new file mode 100644 index 0000000000..d62a755e2b --- /dev/null +++ b/src/Utils/HtmlTable.php @@ -0,0 +1,145 @@ +assertInternalType( 'string', - FormHelper::createLink( 'Foo', $parameters ) + FormHelper::createLinkFromMessage( 'Foo', $parameters ) ); } diff --git a/tests/phpunit/Unit/Utils/HtmlTableTest.php b/tests/phpunit/Unit/Utils/HtmlTableTest.php new file mode 100644 index 0000000000..7c161ccba7 --- /dev/null +++ b/tests/phpunit/Unit/Utils/HtmlTableTest.php @@ -0,0 +1,71 @@ +assertEquals( + '
', + HtmlTable::open() . HtmlTable::close() + ); + + $this->assertEquals( + HtmlTable::table(), + HtmlTable::open() . HtmlTable::close() + ); + } + + public function testHeader() { + + $this->assertEquals( + '
foo
', + HtmlTable::header( 'foo', array( 'class' => 'bar' ) ) + ); + } + + public function testBody() { + + $this->assertEquals( + '
foo
', + HtmlTable::body( 'foo', array( 'class' => 'bar' ) ) + ); + } + + public function testFooter() { + + $this->assertEquals( + '', + HtmlTable::footer( 'foo', array( 'class' => 'bar' ) ) + ); + } + + public function testRow() { + + $this->assertEquals( + '
foo
', + HtmlTable::row( 'foo', array( 'class' => 'bar' ) ) + ); + } + + public function testCell() { + + $this->assertEquals( + '
foo
', + HtmlTable::cell( 'foo', array( 'class' => 'bar' ) ) + ); + } + +}