Completed
Push — master ( 5d1976...30add5 )
by mw
13s
created

includes/querypages/UnusedPropertiesQueryPage.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SMW;
4
5
use Html;
6
use SMWDIError;
7
use SMWTypesValue;
8
9
/**
10
 * Query page that provides content to Special:UnusedProperties
11
 *
12
 * @ingroup QueryPage
13
 *
14
 * @licence GNU GPL v2+
15
 * @since 1.9
16
 *
17
 * @author Markus Krötzsch
18
 * @author mwjames
19
 */
20
class UnusedPropertiesQueryPage extends QueryPage {
21
22
	/** @var Store */
23
	protected $store;
24
25
	/** @var Settings */
26
	protected $settings;
27
28
	/**
29
	 * @var ListLookup
30
	 */
31
	private $listLookup;
32
33
	/**
34
	 * @since 1.9
35
	 *
36
	 * @param Store $store
37
	 * @param Settings $settings
38
	 */
39 5
	public function __construct( Store $store, Settings $settings ) {
40 5
		$this->store = $store;
41 5
		$this->settings = $settings;
42 5
	}
43
44
	/**
45
	 * @codeCoverageIgnore
46
	 * @return string
47
	 */
48
	function getName() {
49
		return "UnusedProperties";
50
	}
51
52
	/**
53
	 * @codeCoverageIgnore
54
	 * @return boolean
55
	 */
56
	function isExpensive() {
57
		return false; // Disables caching for now
58
	}
59
60
	/**
61
	 * @codeCoverageIgnore
62
	 * @return boolean
63
	 */
64
	function isSyndicated() {
65
		return false; // TODO: why not?
66
	}
67
68
	/**
69
	 * @codeCoverageIgnore
70
	 * @return string
71
	 */
72
	function getPageHeader() {
73
		return Html::element( 'p', array(), $this->msg( 'smw_unusedproperties_docu' )->text() );
74
	}
75
76
	/**
77
	 * Format a result in the list of results as a string. We expect the
78
	 * result to be an object of type SMWDIProperty (normally) or maybe
79
	 * SMWDIError (if something went wrong).
80
	 *
81
	 * @param Skin $skin provided by MediaWiki, not needed here
82
	 * @param mixed $result
83
	 *
84
	 * @return String
85
	 * @throws InvalidResultException if the result was not of a supported type
86
	 */
87 4
	function formatResult( $skin, $result ) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
88
89 4
		if ( $result instanceof DIProperty ) {
90 2
			return $this->formatPropertyItem( $result );
91
		} elseif ( $result instanceof SMWDIError ) {
92 1
			return $this->getMessageFormatter()->clear()
93 1
				->setType( 'warning' )
94 1
				->addFromArray( array( $result->getErrors() ) )
95 1
				->getHtml();
96
		} else {
97 1
			throw new InvalidResultException( 'UnusedPropertiesQueryPage expects results that are properties or errors.' );
98
		}
99
	}
100
101
	/**
102
	 * Produce a formatted string representation for showing a property in
103
	 * the list of unused properties.
104
	 *
105
	 * @since 1.8
106
	 *
107
	 * @param DIProperty $property
108
	 *
109
	 * @return string
110
	 */
111 2
	protected function formatPropertyItem( DIProperty $property ) {
112
113
		// Clear formatter before invoking messages and
114
		// avoid having previous data to be present
115 2
		$this->getMessageFormatter()->clear();
116
117 2
		if ( $property->isUserDefined() ) {
118
119 1
			$title = $property->getDiWikiPage()->getTitle();
120
121 1
			if ( !$title instanceof \Title ) {
122
				return '';
123
			}
124
125 1
			$propertyLink = $this->getLinker()->link(
126
				$title,
127 1
				$property->getLabel()
128
			);
129
130 1
			$types = $this->store->getPropertyValues( $property->getDiWikiPage(), new DIProperty( '_TYPE' ) );
131
132 1
			if ( count( $types ) >= 1 ) {
133
				$typeDataValue = DataValueFactory::getInstance()->newDataItemValue( current( $types ), new DIProperty( '_TYPE' ) );
134
			} else {
135 1
				$typeDataValue = SMWTypesValue::newFromTypeId( '_wpg' );
136 1
				$this->getMessageFormatter()->addFromKey( 'smw_propertylackstype', $typeDataValue->getLongHTMLText() );
137
			}
138
139
		} else {
140 1
			$typeDataValue = SMWTypesValue::newFromTypeId( $property->findPropertyTypeID() );
141 1
			$propertyLink  = DataValueFactory::getInstance()->newDataItemValue( $property, null )->getShortHtmlText( $this->getLinker() );
142
		}
143
144 2
		return $this->msg( 'smw_unusedproperty_template', $propertyLink, $typeDataValue->getLongHTMLText( $this->getLinker() )	)->text() . ' ' .
145 2
			$this->getMessageFormatter()->getHtml();
146
	}
147
148
	/**
149
	 * Get the list of results.
150
	 *
151
	 * @param SMWRequestOptions $requestOptions
152
	 * @return array of SMWDIProperty|SMWDIError
153
	 */
154
	function getResults( $requestOptions ) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
155
		$this->listLookup = $this->store->getUnusedPropertiesSpecial( $requestOptions );
156
		return $this->listLookup->fetchList();
157
	}
158
}
159