00001 <?php
00018 class SMWPrintRequest {
00019
00021 const PRINT_CATS = 0;
00023 const PRINT_PROP = 1;
00025 const PRINT_THIS = 2;
00027 const PRINT_CCAT = 3;
00028
00029 protected $m_mode;
00030 protected $m_label;
00031 protected $m_data;
00032 protected $m_typeid = false;
00033 protected $m_outputformat;
00034 protected $m_hash = false;
00035 protected $m_params = array();
00036
00045 public function __construct( $mode, $label, $data = null, $outputformat = false, $params = null ) {
00046 if ( ( ( $mode == self::PRINT_CATS || $mode == self::PRINT_THIS ) &&
00047 !is_null( $data ) ) ||
00048 ( $mode == self::PRINT_PROP &&
00049 ( !( $data instanceof SMWPropertyValue ) || !$data->isValid() ) ) ||
00050 ( $mode == self::PRINT_CCAT &&
00051 !( $data instanceof Title ) ) ) {
00052 throw new InvalidArgumentException( 'Data provided for print request does not fit the type of printout.' );
00053 }
00054
00055 $this->m_mode = $mode;
00056 $this->m_label = $label;
00057 $this->m_data = $data;
00058 $this->m_outputformat = $outputformat;
00059
00060 if ( ( $mode == self::PRINT_CCAT ) && ( $outputformat == false ) ) {
00061 $this->m_outputformat = 'x';
00062 }
00063
00064 if ( $this->m_data instanceof SMWDataValue ) {
00065
00066 $this->m_data->setCaption( $label );
00067 }
00068
00069 if ( null != $params ) $this->m_params = $params;
00070 }
00071
00072 public function getMode() {
00073 return $this->m_mode;
00074 }
00075
00076 public function getLabel() {
00077 return $this->m_label;
00078 }
00079
00085 public function getHTMLText( $linker = null ) {
00086 if ( is_null( $linker ) || ( $this->m_label === '' ) ) {
00087 return htmlspecialchars( $this->m_label );
00088 }
00089
00090 switch ( $this->m_mode ) {
00091 case self::PRINT_CATS:
00092 return htmlspecialchars( $this->m_label );
00093 case self::PRINT_CCAT:
00094 return $linker->makeLinkObj( $this->m_data, htmlspecialchars( $this->m_label ) );
00095 case self::PRINT_PROP:
00096 return $this->m_data->getShortHTMLText( $linker );
00097 case self::PRINT_THIS: default: return htmlspecialchars( $this->m_label );
00098 }
00099 }
00100
00104 public function getWikiText( $linked = false ) {
00105 if ( is_null( $linked ) || ( $linked === false ) || ( $this->m_label === '' ) ) {
00106 return $this->m_label;
00107 } else {
00108 switch ( $this->m_mode ) {
00109 case self::PRINT_CATS:
00110 return $this->m_label;
00111 case self::PRINT_PROP:
00112 return $this->m_data->getShortWikiText( $linked );
00113 case self::PRINT_CCAT:
00114 return '[[:' . $this->m_data->getPrefixedText() . '|' . $this->m_label . ']]';
00115 case self::PRINT_THIS: default:
00116 return $this->m_label;
00117 }
00118 }
00119 }
00120
00124 public function getText( $outputmode, $linker = null ) {
00125 switch ( $outputmode ) {
00126 case SMW_OUTPUT_WIKI: return $this->getWikiText( $linker );
00127 case SMW_OUTPUT_HTML: case SMW_OUTPUT_FILE: default: return $this->getHTMLText( $linker );
00128 }
00129 }
00130
00136 public function getData() {
00137 return $this->m_data;
00138 }
00139
00140 public function getOutputFormat() {
00141 return $this->m_outputformat;
00142 }
00143
00150 public function getTypeID() {
00151 if ( $this->m_typeid === false ) {
00152 if ( $this->m_mode == self::PRINT_PROP ) {
00153 $this->m_typeid = $this->m_data->getDataItem()->findPropertyTypeID();
00154 } else {
00155 $this->m_typeid = '_wpg';
00156 }
00157 }
00158
00159 return $this->m_typeid;
00160 }
00161
00170 public function getHash() {
00171 if ( $this->m_hash === false ) {
00172 $this->m_hash = $this->m_mode . ':' . $this->m_label . ':';
00173
00174 if ( $this->m_data instanceof Title ) {
00175 $this->m_hash .= $this->m_data->getPrefixedText() . ':';
00176 } elseif ( $this->m_data instanceof SMWDataValue ) {
00177 $this->m_hash .= $this->m_data->getHash() . ':';
00178 }
00179
00180 $this->m_hash .= $this->m_outputformat . ':' . implode( '|', $this->m_params );
00181 }
00182
00183 return $this->m_hash;
00184 }
00185
00191 public function getSerialisation( $showparams = false ) {
00192 $parameters = '';
00193
00194 if ( $showparams ) foreach ( $this->m_params as $key => $value ) {
00195 $parameters .= "|+" . $key . "=" . $value;
00196 }
00197
00198 switch ( $this->m_mode ) {
00199 case self::PRINT_CATS:
00200 global $wgContLang;
00201 $catlabel = $wgContLang->getNSText( NS_CATEGORY );
00202 $result = '?' . $catlabel;
00203 if ( $this->m_label != $catlabel ) {
00204 $result .= '=' . $this->m_label;
00205 }
00206 return $result . $parameters;
00207 case self::PRINT_PROP: case self::PRINT_CCAT:
00208 if ( $this->m_mode == self::PRINT_CCAT ) {
00209 $printname = $this->m_data->getPrefixedText();
00210 $result = '?' . $printname;
00211
00212 if ( $this->m_outputformat != 'x' ) {
00213 $result .= '#' . $this->m_outputformat;
00214 }
00215 } else {
00216 $printname = $this->m_data->getWikiValue();
00217 $result = '?' . $printname;
00218
00219 if ( $this->m_outputformat !== '' ) {
00220 $result .= '#' . $this->m_outputformat;
00221 }
00222 }
00223 if ( $printname != $this->m_label ) {
00224 $result .= '=' . $this->m_label;
00225 }
00226 return $result . $parameters;
00227 case self::PRINT_THIS:
00228 $result = '?';
00229
00230 if ( $this->m_label !== '' ) {
00231 $result .= '=' . $this->m_label;
00232 }
00233
00234 if ( $this->m_outputformat !== '' ) {
00235 $result .= '#' . $this->m_outputformat;
00236 }
00237
00238 return $result . $parameters;
00239 default: return '';
00240 }
00241 }
00242
00250 public function getParameter( $key ) {
00251 return array_key_exists( $key, $this->m_params ) ? $this->m_params[$key] : false;
00252 }
00253
00259 public function getParameters() {
00260 return $this->m_params;
00261 }
00262
00269 public function setParameter( $key, $value ) {
00270 $this->m_params[$key] = $value;
00271 }
00272
00273 }