Skip to content

Commit be359f1

Browse files
committed
Split up error prone do-everything-under-the-sun function addDataToPage(), with tests
1 parent 860aff8 commit be359f1

2 files changed

Lines changed: 113 additions & 29 deletions

File tree

classes/parsers/RDFIO_ARC2ToWikiConverter.php

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ public function convert( $arc2Triples, $arc2ResourceIndex, $arc2NSPrefixes ) {
3636
$catPageTitle = $uriToTitleConv->convert( $triple['o'] );
3737
$catPageTitleWithNS = Title::makeTitleSafe( NS_CATEGORY, $catPageTitle )->getFullText();
3838
// Add data for the subject page
39-
$this->addDataToPage( $wikiPageTitle, $triple['s'], $fact = null, $catPageTitleWithNS );
39+
$this->addEquivUriToPage( $triple['s'], $wikiPageTitle );
40+
$this->addCategoryToPage( $catPageTitleWithNS, $wikiPageTitle );
4041
// Add data for the category page
41-
$this->addDataToPage( $catPageTitleWithNS, $triple['o'] );
42+
$this->addEquivUriToPage( $triple['o'], $catPageTitleWithNS );
4243

4344
} else if ( $triple['p'] === 'http://www.w3.org/2000/01/rdf-schema#subClassOf' ) {
4445

@@ -48,18 +49,19 @@ public function convert( $arc2Triples, $arc2ResourceIndex, $arc2NSPrefixes ) {
4849
$pageTitleWithNS = Title::makeTitleSafe( NS_CATEGORY, $wikiPageTitle )->getFullText();
4950

5051
// Add data for the subject page
51-
$this->addDataToPage( $pageTitleWithNS, $triple['s'], $fact = null, $catPageTitleWithNS );
52+
$this->addEquivUriToPage( $triple['s'], $pageTitleWithNS );
53+
$this->addCategoryToPage( $catPageTitleWithNS, $pageTitleWithNS );
5254

5355
// Add data for the category page
54-
$this->addDataToPage( $catPageTitleWithNS, $triple['o'] );
56+
$this->addEquivUriToPage( $triple['o'], $catPageTitleWithNS );
5557

5658
} else {
5759
// Separate handling for properties
5860
$propertyTitle = $uriToPropTitleConv->convert( $triple['p'] );
5961
// Add the property namespace to property title
6062
$propTitleWithNS = Title::makeTitleSafe( SMW_NS_PROPERTY, $propertyTitle )->getFullText();
6163
// Add Equivalent URI to property page
62-
$this->addDataToPage( $propTitleWithNS, $triple['p'] );
64+
$this->addEquivUriToPage( $triple['p'], $propTitleWithNS );
6365

6466
/*
6567
* Decide whether to create a page for the linked "object" or not,
@@ -70,13 +72,13 @@ public function convert( $arc2Triples, $arc2ResourceIndex, $arc2NSPrefixes ) {
7072
case 'bnode':
7173
case 'uri':
7274
// Create new page for the object
73-
$object = $uriToTitleConv->convert( $triple['o'] );
74-
$this->addDataToPage( $object, $triple['o'] );
75+
$objectTitleOrVal = $uriToTitleConv->convert( $triple['o'] );
76+
$this->addEquivUriToPage( $triple['o'], $objectTitleOrVal );
7577
// Since URIs convert to pages, properties linking to URIs will be of 'Page' type
7678
$propertyDataType = 'Page';
7779
break;
7880
case 'literal':
79-
$object = $triple['o'];
81+
$objectTitleOrVal = $triple['o'];
8082

8183
// Determine data type of property
8284
$xsd = 'http://www.w3.org/2001/XMLSchema#';
@@ -116,7 +118,7 @@ public function convert( $arc2Triples, $arc2ResourceIndex, $arc2NSPrefixes ) {
116118
$propertyDataType = 'URL';
117119
break;
118120
default:
119-
if ( substr( $object, 0, 4 ) === 'http' ) {
121+
if ( substr( $objectTitleOrVal, 0, 4 ) === 'http' ) {
120122
$propertyDataType = 'URL';
121123
break;
122124
}
@@ -131,46 +133,79 @@ public function convert( $arc2Triples, $arc2ResourceIndex, $arc2NSPrefixes ) {
131133
// NOTE: This is important to do BEFORE adding any fact using the property,
132134
// in order for the fact to get correct encoding in the ARC2 store.
133135
if( !is_null( $propertyDataType ) ) {
134-
$this->addDataToPage( $propTitleWithNS, null, null, null, $propertyDataType );
136+
$this->addDataTypeToPage( $propertyDataType, $propTitleWithNS );
135137
}
136138

137139
// Create a fact array
138-
$fact = array( 'p' => $propertyTitle, 'o' => $object );
140+
$fact = array( 'p' => $propertyTitle, 'o' => $objectTitleOrVal );
139141
// Add data to class variables
140-
$this->addDataToPage( $wikiPageTitle, $triple['s'], $fact );
142+
$this->addEquivUriToPage( $triple['s'], $wikiPageTitle );
143+
$this->addFactToPage( $fact, $wikiPageTitle );
141144
}
142145
}
143146

144147
return $this->wikiPages;
145148
}
146149

150+
147151
/**
148-
* Convenience function to add a $fact (predicate/object tuple) or $category into
149-
* the RDFIOWikiPage according to the title specified in $pageTitle
150-
* @param string $pageTitle
151152
* @param string $equivURI
152-
* @param string $fact
153-
* @param string $category
153+
* @param string $pageTitle
154154
*/
155-
private function addDataToPage( $pageTitle, $equivURI = null, $fact = null, $category = null, $dataType = null ) {
156-
// Create page array if not exists in array
157-
if ( !array_key_exists( $pageTitle, $this->wikiPages ) ) {
158-
$this->wikiPages[$pageTitle] = new RDFIOWikiPage( $pageTitle );
159-
}
155+
private function addEquivUriToPage( $equivURI, $pageTitle ) {
160156
if ( !is_null( $equivURI ) ) {
161-
$this->wikiPages[$pageTitle]->addEquivalentURI( $equivURI );
157+
$this->getPage( $pageTitle )->addEquivalentURI( $equivURI );
162158
}
159+
}
160+
161+
/**
162+
* @param array $fact
163+
* @param string $pageTitle
164+
*/
165+
private function addFactToPage( $fact, $pageTitle ) {
163166
if ( !is_null( $fact ) ) {
164-
$this->wikiPages[$pageTitle]->addFact( $fact );
167+
$this->getPage( $pageTitle )->addFact( $fact );
165168
}
169+
}
170+
171+
/**
172+
* @param string $category
173+
* @param string $pageTitle
174+
*/
175+
private function addCategoryToPage( $category, $pageTitle ) {
166176
if ( !is_null( $category ) ) {
167-
$this->wikiPages[$pageTitle]->addCategory( $category );
177+
$this->getPage( $pageTitle )->addCategory( $category );
168178
}
179+
}
180+
181+
/**
182+
* @param string $dataType
183+
* @param string $pageTitle
184+
*/
185+
private function addDataTypeToPage( $dataType, $pageTitle ) {
169186
if ( !is_null( $dataType ) ) {
170-
$this->wikiPages[$pageTitle]->addDataType( $dataType );
187+
$this->getPage( $pageTitle )->addDataType( $dataType );
188+
}
189+
}
190+
191+
/**
192+
* @param string $pageTitle
193+
*/
194+
private function ensurePageExists( $pageTitle ) {
195+
// Create page array if not exists in array
196+
if ( !array_key_exists( $pageTitle, $this->wikiPages ) ) {
197+
$this->wikiPages[$pageTitle] = new RDFIOWikiPage( $pageTitle );
171198
}
172199
}
173200

201+
/**
202+
* @param string $pageTitle
203+
* @return RDFIOWikiPage $page
204+
*/
205+
private function getPage( $pageTitle ) {
206+
$this->ensurePageExists( $pageTitle );
207+
return $this->wikiPages[$pageTitle];
208+
}
174209
}
175210

176211
class RDFIOARC2ToWikiConverterException extends MWException {

tests/phpunit/classes/parsers/RDFIO_ARC2ToWikiConverterTest.php

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
class RDFIOARC2ToWikiConverterTest extends MediaWikiTestCase {
3+
class RDFIOARC2ToWikiConverterTest extends RDFIOTestCase {
44

55
protected function setUp() {
66
parent::setUp();
@@ -23,10 +23,59 @@ public function testConvert() {
2323
$tripleIndex = $arc2rdfxmlparser->getSimpleIndex();
2424
$namespaces = $arc2rdfxmlparser->nsp;
2525

26-
$arc2towikiconverter = new RDFIOARC2ToWikiConverter();
27-
$wikiPages = $arc2towikiconverter->convert( $triples, $tripleIndex, $namespaces );
26+
$arc2ToWikiConv = new RDFIOARC2ToWikiConverter();
27+
$wikiPages = $arc2ToWikiConv->convert( $triples, $tripleIndex, $namespaces );
2828

2929
$this->assertEquals( 12, count( $wikiPages ), "No wiki pages converted from triples!" );
3030
}
3131

32+
public function testAddEquivUriToPage() {
33+
$arc2ToWiki = new RDFIOARC2ToWikiConverter();
34+
35+
$equivUri = 'http://example.org/example_uri';
36+
$pageTitle = 'ExamplePage';
37+
38+
$this->invokeMethod( $arc2ToWiki, 'addEquivUriToPage', array( $equivUri, $pageTitle ) );
39+
$page = $this->invokeMethod( $arc2ToWiki, 'getPage', array( $pageTitle ) );
40+
41+
$equivUriExists = $this->invokeMethod( $page, 'equivalentURIExists', array( $equivUri ) );
42+
$this->assertTrue( $equivUriExists );
43+
}
44+
45+
public function testAddFactToPage() {
46+
$arc2ToWiki = new RDFIOARC2ToWikiConverter();
47+
48+
$fact = array( 'p' => 'example_property', 'o' => 'example_object' );
49+
$pageTitle = 'ExamplePage';
50+
51+
$this->invokeMethod( $arc2ToWiki, 'addFactToPage', array( $fact, $pageTitle ) );
52+
$page = $this->invokeMethod( $arc2ToWiki, 'getPage', array( $pageTitle ) );
53+
54+
$facts = $this->invokeMethod( $page, 'getFacts' );
55+
$this->assertArrayEquals( $fact, $facts[0] );
56+
}
57+
58+
public function testAddCategoryToPage() {
59+
$arc2ToWiki = new RDFIOARC2ToWikiConverter();
60+
61+
$category = 'ExampleCategory';
62+
$pageTitle = 'ExamplePage';
63+
64+
$this->invokeMethod( $arc2ToWiki, 'addCategoryToPage', array( $category, $pageTitle ) );
65+
$page = $this->invokeMethod( $arc2ToWiki, 'getPage', array( $pageTitle ) );
66+
67+
}
68+
69+
public function testAddDataTypeToPage() {
70+
$arc2ToWiki = new RDFIOARC2ToWikiConverter();
71+
72+
$dataType = 'Telephone number';
73+
$pageTitle = 'ExamplePage';
74+
75+
$this->invokeMethod( $arc2ToWiki, 'addDataTypeToPage', array( $dataType, $pageTitle ) );
76+
$page = $this->invokeMethod( $arc2ToWiki, 'getPage', array( $pageTitle ) );
77+
78+
$facts = $this->invokeMethod( $page, 'getFacts' );
79+
$this->assertArrayEquals( array( 'p' => 'Has type', 'o' => $dataType ), $facts[0] );
80+
}
3281
}

0 commit comments

Comments
 (0)