00001 <?php
00019 class SMWInfolink {
00020
00026 protected $mTarget;
00027
00033 protected $mCaption;
00034
00041 protected $mStyle;
00042
00048 protected $mInternal;
00049
00055 protected $mParams;
00056
00066 public function __construct( $internal, $caption, $target, $style = false, array $params = array() ) {
00067 $this->mInternal = $internal;
00068 $this->mCaption = $caption;
00069 $this->mTarget = $target;
00070 $this->mStyle = $style;
00071 $this->mParams = $params;
00072 }
00073
00085 public static function newInternalLink( $caption, $target, $style = false, array $params = array() ) {
00086 return new SMWInfolink( true, $caption, $target, $style, $params );
00087 }
00088
00099 public static function newExternalLink( $caption, $url, $style = false, array $params = array() ) {
00100 return new SMWInfolink( false, $caption, $url, $style, $params );
00101 }
00102
00113 public static function newPropertySearchLink( $caption, $propertyName, $propertyValue, $style = 'smwsearch' ) {
00114 global $wgContLang;
00115 return new SMWInfolink(
00116 true,
00117 $caption,
00118 $wgContLang->getNsText( NS_SPECIAL ) . ':SearchByProperty',
00119 $style,
00120 array( $propertyName, $propertyValue )
00121 );
00122 }
00123
00134 public static function newInversePropertySearchLink( $caption, $subject, $propertyname, $style = false ) {
00135 global $wgContLang;
00136 return new SMWInfolink(
00137 true,
00138 $caption,
00139 $wgContLang->getNsText( NS_SPECIAL ) . ':PageProperty/' . $subject . '::' . $propertyname,
00140 $style
00141 );
00142 }
00143
00153 public static function newBrowsingLink( $caption, $titleText, $style = 'smwbrowse' ) {
00154 global $wgContLang;
00155 return new SMWInfolink(
00156 true,
00157 $caption,
00158 $wgContLang->getNsText( NS_SPECIAL ) . ':Browse',
00159 $style,
00160 array( $titleText )
00161 );
00162 }
00163
00170 public function setParameter( $value, $key = false ) {
00171 if ( $key === false ) {
00172 $this->mParams[] = $value;
00173 } else {
00174 $this->mParams[$key] = $value;
00175 }
00176 }
00177
00182 public function getParameter( $key ) {
00183 if ( array_key_exists( $key, $this->mParams ) ) {
00184 return $this->mParams[$key];
00185 } else {
00186 return null;
00187 }
00188 }
00189
00193 public function setCaption( $caption ) {
00194 $this->mCaption = $caption;
00195 }
00196
00200 public function setStyle( $style ) {
00201 $this->mStyle = $style;
00202 }
00203
00212 public function getText( $outputformat, $linker = null ) {
00213 if ( $this->mStyle !== false ) {
00214 SMWOutputs::requireResource( 'ext.smw.style' );
00215 $start = "<span class=\"$this->mStyle\">";
00216 $end = '</span>';
00217 } else {
00218 $start = '';
00219 $end = '';
00220 }
00221
00222 if ( $this->mInternal ) {
00223 if ( count( $this->mParams ) > 0 ) {
00224 $titletext = $this->mTarget . '/' . SMWInfolink::encodeParameters( $this->mParams );
00225 } else {
00226 $titletext = $this->mTarget;
00227 }
00228
00229 $title = Title::newFromText( $titletext );
00230
00231 if ( !is_null( $title ) ) {
00232 if ( $outputformat == SMW_OUTPUT_WIKI ) {
00233 $link = "[[$titletext|$this->mCaption]]";
00234 } else {
00235 $link = $this->getLinker( $linker )->link( $title, $this->mCaption );
00236 }
00237 } else {
00238
00239
00240
00241 $title = Title::newFromText( $this->mTarget );
00242
00243 if ( !is_null( $title ) ) {
00244 if ( $outputformat == SMW_OUTPUT_WIKI ) {
00245 $link = '[' . $title->getFullURL( SMWInfolink::encodeParameters( $this->mParams, false ) ) . " $this->mCaption]";
00246 } else {
00247 $link = $this->getLinker( $linker )->link(
00248 $title,
00249 $this->mCaption,
00250 array(),
00251 SMWInfolink::encodeParameters( $this->mParams, false )
00252 );
00253 }
00254 } else {
00255 return '';
00256 }
00257 }
00258 } else {
00259 $target = $this->getURL();
00260
00261 if ( $outputformat == SMW_OUTPUT_WIKI ) {
00262 $link = "[$target $this->mCaption]";
00263 } else {
00264 $link = '<a href="' . htmlspecialchars( $target ) . "\">$this->mCaption</a>";
00265 }
00266 }
00267
00268 return $start . $link . $end;
00269 }
00270
00276 public function getHTML( $linker = null ) {
00277 return $this->getText( SMW_OUTPUT_HTML, $linker );
00278 }
00279
00285 public function getWikiText( $linker = null ) {
00286 return $this->getText( SMW_OUTPUT_WIKI, $linker );
00287 }
00288
00296 public function getURL() {
00297 if ( $this->mInternal ) {
00298 $title = Title::newFromText( $this->mTarget );
00299
00300 if ( !is_null( $title ) ) {
00301 return $title->getFullURL( SMWInfolink::encodeParameters( $this->mParams, false ) );
00302 } else {
00303 return '';
00304 }
00305 } else {
00306 if ( count( $this->mParams ) > 0 ) {
00307 if ( strpos( SMWExporter::expandURI( '&wikiurl;' ), '?' ) === false ) {
00308 $target = $this->mTarget . '?' . SMWInfolink::encodeParameters( $this->mParams, false );
00309 } else {
00310 $target = $this->mTarget . '&' . SMWInfolink::encodeParameters( $this->mParams, false );
00311 }
00312 } else {
00313 $target = $this->mTarget;
00314 }
00315
00316 return $target;
00317 }
00318 }
00319
00320
00328 protected function getLinker( &$linker = null ) {
00329 if ( is_null( $linker ) ) {
00330 $linker = class_exists('DummyLinker') ? new DummyLinker : new Linker;
00331 }
00332 return $linker;
00333 }
00334
00352 static public function encodeParameters( array $params, $forTitle = true ) {
00353 $result = '';
00354
00355 if ( $forTitle ) {
00356 foreach ( $params as $name => $value ) {
00357 if ( is_string( $name ) && ( $name !== '' ) ) {
00358 $value = $name . '=' . $value;
00359 }
00360
00361
00362
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378 $value = str_replace(
00379 array( '-', '#', "\n", ' ', '/', '[', ']', '<', '>', '<', '>', '&', '\'\'', '|', '&', '%', '?', '$' ),
00380 array( '-2D', '-23', '-0A', '-20', '-2F', '-5B', '-5D', '-3C', '-3E', '-3C', '-3E', '-26', '-27-27', '-7C', '-26', '-25', '-3F', '-24' ),
00381 $value
00382 );
00383
00384 if ( $result !== '' ) {
00385 $result .= '/';
00386 }
00387
00388 $result .= $value;
00389 }
00390 } else {
00391 $q = array();
00392
00393 foreach ( $params as $name => $value ) {
00394 if ( is_string( $name ) && ( $name !== '' ) ) {
00395 $value = $name . '=' . rawurlencode( $value );
00396
00397 if ( $result !== '' ) {
00398 $result .= '&';
00399 }
00400
00401 $result .= $value;
00402 } else {
00403 $q[] = $value;
00404 }
00405 }
00406 if ( count( $q ) > 0 ) {
00407 if ( $result !== '' ) {
00408 $result = '&' . $result;
00409 }
00410 $result = 'x=' . rawurlencode( SMWInfolink::encodeParameters( $q, true ) ) . $result;
00411 }
00412 }
00413
00414 return $result;
00415 }
00416
00440 static public function decodeParameters( $titleParam = '', $allParams = false ) {
00441 global $wgRequest;
00442
00443 $result = array();
00444
00445 if ( $allParams ) {
00446 $result = $wgRequest->getValues();
00447
00448 if ( array_key_exists( 'x', $result ) ) {
00449 if ( $titleParam !== '' ) {
00450 $titleParam .= '/';
00451 }
00452 $titleParam .= $result['x'];
00453 unset( $result['x'] );
00454 }
00455 }
00456
00457 if ( is_array( $titleParam ) ) {
00458 return $titleParam;
00459 } elseif ( $titleParam !== '' ) {
00460
00461 $ps = explode( '/', $titleParam );
00462
00463 foreach ( $ps as $p ) {
00464 if ( $p !== '' ) {
00465 $p = rawurldecode( str_replace( '-', '%', $p ) );
00466 $parts = explode( '=', $p, 2 );
00467
00468 if ( count( $parts ) > 1 ) {
00469 $result[$parts[0]] = $parts[1];
00470 } else {
00471 $result[] = $p;
00472 }
00473 }
00474 }
00475 }
00476
00477 return $result;
00478 }
00479
00480 }