summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/PropertyAnnotators/EditProtectedPropertyAnnotator.php
blob: 8bb392f0aa9f1ca1662c8e98691a4978494dca96 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php

namespace SMW\PropertyAnnotators;

use Html;
use ParserOutput;
use SMW\Message;
use SMW\PropertyAnnotator;
use Title;

/**
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
class EditProtectedPropertyAnnotator extends PropertyAnnotatorDecorator {

	/**
	 * Indicates whether the annotation was maintained by
	 * the system or not.
	 */
	const SYSTEM_ANNOTATION = 'editprotectedpropertyannotator.system.annotation';

	/**
	 * @var Title
	 */
	private $title;

	/**
	 * @var boolean
	 */
	private $editProtectionRight = false;

	/**
	 * @since 1.9
	 *
	 * @param PropertyAnnotator $propertyAnnotator
	 * @param Title $title
	 */
	public function __construct( PropertyAnnotator $propertyAnnotator, Title $title ) {
		parent::__construct( $propertyAnnotator );
		$this->title = $title;
	}

	/**
	 * @since 2.5
	 *
	 * @param string|boolean $editProtectionRight
	 */
	public function setEditProtectionRight( $editProtectionRight ) {
		$this->editProtectionRight = $editProtectionRight;
	}

	/**
	 * @since 2.5
	 *
	 * @param ParserOutput
	 */
	public function addTopIndicatorTo( ParserOutput $parserOutput ) {

		if ( $this->editProtectionRight === false ) {
			return false;
		}

		// FIXME 3.0; Only MW 1.25+ (ParserOutput::setIndicator)
		if ( !method_exists( $parserOutput, 'setIndicator' ) ) {
			return false;
		}

		$property = $this->dataItemFactory->newDIProperty( '_EDIP' );

		if ( !$this->isEnabledProtection( $property ) && !$this->hasEditProtection() ) {
			return;
		}

		$html = Html::rawElement(
			'div',
			[
				'class' => 'smw-edit-protection',
				'title' => Message::get( 'smw-edit-protection-enabled', Message::TEXT, Message::USER_LANGUAGE )
			], ''
		);

		$parserOutput->setIndicator(
			'smw-protection-indicator',
			Html::rawElement( 'div', [ 'class' => 'smw-protection-indicator' ], $html )
		);
	}

	/**
	 * @see PropertyAnnotatorDecorator::addPropertyValues
	 */
	protected function addPropertyValues() {

		if ( $this->editProtectionRight === false ) {
			return false;
		}

		$property = $this->dataItemFactory->newDIProperty( '_EDIP' );

		if ( $this->getSemanticData()->hasProperty( $property ) || !$this->hasEditProtection() ) {
			return;
		}

		// Notify preceding processes that this property is set as part of the
		// protection restriction detection in order to decide whether this
		// property was added manually or by the system
		$dataItem = $this->dataItemFactory->newDIBoolean( true );
		$dataItem->setOption( self::SYSTEM_ANNOTATION, true );

		// Since edit protection is active, add the property as indicator this is
		// especially to retain the status when purging a page
		$this->getSemanticData()->addPropertyObjectValue(
			$property,
			$dataItem
		);
	}

	private function hasEditProtection() {

		//$this->title->flushRestrictions();

		if ( !$this->title->isProtected( 'edit' ) ) {
			return false;
		}

		$restrictions = array_flip( $this->title->getRestrictions( 'edit' ) );

		// There could by any edit protections but the `Is edit protected` is
		// bound to the `smwgEditProtectionRight` setting
		return isset( $restrictions[$this->editProtectionRight] );
	}

	private function isEnabledProtection( $property ) {

		if ( !$this->getSemanticData()->hasProperty( $property ) ) {
			return false;
		}

		$semanticData = $this->getSemanticData();

		$dataItems = $semanticData->getPropertyValues( $property );
		$isEnabledProtection = false;

		// In case of two competing values, true always wins
		foreach ( $dataItems as $dataItem ) {

			$isEnabledProtection = $dataItem->getBoolean();

			if ( $isEnabledProtection ) {
				break;
			}
		}

		return $isEnabledProtection;
	}

}