summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Query/ScoreSet.php
blob: 5ac38724005d50f12476edc248f15994113965d3 (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
<?php

namespace SMW\Query;

use SMW\DIWikiPage;

/**
 * Record scores for query results retrieved from stores that support the computation
 * of relevance scores.
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class ScoreSet {

	/**
	 * @var []
	 */
	private $scores = [];

	/**
	 * @var integer|null
	 */
	private $max_score = null;

	/**
	 * @var integer|null
	 */
	private $min_score = null;

	/**
	 * @since 3.0
	 *
	 * @param string|integer $max_score
	 */
	public function max_score( $max_score ) {
		$this->max_score = $max_score;
	}

	/**
	 * @since 3.0
	 *
	 * @param string|integer $min_score
	 */
	public function min_score( $min_score ) {
		$this->min_score = $min_score;
	}

	/**
	 * @note The hash is expected to match DIWikiPage::getHash to easily match
	 * result subjects available in an QueryResult instance.
	 *
	 * @since 3.0
	 *
	 * @param DIWikiPage|string $hash
	 * @param string|integer $score
	 */
	public function addScore( $hash, $score, $pos = null ) {

		if ( $hash instanceof DIWikiPage ) {
			$hash = $hash->getHash();
		}

		if ( $pos === null ) {
			$this->scores[] = [ $hash, $score ];
		} else {
			$this->scores[$pos] = [ $hash, $score ];
		}
	}

	/**
	 * @since 3.0
	 *
	 * @param DIWikiPage|string $hash
	 *
	 * @return string|integer|false
	 */
	public function getScore( $hash ) {

		if ( $hash instanceof DIWikiPage ) {
			$hash = $hash->getHash();
		}

		foreach ( $this->scores as $map ) {
			if ( $map[0] === $hash ) {
				return $map[1];
			}
		}

		return false;
	}

	/**
	 * @since 3.0
	 *
	 * @return []
	 */
	public function getScores() {
		return $this->scores;
	}

	/**
	 * @since 3.0
	 *
	 * @param boolean $usort
	 */
	public function usort( $usort ) {

		if ( !$usort|| $this->scores === [] ) {
			return;
		}

		usort( $this->scores, function( $a, $b ) {

			if ( $a[1] == $b[1] ) {
				return 0;
			}

			return ( $a[1] > $b[1] ) ? -1 : 1;
		} );
	}

	/**
	 * @since 3.0
	 *
	 * @param string $class
	 *
	 * @return string
	 */
	public function asTable( $class = '' ) {

		if ( $this->scores === [] ) {
			return '';
		}

		$table = "<table class='$class'><thead>";
		$table .= "<th>Score</th><th>Subject</th><th><span title='Sorting position'>Pos</span></th>";
		$table .= "</thead><tbody>";

		ksort( $this->scores );

		foreach ( $this->scores as $pos => $set ) {
			$table .= '<tr><td>' . $set[1] . '</td><td>' . $set[0] . '</td><td>' . $pos . '</td></tr>';
		}

		$table .= '</tbody></table>';

		return $table;
	}

}