summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/MediaWiki/Search/Form/FormsFinder.php
blob: 6eda37e57fa21665d52c60a1e0cfe3db14307f59 (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
<?php

namespace SMW\MediaWiki\Search\Form;

use SMW\DIProperty;
use SMW\MediaWiki\Search\SearchProfileForm;
use SMW\RequestOptions;
use SMW\Store;
use SMWDIBlob as DIBlob;
use Title;
use WikiPage;

/**
 * @private
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class FormsFinder {

	/**
	 * @var Store
	 */
	private $store;

	/**
	 * @since 3.0
	 *
	 * @param Store $store
	 */
	public function __construct( Store $store ) {
		$this->store = $store;
	}

	/**
	 * @since 3.0
	 *
	 * @return array
	 */
	public function getFormDefinitions() {

		$data = [];
		$requestOptions = new RequestOptions();
		$requestOptions->setOption( 'DISTINCT', false );

		$subjects = $this->store->getPropertySubjects(
			new DIProperty( '_SCHEMA_TYPE' ),
			new DIBlob( SearchProfileForm::SCHEMA_TYPE ),
			$requestOptions
		);

		foreach ( $subjects as $subject ) {

			if ( ( $nativeData = $this->getNativeData( $subject->getTitle() ) ) === '' ) {
				continue;
			}

			$d = json_decode( $nativeData, true );

			if ( json_last_error() !== JSON_ERROR_NONE ) {
				continue;
			}

			$data = array_merge_recursive( $data, $d );
		}

		return $data;
	}

	protected function getNativeData( $title ) {

		if ( $title === null ) {
			return '';
		}

		$content = WikiPage::factory( $title )->getContent();

		if ( $content === null ) {
			return '';
		}

		return $content->getNativeData();
	}

}