r109014 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r109013‎ | r109014 | r109015 >
Date:23:56, 15 January 2012
Author:jeroendedauw
Status:ok
Tags:
Comment:
split resultarray and queryresult classes into separate files
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DISerializer.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_QueryResult.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_ResultArray.php (added) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DISerializer.php
@@ -16,7 +16,6 @@
1717 * @licence GNU GPL v3+
1818 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
1919 */
20 -
2120 class SMWDISerializer {
2221
2322 /**
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_QueryResult.php
@@ -1,17 +1,4 @@
22 <?php
3 -/**
4 - * This file contains classes that are used for representing query results,
5 - * basically several containers/iterators for accessing all parts of a query result.
6 - * These classes might once be replaced by interfaces that are implemented
7 - * by storage-specific classes if this is useful (e.g. for performance gains by
8 - * lazy retrieval).
9 - *
10 - * @author Markus Krötzsch
11 - * @author Jeroen De Dauw
12 - *
13 - * @file
14 - * @ingroup SMWQuery
15 - */
163
174 /**
185 * Objects of this class encapsulate the result of a query in SMW. They
@@ -23,7 +10,12 @@
2411 * It is also possible to access the set of result pages directly using
2512 * getResults(). This is useful for printers that disregard printouts and
2613 * only are interested in the actual list of pages.
 14+ *
 15+ * @file SMW_QueryResult.php
2716 * @ingroup SMWQuery
 17+ *
 18+ * @author Markus Krötzsch
 19+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
2820 */
2921 class SMWQueryResult {
3022
@@ -257,271 +249,3 @@
258250 }
259251
260252 }
261 -
262 -/**
263 - * Container for the contents of a single result field of a query result,
264 - * i.e. basically an array of SMWDataItems with some additional parameters.
265 - * The content of the array is fetched on demand only.
266 - * @ingroup SMWQuery
267 - */
268 -class SMWResultArray {
269 - /**
270 - * @var SMWPrintRequest
271 - */
272 - protected $mPrintRequest;
273 -
274 - /**
275 - * @var SMWDIWikiPage
276 - */
277 - protected $mResult;
278 -
279 - /**
280 - * @var SMWStore
281 - */
282 - protected $mStore;
283 -
284 - /**
285 - * @var array of SMWDataItem or false
286 - */
287 - protected $mContent;
288 -
289 - static protected $catCacheObj = false;
290 - static protected $catCache = false;
291 -
292 - /**
293 - * Constructor.
294 - *
295 - * @param SMWDIWikiPage $resultPage
296 - * @param SMWPrintRequest $printRequest
297 - * @param SMWStore $store
298 - */
299 - public function __construct( SMWDIWikiPage $resultPage, SMWPrintRequest $printRequest, SMWStore $store ) {
300 - $this->mResult = $resultPage;
301 - $this->mPrintRequest = $printRequest;
302 - $this->mStore = $store;
303 - $this->mContent = false;
304 - }
305 -
306 - /**
307 - * Get the SMWStore object that this result is based on.
308 - *
309 - * @return SMWStore
310 - */
311 - public function getStore() {
312 - return $this->mStore;
313 - }
314 -
315 - /**
316 - * Returns the SMWDIWikiPage object to which this SMWResultArray refers.
317 - * If you only care for those objects, consider using SMWQueryResult::getResults()
318 - * directly.
319 - *
320 - * @return SMWDIWikiPage
321 - */
322 - public function getResultSubject() {
323 - return $this->mResult;
324 - }
325 -
326 - /**
327 - * Returns an array of SMWDataItem objects that contain the results of
328 - * the given print request for the given result object.
329 - *
330 - * @return array of SMWDataItem or false
331 - */
332 - public function getContent() {
333 - $this->loadContent();
334 - return $this->mContent;
335 - }
336 -
337 - /**
338 - * Return an SMWPrintRequest object describing what is contained in this
339 - * result set.
340 - *
341 - * @return SMWPrintRequest
342 - */
343 - public function getPrintRequest() {
344 - return $this->mPrintRequest;
345 - }
346 -
347 - /**
348 - * Compatibility alias for getNextDatItem().
349 - * @deprecated since 1.6. Call getNextDataValue() or getNextDataItem() directly as needed. Method will vanish before SMW 1.7.
350 - */
351 - public function getNextObject() {
352 - return $this->getNextDataValue();
353 - }
354 -
355 - /**
356 - * Return the next SMWDataItem object or false if no further object exists.
357 - *
358 - * @since 1.6
359 - *
360 - * @return SMWDataItem or false
361 - */
362 - public function getNextDataItem() {
363 - $this->loadContent();
364 - $result = current( $this->mContent );
365 - next( $this->mContent );
366 - return $result;
367 - }
368 -
369 -
370 - /**
371 - * Return an SMWDataValue object for the next SMWDataItem object or
372 - * false if no further object exists.
373 - *
374 - * @since 1.6
375 - *
376 - * @return SMWDataValue or false
377 - */
378 - public function getNextDataValue() {
379 - $di = $this->getNextDataItem();
380 - if ( $di === false ) {
381 - return false;
382 - }
383 - if ( $this->mPrintRequest->getMode() == SMWPrintRequest::PRINT_PROP ) {
384 - $diProperty = $this->mPrintRequest->getData()->getDataItem();
385 - } else {
386 - $diProperty = null;
387 - }
388 - $dv = SMWDataValueFactory::newDataItemValue( $di, $diProperty );
389 - if ( $this->mPrintRequest->getOutputFormat() ) {
390 - $dv->setOutputFormat( $this->mPrintRequest->getOutputFormat() );
391 - }
392 - return $dv;
393 - }
394 -
395 - /**
396 - * Return the main text representation of the next SMWDataItem object
397 - * in the specified format, or false if no further object exists.
398 - *
399 - * The parameter $linker controls linking of title values and should
400 - * be some Linker object (or NULL for no linking).
401 - *
402 - * @param integer $outputMode
403 - * @param mixed $linker
404 - *
405 - * @return string or false
406 - */
407 - public function getNextText( $outputMode, $linker = null ) {
408 - $dataValue = $this->getNextDataValue();
409 - if ( $dataValue !== false ) { // Print data values.
410 - return $dataValue->getShortText( $outputMode, $linker );
411 - } else {
412 - return false;
413 - }
414 - }
415 -
416 - /**
417 - * Load results of the given print request and result subject. This is only
418 - * done when needed.
419 - */
420 - protected function loadContent() {
421 - if ( $this->mContent !== false ) return;
422 -
423 - wfProfileIn( 'SMWQueryResult::loadContent (SMW)' );
424 -
425 - switch ( $this->mPrintRequest->getMode() ) {
426 - case SMWPrintRequest::PRINT_THIS: // NOTE: The limit is ignored here.
427 - $this->mContent = array( $this->mResult );
428 - break;
429 - case SMWPrintRequest::PRINT_CATS:
430 - // Always recompute cache here to ensure output format is respected.
431 - self::$catCache = $this->mStore->getPropertyValues( $this->mResult,
432 - new SMWDIProperty( '_INST' ), $this->getRequestOptions( false ) );
433 - self::$catCacheObj = $this->mResult->getHash();
434 -
435 - $limit = $this->mPrintRequest->getParameter( 'limit' );
436 - $this->mContent = ( $limit === false ) ? ( self::$catCache ) :
437 - array_slice( self::$catCache, 0, $limit );
438 - break;
439 - case SMWPrintRequest::PRINT_PROP:
440 - $propertyValue = $this->mPrintRequest->getData();
441 - if ( $propertyValue->isValid() ) {
442 - $this->mContent = $this->mStore->getPropertyValues( $this->mResult,
443 - $propertyValue->getDataItem(), $this->getRequestOptions() );
444 - } else {
445 - $this->mContent = array();
446 - }
447 -
448 - // Print one component of a multi-valued string.
449 - // Known limitation: the printrequest still is of type _rec, so if printers check
450 - // for this then they will not recognize that it returns some more concrete type.
451 - if ( ( $this->mPrintRequest->getTypeID() == '_rec' ) &&
452 - ( $this->mPrintRequest->getParameter( 'index' ) !== false ) ) {
453 - $pos = $this->mPrintRequest->getParameter( 'index' ) - 1;
454 - $newcontent = array();
455 -
456 - foreach ( $this->mContent as $diContainer ) {
457 - /* SMWRecordValue */ $recordValue = SMWDataValueFactory::newDataItemValue( $diContainer, $propertyValue->getDataItem() );
458 - $dataItems = $recordValue->getDataItems();
459 -
460 - if ( array_key_exists( $pos, $dataItems ) &&
461 - ( !is_null( $dataItems[$pos] ) ) ) {
462 - $newcontent[] = $dataItems[$pos];
463 - }
464 - }
465 -
466 - $this->mContent = $newcontent;
467 - }
468 - break;
469 - case SMWPrintRequest::PRINT_CCAT: ///NOTE: The limit is ignored here.
470 - if ( self::$catCacheObj != $this->mResult->getHash() ) {
471 - self::$catCache = $this->mStore->getPropertyValues( $this->mResult, new SMWDIProperty( '_INST' ) );
472 - self::$catCacheObj = $this->mResult->getHash();
473 - }
474 -
475 - $found = '0';
476 - $prkey = $this->mPrintRequest->getData()->getDBkey();
477 -
478 - foreach ( self::$catCache as $cat ) {
479 - if ( $cat->getDBkey() == $prkey ) {
480 - $found = '1';
481 - break;
482 - }
483 - }
484 - $this->mContent = array( new SMWDIBoolean( $found ) );
485 - break;
486 - default: $this->mContent = array(); // Unknown print request.
487 - }
488 -
489 - reset( $this->mContent );
490 -
491 - wfProfileOut( 'SMWQueryResult::loadContent (SMW)' );
492 - }
493 -
494 - /**
495 - * Make a request option object based on the given parameters, and
496 - * return NULL if no such object is required. The parameter defines
497 - * if the limit should be taken into account, which is not always desired
498 - * (especially if results are to be cached for future use).
499 - *
500 - * @param boolean $useLimit
501 - *
502 - * @return SMWRequestOptions or null
503 - */
504 - protected function getRequestOptions( $useLimit = true ) {
505 - $limit = $useLimit ? $this->mPrintRequest->getParameter( 'limit' ) : false;
506 - $order = trim( $this->mPrintRequest->getParameter( 'order' ) );
507 -
508 - // Important: use "!=" for order, since trim() above does never return "false", use "!==" for limit since "0" is meaningful here.
509 - if ( ( $limit !== false ) || ( $order != false ) ) {
510 - $options = new SMWRequestOptions();
511 -
512 - if ( $limit !== false ) $options->limit = trim( $limit );
513 -
514 - if ( ( $order == 'descending' ) || ( $order == 'reverse' ) || ( $order == 'desc' ) ) {
515 - $options->sort = true;
516 - $options->ascending = false;
517 - } elseif ( ( $order == 'ascending' ) || ( $order == 'asc' ) ) {
518 - $options->sort = true;
519 - $options->ascending = true;
520 - }
521 - } else {
522 - $options = null;
523 - }
524 -
525 - return $options;
526 - }
527 -
528 -}
\ No newline at end of file
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_ResultArray.php
@@ -0,0 +1,275 @@
 2+<?php
 3+
 4+/**
 5+ * Container for the contents of a single result field of a query result,
 6+ * i.e. basically an array of SMWDataItems with some additional parameters.
 7+ * The content of the array is fetched on demand only.
 8+ *
 9+ * @file SMW_ResultArray.php
 10+ * @ingroup SMWQuery
 11+ *
 12+ * @author Markus Krötzsch
 13+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 14+ */
 15+class SMWResultArray {
 16+
 17+ /**
 18+ * @var SMWPrintRequest
 19+ */
 20+ protected $mPrintRequest;
 21+
 22+ /**
 23+ * @var SMWDIWikiPage
 24+ */
 25+ protected $mResult;
 26+
 27+ /**
 28+ * @var SMWStore
 29+ */
 30+ protected $mStore;
 31+
 32+ /**
 33+ * @var array of SMWDataItem or false
 34+ */
 35+ protected $mContent;
 36+
 37+ static protected $catCacheObj = false;
 38+ static protected $catCache = false;
 39+
 40+ /**
 41+ * Constructor.
 42+ *
 43+ * @param SMWDIWikiPage $resultPage
 44+ * @param SMWPrintRequest $printRequest
 45+ * @param SMWStore $store
 46+ */
 47+ public function __construct( SMWDIWikiPage $resultPage, SMWPrintRequest $printRequest, SMWStore $store ) {
 48+ $this->mResult = $resultPage;
 49+ $this->mPrintRequest = $printRequest;
 50+ $this->mStore = $store;
 51+ $this->mContent = false;
 52+ }
 53+
 54+ /**
 55+ * Get the SMWStore object that this result is based on.
 56+ *
 57+ * @return SMWStore
 58+ */
 59+ public function getStore() {
 60+ return $this->mStore;
 61+ }
 62+
 63+ /**
 64+ * Returns the SMWDIWikiPage object to which this SMWResultArray refers.
 65+ * If you only care for those objects, consider using SMWQueryResult::getResults()
 66+ * directly.
 67+ *
 68+ * @return SMWDIWikiPage
 69+ */
 70+ public function getResultSubject() {
 71+ return $this->mResult;
 72+ }
 73+
 74+ /**
 75+ * Returns an array of SMWDataItem objects that contain the results of
 76+ * the given print request for the given result object.
 77+ *
 78+ * @return array of SMWDataItem or false
 79+ */
 80+ public function getContent() {
 81+ $this->loadContent();
 82+ return $this->mContent;
 83+ }
 84+
 85+ /**
 86+ * Return an SMWPrintRequest object describing what is contained in this
 87+ * result set.
 88+ *
 89+ * @return SMWPrintRequest
 90+ */
 91+ public function getPrintRequest() {
 92+ return $this->mPrintRequest;
 93+ }
 94+
 95+ /**
 96+ * Compatibility alias for getNextDatItem().
 97+ * @deprecated since 1.6. Call getNextDataValue() or getNextDataItem() directly as needed. Method will vanish before SMW 1.7.
 98+ */
 99+ public function getNextObject() {
 100+ return $this->getNextDataValue();
 101+ }
 102+
 103+ /**
 104+ * Return the next SMWDataItem object or false if no further object exists.
 105+ *
 106+ * @since 1.6
 107+ *
 108+ * @return SMWDataItem or false
 109+ */
 110+ public function getNextDataItem() {
 111+ $this->loadContent();
 112+ $result = current( $this->mContent );
 113+ next( $this->mContent );
 114+ return $result;
 115+ }
 116+
 117+
 118+ /**
 119+ * Return an SMWDataValue object for the next SMWDataItem object or
 120+ * false if no further object exists.
 121+ *
 122+ * @since 1.6
 123+ *
 124+ * @return SMWDataValue or false
 125+ */
 126+ public function getNextDataValue() {
 127+ $di = $this->getNextDataItem();
 128+ if ( $di === false ) {
 129+ return false;
 130+ }
 131+ if ( $this->mPrintRequest->getMode() == SMWPrintRequest::PRINT_PROP ) {
 132+ $diProperty = $this->mPrintRequest->getData()->getDataItem();
 133+ } else {
 134+ $diProperty = null;
 135+ }
 136+ $dv = SMWDataValueFactory::newDataItemValue( $di, $diProperty );
 137+ if ( $this->mPrintRequest->getOutputFormat() ) {
 138+ $dv->setOutputFormat( $this->mPrintRequest->getOutputFormat() );
 139+ }
 140+ return $dv;
 141+ }
 142+
 143+ /**
 144+ * Return the main text representation of the next SMWDataItem object
 145+ * in the specified format, or false if no further object exists.
 146+ *
 147+ * The parameter $linker controls linking of title values and should
 148+ * be some Linker object (or NULL for no linking).
 149+ *
 150+ * @param integer $outputMode
 151+ * @param mixed $linker
 152+ *
 153+ * @return string or false
 154+ */
 155+ public function getNextText( $outputMode, $linker = null ) {
 156+ $dataValue = $this->getNextDataValue();
 157+ if ( $dataValue !== false ) { // Print data values.
 158+ return $dataValue->getShortText( $outputMode, $linker );
 159+ } else {
 160+ return false;
 161+ }
 162+ }
 163+
 164+ /**
 165+ * Load results of the given print request and result subject. This is only
 166+ * done when needed.
 167+ */
 168+ protected function loadContent() {
 169+ if ( $this->mContent !== false ) return;
 170+
 171+ wfProfileIn( 'SMWQueryResult::loadContent (SMW)' );
 172+
 173+ switch ( $this->mPrintRequest->getMode() ) {
 174+ case SMWPrintRequest::PRINT_THIS: // NOTE: The limit is ignored here.
 175+ $this->mContent = array( $this->mResult );
 176+ break;
 177+ case SMWPrintRequest::PRINT_CATS:
 178+ // Always recompute cache here to ensure output format is respected.
 179+ self::$catCache = $this->mStore->getPropertyValues( $this->mResult,
 180+ new SMWDIProperty( '_INST' ), $this->getRequestOptions( false ) );
 181+ self::$catCacheObj = $this->mResult->getHash();
 182+
 183+ $limit = $this->mPrintRequest->getParameter( 'limit' );
 184+ $this->mContent = ( $limit === false ) ? ( self::$catCache ) :
 185+ array_slice( self::$catCache, 0, $limit );
 186+ break;
 187+ case SMWPrintRequest::PRINT_PROP:
 188+ $propertyValue = $this->mPrintRequest->getData();
 189+ if ( $propertyValue->isValid() ) {
 190+ $this->mContent = $this->mStore->getPropertyValues( $this->mResult,
 191+ $propertyValue->getDataItem(), $this->getRequestOptions() );
 192+ } else {
 193+ $this->mContent = array();
 194+ }
 195+
 196+ // Print one component of a multi-valued string.
 197+ // Known limitation: the printrequest still is of type _rec, so if printers check
 198+ // for this then they will not recognize that it returns some more concrete type.
 199+ if ( ( $this->mPrintRequest->getTypeID() == '_rec' ) &&
 200+ ( $this->mPrintRequest->getParameter( 'index' ) !== false ) ) {
 201+ $pos = $this->mPrintRequest->getParameter( 'index' ) - 1;
 202+ $newcontent = array();
 203+
 204+ foreach ( $this->mContent as $diContainer ) {
 205+ /* SMWRecordValue */ $recordValue = SMWDataValueFactory::newDataItemValue( $diContainer, $propertyValue->getDataItem() );
 206+ $dataItems = $recordValue->getDataItems();
 207+
 208+ if ( array_key_exists( $pos, $dataItems ) &&
 209+ ( !is_null( $dataItems[$pos] ) ) ) {
 210+ $newcontent[] = $dataItems[$pos];
 211+ }
 212+ }
 213+
 214+ $this->mContent = $newcontent;
 215+ }
 216+ break;
 217+ case SMWPrintRequest::PRINT_CCAT: ///NOTE: The limit is ignored here.
 218+ if ( self::$catCacheObj != $this->mResult->getHash() ) {
 219+ self::$catCache = $this->mStore->getPropertyValues( $this->mResult, new SMWDIProperty( '_INST' ) );
 220+ self::$catCacheObj = $this->mResult->getHash();
 221+ }
 222+
 223+ $found = '0';
 224+ $prkey = $this->mPrintRequest->getData()->getDBkey();
 225+
 226+ foreach ( self::$catCache as $cat ) {
 227+ if ( $cat->getDBkey() == $prkey ) {
 228+ $found = '1';
 229+ break;
 230+ }
 231+ }
 232+ $this->mContent = array( new SMWDIBoolean( $found ) );
 233+ break;
 234+ default: $this->mContent = array(); // Unknown print request.
 235+ }
 236+
 237+ reset( $this->mContent );
 238+
 239+ wfProfileOut( 'SMWQueryResult::loadContent (SMW)' );
 240+ }
 241+
 242+ /**
 243+ * Make a request option object based on the given parameters, and
 244+ * return NULL if no such object is required. The parameter defines
 245+ * if the limit should be taken into account, which is not always desired
 246+ * (especially if results are to be cached for future use).
 247+ *
 248+ * @param boolean $useLimit
 249+ *
 250+ * @return SMWRequestOptions or null
 251+ */
 252+ protected function getRequestOptions( $useLimit = true ) {
 253+ $limit = $useLimit ? $this->mPrintRequest->getParameter( 'limit' ) : false;
 254+ $order = trim( $this->mPrintRequest->getParameter( 'order' ) );
 255+
 256+ // Important: use "!=" for order, since trim() above does never return "false", use "!==" for limit since "0" is meaningful here.
 257+ if ( ( $limit !== false ) || ( $order != false ) ) {
 258+ $options = new SMWRequestOptions();
 259+
 260+ if ( $limit !== false ) $options->limit = trim( $limit );
 261+
 262+ if ( ( $order == 'descending' ) || ( $order == 'reverse' ) || ( $order == 'desc' ) ) {
 263+ $options->sort = true;
 264+ $options->ascending = false;
 265+ } elseif ( ( $order == 'ascending' ) || ( $order == 'asc' ) ) {
 266+ $options->sort = true;
 267+ $options->ascending = true;
 268+ }
 269+ } else {
 270+ $options = null;
 271+ }
 272+
 273+ return $options;
 274+ }
 275+
 276+}
Property changes on: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_ResultArray.php
___________________________________________________________________
Added: svn:eol-style
1277 + native
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php
@@ -302,6 +302,7 @@
303303 $stoDir = $smwgIP . 'includes/storage/';
304304 $wgAutoloadClasses['SMWQuery'] = $stoDir . 'SMW_Query.php';
305305 $wgAutoloadClasses['SMWQueryResult'] = $stoDir . 'SMW_QueryResult.php';
 306+ $wgAutoloadClasses['SMWResultArray'] = $stoDir . 'SMW_ResultArray.php';
306307 $wgAutoloadClasses['SMWStore'] = $stoDir . 'SMW_Store.php';
307308 $wgAutoloadClasses['SMWStringCondition'] = $stoDir . 'SMW_Store.php';
308309 $wgAutoloadClasses['SMWRequestOptions'] = $stoDir . 'SMW_Store.php';

Status & tagging log