summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/DataValues/AllowsListValue.php
blob: 9388dc7e63c636a46e0112094755e06eab7e0708 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php

namespace SMW\DataValues;

use SMW\Localizer;

/**
 * To support value list via the NS_MEDIAWIKI namespace
 *
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
class AllowsListValue extends StringValue {

	/**
	 * DV identifier
	 */
	const TYPE_ID = '__pvali';

	/**
	 * Fixed Mediawiki NS prefix
	 */
	const LIST_PREFIX = 'Smw_allows_list_';

	/**
	 * @param string $typeid
	 */
	public function __construct( $typeid = '' ) {
		parent::__construct( self::TYPE_ID );
	}

	/**
	 * @see DataValue::parseUserValue
	 *
	 * @param string $value
	 */
	protected function parseUserValue( $value ) {

		if ( $value === '' ) {
			$this->addErrorMsg( 'smw_emptystring' );
		}

		$allowsListValueParser = $this->dataValueServiceFactory->getValueParser( $this );

		$allowsListValueParser->parse( $value );

		if ( $allowsListValueParser->getErrors() !== [] ) {
			foreach ( $allowsListValueParser->getErrors() as $error ) {
				$this->addErrorMsg( $error );
			}
		} else {
			parent::parseUserValue( $value );
		}
	}

	/**
	 * @see DataValue::getShortWikiText
	 *
	 * @param string $value
	 */
	public function getShortWikiText( $linker = null ) {

		if ( !$this->isValid() ) {
			return '';
		}

		$id = $this->getDataItem()->getString();

		return '[['. Localizer::getInstance()->getNamespaceTextById( NS_MEDIAWIKI ) . ':' . self::LIST_PREFIX . $id . '|' . $id .']]';
	}

	/**
	 * @see DataValue::getLongHtmlText
	 *
	 * @param string $value
	 */
	public function getLongHtmlText( $linker = null ) {
		return $this->getShortHtmlText( $linker );
	}

	/**
	 * @see DataValue::getShortHtmlText
	 *
	 * @param string $value
	 */
	public function getShortHtmlText( $linker = null ) {

		if ( !$this->isValid() ) {
			return '';
		}

		$id = $this->getDataItem()->getString();
		$title = \Title::newFromText( self::LIST_PREFIX . $id, NS_MEDIAWIKI );

		return \Html::rawElement(
			'a',
			[
				'href'   => $title->getLocalUrl(),
				'target' => '_blank'
			],
			$id
		);
	}

}