summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Specials/SpecialPageProperty.php
blob: d1b287f0c6ad96f5a539f705d48eeb1b15088bd2 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php

namespace SMW\MediaWiki\Specials;

use SMW\ApplicationFactory;
use SMW\DataValueFactory;
use SMW\Encoder;
use SMW\MediaWiki\Specials\PageProperty\PageBuilder;
use SMW\Options;
use SMW\RequestOptions;
use SMWInfolink as Infolink;
use SpecialPage;

/**
 * This special page implements a view on a object-relation pair, i.e. a page that
 * shows all the values of a property for a certain page.
 *
 * This is typically used for overflow results from other dynamic output pages.
 *
 * @license GNU GPL v2+
 * @since 1.4
 *
 * @author Denny Vrandecic
 * @author mwjames
 */
class SpecialPageProperty extends SpecialPage {

	/**
	 * @codeCoverageIgnore
	 */
	public function __construct() {
		parent::__construct( 'PageProperty', '', false );
	}

	/**
	 * @see SpecialPage::execute
	 */
	public function execute( $query ) {

		$request = $this->getRequest();

		if ( $request->getText( 'cl', '' ) !== '' ) {
			$query = Infolink::decodeCompactLink( 'cl:'. $request->getText( 'cl' ) );
		} else {
			$query = Infolink::decodeCompactLink( $query );
		}

		if ( $query !== '' ) {
			$query = Encoder::unescape( $query );
		}

		// Get parameters
		$pagename = $request->getVal( 'from' );
		$propname = $request->getVal( 'type' );

		// No GET parameters? Try the URL with the convention `PageName::PropertyName`
		if ( $propname == '' ) {
			$queryparts = explode( '::', $query );
			$propname = $query;
			if ( count( $queryparts ) > 1 ) {
				$pagename = $queryparts[0];
				$propname = implode( '::', array_slice( $queryparts, 1 ) );
			}
		}

		$options = new Options(
			[
				'from' => $pagename,
				'type' => $propname,
				'property' => $propname,
				'limit' => $request->getVal( 'limit', 20 ),
				'offset' => $request->getVal( 'offset', 0 ),
			]
		);

		$this->addHelpLink(
			wfMessage( 'smw-special-pageproperty-helplink' )->escaped(),
			true
		);

		$this->load( $options );
	}

	/**
	 * @see SpecialPage::getGroupName
	 */
	protected function getGroupName() {
		return 'smw_group';
	}

	private function load( $options ) {

		$applicationFactory = ApplicationFactory::getInstance();
		$dataValueFactory = DataValueFactory::getInstance();

		$subject = $dataValueFactory->newTypeIDValue(
			'_wpg',
			$options->get( 'from' )
		);

		$propertyValue = $dataValueFactory->newPropertyValueByLabel(
			$options->get( 'property' )
		);

		$pagename = '';
		$propname = '';

		if ( $subject->isValid() ) {
			$pagename = $subject->getPrefixedText();
		}

		if ( $propertyValue->isValid() ) {
			$propname = $propertyValue->getWikiValue();
		}

		$options->set( 'from', $pagename );
		$options->set( 'property', $propname );
		$options->set( 'type', $propname );

		$htmlFormRenderer = $applicationFactory->newMwCollaboratorFactory()->newHtmlFormRenderer(
			$this->getContext()->getTitle(),
			$this->getLanguage()
		);

		$pageBuilder = new PageBuilder(
			$htmlFormRenderer,
			$options
		);

		$html = '';

		// No property given, no results
		if ( $propname === '' ) {
			$html .= $pageBuilder->buildForm();
			$html .= wfMessage( 'smw_result_noresults' )->text();
		} else {

			$requestOptions = new RequestOptions();
			$requestOptions->setLimit( $options->get( 'limit' ) + 1 );
			$requestOptions->setOffset( $options->get( 'offset' ) );
			$requestOptions->sort = true;

			// Restrict the request otherwise the entire SemanticData record
			// is fetched which can in case of a subject with a large
			// subobject/subpage pool create excessive DB queries that are not
			// used for the display
			$requestOptions->conditionConstraint = true;

			$dataItem = $pagename !== '' ? $subject->getDataItem() : null;

			$results = $applicationFactory->getStore()->getPropertyValues(
				$dataItem,
				$propertyValue->getDataItem(),
				$requestOptions
			);

			$html .= $pageBuilder->buildForm( count( $results ) );
			$html .= $pageBuilder->buildHtml( $results );
		}

		$output = $this->getOutput();
		$output->setPagetitle( wfMessage( 'pageproperty' )->text() );

		$output->addModuleStyles( 'ext.smw.special.style' );
		$output->addModules( 'ext.smw.tooltip' );

		$output->addModules( 'ext.smw.autocomplete.property' );
		$output->addModules( 'ext.smw.autocomplete.article' );

		$output->addHTML( $html );
	}

}