-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateFromFile.smw.php
103 lines (78 loc) · 2.87 KB
/
CreateFromFile.smw.php
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
<?php
if (!defined('MEDIAWIKI')) { die(-1); }
if ( !defined( 'SMW_VERSION' ) ) {
echo 'This extension requires Semantic MediaWiki to be installed.';
exit( 1 );
}
/**
* This class handles SMW search for CreateFromFile.
*/
class CreateFromFileSMW {
public static function searchJS( $rows ) {
$properties = array();
$results = "";
$existingArr = array();
foreach ( $rows as $row ) {
$host = array();
$query = array();
foreach ( $row as $key => $value ) {
array_push( $query, "[[$key::$value]]" );
$host[$key] = $value;
}
$results = self::getQueryResults( implode("", $query), $properties, false );
while ( $result = $results->getNext() ) {
// TODO: Here we should inform which value already exists! -> Ideally even modifying somewhere in the spreadsheet
array_push( $existingArr, $host );
}
}
return json_encode( $existingArr );
}
/**
* This function returns to results of a certain query
* Thank you Yaron Koren for advices concerning this code
* @param $query_string String : the query
* @param $properties_to_display array(String): array of property names to display
* @param $display_title Boolean : add the page title in the result
* @return TODO
*/
static function getQueryResults( $query_string, $properties_to_display, $display_title ) {
// We use the Semantic MediaWiki Processor
// $smwgIP is defined by Semantic MediaWiki, and we don't allow
// this file to be sourced unless Semantic MediaWiki is included.
global $smwgIP;
if ( file_exists( $smwgIP . "/includes/SMW_QueryProcessor.php") ) {
include_once( $smwgIP . "/includes/SMW_QueryProcessor.php" );
} else {
include_once( $smwgIP . "/includes/query/SMW_QueryProcessor.php" );
}
$params = array();
$inline = true;
$printlabel = "";
$printouts = array();
// add the page name to the printouts
if ( $display_title ) {
$to_push = new SMWPrintRequest( SMWPrintRequest::PRINT_THIS, $printlabel );
array_push( $printouts, $to_push );
}
// Push the properties to display in the printout array.
foreach ( $properties_to_display as $property ) {
if ( class_exists( 'SMWPropertyValue' ) ) { // SMW 1.4
$to_push = new SMWPrintRequest( SMWPrintRequest::PRINT_PROP, $printlabel, SMWPropertyValue::makeProperty( $property ) );
} else {
$to_push = new SMWPrintRequest( SMWPrintRequest::PRINT_PROP, $printlabel, Title::newFromText( $property, SMW_NS_PROPERTY ) );
}
array_push( $printouts, $to_push );
}
if ( version_compare( SMW_VERSION, '1.6.1', '>' ) ) {
SMWQueryProcessor::addThisPrintout( $printouts, $params );
$params = SMWQueryProcessor::getProcessedParams( $params, $printouts );
$format = null;
}
else {
$format = 'auto';
}
$query = SMWQueryProcessor::createQuery( $query_string, $params, $inline, $format, $printouts );
$results = smwfGetStore()->getQueryResult( $query );
return $results;
}
}