示例#1
0
 function setTypeAndPossibleValues()
 {
     $proptitle = Title::makeTitleSafe(SMW_NS_PROPERTY, $this->mSemanticProperty);
     if ($proptitle === null) {
         return;
     }
     $store = smwfGetStore();
     // this returns an array of objects
     $allowed_values = SFUtils::getSMWPropertyValues($store, $proptitle, "Allows value");
     $label_formats = SFUtils::getSMWPropertyValues($store, $proptitle, "Has field label format");
     $propValue = SMWDIProperty::newFromUserLabel($this->mSemanticProperty);
     $this->mPropertyType = $propValue->findPropertyTypeID();
     foreach ($allowed_values as $allowed_value) {
         // HTML-unencode each value
         $this->mPossibleValues[] = html_entity_decode($allowed_value);
         if (count($label_formats) > 0) {
             $label_format = $label_formats[0];
             $prop_instance = SMWDataValueFactory::findTypeID($this->mPropertyType);
             $label_value = SMWDataValueFactory::newTypeIDValue($prop_instance, $wiki_value);
             $label_value->setOutputFormat($label_format);
             $this->mValueLabels[$wiki_value] = html_entity_decode($label_value->getWikiValue());
         }
     }
     // HACK - if there were any possible values, set the property
     // type to be 'enumeration', regardless of what the actual type is
     if (count($this->mPossibleValues) > 0) {
         $this->mPropertyType = 'enumeration';
     }
 }
 function setTypeAndPossibleValues()
 {
     if (!defined('SMW_NS_PROPERTY')) {
         return;
     }
     // The presence of "-" at the beginning of a property name
     // (which happens if SF tries to parse an inverse query)
     // leads to an error in SMW - just exit if that's the case.
     if (strpos($this->mSemanticProperty, '-') === 0) {
         return;
     }
     $proptitle = Title::makeTitleSafe(SMW_NS_PROPERTY, $this->mSemanticProperty);
     if ($proptitle === null) {
         return;
     }
     $store = SFUtils::getSMWStore();
     // this returns an array of objects
     $allowed_values = SFUtils::getSMWPropertyValues($store, $proptitle, "Allows value");
     $label_formats = SFUtils::getSMWPropertyValues($store, $proptitle, "Has field label format");
     $propValue = SMWDIProperty::newFromUserLabel($this->mSemanticProperty);
     $this->mPropertyType = $propValue->findPropertyTypeID();
     foreach ($allowed_values as $allowed_value) {
         // HTML-unencode each value
         $this->mPossibleValues[] = html_entity_decode($allowed_value);
         if (count($label_formats) > 0) {
             $label_format = $label_formats[0];
             $prop_instance = SMWDataValueFactory::findTypeID($this->mPropertyType);
             $label_value = SMWDataValueFactory::newTypeIDValue($prop_instance, $wiki_value);
             $label_value->setOutputFormat($label_format);
             $this->mValueLabels[$wiki_value] = html_entity_decode($label_value->getWikiValue());
         }
     }
     // HACK - if there were any possible values, set the property
     // type to be 'enumeration', regardless of what the actual type is
     if (count($this->mPossibleValues) > 0) {
         $this->mPropertyType = 'enumeration';
     }
 }
示例#3
0
 /**
  * Determine what kind of OWL property some SMW property should be exported as.
  * The input is an SMWTypesValue object, a typeid string, or empty (use default)
  * @todo An improved mechanism for selecting property types here is needed.
  */
 public static function getOWLPropertyType($type = '')
 {
     if ($type instanceof SMWDIWikiPage) {
         $type = SMWDataValueFactory::findTypeID(str_replace('_', ' ', $type->getDBkey()));
     } elseif ($type == false) {
         $type = '';
     }
     // else keep $type
     switch ($type) {
         case '_anu':
             return 'AnnotationProperty';
         case '':
         case '_wpg':
         case '_wpp':
         case '_wpc':
         case '_wpf':
         case '_uri':
         case '_ema':
         case '_tel':
         case '_rec':
         case '__typ':
         case '__red':
         case '__spf':
         case '__spu':
             return 'ObjectProperty';
         default:
             return 'DatatypeProperty';
     }
 }
示例#4
0
 /**
  * This class uses type ids as DB keys.
  *
  * @return string
  */
 public function getDBkey()
 {
     return $this->isValid() ? SMWDataValueFactory::findTypeID($this->m_realLabel) : '';
 }
 public function getPropertyWithType($match, $typeLabel)
 {
     $db =& wfGetDB(DB_SLAVE);
     $smw_spec2 = $db->tableName('smw_spec2');
     $smw_ids = $db->tableName('smw_ids');
     $page = $db->tableName('page');
     $result = array();
     $typeID = SMWDataValueFactory::findTypeID($typeLabel);
     $hasTypePropertyID = smwfGetStore()->getSMWPropertyID(SMWPropertyValue::makeProperty("_TYPE"));
     $res = $db->query('(SELECT i2.smw_title AS title FROM ' . $smw_ids . ' i2 ' . 'JOIN ' . $smw_spec2 . ' s1 ON i2.smw_id = s1.s_id AND s1.p_id = ' . $hasTypePropertyID . ' ' . 'JOIN ' . $smw_ids . ' i ON s1.value_string = i.smw_title AND i.smw_namespace = ' . SMW_NS_TYPE . ' ' . 'JOIN ' . $smw_spec2 . ' s2 ON s2.s_id = i.smw_id AND s2.value_string REGEXP ' . $db->addQuotes("([0-9].?[0-9]*|,) {$typeLabel}(,|\$)") . 'WHERE i2.smw_namespace = ' . SMW_NS_PROPERTY . ' AND UPPER(' . DBHelper::convertColumn('i2.smw_title') . ') LIKE UPPER(' . $db->addQuotes("%{$match}%") . '))' . ' UNION (SELECT smw_title AS title FROM smw_ids i ' . 'JOIN ' . $smw_spec2 . ' s1 ON i.smw_id = s1.s_id AND s1.p_id = ' . $hasTypePropertyID . ' ' . 'WHERE UPPER(' . DBHelper::convertColumn('i.smw_title') . ') LIKE UPPER(' . $db->addQuotes('%' . $match . '%') . ') AND ' . 'UPPER(' . DBHelper::convertColumn('s1.value_string') . ') = UPPER(' . $db->addQuotes($typeID) . ') AND smw_namespace = ' . SMW_NS_PROPERTY . ') ' . 'ORDER BY title LIMIT ' . SMW_AC_MAX_RESULTS);
     if ($db->numRows($res) > 0) {
         while ($row = $db->fetchObject($res)) {
             if (smwf_om_userCan($row->title, 'read', SMW_NS_PROPERTY) == 'true') {
                 $result[] = Title::newFromText($row->title, SMW_NS_PROPERTY);
             }
         }
     }
     $db->freeResult($res);
     return $result;
 }
 /**
  * Modify the given query object to account for some property condition for the given property.
  * The parameter $property may be a property object or an internal storage id. This is what makes
  * this method useful: it can be used even with internal properties that have no MediaWiki Title.
  * $typeid is set if property ids are used, since internal properties may not have a defined type.
  * Some properties cannot be queried for; in this case, the query type is changed to SMW_SQL2_NOQUERY.
  * Callers need to check for this.
  */
 protected function compilePropertyCondition(&$query, $property, SMWDescription $valuedesc, $typeid = false)
 {
     $query->joinfield = "{$query->alias}.s_id";
     if ($property instanceof SMWPropertyValue) {
         $typeid = $property->getPropertyTypeID();
         $mode = SMWSQLStore2::getStorageMode($typeid);
         $pid = $this->m_store->getSMWPropertyID($property);
         $sortkey = $property->getDBkey();
         // / TODO: strictly speaking, the DB key is not what we want here, since sortkey is based on a "wiki value"
         if ($mode != SMW_SQL2_SUBS2) {
             // also make property hierarchy (though not for all properties)
             $pqid = SMWSQLStore2Query::$qnum;
             $pquery = new SMWSQLStore2Query();
             $pquery->type = SMW_SQL2_PROP_HIERARCHY;
             $pquery->joinfield = array($pid);
             $query->components[$pqid] = "{$query->alias}.p_id";
             $this->m_queries[$pqid] = $pquery;
         }
     } else {
         $pid = $property;
         $sortkey = false;
         $mode = SMWSQLStore2::getStorageMode($typeid);
         if ($mode != SMW_SQL2_SUBS2) {
             // no property hierarchy, but normal query (not for all properties)
             $query->where = "{$query->alias}.p_id=" . $this->m_dbs->addQuotes($pid);
         }
     }
     $mode = SMWSQLStore2::getStorageMode($typeid);
     $sortfield = '';
     // used if we should sort by this property
     switch ($mode) {
         case SMW_SQL2_RELS2:
         case SMW_SQL2_SUBS2:
             // subconditions as subqueries (compiled)
             $query->jointable = $mode == SMW_SQL2_RELS2 ? 'smw_rels2' : 'smw_subs2';
             $sub = $this->compileQueries($valuedesc);
             if ($sub >= 0) {
                 $query->components[$sub] = "{$query->alias}.o_id";
             }
             if ($sortkey && array_key_exists($sortkey, $this->m_sortkeys)) {
                 $query->from = ' INNER JOIN ' . $this->m_dbs->tableName('smw_ids') . " AS ids{$query->alias} ON ids{$query->alias}.smw_id={$query->alias}.o_id";
                 $sortfield = "ids{$query->alias}.smw_title";
                 // / TODO: as below, smw_ids here is possibly duplicated! Can we prevent that? (PERFORMANCE)
             }
             break;
         case SMW_SQL2_NARY2:
             $query->jointable = 'smw_rels2';
             if ($valuedesc instanceof SMWValueList) {
                 // anything else is ignored!
                 $typevalue = $property->getTypesValue();
                 $typelabels = $typevalue->getTypeLabels();
                 reset($typelabels);
                 $subqid = SMWSQLStore2Query::$qnum;
                 $subquery = new SMWSQLStore2Query();
                 $subquery->type = SMW_SQL2_CONJUNCTION;
                 $query->components[$subqid] = "{$query->alias}.o_id";
                 $this->m_queries[$subqid] = $subquery;
                 for ($i = 0; $i < $valuedesc->getCount(); $i++) {
                     $desc = $valuedesc->getDescription($i);
                     if ($desc !== NULL) {
                         $stypeid = SMWDataValueFactory::findTypeID(current($typelabels));
                         $valpid = $this->m_store->getSMWPageID(strval($i), SMW_NS_PROPERTY, SMW_SQL2_SMWIW);
                         $valqid = SMWSQLStore2Query::$qnum;
                         $valquery = new SMWSQLStore2Query();
                         $this->compilePropertyCondition($valquery, $valpid, $desc, $stypeid);
                         if ($valquery->type != SMW_SQL2_NOQUERY) {
                             $subquery->components[$valqid] = true;
                             $this->m_queries[$valqid] = $valquery;
                         }
                     }
                     next($typelabels);
                 }
             }
             break;
         case SMW_SQL2_TEXT2:
             // no subconditions
             $query->jointable = 'smw_text2';
             break;
         case SMW_SQL2_ATTS2:
         case SMW_SQL2_SPEC2:
             // subquery only conj/disj of values, compile to single "where"
             $query->jointable = $mode == SMW_SQL2_ATTS2 ? 'smw_atts2' : 'smw_spec2';
             $aw = $this->compileAttributeWhere($valuedesc, "{$query->alias}");
             if ($aw != '') {
                 $query->where .= ($query->where ? ' AND ' : '') . $aw;
             }
             if ($sortkey && array_key_exists($sortkey, $this->m_sortkeys)) {
                 if ($mode == SMW_SQL2_ATTS2) {
                     $sortfield = "{$query->alias}." . (SMWDataValueFactory::newTypeIDValue($typeid)->isNumeric() ? 'value_num' : 'value_xsd');
                 } else {
                     $sortfield = "{$query->alias}.value_string";
                 }
             }
             break;
         default:
             // drop this query
             $query->type = SMW_SQL2_NOQUERY;
             $sortfield = false;
     }
     if ($sortfield) {
         $query->sortfields[$sortkey] = $sortfield;
     }
 }
示例#7
0
function smwf_qi_QIAccess($method, $params)
{
    $p_array = explode(",", $params);
    global $smwgQEnabled;
    if ($method == "getPropertyInformation") {
        return qiGetPropertyInformation($p_array[0]);
    } else {
        if ($method == "getPropertyTypes") {
            $p_array = func_get_args();
            $types = "<propertyTypes>";
            for ($i = 1; $i < count($p_array); $i++) {
                $types .= qiGetPropertyInformation($p_array[$i]);
            }
            $types .= "</propertyTypes>";
            return $types;
        } else {
            if ($method == "getNumericTypes") {
                $numtypes = array();
                $types = SMWDataValueFactory::getKnownTypeLabels();
                foreach ($types as $v) {
                    $id = SMWDataValueFactory::findTypeID($v);
                    if (SMWDataValueFactory::newTypeIDValue($id)->isNumeric()) {
                        array_push($numtypes, strtolower($v));
                    }
                }
                return implode(",", $numtypes);
            } else {
                if ($method == "getQueryResult") {
                    $result = "null";
                    if ($smwgQEnabled) {
                        // read fix parameters from QI GUI
                        $params = count($p_array) > 1 ? explode("|", $p_array[1]) : array();
                        $fixparams = array();
                        foreach ($params as $p) {
                            if (strlen($p) > 0 && strpos($p, "=") !== false) {
                                list($key, $value) = explode("=", $p);
                                $fixparams[trim($key)] = str_replace('%2C', ',', $value);
                            }
                        }
                        // indicate that it comes from an ajax call
                        $fixparams['ajaxCall'] = true;
                        // fix bug 10812: if query string contains a ,
                        $p_array[0] = str_replace('%2C', ',', $p_array[0]);
                        // read query with printouts and (possibly) other parameters like sort, order, limit, etc...
                        $pos = strpos($p_array[0], "|?");
                        if ($pos > 0) {
                            $rawparams[] = trim(substr($p_array[0], 0, $pos));
                            $ps = explode("|?", trim(substr($p_array[0], $pos + 2)));
                            foreach ($ps as $param) {
                                $rawparams[] = "?" . trim($param);
                            }
                        } else {
                            $ps = preg_split('/[^\\|]{1}\\|{1}(?!\\|)/s', $p_array[0]);
                            if (count($ps) > 1) {
                                // last char of query condition is missing (matched with [^\|]{1}) therefore copy from original
                                $rawparams[] = trim(substr($p_array[0], 0, strlen($ps[0]) + 1));
                                array_shift($ps);
                                // remove the query condition
                                // add other params for formating etc.
                                foreach ($ps as $param) {
                                    $rawparams[] = trim($param);
                                }
                            } else {
                                $rawparams[] = trim($p_array[0]);
                            }
                        }
                        $rawparams = array_merge($rawparams, $fixparams);
                        // set some default values, if params are not set
                        if (!in_array('reasoner', array_keys($fixparams))) {
                            $fixparams['reasoner'] = 'ask';
                        }
                        if (!in_array('format', array_keys($fixparams))) {
                            $fixparams['format'] = 'table';
                        }
                        // use SMW classes or TSC classes and parse params and answer query
                        if ($fixparams['reasoner'] == 'ask') {
                            SMWQueryProcessor::processFunctionParams($rawparams, $querystring, $params, $printouts);
                        } else {
                            if ($fixparams['reasoner'] == 'sparql') {
                                SMWSPARQLQueryProcessor::processFunctionParams($rawparams, $querystring, $params, $printouts);
                            }
                        }
                        // check if there is any result and if it corresponds to the selected format
                        $mainlabel = isset($rawparams['mainlabel']) && $rawparams['mainlabel'] == '-';
                        $invalidRes = smwf_qi_CheckValidResult($printouts, $fixparams['format'], $mainlabel);
                        if ($invalidRes != 0) {
                            return wfMsg('smw_qi_printout_err' . $invalidRes);
                        }
                        // quickfix: unset conflicting params for maps
                        if (in_array($fixparams['format'], array("map", "googlemaps2", "openlayers", "yahoomaps"))) {
                            if (isset($params['reasoner'])) {
                                unset($params['reasoner']);
                            }
                            if (isset($params['ajaxcall'])) {
                                unset($params['ajaxcall']);
                            }
                            if (isset($params['merge'])) {
                                unset($params['merge']);
                            }
                        }
                        // answer query using the SMW classes or TSC classes
                        if ($fixparams['reasoner'] == 'ask') {
                            $result = SMWQueryProcessor::getResultFromQueryString($querystring, $params, $printouts, SMW_OUTPUT_WIKI);
                        } else {
                            if ($fixparams['reasoner'] == 'sparql') {
                                $result = SMWSPARQLQueryProcessor::getResultFromQueryString($querystring, $params, $printouts, SMW_OUTPUT_WIKI);
                            }
                        }
                        // check for empty result
                        if (is_array($result) && trim($result[0]) == '' || trim($result == '')) {
                            return wfMsg('smw_qi_printout_err4');
                        }
                        switch ($fixparams['format']) {
                            case 'timeline':
                            case 'exhibit':
                            case 'eventline':
                                return $result;
                                break;
                            case 'gallery':
                            case 'googlepie':
                            case 'googlebar':
                            case 'ofc-pie':
                            case 'ofc-bar':
                            case 'ofc-bar_3d':
                            case 'ofc-line':
                            case 'ofc-scatterline':
                                return is_array($result) ? $result[0] : $result;
                                break;
                            case 'map':
                            case 'googlemaps2':
                            case 'openlayers':
                            case 'yahoomaps':
                                return wfMsg('smw_qi_printout_notavailable');
                            default:
                        }
                        $result = parseWikiText($result);
                        // add target="_new" for all links
                        $pattern = "|<a|i";
                        $result = preg_replace($pattern, '<a target="_new"', $result);
                        return $result;
                    }
                } else {
                    if ($method == "getQueryResultForDownload") {
                        $result = "null";
                        if ($smwgQEnabled) {
                            $params = array('format' => $p_array[1], 'link' => $p_array[2], 'intro' => $p_array[3], 'sort' => $p_array[4], 'limit' => $p_array[5], 'mainlabel' => $p_array[6], 'order' => $p_array[7], 'default' => $p_array[8], 'headers' => $p_array[9]);
                            $result = applyQueryHighlighting($p_array[0], $params);
                            // add target="_new" for all links
                            $pattern = "|<a|i";
                            $result = preg_replace($pattern, '<a target="_new"', $result);
                        }
                        if ($result != "null" && $result != "") {
                            global $request_query;
                            $request_query = true;
                        }
                        return $result;
                    } else {
                        if ($method == "getSupportedParameters") {
                            global $smwgResultFormats;
                            wfLoadExtensionMessages('SemanticMediaWiki');
                            $format = $p_array[0];
                            if (array_key_exists($format, $smwgResultFormats)) {
                                $formatclass = $smwgResultFormats[$format];
                            } else {
                                $formatclass = "SMWListResultPrinter";
                            }
                            // fix for missing parameter order
                            $order_missing = true;
                            $intro_missing = true;
                            $outro_missing = true;
                            $qp = new $formatclass($format, false);
                            $params = $qp->getParameters();
                            // repair some misplaced parameters
                            for ($i = 0; $i < count($params); $i++) {
                                switch ($params[$i]['name']) {
                                    case "order":
                                        $order_missing = false;
                                        break;
                                    case "template":
                                        if ($format != "template") {
                                            array_splice($params, $i, 1);
                                        }
                                        break;
                                    case "intro":
                                        if (substr($format, 0, 4) != "ofc-") {
                                            $intro_missing = false;
                                        }
                                        break;
                                    case "outro":
                                        if (substr($format, 0, 4) != "ofc-") {
                                            $outro_missing = false;
                                        }
                                        break;
                                    case "headers":
                                        if ($format != "table" && $format != "broadtable") {
                                            array_splice($params, $i, 1);
                                        }
                                        break;
                                }
                            }
                            if ($order_missing) {
                                $params[] = array('name' => 'order', 'type' => 'enumeration', 'description' => wfMsg('smw_qi_tt_order'), 'values' => array('ascending', 'descending'));
                            }
                            if ($intro_missing) {
                                $params[] = array('name' => 'intro', 'type' => 'string', 'description' => wfMsg('smw_qi_tt_intro'));
                            }
                            if ($outro_missing) {
                                $params[] = array('name' => 'outro', 'type' => 'string', 'description' => wfMsg('smw_qi_tt_outro'));
                            }
                            $jsonEnc = new Services_JSON();
                            return $jsonEnc->encode($params);
                        } else {
                            return "false";
                        }
                    }
                }
            }
        }
    }
}