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

includes/datavalues/SMW_DV_Types.php (1 issue)

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
use SMW\DataTypeRegistry;
4
5
/**
6
 * This datavalue implements special processing suitable for defining types of
7
 * properties. Types behave largely like values of type SMWWikiPageValue
8
 * with three main differences. First, they actively check if a value is an
9
 * alias for another type, modifying the internal representation accordingly.
10
 * Second, they have a modified display for emphasizing if some type is defined
11
 * in SMW (built-in). Third, they use type ids for storing data (DB keys)
12
 * instead of using page titles.
13
 *
14
 * @author Markus Krötzsch
15
 * @ingroup SMWDataValues
16
 */
17
class SMWTypesValue extends SMWDataValue {
18
	protected $m_isAlias; // record whether this is an alias to another type, used to avoid duplicates when listing page types
19
	protected $m_realLabel;
20
	protected $m_givenLabel;
21
	protected $m_typeId;
22
23 1
	public static function newFromTypeId( $typeId ) {
24 1
		$result = new SMWTypesValue( '__typ' );
25
		try {
26 1
			$dataItem = self::getTypeUriFromTypeId( $typeId );
27
		} catch ( SMWDataItemException $e ) {
28
			$dataItem = self::getTypeUriFromTypeId( 'notype' );
29
		}
30 1
		$result->setDataItem( $dataItem );
31 1
		return $result;
32
	}
33
34 116
	public static function getTypeUriFromTypeId( $typeId ) {
35 116
		return new SMWDIUri( 'http', 'semantic-mediawiki.org/swivt/1.0', '', $typeId );
36
	}
37
38 116
	protected function parseUserValue( $value ) {
39 116
		global $wgContLang, $smwgHistoricTypeNamespace;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
40
41 116
		if ( $this->m_caption === false ) {
42 116
			$this->m_caption = $value;
43
		}
44
45 116
		$valueParts = explode( ':', $value, 2 );
46 116
		if ( $smwgHistoricTypeNamespace && count( $valueParts ) > 1 ) {
47
			$namespace = smwfNormalTitleText( $valueParts[0] );
48
			$value = $valueParts[1];
49
			$typeNamespace = $wgContLang->getNsText( SMW_NS_TYPE );
50
			if ( $namespace != $typeNamespace ) {
51
				$this->addError( wfMessage( 'smw_wrong_namespace', $typeNamespace )->inContentLanguage()->text() );
52
			}
53
		}
54
55 116
		if ( $value{0} === '_' ) {
56 10
			$this->m_typeId = $value;
57
		} else {
58 109
			$this->m_givenLabel = smwfNormalTitleText( $value );
59 109
			$this->m_typeId = DataTypeRegistry::getInstance()->findTypeId( $this->m_givenLabel );
60
		}
61
62 116
		if ( $this->m_typeId === '' ) {
63
			$this->addError( wfMessage( 'smw_unknowntype', $this->m_givenLabel )->inContentLanguage()->text() );
64
			$this->m_realLabel = $this->m_givenLabel;
65
		} else {
66 116
			$this->m_realLabel = DataTypeRegistry::getInstance()->findTypeLabel( $this->m_typeId );
67
		}
68 116
		$this->m_isAlias = ( $this->m_realLabel === $this->m_givenLabel ) ? false : true;
69
70
		try {
71 116
			$this->m_dataitem = self::getTypeUriFromTypeId( $this->m_typeId );
72
		} catch ( SMWDataItemException $e ) {
73
			$this->m_dataitem = self::getTypeUriFromTypeId( 'notype' );
74
			$this->addError( wfMessage( 'smw_parseerror' )->inContentLanguage()->text() );
75
		}
76 116
	}
77
78
	/**
79
	 * @see SMWDataValue::loadDataItem()
80
	 * @param $dataitem SMWDataItem
81
	 * @return boolean
82
	 */
83 4
	protected function loadDataItem( SMWDataItem $dataItem ) {
84 4
		if ( ( $dataItem instanceof SMWDIUri ) && ( $dataItem->getScheme() == 'http' ) &&
85 4
		     ( $dataItem->getHierpart() == 'semantic-mediawiki.org/swivt/1.0' ) &&
86 4
		     ( $dataItem->getQuery() === '' ) ) {
87 4
			$this->m_isAlias = false;
88 4
			$this->m_typeId = $dataItem->getFragment();
89 4
			$this->m_realLabel = DataTypeRegistry::getInstance()->findTypeLabel( $this->m_typeId );
90 4
			$this->m_caption = $this->m_givenLabel = $this->m_realLabel;
91 4
			$this->m_dataitem = $dataItem;
92 4
			return true;
93
		} else {
94
			return false;
95
		}
96
	}
97
98 108
	public function getShortWikiText( $linker = null ) {
99 108
		global $wgContLang;
100 108
		if ( !$linker || $this->m_outformat === '-' || $this->m_caption === '' ) {
101
			return $this->m_caption;
102
		} else {
103 108
			$titleText = $this->getSpecialPageTitleText();
104 108
			$namespace = $wgContLang->getNsText( NS_SPECIAL );
105 108
			return "[[$namespace:$titleText|{$this->m_caption}]]";
106
		}
107
	}
108
109
	public function getShortHTMLText( $linker = null ) {
110
		if ( !$linker || $this->m_outformat === '-' || $this->m_caption === ''  ) {
111
			return htmlspecialchars( $this->m_caption );
112
		} else {
113
			$title = Title::makeTitle( NS_SPECIAL, $this->getSpecialPageTitleText() );
114
			return $linker->link( $title, htmlspecialchars( $this->m_caption ) );
115
		}
116
	}
117
118
	public function getLongWikiText( $linker = null ) {
119
		global $wgContLang;
120
		if ( !$linker || $this->m_realLabel === '' ) {
121
			return $this->m_realLabel;
122
		} else {
123
			$titleText = $this->getSpecialPageTitleText();
124
			$namespace = $wgContLang->getNsText( NS_SPECIAL );
125
			return "[[$namespace:$titleText|{$this->m_realLabel}]]";
126
		}
127
	}
128
129 1
	public function getLongHTMLText( $linker = null ) {
130 1
		if ( !$linker || $this->m_realLabel === '' ) {
131 1
			return htmlspecialchars( $this->m_realLabel );
132
		} else {
133 1
			$title = Title::makeTitle( NS_SPECIAL, $this->getSpecialPageTitleText() );
134 1
			return $linker->link( $title, htmlspecialchars( $this->m_realLabel ) );
135
		}
136
	}
137
138
	/**
139
	 * Gets the title text for the types special page.
140
	 *
141
	 * @since 1.6
142
	 *
143
	 * @return string
144
	 */
145 108
	protected function getSpecialPageTitleText() {
146 108
		return SpecialPageFactory::getLocalNameFor( 'Types', $this->m_realLabel );
147
	}
148
149 3
	public function getWikiValue() {
150 3
		return $this->m_realLabel;
151
	}
152
153
	public function getHash() {
154
		return $this->m_realLabel;
155
	}
156
157
	/**
158
	 * This class uses type ids as DB keys.
159
	 *
160
	 * @return string
161
	 */
162
	public function getDBkey() {
163
		return ( $this->isValid() ) ? DataTypeRegistry::getInstance()->findTypeID( $this->m_realLabel ) : '';
164
	}
165
166
	/**
167
	 * Is this a built-in datatype shipped with SMW (or an extension of SMW)?
168
	 * (Alternatively it would be a user-defined derived datatype.)
169
	 *
170
	 * @deprecated As of SMW 1.6, there are no more user-defined datatypes, making this method useless. Will vanish in SMW 1.6.
171
	 */
172
	public function isBuiltIn() {
173
		return true;
174
	}
175
176
	/**
177
	 * Is this an alias for another datatype in SMW? This information is used to
178
	 * explain entries in Special:Types that are found since they have pages.
179
	 *
180
	 * @return boolean
181
	 */
182
	public function isAlias() {
183
		return $this->m_isAlias;
184
	}
185
186
}
187
188