00001 <?php
00028 class SMWWikiPageValue extends SMWDataValue {
00029
00035 protected $m_fragment = '';
00036
00042 protected $m_prefixedtext = '';
00043
00049 protected $m_id = -1;
00050
00056 protected $m_title = null;
00057
00065 protected $m_fixNamespace = NS_MAIN;
00066
00067 public function __construct( $typeid ) {
00068 parent::__construct( $typeid );
00069 switch ( $typeid ) {
00070 case '__typ':
00071 $this->m_fixNamespace = SMW_NS_TYPE;
00072 break;
00073 case '_wpp' : case '__sup':
00074 $this->m_fixNamespace = SMW_NS_PROPERTY;
00075 break;
00076 case '_wpc' : case '__suc': case '__sin':
00077 $this->m_fixNamespace = NS_CATEGORY;
00078 break;
00079 case '_wpf' : case '__spf':
00080 $this->m_fixNamespace = SF_NS_FORM;
00081 break;
00082 default:
00083 $this->m_fixNamespace = NS_MAIN;
00084 }
00085 }
00086
00087 protected function parseUserValue( $value ) {
00088 global $wgContLang;
00089
00090
00091
00092 $value = ltrim( rtrim( $value, ' ]' ), ' [' );
00093
00094 if ( $this->m_caption === false ) {
00095 $this->m_caption = $value;
00096 }
00097
00098 if ( $value !== '' ) {
00099 if ( $value[0] == '#' ) {
00100 if ( is_null( $this->m_contextPage ) ) {
00101 $this->addError( wfMsgForContent( 'smw_notitle', $value ) );
00102 return;
00103 } else {
00104 $this->m_title = Title::makeTitle( $this->m_contextPage->getNamespace(),
00105 $this->m_contextPage->getDBkey(), substr( $value, 1 ),
00106 $this->m_contextPage->getInterwiki() );
00107 }
00108 } else {
00109 $this->m_title = Title::newFromText( $value, $this->m_fixNamespace );
00110 }
00111
00113 if ( is_null( $this->m_title ) ) {
00114 $this->addError( wfMsgForContent( 'smw_notitle', $value ) );
00115 } elseif ( ( $this->m_fixNamespace != NS_MAIN ) &&
00116 ( $this->m_fixNamespace != $this->m_title->getNamespace() ) ) {
00117 $this->addError( wfMsgForContent( 'smw_wrong_namespace',
00118 $wgContLang->getNsText( $this->m_fixNamespace ) ) );
00119 } else {
00120 $this->m_fragment = str_replace( ' ', '_', $this->m_title->getFragment() );
00121 $this->m_prefixedtext = '';
00122 $this->m_id = -1;
00123 $this->m_dataitem = SMWDIWikiPage::newFromTitle( $this->m_title, $this->m_typeid );
00124 }
00125 } else {
00126 $this->addError( wfMsgForContent( 'smw_notitle', $value ) );
00127 }
00128 }
00129
00135 protected function loadDataItem( SMWDataItem $dataItem ) {
00136 if ( $dataItem->getDIType() == SMWDataItem::TYPE_CONTAINER ) {
00137
00138 $dataItem = $dataItem->getSemanticData()->getSubject();
00139 }
00140
00141 if ( $dataItem->getDIType() == SMWDataItem::TYPE_WIKIPAGE ) {
00142 $this->m_dataitem = $dataItem;
00143 $this->m_id = -1;
00144 $this->m_title = null;
00145 $this->m_fragment = $dataItem->getSubobjectName();
00146 $this->m_prefixedtext = '';
00147 $this->m_caption = false;
00148
00149 if ( ( $this->m_fixNamespace != NS_MAIN ) &&
00150 ( $this->m_fixNamespace != $dataItem->getNamespace() ) ) {
00151 global $wgContLang;
00152 $this->addError( wfMsgForContent( 'smw_wrong_namespace',
00153 $wgContLang->getNsText( $this->m_fixNamespace ) ) );
00154 }
00155 return true;
00156 } else {
00157 return false;
00158 }
00159 }
00160
00185 public function getShortWikiText( $linked = null ) {
00186 if ( is_null( $linked ) || $linked === false ||
00187 $this->m_outformat == '-' || !$this->isValid() ||
00188 $this->m_caption === '' ) {
00189 return $this->m_caption !== false ? $this->m_caption : $this->getWikiValue();
00190 } else {
00191 if ( $this->m_dataitem->getNamespace() == NS_FILE ) {
00192 $linkEscape = '';
00193 $defaultCaption = '|' . $this->getShortCaptionText() . '|frameless|border|text-top';
00194 } else {
00195 $linkEscape = ':';
00196 $defaultCaption = '|' . $this->getShortCaptionText();
00197 }
00198 if ( $this->m_caption === false ) {
00199 return '[[' . $linkEscape . $this->getWikiLinkTarget() . $defaultCaption . ']]';
00200 } else {
00201 return '[[' . $linkEscape . $this->getWikiLinkTarget() . '|' . $this->m_caption . ']]';
00202 }
00203 }
00204 }
00205
00213 public function getShortHTMLText( $linker = null ) {
00214
00215 if ( !is_null( $linker ) && $linker !== false &&
00216 $this->m_caption !== '' && $this->m_outformat != '-' ) {
00217 $this->getTitle();
00218 }
00219
00220 if ( is_null( $linker ) || $linker === false || !$this->isValid() ||
00221 $this->m_outformat == '-' || $this->m_caption === '' ) {
00222 return htmlspecialchars( $this->m_caption !== false ? $this->m_caption : $this->getWikiValue() );
00223 } else {
00224 $caption = htmlspecialchars(
00225 $this->m_caption !== false ? $this->m_caption : $this->getShortCaptionText() );
00226 if ( $this->getNamespace() == NS_MEDIA ) {
00227 return $linker->makeMediaLinkObj( $this->getTitle(), $caption );
00228 } else {
00229 return $linker->makeLinkObj( $this->getTitle(), $caption );
00230 }
00231 }
00232 }
00233
00242 public function getLongWikiText( $linked = null ) {
00243 if ( !$this->isValid() ) {
00244 return $this->getErrorText();
00245 }
00246
00247 if ( is_null( $linked ) || $linked === false || $this->m_outformat == '-' ) {
00248 return $this->getWikiValue();
00249 } elseif ( $this->m_dataitem->getNamespace() == NS_FILE ) {
00250
00251
00252
00253
00254 return '[[' . $this->getWikiLinkTarget() . '|' .
00255 $this->getLongCaptionText() . '|frameless|border|text-top]]';
00256 } else {
00257 return '[[:' . $this->getWikiLinkTarget() . '|' . $this->getLongCaptionText() . ']]';
00258 }
00259 }
00260
00268 public function getLongHTMLText( $linker = null ) {
00269
00270 if ( !is_null( $linker ) && ( $this->m_outformat != '-' ) ) {
00271 $this->getTitle();
00272 }
00273 if ( !$this->isValid() ) {
00274 return $this->getErrorText();
00275 }
00276
00277 if ( is_null( $linker ) || $this->m_outformat == '-' ) {
00278 return htmlspecialchars( $this->getWikiValue() );
00279 } elseif ( $this->getNamespace() == NS_MEDIA ) {
00280 return $linker->makeMediaLinkObj( $this->getTitle(),
00281 htmlspecialchars( $this->getLongCaptionText() ) );
00282 } else {
00283 return $linker->makeLinkObj( $this->getTitle(),
00284 htmlspecialchars( $this->getLongCaptionText() ) );
00285 }
00286 }
00287
00298 public function getWikiValue() {
00299 return ( $this->m_fixNamespace == NS_MAIN ? $this->getPrefixedText() : $this->getText() ) .
00300 ( $this->m_fragment !== '' ? "#{$this->m_fragment}" : '' );
00301 }
00302
00303 public function getHash() {
00304 return $this->isValid() ? $this->getPrefixedText() : implode( "\t", $this->getErrors() );
00305 }
00306
00314 protected function getServiceLinkParams() {
00315 if ( $this->isValid() ) {
00316 return array( rawurlencode( str_replace( '_', ' ', $this->m_dataitem->getDBkey() ) ) );
00317 } else {
00318 return array();
00319 }
00320 }
00321
00323
00334 public function getTitle() {
00335 if ( ( $this->isValid() ) && is_null( $this->m_title ) ) {
00336 $this->m_title = $this->m_dataitem->getTitle();
00337
00338 if ( is_null( $this->m_title ) ) {
00339 global $wgContLang;
00340 $this->addError( wfMsgForContent( 'smw_notitle', $wgContLang->getNsText( $this->m_dataitem->getNamespace() ) . ':' . $this->m_dataitem->getDBkey() ) );
00341 }
00342 }
00343
00344 return $this->m_title;
00345 }
00346
00352 public function getArticleID() {
00353 if ( $this->m_id === false ) {
00354 $this->m_id = !is_null( $this->getTitle() ) ? $this->m_title->getArticleID() : 0;
00355 }
00356
00357 return $this->m_id;
00358 }
00359
00365 public function getNamespace() {
00366 return $this->m_dataitem->getNamespace();
00367 }
00368
00377 public function getDBkey() {
00378 return $this->m_dataitem->getDBkey();
00379 }
00380
00386 public function getText() {
00387 return str_replace( '_', ' ', $this->m_dataitem->getDBkey() );
00388 }
00389
00396 public function getPrefixedText() {
00397 global $wgContLang;
00398 if ( $this->m_prefixedtext === '' ) {
00399 if ( $this->isValid() ) {
00400 $nstext = $wgContLang->getNSText( $this->m_dataitem->getNamespace() );
00401 $this->m_prefixedtext =
00402 ( $this->m_dataitem->getInterwiki() !== '' ?
00403 $this->m_dataitem->getInterwiki() . ':' : '' ) .
00404 ( $nstext !== '' ? "$nstext:" : '' ) . $this->getText();
00405 } else {
00406 $this->m_prefixedtext = 'NO_VALID_VALUE';
00407 }
00408 }
00409 return $this->m_prefixedtext;
00410 }
00411
00417 public function getInterwiki() {
00418 return $this->m_dataitem->getInterwiki();
00419 }
00420
00431 protected function getShortCaptionText() {
00432 if ( $this->m_fragment !== '' && $this->m_fragment[0] != '_' ) {
00433 $fragmentText = '#' . $this->m_fragment;
00434 } else {
00435 $fragmentText = '';
00436 }
00437 return $this->getText() . $fragmentText;
00438 }
00439
00450 protected function getLongCaptionText() {
00451 if ( $this->m_fragment !== '' && $this->m_fragment[0] != '_' ) {
00452 $fragmentText = '#' . $this->m_fragment;
00453 } else {
00454 $fragmentText = '';
00455 }
00456 return ( $this->m_fixNamespace == NS_MAIN ? $this->getPrefixedText() : $this->getText() ) . $fragmentText;
00457 }
00458
00467 protected function getWikiLinkTarget() {
00468 return str_replace( "'", ''', $this->getPrefixedText() ) .
00469 ( $this->m_fragment !== '' ? "#{$this->m_fragment}" : '' );
00470 }
00471
00479 public function getSortKey() {
00480 return smwfGetStore()->getWikiPageSortKey( $this->m_dataitem );
00481 }
00482
00496 static public function makePage( $dbkey, $namespace, $ignoredParameter = '', $interwiki = '' ) {
00497 $diWikiPage = new SMWDIWikiPage( $dbkey, $namespace, $interwiki );
00498 $dvWikiPage = new SMWWikiPageValue( '_wpg' );
00499 $dvWikiPage->setDataItem( $diWikiPage );
00500 return $dvWikiPage;
00501 }
00502
00511 static public function makePageFromTitle( Title $title ) {
00512 $dvWikiPage = new SMWWikiPageValue( '_wpg' );
00513 $diWikiPage = SMWDIWikiPage::newFromTitle( $title );
00514 $dvWikiPage->setDataItem( $diWikiPage );
00515 $dvWikiPage->m_title = $title;
00516 return $dvWikiPage;
00517 }
00518
00519 }
00520