Completed
Push — master ( b9b0af...b944a3 )
by mw
279:13 queued 244:18
created

SpecialBrowse::newContentsBuilder()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 26
nc 1
nop 2
dl 0
loc 44
ccs 25
cts 25
cp 1
crap 1
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\MediaWiki\Specials;
4
5
use SMW\ApplicationFactory;
6
use SMW\DataValueFactory;
7
use SMW\DIProperty;
8
use SMW\Localizer;
9
use SMW\SemanticData;
10
use SMW\UrlEncoder;
11
use SMW\MediaWiki\Specials\Browse\ContentsBuilder;
12
use SMW\MediaWiki\Specials\Browse\FormHelper;
13
use SMW\Message;
14
use SpecialPage;
15
use Html;
16
17
/**
18
 * A factbox view on one specific article, showing all the Semantic data about it
19
 *
20
 * @license GNU GPL v2+
21
 * @since 1.6
22
 *
23
 * @author mwjames
24
 */
25
class SpecialBrowse extends SpecialPage {
26
27
	/**
28
	 * @var DataValue
29
	 */
30
	private $subjectDV = null;
31
32
	/**
33
	 * @var ApplicationFactory
34
	 */
35
	private $applicationFactory = null;
36
37
	/**
38
	 * @see SpecialPage::__construct
39 4
	 */
40 4
	public function __construct() {
41 4
		parent::__construct( 'Browse', '', true, false, 'default', true );
42 4
		$this->applicationFactory = ApplicationFactory::getInstance();
43
	}
44
45
	/**
46
	 * @see SpecialPage::execute
47
	 *
48
	 * @param string $query string
49 11
	 */
50
	public function execute( $query ) {
51 11
52 11
		$this->setHeaders();
53
		$webRequest = $this->getRequest();
54
55 11
		// get the GET parameters
56 11
		$articletext = $webRequest->getVal( 'article' );
57
		$isEmptyRequest = $query === null && ( $webRequest->getVal( 'article' ) === '' || $webRequest->getVal( 'article' ) === null );
58
59 11
		// @see SMWInfolink::encodeParameters
60
		if ( $query === null && $this->getRequest()->getCheck( 'x' ) ) {
61
			$query = $this->getRequest()->getVal( 'x' );
62
		}
63
64 11
		// Auto-generated link is marked with a leading :
65 3
		if ( $query !== '' && $query{0} === ':' ) {
66 9
			$articletext = UrlEncoder::unescape( $query );
67 9
		} elseif ( $articletext === null ) {
68
			$articletext = $query;
69
		}
70
71 11
		// no GET parameters? Then try the URL
72
		if ( $articletext === null ) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
73
		}
74 11
75 11
		$this->subjectDV = DataValueFactory::getInstance()->newTypeIDValue(
0 ignored issues
show
Documentation Bug introduced by
It seems like \SMW\DataValueFactory::g...e('_wpg', $articletext) of type object<SMWDataValue> is incompatible with the declared type object<SMW\MediaWiki\Specials\DataValue> of property $subjectDV.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Deprecated Code introduced by
The method SMW\DataValueFactory::newTypeIdValue() has been deprecated with message: since 2.4, use DataValueFactory::newDataValueByType

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
76
			'_wpg',
77
			$articletext
78
		);
79 11
80 11
		$out = $this->getOutput();
81
		$out->setHTMLTitle( $this->subjectDV->getTitle() );
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SMWDataValue as the method getTitle() does only exist in the following sub-classes of SMWDataValue: SMWWikiPageValue. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
82 11
83 11
		$out->addModuleStyles( array(
84
			'mediawiki.ui',
85
			'mediawiki.ui.button',
86
			'mediawiki.ui.input'
87
		) );
88 11
89 11
		$out->addModules( array(
90
			'ext.smw.browse',
91
			'ext.smw.tooltip'
92
		) );
93 11
94 11
		$out->addHTML(
95
			$this->getHtml( $webRequest, $isEmptyRequest )
96
		);
97 11
98 11
		$this->addExternalHelpLinks();
99
	}
100 11
101
	private function getHtml( $webRequest, $isEmptyRequest ) {
102 11
103
		if ( $isEmptyRequest && !$this->including() ) {
104
			return Message::get( 'smw-browse-intro', Message::TEXT, Message::USER_LANGUAGE ) . FormHelper::getQueryForm();
105
		}
106 11
107
		if ( !$this->subjectDV->isValid() ) {
108 4
109 4
			foreach ( $this->subjectDV->getErrors() as $error ) {
110
				$error = Message::decode( $error, Message::TEXT );
111
			}
112 4
113 4
			$html = Html::rawElement(
114
				'div',
115
				array(
116 4
					'class' => 'smw-callout smw-callout-error'
117 4
				),
118
				Message::get( array( 'smw-browse-invalid-subject', $error ), Message::TEXT )
0 ignored issues
show
Bug introduced by
The variable $error does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
119
			);
120 4
121 4
			if ( !$this->including() ) {
122
				$html .= FormHelper::getQueryForm( $webRequest->getVal( 'article' ) );
123
			}
124 4
125
			return $html;
126
		}
127 7
128
		$contentsBuilder = $this->newContentsBuilder(
129 7
			$webRequest,
130
			$this->applicationFactory->getSettings()
131
		);
132 7
133 5
		if ( $webRequest->getVal( 'output' ) === 'legacy' || !$contentsBuilder->getOption( 'byApi' ) ) {
134
			return $contentsBuilder->getHtml();
135
		}
136
137 2
		$options = array(
138 2
			'dir'         => $contentsBuilder->getOption( 'dir' ),
139 2
			'offset'      => $contentsBuilder->getOption( 'offset' ),
140 2
			'printable'   => $contentsBuilder->getOption( 'printable' ),
141 2
			'showInverse' => $contentsBuilder->getOption( 'showInverse' ),
142 2
			'showAll'     => $contentsBuilder->getOption( 'showAll' ),
143
			'including'   => $contentsBuilder->getOption( 'including' )
144
		);
145
146 2
		// Ajax/API is doing the data fetch
147 2
		$html = Html::rawElement(
148
			'div',
149 2
			array(
150 2
				'class' => 'smwb-container',
151 2
				'data-subject' => $this->subjectDV->getDataItem()->getHash(),
152
				'data-options' => json_encode( $options )
153 2
			),
154 2
			Html::rawElement(
155
				'div',
156
				array(
157 2
					'class' => 'smwb-status'
158 2
				),
159 2
				Html::rawElement(
160 2
					'noscript',
161 2
					array(),
162 2
					Html::rawElement(
163
						'div',
164 2
						array(
165
							'class' => 'smw-callout smw-callout-error',
166 2
						),
167
						Message::get( 'smw-browse-js-disabled', Message::PARSE )
168
					)
169 2
				)
170 2
			) . Html::rawElement(
171
				'div',
172
				array(
173 2
					'class' => 'smwb-content is-disabled'
174 2
				),
175 2
				Html::rawElement(
176
					'span',
177
					array(
178 2
						'class' => 'smw-overlay-spinner large inline'
179 2
					)
180
				) . $contentsBuilder->getEmptyHtml()
181
			)
182
		);
183 2
184
		return $html;
185
	}
186 7
187
	private function newContentsBuilder( $webRequest, $settings ) {
188 7
189 7
		$contentsBuilder = new ContentsBuilder(
190 7
			$this->applicationFactory->getStore(),
191
			$this->subjectDV->getDataItem()
192
		);
193 7
194 7
		$contentsBuilder->setOption(
195 7
			'dir',
196
			$webRequest->getVal( 'dir' )
197
		);
198 7
199 7
		$contentsBuilder->setOption(
200 7
			'printable',
201
			$webRequest->getVal( 'printable' )
202
		);
203 7
204 7
		$contentsBuilder->setOption(
205 7
			'offset',
206
			$webRequest->getVal( 'offset' )
207
		);
208 7
209 7
		$contentsBuilder->setOption(
210 7
			'including',
211
			$this->including()
212
		);
213 7
214 7
		$contentsBuilder->setOption(
215 7
			'showInverse',
216
			$settings->get( 'smwgBrowseShowInverse' )
217
		);
218 7
219 7
		$contentsBuilder->setOption(
220 7
			'showAll',
221
			$settings->get( 'smwgBrowseShowAll' )
222
		);
223 7
224 7
		$contentsBuilder->setOption(
225 7
			'byApi',
226
			$settings->get( 'smwgBrowseByApi' )
227
		);
228 7
229
		return $contentsBuilder;
230
	}
231 11
232
	private function addExternalHelpLinks() {
233 11
234
		if ( $this->getRequest()->getVal( 'printable' ) === 'yes' ) {
235
			return null;
236
		}
237
238 11
		// FIXME with SMW 3.0, allow to be used with MW 1.25-
239
		if ( !method_exists( $this, 'addHelpLink' ) ) {
240
			return null;
241
		}
242 11
243 7
		if ( $this->subjectDV->isValid() ) {
244
			$link = SpecialPage::getTitleFor( 'ExportRDF', $this->subjectDV->getTitle()->getPrefixedText() );
245 7
246 7
			$this->getOutput()->setIndicators( array(
247 7
				'browse' => Html::rawElement(
248
					'div',
249
					array(
250 7
						'class' => 'smw-page-indicator-rdflink'
251 7
					),
252 7
					Html::rawElement(
253
						'a',
254 7
						array(
255
							'href' => $link->getLocalUrl( 'syntax=rdf' )
256 7
						),
257
						'RDF'
258
					)
259
				)
260
			) );
261
		}
262 11
263 11
		$this->addHelpLink( wfMessage( 'smw-specials-browse-helplink' )->escaped(), true );
264
	}
265
266
	protected function getGroupName() {
267
		return 'smw_group';
268
	}
269
270
}
271