00001 <?php
00020 class SRFvCard extends SMWResultPrinter {
00021
00022 protected $m_title = '';
00023 protected $m_description = '';
00024
00025 public function getMimeType( $res ) {
00026 return 'text/x-vcard';
00027 }
00028
00029 public function getFileName( $res ) {
00030 if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) {
00031 return str_replace( ' ', '_', $this->getSearchLabel( SMW_OUTPUT_WIKI ) ) . '.vcf';
00032 } else {
00033 return 'vCard.vcf';
00034 }
00035 }
00036
00037 public function getQueryMode( $context ) {
00038 return ( $context == SMWQueryProcessor::SPECIAL_PAGE ) ? SMWQuery::MODE_INSTANCES : SMWQuery::MODE_NONE;
00039 }
00040
00041 public function getName() {
00042 return wfMsg( 'srf_printername_vcard' );
00043 }
00044
00045 protected function getResultText( SMWQueryResult $res, $outputmode ) {
00046 global $wgSitename;
00047 $result = '';
00048 $items = array();
00049 if ( $outputmode == SMW_OUTPUT_FILE ) {
00050 if ( $this->m_title == '' ) {
00051 $this->m_title = $wgSitename;
00052 }
00053 $row = $res->getNext();
00054 while ( $row !== false ) {
00055 $wikipage = $row[0]->getResultSubject();
00056
00057 $prefix = '';
00058 $firstname = '';
00059 $additionalname = '';
00060 $lastname = '';
00061 $suffix = '';
00062 $fullname = '';
00063
00064 $emails = array();
00065 $tels = array();
00066 $addresses = array();
00067
00068 $organization = '';
00069 $jobtitle = '';
00070 $role = '';
00071 $department = '';
00072
00073 $category = '';
00074 $birthday = '';
00075 $url = '';
00076 $note = '';
00077 $workaddress = false;
00078 $homeaddress = false;
00079
00080 $workpostofficebox = '';
00081 $workextendedaddress = '';
00082 $workstreet = '';
00083 $worklocality = '';
00084 $workregion = '';
00085 $workpostalcode = '';
00086 $workcountry = '';
00087
00088
00089 $homepostofficebox = '';
00090 $homeextendedaddress = '';
00091 $homestreet = '';
00092 $homelocality = '';
00093 $homeregion = '';
00094 $homepostalcode = '';
00095 $homecountry = '';
00096
00097 foreach ( $row as $field ) {
00098
00099
00100
00101 $req = $field->getPrintRequest();
00102
00103 switch( strtolower( $req->getLabel() ) ) {
00104 case "name":
00105 $value = $field->getNextDataValue();
00106 if ( $value !== false ) {
00107 $fullname = $value->getShortWikiText();
00108 }
00109 break;
00110
00111 case "prefix":
00112 while( $value = $field->getNextDataValue() ) {
00113 $prefix .= ( $prefix ? ',':'' ) . $value->getShortWikiText();
00114 }
00115 break;
00116
00117 case "suffix":
00118 while( $value = $field->getNextDataValue() ) {
00119 $suffix .= ( $suffix ? ',':'' ) . $value->getShortWikiText();
00120 }
00121 break;
00122
00123 case "firstname":
00124 $value = $field->getNextDataValue();
00125 if ( $value !== false ) {
00126 $firstname = $value->getShortWikiText();
00127 }
00128 break;
00129
00130 case "extraname":
00131 while( $value = $field->getNextDataValue() ) {
00132 $additionalname .= ( $additionalname ? ',':'' ) . $value->getShortWikiText();
00133 }
00134 break;
00135
00136 case "lastname":
00137 $value = $field->getNextDataValue();
00138 if ( $value !== false ) {
00139 $lastname = $value->getShortWikiText();
00140 }
00141 break;
00142
00143 case "note":
00144 while( $value = $field->getNextDataValue() ) {
00145 $note .= ( $note ? ', ':'' ) . $value->getShortWikiText();
00146 }
00147 break;
00148
00149 case "email":
00150 while( $value = $field->getNextDataValue() ) {
00151 $emails[] = new SRFvCardEmail( 'internet', $value->getShortWikiText() );
00152 }
00153 break;
00154
00155 case "workphone":
00156 while( $value = $field->getNextDataValue() ) {
00157 $tels[] = new SRFvCardTel( 'WORK', $value->getShortWikiText() );
00158 }
00159 break;
00160
00161 case "cellphone":
00162 while( $value = $field->getNextDataValue() ) {
00163 $tels[] = new SRFvCardTel( 'CELL', $value->getShortWikiText() );
00164 }
00165 break;
00166
00167 case "homephone":
00168 while( $value = $field->getNextDataValue() ) {
00169 $tels[] = new SRFvCardTel( 'HOME', $value->getShortWikiText() );
00170 }
00171 break;
00172
00173 case "organization":
00174 $value = $field->getNextDataValue();
00175 if ( $value !== false ) {
00176 $organization = $value->getShortWikiText();
00177 }
00178 break;
00179
00180 case "workpostofficebox":
00181 $value = $field->getNextDataValue();
00182 if ( $value !== false ) {
00183 $workpostofficebox = $value->getShortWikiText();
00184 $workaddress = true;
00185 }
00186 break;
00187
00188 case "workextendedaddress":
00189 $value = $field->getNextDataValue();
00190 if ( $value !== false ) {
00191 $workextendedaddress = $value->getShortWikiText();
00192 $workaddress = true;
00193 }
00194 break;
00195
00196 case "workstreet":
00197 $value = $field->getNextDataValue();
00198 if ( $value !== false ) {
00199 $workstreet = $value->getShortWikiText();
00200 $workaddress = true;
00201 }
00202 break;
00203
00204 case "worklocality":
00205 $value = $field->getNextDataValue();
00206 if ( $value !== false ) {
00207 $worklocality = $value->getShortWikiText();
00208 $workaddress = true;
00209 }
00210 break;
00211
00212 case "workregion":
00213 $value = $field->getNextDataValue();
00214 if ( $value !== false ) {
00215 $workregion = $value->getShortWikiText();
00216 $workaddress = true;
00217 }
00218 break;
00219
00220 case "workpostalcode":
00221 $value = $field->getNextDataValue();
00222 if ( $value !== false ) {
00223 $workpostalcode = $value->getShortWikiText();
00224 $workaddress = true;
00225 }
00226 break;
00227
00228 case "workcountry":
00229 $value = $field->getNextDataValue();
00230 if ( $value !== false ) {
00231 $workcountry = $value->getShortWikiText();
00232 $workaddress = true;
00233 }
00234 break;
00235
00236 case "homepostofficebox":
00237 $value = $field->getNextDataValue();
00238 if ( $value !== false ) {
00239 $homepostofficebox = $value->getShortWikiText();
00240 $homeaddress = true;
00241 }
00242 break;
00243
00244 case "homeextendedaddress":
00245 $value = $field->getNextDataValue();
00246 if ( $value !== false ) {
00247 $homeextendedaddress = $value->getShortWikiText();
00248 $homeaddress = true;
00249 }
00250 break;
00251
00252 case "homestreet":
00253 $value = $field->getNextDataValue();
00254 if ( $value !== false ) {
00255 $homestreet = $value->getShortWikiText();
00256 $homeaddress = true;
00257 }
00258 break;
00259
00260 case "homelocality":
00261 $value = $field->getNextDataValue();
00262 if ( $value !== false ) {
00263 $homelocality = $value->getShortWikiText();
00264 $homeaddress = true;
00265 }
00266 break;
00267
00268 case "homeregion":
00269 $value = $field->getNextDataValue();
00270 if ( $value !== false ) {
00271 $homeregion = $value->getShortWikiText();
00272 $homeaddress = true;
00273 }
00274 break;
00275
00276 case "homepostalcode":
00277 $value = $field->getNextDataValue();
00278 if ( $value !== false ) {
00279 $homepostalcode = $value->getShortWikiText();
00280 $homeaddress = true;
00281 }
00282 break;
00283
00284 case "homecountry":
00285 $value = $field->getNextDataValue();
00286 if ( $value !== false ) {
00287 $homecountry = $value->getShortWikiText();
00288 $homeaddress = true;
00289 }
00290 break;
00291
00292 case "birthday":
00293 if ( $req->getTypeID() == "_dat" ) {
00294 $value = $field->getNextDataValue();
00295 if ( $value !== false ) {
00296 $birthday = $value->getXMLSchemaDate();
00297 }
00298 }
00299 break;
00300
00301 case "homepage":
00302 if ( $req->getTypeID() == "_uri" ) {
00303 $value = $field->getNextDataValue();
00304 if ( $value !== false ) {
00305 $url = $value->getWikiValue();
00306 }
00307 }
00308 break;
00309 }
00310 }
00311 $pagetitle = $wikipage->getTitle();
00312 if ( $workaddress ) $addresses[] = new SRFvCardAddress ( 'WORK', $workpostofficebox, $workextendedaddress, $workstreet, $worklocality, $workregion, $workpostalcode, $workcountry );
00313 if ( $homeaddress ) $addresses[] = new SRFvCardAddress ( 'HOME', $homepostofficebox, $homeextendedaddress, $homestreet, $homelocality, $homeregion, $homepostalcode, $homecountry );
00314 $items[] = new SRFvCardEntry( $pagetitle, $prefix, $firstname, $lastname, $additionalname, $suffix, $fullname, $tels, $addresses, $emails, $birthday, $jobtitle, $role, $organization, $department, $category, $url, $note );
00315 $row = $res->getNext();
00316 }
00317 foreach ( $items as $item ) {
00318 $result .= $item->text();
00319 }
00320 } else {
00321 if ( $this->getSearchLabel( $outputmode ) ) {
00322 $label = $this->getSearchLabel( $outputmode );
00323 } else {
00324 $label = wfMsgForContent( 'srf_vcard_link' );
00325 }
00326 $link = $res->getQueryLink( $label );
00327 $link->setParameter( 'vcard', 'format' );
00328 if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) {
00329 $link->setParameter( $this->getSearchLabel( SMW_OUTPUT_WIKI ), 'searchlabel' );
00330 }
00331 if ( array_key_exists( 'limit', $this->m_params ) ) {
00332 $link->setParameter( $this->m_params['limit'], 'limit' );
00333 } else {
00334 $link->setParameter( 20, 'limit' );
00335 }
00336 $result .= $link->getText( $outputmode, $this->mLinker );
00337 $this->isHTML = ( $outputmode == SMW_OUTPUT_HTML );
00338 }
00339 return $result;
00340 }
00341
00342 public function getParameters() {
00343 return array_merge( parent::getParameters(), $this->exportFormatParameters() );
00344 }
00345 }
00346
00351 class SRFvCardEntry {
00352 private $uri;
00353 private $label;
00354 private $fullname;
00355 private $firstname;
00356 private $lastname;
00357 private $additionalname;
00358 private $prefix;
00359 private $suffix;
00360 private $tels = array();
00361 private $addresses = array();
00362 private $emails = array();
00363 private $birthday;
00364 private $dtstamp;
00365 private $title;
00366 private $role;
00367 private $organization;
00368 private $department;
00369 private $category;
00370 private $note;
00371
00375 public function __construct( Title $t, $prefix, $firstname, $lastname, $additionalname, $suffix, $fullname, $tels, $addresses, $emails, $birthday, $jobtitle, $role, $organization, $department, $category, $url, $note ) {
00376 $this->uri = $t->getFullURL();
00377 $this->url = $url;
00378
00379 if ( $fullname != '' ) {
00380 $this->label = $fullname;
00381 } elseif ( $firstname . $lastname != '' ) {
00382 $this->label = $firstname . ( ( ( $firstname != '' ) && ( $lastname != '' ) ) ? ' ':'' ) . $lastname;
00383 } else {
00384 $this->label = $t->getText();
00385 }
00386 $this->label = SRFVCardEntry::vCardEscape( $this->label );
00387
00388 if ( $firstname . $lastname == '' ) {
00389 $nameparts = explode( ' ', $this->label );
00390
00391
00392
00393
00394 $this->lastname = SRFvCardEntry::vCardEscape( array_pop( $nameparts ) );
00395 if ( count( $nameparts ) > 0 ) $this->firstname = SRFvCardEntry::vCardEscape( array_shift( $nameparts ) );
00396 foreach ( $nameparts as $name ) {
00397 $this->additionalname .= ( $this->additionalname != '' ? ',':'' ) . SRFvCardEntry::vCardEscape( $name );
00398 }
00399 } else {
00400 $this->firstname = SRFvCardEntry::vCardEscape( $firstname );
00401 $this->lastname = SRFvCardEntry::vCardEscape( $lastname );
00402 }
00403 if ( $additionalname != '' ) $this->additionalname = $additionalname;
00404
00405 $this->prefix = SRFvCardEntry::vCardEscape( $prefix );
00406 $this->suffix = SRFvCardEntry::vCardEscape( $suffix );
00407 $this->tels = $tels;
00408 $this->addresses = $addresses;
00409 $this->emails = $emails;
00410 $this->birthday = $birthday;
00411 $this->title = SRFvCardEntry::vCardEscape( $jobtitle );
00412 $this->role = SRFvCardEntry::vCardEscape( $role );
00413 $this->organization = SRFvCardEntry::vCardEscape( $organization );
00414 $this->department = SRFvCardEntry::vCardEscape( $department );
00415 $this->category = $category;
00416 $this->note = SRFvCardEntry::vCardEscape( $note );
00417
00418 $article = new Article( $t );
00419 $this->dtstamp = $article->getTimestamp();
00420 }
00421
00422
00428 public function text() {
00429 $text = "BEGIN:VCARD\r\n";
00430 $text .= "VERSION:3.0\r\n";
00431
00432 $text .= "N;CHARSET=UTF-8:$this->lastname;$this->firstname;$this->additionalname;$this->prefix;$this->suffix\r\n";
00433 $text .= "FN;CHARSET=UTF-8:$this->label\r\n";
00434
00435 global $wgGroupPermissions;
00436 if ( ( array_key_exists( '*', $wgGroupPermissions ) ) &&
00437 ( array_key_exists( 'read', $wgGroupPermissions['*'] ) ) ) {
00438 $public = $wgGroupPermissions['*']['read'];
00439 } else {
00440 $public = true;
00441 }
00442 $text .= ( $public ? 'CLASS:PUBLIC':'CLASS:CONFIDENTIAL' ) . "\r\n";
00443 if ( $this->birthday !== "" ) $text .= "BDAY:$this->birthday\r\n";
00444 if ( $this->title !== "" ) $text .= "TITLE;CHARSET=UTF-8:$this->title\r\n";
00445 if ( $this->role !== "" ) $text .= "ROLE;CHARSET=UTF-8:$this->role\r\n";
00446 if ( $this->organization !== "" ) $text .= "ORG;CHARSET=UTF-8:$this->organization;$this->department\r\n";
00447 if ( $this->category !== "" ) $text .= "CATEGORIES;CHARSET=UTF-8:$this->category\r\n";
00448 foreach ( $this->emails as $entry ) $text .= $entry->createVCardEmailText();
00449 foreach ( $this->addresses as $entry ) $text .= $entry->createVCardAddressText();
00450 foreach ( $this->tels as $entry ) $text .= $entry->createVCardTelText();
00451 if ( $this->note !== "" ) $text .= "NOTE;CHARSET=UTF-8:$this->note\r\n";
00452 $text .= "SOURCE;CHARSET=UTF-8:$this->uri\r\n";
00453 $text .= "PRODID:-////Semantic MediaWiki\r\n";
00454 $text .= "REV:$this->dtstamp\r\n";
00455 $text .= "URL:" . ( $this->url ? $this->url:$this->uri ) . "\r\n";
00456 $text .= "UID:$this->uri\r\n";
00457 $text .= "END:VCARD\r\n";
00458 return $text;
00459 }
00460
00461 public static function vCardEscape( $text ) {
00462 return str_replace( array( '\\', ',', ':', ';' ), array( '\\\\', '\,', '\:', '\;' ), $text );
00463 }
00464
00465 }
00466
00471 class SRFvCardAddress {
00472 private $type;
00473 private $postofficebox;
00474 private $extendedaddress;
00475 private $street;
00476 private $locality;
00477 private $region;
00478 private $postalcode;
00479 private $country;
00480
00484 public function __construct( $type, $postofficebox, $extendedaddress, $street, $locality, $region, $postalcode, $country ) {
00485 $this->type = $type;
00486 $this->postofficebox = SRFvCardEntry::vCardEscape( $postofficebox );
00487 $this->extendedaddress = SRFvCardEntry::vCardEscape( $extendedaddress );
00488 $this->street = SRFvCardEntry::vCardEscape( $street );
00489 $this->locality = SRFvCardEntry::vCardEscape( $locality );
00490 $this->region = SRFvCardEntry::vCardEscape( $region );
00491 $this->postalcode = SRFvCardEntry::vCardEscape( $postalcode );
00492 $this->country = SRFvCardEntry::vCardEscape( $country );
00493 }
00494
00498 public function createVCardAddressText() {
00499 if ( $this->type == "" ) $this->type = "work";
00500 $text = "ADR;TYPE=$this->type;CHARSET=UTF-8:$this->postofficebox;$this->extendedaddress;$this->street;$this->locality;$this->region;$this->postalcode;$this->country\r\n";
00501 return $text;
00502 }
00503 }
00504
00509 class SRFvCardTel {
00510 private $type;
00511 private $telnumber;
00512
00516 public function __construct( $type, $telnumber ) {
00517 $this->type = $type;
00518 $this->telnumber = SRFvCardEntry::vCardEscape( $telnumber );
00519 }
00520
00524 public function createVCardTelText() {
00525 if ( $this->type == "" ) $this->type = "work";
00526 $text = "TEL;TYPE=$this->type:$this->telnumber\r\n";
00527 return $text;
00528 }
00529 }
00530
00535 class SRFvCardEmail {
00536 private $type;
00537 private $emailaddress;
00538
00542 public function __construct( $type, $emailaddress ) {
00543 $this->type = $type;
00544 $this->emailaddress = $emailaddress;
00545 }
00546
00550 public function createVCardEmailText() {
00551 if ( $this->type == "" ) $this->type = "internet";
00552 $text = "EMAIL;TYPE=$this->type:$this->emailaddress\r\n";
00553 return $text;
00554 }
00555 }