summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/QueryDependency/QueryReferenceBacklinks.php
blob: b683627c4b5189a0b1dcac009067f5fc3a4be270 (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
<?php

namespace SMW\SQLStore\QueryDependency;

use SMW\DIProperty;
use SMW\DIWikiPage;
use SMW\Message;
use SMW\RequestOptions;
use SMW\SemanticData;

/**
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
class QueryReferenceBacklinks {

	/**
	 * @var QueryDependencyLinksStore
	 */
	private $queryDependencyLinksStore = null;

	/**
	 * @since 2.5
	 *
	 * @param QueryDependencyLinksStore $queryDependencyLinksStore
	 */
	public function __construct( QueryDependencyLinksStore $queryDependencyLinksStore ) {
		$this->queryDependencyLinksStore = $queryDependencyLinksStore;
	}

	/**
	 * @since 2.5
	 *
	 * @param SemanticData $semanticData
	 * @param RequestOptions|null $requestOptions
	 *
	 * @return boolean
	 */
	public function addReferenceLinksTo( SemanticData $semanticData, RequestOptions $requestOptions = null ) {

		if ( !$this->queryDependencyLinksStore->isEnabled() ) {
			return false;
		}

		// Don't display a reference where the requesting page is
		// part of the list that contains queries (suppress self-embedded queries)
		foreach ( $this->queryDependencyLinksStore->findEmbeddedQueryIdListBySubject( $semanticData->getSubject() ) as $key => $qid ) {
			$requestOptions->addExtraCondition( 's_id!=' . $qid );
		}

		$referenceLinks = $this->findReferenceLinks( $semanticData->getSubject(), $requestOptions );

		$property = new DIProperty(
			'_ASK'
		);

		foreach ( $referenceLinks as $subject ) {
			$semanticData->addPropertyObjectValue( $property, DIWikiPage::doUnserialize( $subject ) );
		}

		return true;
	}

	/**
	 * @since 2.5
	 *
	 * @param DIWikiPage $subject
	 * @param integer $limit
	 * @param integer $offset
	 *
	 * @return array
	 */
	public function findReferenceLinks( DIWikiPage $subject, RequestOptions $requestOptions = null ) {

		$queryTargetLinksHashList = $this->queryDependencyLinksStore->findDependencyTargetLinksForSubject(
			$subject,
			$requestOptions
		);

		return $queryTargetLinksHashList;
	}

	/**
	 * @since 2.5
	 *
	 * @param DIProperty $property
	 * @param DIWikiPage $subject
	 *
	 * @return boolean
	 */
	public function doesRequireFurtherLink( DIProperty $property, DIWikiPage $subject, &$html ) {

		if ( $property->getKey() !== '_ASK' ) {
			return true;
		}

		$localURL = \SpecialPage::getSafeTitleFor( 'SearchByProperty' )->getLocalURL(
			[
				 'property' => $property->getLabel(),
				 'value'    => $subject->getTitle()->getPrefixedText()
			]
		);

		$html .= \Html::element(
			'a',
			[ 'href' => $localURL ],
			Message::get( 'smw_browse_more' )
		);

		// Return false in order to stop the link creation process the replace the
		// generate link
		return false;
	}

}