forked from wikimedia/mediawiki-extensions-SemanticGenealogy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersonPageValues.php
227 lines (211 loc) · 6.04 KB
/
PersonPageValues.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
* Model object that store genealogical data of a person
*
* @file PersonPageValues.php
* @ingroup SemanticGenealogy
*
* @licence GNU GPL v2+
* @author Thomas Pellissier Tanon <thomaspt@hotmail.fr>
*/
class PersonPageValues {
protected $page;
public $title;
public $fullname;
public $surname;
public $givenname;
public $nickname;
public $prefix;
public $suffix;
public $sex;
public $birthdate;
public $birthplace;
public $deathdate;
public $deathplace;
public $father;
public $mother;
public $partner;
protected $children;
/**
* Constructor for a single indi in the file.
*
* @param SMWDIWikiPage $page
*/
public function __construct( SMWDIWikiPage $page ) {
$values = [];
$storage = smwfGetStore();
$this->page = $page;
$this->title = $page->getTitle();
$properties = SemanticGenealogy::getProperties();
foreach ( $properties as $key => $prop ) {
$values = $storage->getPropertyValues( $page, $prop );
if ( count( $values ) != 0 && property_exists( 'PersonPageValues', $key ) ) {
$this->$key = $values[0];
}
}
if ( !( $this->fullname instanceof SMWDIBlob ) ) {
if ( $this->surname instanceof SMWDIBlob && $this->surname->getString() != '' ) {
$fullname = '';
if ( $this->givenname instanceof SMWDIBlob ) {
$fullname .= $this->givenname->getString() . ' ';
}
$this->fullname = new SMWDIBlob( $fullname . $this->surname->getString() );
} else {
$this->fullname = new SMWDIBlob( $this->title->getText() );
}
}
}
/**
* Get the page of the person
*
* @return SMWDIWikiPage the current page
*/
public function getPage() {
return $this->page;
}
/**
* Return all the children as PersonPageValues
*
* @return array
*/
public function getChildren() {
if ( $this->children !== null ) {
return $this->children;
}
$this->children = [];
$storage = smwfGetStore();
$properties = SemanticGenealogy::getProperties();
if ( $properties['father'] instanceof SMWDIProperty ) {
$childrenPage = $storage->getPropertySubjects( $properties['father'], $this->page );
foreach ( $childrenPage as $page ) {
$this->children[] = new PersonPageValues( $page );
}
}
if ( $properties['mother'] instanceof SMWDIProperty ) {
$childrenPage = $storage->getPropertySubjects( $properties['mother'], $this->page );
foreach ( $childrenPage as $page ) {
$this->children[] = new PersonPageValues( $page );
}
}
usort( $this->children, [ "PersonPageValues", "comparePeopleByBirthDate" ] );
return $this->children;
}
/**
* Return the partner
*
* @return array
*/
public function getPartner() {
if ( $this->partner !== null ) {
return $this->partner;
}
$storage = smwfGetStore();
$properties = SemanticGenealogy::getProperties();
if ( $properties['partner'] instanceof SMWDIProperty ) {
$page = $storage->getPropertySubjects( $properties['father'], $this->page );
if ( $page instanceof SMWDIWikiPage ) {
$this->partner = new PersonPageValues( $page );
}
}
return $this->partner;
}
/**
* Sorter to compare 2 persons based on their date of birth
*
* @return integer a comparaison integer
*/
private static function comparePeopleByBirthDate( PersonPageValues $personA,
PersonPageValues $personB ) {
if ( $personA->birthdate instanceof SMWDITime ) {
$aKey = $personA->birthdate->getSortKey();
} else {
$aKey = 3000;
}
if ( $personB->birthdate instanceof SMWDITime ) {
$bKey = $personB->birthdate->getSortKey();
} else {
$bKey = 3000;
}
if ( $bKey < $aKey ) {
return 1;
} elseif ( $bKey == $aKey ) {
return 0;
} else {
return -1;
}
}
/**
* Get the correct name to display a person (either the fullname, or the pagename)
*
* @param string $displayName the name to display
*
* @return string the name of the person
*/
public function getPersonName( $displayName ) {
if ( $displayName == 'pagename' ) {
return $this->title->getFullText();
} elseif ( $displayName == 'fullname' ) {
return $this->fullname->getString();
}
return $this->title->getFullText();
}
/**
* Generate the Person description wiki text based on the special pages options
*
* @param boolean $withBr adding <br> tags or not
* @param string $displayName the display type tag
*
* @return string the text to display
*/
public function getDescriptionWikiText( $withBr = false, $displayName = 'fullname' ) {
$yearRegexp = "/.*\b(\d\d\d\d)\b.*/";
$text = '<div class="person-block">';
$text .= '<div class="person-name">';
$text .= '[[' . $this->title->getFullText() . '|' . $this->getPersonName( $displayName ). ']]';
$text .= '</div>';
if ( $this->birthdate || $this->deathdate ) {
$text .= '<span class="person-dates">';
if ( $withBr ) {
$text .= '<br />';
}
$text .= '(';
if ( $this->birthdate instanceof SMWDITime ) {
$text .= static::getWikiTextDateFromSMWDITime( $this->birthdate ) . ' ';
} elseif ( is_string( $this->birthdate ) && preg_match( $yearRegexp, $this->birthdate ) ) {
$text .= preg_replace( $yearRegexp, "$1", $this->birthdate );
}
$text .= '-';
if ( $this->deathdate instanceof SMWDITime ) {
$text .= ' ' . static::getWikiTextDateFromSMWDITime( $this->deathdate );
} elseif ( is_string( $this->deathdate ) && preg_match( $yearRegexp, $this->deathdate ) ) {
$text .= preg_replace( $yearRegexp, "$1", $this->deathdate );
}
$text .= ')</span>';
}
$text .= '</div>';
return $text;
}
/**
* Get a string base on the SMWDITime object
*
* @param SMWDITime $dataItem the time item
*
* @return string the wiki text for a given date
*/
protected static function getWikiTextDateFromSMWDITime( SMWDITime $dataItem ) {
$val = new SMWTimeValue( SMWDataItem::TYPE_TIME );
$val->setDataItem( $dataItem );
return $val->getShortWikiText();
}
/**
* Get the page from the pageName
*
* @param string $pageName the page name
*
* @return SMWDIWikiPage the page object
*/
public static function getPageFromName( $pageName ) {
$pageTitle = Title::newFromText( $pageName );
return SMWDIWikiPage::newFromTitle( $pageTitle );
}
}