/**
  * Return relevant data set
  *
  * @param SMWQueryResult $res
  * @param $outputMode
  *
  * @return array
  */
 protected function getResultData(SMWQueryResult $res, $outputMode)
 {
     // Init
     $properties = array();
     $excludeProperties = explode($this->params['sep'], $this->params['exclude']);
     // Options used when retrieving data from the store
     $reqOptions = new SMWRequestOptions();
     $reqOptions->sort = true;
     $reqOptions->limit = $this->params['limit'];
     foreach ($res->getResults() as $key => $page) {
         // Find properties assigned to selected subject page
         foreach ($res->getStore()->getInProperties($page, $reqOptions) as $property) {
             $propertyLabel = $property->getLabel();
             // Exclude property from result set
             if (in_array($propertyLabel, $excludeProperties)) {
                 continue;
             }
             // NOTE There could be thousands of incoming links for one property,
             // counting the length of an array is inefficient but we don't want
             // to implement any native db select outside of smw core and rather
             // would like to see a counting method available but to counter
             // any potential inefficiencies we curb the selection by using
             // SMWRequestOptions -> limit as boundary
             $count = count($res->getStore()->getPropertySubjects($property, $page, $reqOptions));
             // Limit ouput by threshold
             if ($this->params['min'] <= $count) {
                 $properties[$propertyLabel] = $count;
             }
         }
     }
     return $properties;
 }
 /**
  * Return serialised results in specified format.
  * Implemented by subclasses.
  */
 protected function getResultText(SMWQueryResult $res, $outputmode)
 {
     $html = '';
     $id = uniqid();
     // build an array of article IDs contained in the result set
     $objects = array();
     foreach ($res->getResults() as $key => $object) {
         $objects[] = array($object->getTitle()->getArticleId());
         $html .= $key . ': ' . $object->getSerialization() . "<br>\n";
     }
     // build an array of data about the printrequests
     $printrequests = array();
     foreach ($res->getPrintRequests() as $key => $printrequest) {
         $data = $printrequest->getData();
         if ($data instanceof SMWPropertyValue) {
             $name = $data->getDataItem()->getKey();
         } else {
             $name = null;
         }
         $printrequests[] = array($printrequest->getMode(), $printrequest->getLabel(), $name, $printrequest->getOutputFormat(), $printrequest->getParameters());
     }
     // write out results and query params into JS arrays
     // Define the srf_filtered_values array
     SMWOutputs::requireScript('srf_slideshow', Html::inlineScript('srf_slideshow = {};'));
     SMWOutputs::requireScript('srf_slideshow' . $id, Html::inlineScript('srf_slideshow["' . $id . '"] = ' . json_encode(array($objects, $this->params['template'], $this->params['delay'] * 1000, $this->params['height'], $this->params['width'], $this->params['nav controls'], $this->params['effect'], json_encode($printrequests))) . ';'));
     SMWOutputs::requireResource('ext.srf.slideshow');
     if ($this->params['nav controls']) {
         SMWOutputs::requireResource('jquery.ui.slider');
     }
     return Html::element('div', array('id' => $id, 'class' => 'srf-slideshow ' . $id . ' ' . $this->params['class']));
 }
 /**
  * @since 2.3
  *
  * @return DIWikiPage[]|[]
  */
 public function getQueryDependencySubjectList()
 {
     // Resolving dependencies for non-embedded queries or limit=0 (which only
     // links to Special:Ask via further results) is not required
     if ($this->getSubject() === null || $this->getQuery()->getLimit() == 0) {
         return array();
     }
     $description = $this->getQuery()->getDescription();
     $dependencySubjectList = array($this->getSubject());
     // Find entities described by the query
     $this->doResolveDependenciesFromDescription($dependencySubjectList, $description);
     $this->doResolveDependenciesFromPrintRequest($dependencySubjectList, $description->getPrintRequests());
     $dependencySubjectList = array_merge($dependencySubjectList, $this->queryResult->getResults());
     $this->queryResult->reset();
     return $dependencySubjectList;
 }
示例#4
0
 /**
  * Get the serialization for a SMWQueryResult object.
  *
  * @since 1.7
  *
  * @param SMWQueryResult $result
  *
  * @return array
  */
 public static function getSerializedQueryResult(SMWQueryResult $queryResult)
 {
     $results = array();
     $printRequests = array();
     foreach ($queryResult->getPrintRequests() as $printRequest) {
         $printRequests[] = array('label' => $printRequest->getLabel(), 'typeid' => $printRequest->getTypeID(), 'mode' => $printRequest->getMode());
     }
     foreach ($queryResult->getResults() as $diWikiPage) {
         $result = array('printouts' => array());
         foreach ($queryResult->getPrintRequests() as $printRequest) {
             $resultAarray = new SMWResultArray($diWikiPage, $printRequest, $queryResult->getStore());
             if ($printRequest->getMode() === SMWPrintRequest::PRINT_THIS) {
                 $dataItems = $resultAarray->getContent();
                 $result += self::getSerialization(array_shift($dataItems));
             } else {
                 $result['printouts'][$printRequest->getLabel()] = array_map(array(__CLASS__, 'getSerialization'), $resultAarray->getContent());
             }
         }
         $results[$diWikiPage->getTitle()->getFullText()] = $result;
     }
     return array('printrequests' => $printRequests, 'results' => $results);
 }
示例#5
0
 protected function getResultText(SMWQueryResult $res, $outputmode)
 {
     if (!isset($this->paramWidth)) {
         $this->paramWidth = -1;
     }
     $this->isHTML = true;
     global $wgOut;
     $wgOut->addModules('ext.SemanticImageAnnotator.ResultFormat');
     $resultPages = $res->getResults();
     $store = smwfGetStore();
     $annotationArray = array();
     foreach ($resultPages as $page) {
         $namespace = $page->getNamespace();
         if ($namespace != 380) {
             continue;
         }
         $coords = "";
         $imgUrl = "";
         $pageName = $page->getDBkey();
         $propertyCoords = new SMWDIProperty("__SIA_RECTCOORDS");
         $propertyImgUrl = new SMWDIProperty("__SIA_IMG_URL");
         $coordValues = $store->getPropertyValues($page, $propertyCoords);
         $urlValues = $store->getPropertyValues($page, $propertyImgUrl);
         if (count($coordValues) == 1 && $coordValues[0] instanceof SMWDIString) {
             $coords = $coordValues[0]->getString();
         }
         if (count($urlValues) == 1 && $coordValues[0] instanceof SMWDIString) {
             $imgUrl = $urlValues[0]->getString();
         }
         if ($coords != "" && $imgUrl != "") {
             $annotationArray[$imgUrl][] = array($pageName, $coords);
         }
     }
     $returnString = "";
     foreach ($annotationArray as $image => $annotationData) {
         $returnString .= $this->createAnnotatedImg($image, $annotationData);
     }
     return $returnString;
 }
 /**
  * Get the serialization for a SMWQueryResult object.
  *
  * @since 1.7
  *
  * @param SMWQueryResult $result
  *
  * @return array
  */
 public static function getSerializedQueryResult(QueryResult $queryResult)
 {
     $results = array();
     $printRequests = array();
     foreach ($queryResult->getPrintRequests() as $printRequest) {
         $printRequests[] = self::getSerializedPrintRequestFormat($printRequest);
     }
     /**
      * @var DIWikiPage $diWikiPage
      * @var PrintRequest $printRequest
      */
     foreach ($queryResult->getResults() as $diWikiPage) {
         if (!$diWikiPage->getTitle() instanceof Title) {
             continue;
         }
         $result = array('printouts' => array());
         foreach ($queryResult->getPrintRequests() as $printRequest) {
             $resultArray = new SMWResultArray($diWikiPage, $printRequest, $queryResult->getStore());
             if ($printRequest->getMode() === PrintRequest::PRINT_THIS) {
                 $dataItems = $resultArray->getContent();
                 $result += self::getSerialization(array_shift($dataItems), $printRequest);
             } elseif ($resultArray->getContent() !== array()) {
                 $values = array();
                 foreach ($resultArray->getContent() as $dataItem) {
                     $values[] = self::getSerialization($dataItem, $printRequest);
                 }
                 $result['printouts'][$printRequest->getLabel()] = $values;
             } else {
                 // For those objects that are empty return an empty array
                 // to keep the output consistent
                 $result['printouts'][$printRequest->getLabel()] = array();
             }
         }
         $results[$diWikiPage->getTitle()->getFullText()] = $result;
     }
     return array('printrequests' => $printRequests, 'results' => $results);
 }
 public function getQueryResult(SMWQuery $query, $force = false, $cacheThis = true)
 {
     //get title of article in which query was executed
     //or set title to false if query was executed somehow else
     global $wgParser;
     $title = false;
     if ($wgParser && $wgParser->getTitle()) {
         $title = $wgParser->getTitle();
     }
     $defaultStore = smwfGetStore();
     // update the semdata object for this title, respectively
     // the query management annotations
     if ($title !== false && $cacheThis) {
         SMWQRCQueryManagementHandler::getInstance()->storeQueryMetadata($title, $query);
     }
     //delegate query processing to the responsible store
     if ($query instanceof SMWSPARQLQuery) {
         $store = $defaultStore;
     } else {
         global $smwgBaseStore;
         $store = new $smwgBaseStore();
     }
     $queryData = $this->getQueryData($query);
     // execute the query if no valid cache entry is available, if force was
     // set to true (e.g. by the update process) or if the query is executed because
     // of an edit or a purge action
     if ($force || !$this->isReadAccess() || !$this->hasValidCacheEntry($queryData)) {
         $queryResult = $store->doGetQueryResult($query);
         if ($cacheThis) {
             //add the serialized query result to the database
             $qrcStore = SMWQRCStore::getInstance()->getDB();
             $queryId = SMWQRCQueryManagementHandler::getInstance()->getQueryId($query);
             $lastUpdate = time();
             $dirty = false;
             if ($queryData) {
                 //results for this query already have been stored in the cache
                 if ($force) {
                     //this query result update was not triggered by a Wiki user action
                     $accessFrequency = SMWQRCPriorityCalculator::getInstance()->computeNewAccessFrequency($queryData['accessFrequency']);
                     $invalidationFrequency = SMWQRCPriorityCalculator::getInstance()->computeNewInvalidationFrequency($queryData['invalidationFrequency']);
                 } else {
                     $accessFrequency = $queryData['accessFrequency'] + 1;
                     $invalidationFrequency = $queryData['invalidationFrequency'] + 1;
                 }
                 $priority = 0;
                 $qrcStore->updateQueryData($queryId, serialize($queryResult), $lastUpdate, $accessFrequency, $invalidationFrequency, $dirty, $priority);
             } else {
                 $priority = 0;
                 $qrcStore->addQueryData($queryId, serialize($queryResult), $lastUpdate, 1, 0, $dirty, $priority);
             }
         }
     } else {
         $qrcStore = SMWQRCStore::getInstance()->getDB();
         $queryResult = unserialize($queryData['queryResult']);
         //update access frequency and query priority
         $priority = 0;
         $qrcStore->updateQueryData($queryData['queryId'], $queryData['queryResult'], $queryData['lastUpdate'], $queryData['accessFrequency'] + 1, $queryData['invalidationFrequency'], $queryData['dirty'], $priority);
         if ($query instanceof SMWQueryResult) {
             $query->addErrors($queryResult->getErrors());
             $queryResult = new SMWQueryResult($query->getDescription()->getPrintRequests(), $query, $queryResult->getResults(), $store, $queryResult->hasFurtherResults());
         }
     }
     return $queryResult;
 }
示例#8
0
 /**
  * @since 2.0
  *
  * @param  mixed $expected
  * @param  QueryResult $queryResult
  */
 public function assertThatQueryResultHasSubjects($expectedSubjects, QueryResult $queryResult)
 {
     $expectedSubjects = is_array($expectedSubjects) ? $expectedSubjects : array($expectedSubjects);
     $expectedToCount = count($expectedSubjects);
     $actualComparedToCount = 0;
     $this->assertEmpty($queryResult->getErrors());
     if ($expectedToCount == 0) {
         return;
     }
     $resultSubjects = $queryResult->getResults();
     foreach ($resultSubjects as $rKey => $resultSubject) {
         foreach ($expectedSubjects as $ekey => $expectedSubject) {
             if ($expectedSubject instanceof DIWikiPage && $expectedSubject->equals($resultSubject)) {
                 $actualComparedToCount++;
                 unset($expectedSubjects[$ekey]);
                 unset($resultSubjects[$rKey]);
             }
         }
     }
     $this->assertEquals($expectedToCount, $actualComparedToCount, 'Failed asserting that ' . implode(', ', $expectedSubjects) . ' is set.');
     $this->assertEmpty($resultSubjects, 'Failed to match results [ ' . implode(', ', $resultSubjects) . ' ] against the expected subjects.');
 }
示例#9
0
 protected function getResultText(SMWQueryResult $res, $outputmode)
 {
     global $wgParser;
     // No page should embed itself, find out who we are:
     if ($wgParser->getTitle() instanceof Title) {
         $title = $wgParser->getTitle()->getPrefixedText();
     } else {
         // this is likely to be in vain -- this case is typical if we run on special pages
         global $wgTitle;
         $title = $wgTitle->getPrefixedText();
     }
     // print header
     $result = '';
     $footer = '';
     $embstart = '';
     $embend = '';
     $headstart = '';
     $headend = '';
     $this->hasTemplates = true;
     switch ($this->m_embedformat) {
         case 'h1':
         case 'h2':
         case 'h3':
         case 'h4':
         case 'h5':
         case 'h6':
             $headstart = '<' . $this->m_embedformat . '>';
             $headend = '</' . $this->m_embedformat . ">\n";
             break;
         case 'ul':
         case 'ol':
             $result .= '<' . $this->m_embedformat . '>';
             $footer = '</' . $this->m_embedformat . '>';
             $embstart = '<li>';
             $headend = "<br />\n";
             $embend = "</li>\n";
             break;
     }
     // Print all result rows:
     foreach ($res->getResults() as $diWikiPage) {
         if ($diWikiPage instanceof SMWDIWikiPage) {
             // ensure that we deal with title-likes
             $dvWikiPage = SMWDataValueFactory::newDataItemValue($diWikiPage, null);
             $result .= $embstart;
             if ($this->m_showhead) {
                 $result .= $headstart . $dvWikiPage->getLongWikiText($this->mLinker) . $headend;
             }
             if ($dvWikiPage->getLongWikiText() != $title) {
                 if ($diWikiPage->getNamespace() == NS_MAIN) {
                     $result .= '{{:' . $diWikiPage->getDBkey() . '}}';
                 } else {
                     $result .= '{{' . $dvWikiPage->getLongWikiText() . '}}';
                 }
             } else {
                 // block recursion
                 $result .= '<b>' . $dvWikiPage->getLongWikiText() . '</b>';
             }
             $result .= $embend;
         }
     }
     // show link to more results
     if ($this->linkFurtherResults($res)) {
         $result .= $embstart . $this->getFurtherResultsLink($res, $outputmode)->getText(SMW_OUTPUT_WIKI, $this->mLinker) . $embend;
     }
     $result .= $footer;
     return $result;
 }
 public function __construct(\SMWQueryResult $result, $count = null)
 {
     $this->pages = $result->getResults();
     $this->count = $count;
 }