00001 <?php
00002
00014 class SMWResultArray {
00015
00019 protected $mPrintRequest;
00020
00024 protected $mResult;
00025
00029 protected $mStore;
00030
00034 protected $mContent;
00035
00036 static protected $catCacheObj = false;
00037 static protected $catCache = false;
00038
00046 public function __construct( SMWDIWikiPage $resultPage, SMWPrintRequest $printRequest, SMWStore $store ) {
00047 $this->mResult = $resultPage;
00048 $this->mPrintRequest = $printRequest;
00049 $this->mStore = $store;
00050 $this->mContent = false;
00051 }
00052
00058 public function getStore() {
00059 return $this->mStore;
00060 }
00061
00069 public function getResultSubject() {
00070 return $this->mResult;
00071 }
00072
00079 public function getContent() {
00080 $this->loadContent();
00081 return $this->mContent;
00082 }
00083
00090 public function getPrintRequest() {
00091 return $this->mPrintRequest;
00092 }
00093
00098 public function getNextObject() {
00099 return $this->getNextDataValue();
00100 }
00101
00109 public function getNextDataItem() {
00110 $this->loadContent();
00111 $result = current( $this->mContent );
00112 next( $this->mContent );
00113 return $result;
00114 }
00115
00125 public function reset() {
00126 $this->loadContent();
00127 return reset( $this->mContent );
00128 }
00129
00138 public function getNextDataValue() {
00139 $di = $this->getNextDataItem();
00140 if ( $di === false ) {
00141 return false;
00142 }
00143 if ( $this->mPrintRequest->getMode() == SMWPrintRequest::PRINT_PROP &&
00144 $this->mPrintRequest->getTypeID() == '_rec' &&
00145 $this->mPrintRequest->getParameter( 'index' ) !== false ) {
00146
00147
00148 $pos = $this->mPrintRequest->getParameter( 'index' ) - 1;
00149 $recordValue = SMWDataValueFactory::newDataItemValue( $di,
00150 $this->mPrintRequest->getData()->getDataItem() );
00151 $diProperties = $recordValue->getPropertyDataItems();
00152
00153 if ( array_key_exists( $pos, $diProperties ) &&
00154 !is_null( $diProperties[$pos] ) ) {
00155 $diProperty = $diProperties[$pos];
00156 } else {
00157 $diProperty = null;
00158 }
00159 } elseif ( $this->mPrintRequest->getMode() == SMWPrintRequest::PRINT_PROP ) {
00160 $diProperty = $this->mPrintRequest->getData()->getDataItem();
00161 } else {
00162 $diProperty = null;
00163 }
00164 $dv = SMWDataValueFactory::newDataItemValue( $di, $diProperty );
00165 if ( $this->mPrintRequest->getOutputFormat() ) {
00166 $dv->setOutputFormat( $this->mPrintRequest->getOutputFormat() );
00167 }
00168 return $dv;
00169 }
00170
00183 public function getNextText( $outputMode, $linker = null ) {
00184 $dataValue = $this->getNextDataValue();
00185 if ( $dataValue !== false ) {
00186 return $dataValue->getShortText( $outputMode, $linker );
00187 } else {
00188 return false;
00189 }
00190 }
00191
00196 protected function loadContent() {
00197 if ( $this->mContent !== false ) return;
00198
00199 wfProfileIn( 'SMWQueryResult::loadContent (SMW)' );
00200
00201 switch ( $this->mPrintRequest->getMode() ) {
00202 case SMWPrintRequest::PRINT_THIS:
00203 $this->mContent = array( $this->mResult );
00204 break;
00205 case SMWPrintRequest::PRINT_CATS:
00206
00207 self::$catCache = $this->mStore->getPropertyValues( $this->mResult,
00208 new SMWDIProperty( '_INST' ), $this->getRequestOptions( false ) );
00209 self::$catCacheObj = $this->mResult->getHash();
00210
00211 $limit = $this->mPrintRequest->getParameter( 'limit' );
00212 $this->mContent = ( $limit === false ) ? ( self::$catCache ) :
00213 array_slice( self::$catCache, 0, $limit );
00214 break;
00215 case SMWPrintRequest::PRINT_PROP:
00216 $propertyValue = $this->mPrintRequest->getData();
00217 if ( $propertyValue->isValid() ) {
00218 $this->mContent = $this->mStore->getPropertyValues( $this->mResult,
00219 $propertyValue->getDataItem(), $this->getRequestOptions() );
00220 } else {
00221 $this->mContent = array();
00222 }
00223
00224
00225
00226
00227 if ( ( $this->mPrintRequest->getTypeID() == '_rec' ) &&
00228 ( $this->mPrintRequest->getParameter( 'index' ) !== false ) ) {
00229 $pos = $this->mPrintRequest->getParameter( 'index' ) - 1;
00230 $newcontent = array();
00231
00232 foreach ( $this->mContent as $diContainer ) {
00233 $recordValue = SMWDataValueFactory::newDataItemValue( $diContainer, $propertyValue->getDataItem() );
00234 $dataItems = $recordValue->getDataItems();
00235
00236 if ( array_key_exists( $pos, $dataItems ) &&
00237 ( !is_null( $dataItems[$pos] ) ) ) {
00238 $newcontent[] = $dataItems[$pos];
00239 }
00240 }
00241
00242 $this->mContent = $newcontent;
00243 }
00244 break;
00245 case SMWPrintRequest::PRINT_CCAT:
00246 if ( self::$catCacheObj != $this->mResult->getHash() ) {
00247 self::$catCache = $this->mStore->getPropertyValues( $this->mResult, new SMWDIProperty( '_INST' ) );
00248 self::$catCacheObj = $this->mResult->getHash();
00249 }
00250
00251 $found = '0';
00252 $prkey = $this->mPrintRequest->getData()->getDBkey();
00253
00254 foreach ( self::$catCache as $cat ) {
00255 if ( $cat->getDBkey() == $prkey ) {
00256 $found = '1';
00257 break;
00258 }
00259 }
00260 $this->mContent = array( new SMWDIBoolean( $found ) );
00261 break;
00262 default: $this->mContent = array();
00263 }
00264
00265 reset( $this->mContent );
00266
00267 wfProfileOut( 'SMWQueryResult::loadContent (SMW)' );
00268 }
00269
00280 protected function getRequestOptions( $useLimit = true ) {
00281 $limit = $useLimit ? $this->mPrintRequest->getParameter( 'limit' ) : false;
00282 $order = trim( $this->mPrintRequest->getParameter( 'order' ) );
00283
00284
00285 if ( ( $limit !== false ) || ( $order != false ) ) {
00286 $options = new SMWRequestOptions();
00287
00288 if ( $limit !== false ) $options->limit = trim( $limit );
00289
00290 if ( ( $order == 'descending' ) || ( $order == 'reverse' ) || ( $order == 'desc' ) ) {
00291 $options->sort = true;
00292 $options->ascending = false;
00293 } elseif ( ( $order == 'ascending' ) || ( $order == 'asc' ) ) {
00294 $options->sort = true;
00295 $options->ascending = true;
00296 }
00297 } else {
00298 $options = null;
00299 }
00300
00301 return $options;
00302 }
00303
00304 }