Skip to content

Commit

Permalink
Merge pull request #1759 from SemanticMediaWiki/ask-error
Browse files Browse the repository at this point in the history
Improve Special:Ask error output
  • Loading branch information
mwjames committed Aug 7, 2016
2 parents 17ae028 + 4d73d16 commit 230968e
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 1 deletion.
7 changes: 6 additions & 1 deletion includes/specials/SMW_SpecialAsk.php
Expand Up @@ -3,6 +3,7 @@
use ParamProcessor\Param;
use SMW\Query\PrintRequest;
use SMW\Query\QueryLinker;
use SMW\MediaWiki\Specials\Ask\HtmlContentBuilder;

/**
* This special page for MediaWiki implements a customisable form for
Expand Down Expand Up @@ -220,6 +221,7 @@ protected function makeHTMLResult() {

$result = '';
$res = null;
$htmlContentBuilder = new HtmlContentBuilder();

// build parameter strings for URLs, based on current settings
$urlArgs['q'] = $this->m_querystring;
Expand All @@ -235,6 +237,7 @@ protected function makeHTMLResult() {
$printoutstring = '';
$duration = 0;
$navigation = '';
$queryobj = null;

/**
* @var PrintRequest $printout
Expand Down Expand Up @@ -332,7 +335,7 @@ protected function makeHTMLResult() {
}

} else {
$result = '<div style="text-align: center;">' . wfMessage( 'smw_result_noresults' )->escaped() . '</div>';
$result = Html::element( 'div', array( 'class' => 'smw-callout smw-callout-info' ), wfMessage( 'smw_result_noresults' )->escaped() );;
}
}
}
Expand Down Expand Up @@ -371,6 +374,8 @@ protected function makeHTMLResult() {
$isFromCache
) . $result;

$result .= $htmlContentBuilder->getFormattedErrorString( $queryobj );

$this->getOutput()->addHTML( $result );
}
}
Expand Down
20 changes: 20 additions & 0 deletions res/smw/ext.smw.css
Expand Up @@ -517,6 +517,9 @@ label.smw-form-checkbox {
}

.smw-callout-info {
border: 1px solid #d9edf7;
background-color: #d9edf7;
border-left-width: 5px;
border-left-color: #1b809e;
}

Expand All @@ -525,14 +528,31 @@ label.smw-form-checkbox {
}

.smw-callout-warning {
border: 1px solid #fcf8e3;
background-color: #fcf8e3;
border-left-width: 5px;
border-left-color: #aa6708;
}

.smw-callout-warning .title {
color: #aa6708;
}

.smw-callout-success {
border: 1px solid #dff0d8;
background-color: #dff0d8;
border-left-width: 5px;
border-left-color: #3c763d;
}

.smw-callout-success .title {
color: #3c763d;
}

.smw-callout-error {
border: 1px solid #f2dede;
background-color: #f2dede;
border-left-width: 5px;
border-left-color: #ce4844;
}

Expand Down
50 changes: 50 additions & 0 deletions src/MediaWiki/Specials/Ask/HtmlContentBuilder.php
@@ -0,0 +1,50 @@
<?php

namespace SMW\MediaWiki\Specials\Ask;

use SMWQuery as Query;
use Html;

/**
* @license GNU GPL v2+
* @since 2.5
*
* @author mwjames
*/
class HtmlContentBuilder {

/**
* @since 2.5
*
* @param Query|null $query
*
* @return string
*/
public function getFormattedErrorString( Query $query = null ) {

if ( $query === null || !is_array( $query->getErrors() ) || $query->getErrors() === array() ) {
return '';
}

if ( count( $query->getErrors() ) == 1 ) {
$error = implode( ' ', $query->getErrors() );
} else {

// Filter any duplicate messages
$errors = array();
foreach ( $query->getErrors() as $key => $value ) {

if ( is_array( $value ) ) {
$value = implode( ' ', $value );
}

$errors[md5( $value )] = $value;
}

$error = '<li>' . implode( '</li><li>', $errors ) . '</li>';
}

return Html::rawElement( 'div', array( 'class' => 'smw-callout smw-callout-error' ), $error );
}

}
3 changes: 3 additions & 0 deletions tests/phpunit/Integration/SpecialAskTest.php
Expand Up @@ -32,6 +32,9 @@ public function testProducesWellformedHtml( $params ) {
$html = $GLOBALS['wgOut']->getHtml();
$html = '<!DOCTYPE html><html><body>' . $html . '</body></html>';

// Known tags DOMDocument has issues with
$html = str_replace( array( '<nowiki>', '</nowiki>' ), '', $html );

$document = new DOMDocument();
$result = $document->loadHTML( $html );
$this->assertTrue( $result );
Expand Down
@@ -0,0 +1,78 @@
<?php

namespace SMW\Tests\MediaWiki\Specials\Ask;

use SMW\MediaWiki\Specials\Ask\HtmlContentBuilder;

/**
* @covers \SMW\MediaWiki\Specials\Ask\HtmlContentBuilder
* @group semantic-mediawiki
*
* @license GNU GPL v2+
* @since 2.5
*
* @author mwjames
*/
class HtmlContentBuilderTest extends \PHPUnit_Framework_TestCase {

public function testCanConstruct() {

$this->assertInstanceOf(
'\SMW\MediaWiki\Specials\Ask\HtmlContentBuilder',
new HtmlContentBuilder()
);
}

/**
* @dataProvider queryErrorProvider
*/
public function testGetFormattedErrorString( $errors, $expected ) {

$query = $this->getMockBuilder( '\SMWQuery' )
->disableOriginalConstructor()
->getMock();

$query->expects( $this->atLeastOnce() )
->method( 'getErrors' )
->will( $this->returnValue( $errors ) );

$instance = new HtmlContentBuilder();

$this->assertEquals(
$expected,
$instance->getFormattedErrorString( $query )
);
}

public function queryErrorProvider() {

$provider[] = array(
'',
''
);

$provider[] = array(
array( 'Foo' ),
'<div class="smw-callout smw-callout-error">Foo</div>'
);

$provider[] = array(
array( 'Foo', 'Bar' ),
'<div class="smw-callout smw-callout-error"><li>Foo</li><li>Bar</li></div>'
);

$provider[] = array(
array( 'Foo', array( 'Bar' ) ),
'<div class="smw-callout smw-callout-error"><li>Foo</li><li>Bar</li></div>'
);

// Filter duplicate
$provider[] = array(
array( 'Foo', array( 'Bar' ), 'Bar' ),
'<div class="smw-callout smw-callout-error"><li>Foo</li><li>Bar</li></div>'
);

return $provider;
}

}

0 comments on commit 230968e

Please sign in to comment.