public function getInstanceAsTarget($userInputToMatch, $domainRangeAnnotations)
 {
     global $smwgDefaultCollation;
     $db =& wfGetDB(DB_SLAVE);
     $page = $db->tableName('page');
     $categorylinks = $db->tableName('categorylinks');
     if (!isset($smwgDefaultCollation)) {
         $collation = '';
     } else {
         $collation = 'COLLATE ' . $smwgDefaultCollation;
     }
     // create virtual tables
     $db->query('CREATE TEMPORARY TABLE smw_ob_instances (instance VARCHAR(255) ' . $collation . ', namespace INTEGER(11))
                 TYPE=MEMORY', 'SMW::createVirtualTableWithInstances');
     $db->query('CREATE TEMPORARY TABLE smw_ob_instances_sub (category VARCHAR(255) ' . $collation . ' NOT NULL)
                 TYPE=MEMORY', 'SMW::createVirtualTableWithInstances');
     $db->query('CREATE TEMPORARY TABLE smw_ob_instances_super (category VARCHAR(255) ' . $collation . ' NOT NULL)
                 TYPE=MEMORY', 'SMW::createVirtualTableWithInstances');
     // initialize with direct instances
     foreach ($domainRangeAnnotations as $dr) {
         $dvs = $dr->getDVs();
         if ($dvs[1] == NULL || !$dvs[1]->isValid()) {
             continue;
         }
         $db->query('INSERT INTO smw_ob_instances (SELECT page_title AS instance, page_namespace AS namespace FROM ' . $page . ' ' . 'JOIN ' . $categorylinks . ' ON page_id = cl_from ' . 'WHERE page_is_redirect = 0 AND cl_to = ' . $db->addQuotes($dvs[1]->getTitle()->getDBkey()) . ' AND UPPER(' . DBHelper::convertColumn('page_title') . ') LIKE UPPER(' . $db->addQuotes('%' . $userInputToMatch . '%') . '))');
         $db->query('INSERT INTO smw_ob_instances_super VALUES (' . $db->addQuotes($dvs[1]->getTitle()->getDBkey()) . ')');
     }
     $maxDepth = SMW_MAX_CATEGORY_GRAPH_DEPTH;
     // maximum iteration length is maximum category tree depth.
     do {
         $maxDepth--;
         // get next subcategory level
         $db->query('INSERT INTO smw_ob_instances_sub (SELECT DISTINCT page_title AS category FROM ' . $categorylinks . ' JOIN ' . $page . ' ON page_id = cl_from WHERE page_namespace = ' . NS_CATEGORY . ' AND cl_to IN (SELECT * FROM smw_ob_instances_super))');
         // insert direct instances of current subcategory level
         $db->query('INSERT INTO smw_ob_instances (SELECT page_title AS instance, page_namespace AS namespace  FROM ' . $page . ' ' . 'JOIN ' . $categorylinks . ' ON page_id = cl_from ' . 'WHERE page_is_redirect = 0 AND cl_to IN (SELECT * FROM smw_ob_instances_sub) AND UPPER(' . DBHelper::convertColumn('page_title') . ') LIKE UPPER(' . $db->addQuotes('%' . $userInputToMatch . '%') . '))');
         // copy subcatgegories to supercategories of next iteration
         $db->query('DELETE FROM smw_ob_instances_super');
         $db->query('INSERT INTO smw_ob_instances_super (SELECT * FROM smw_ob_instances_sub)');
         // check if there was least one more subcategory. If not, all instances were found.
         $res = $db->query('SELECT COUNT(category) AS numOfSubCats FROM smw_ob_instances_super');
         $numOfSubCats = $db->fetchObject($res)->numOfSubCats;
         $db->freeResult($res);
         $db->query('DELETE FROM smw_ob_instances_sub');
     } while ($numOfSubCats > 0 && $maxDepth > 0);
     $db->query('DROP TEMPORARY TABLE smw_ob_instances_super');
     $db->query('DROP TEMPORARY TABLE smw_ob_instances_sub');
     $res = $db->query('SELECT DISTINCT instance, namespace FROM smw_ob_instances ORDER BY instance LIMIT ' . SMW_AC_MAX_RESULTS);
     $results = array();
     if ($db->numRows($res) > 0) {
         $row = $db->fetchObject($res);
         while ($row) {
             if (smwf_om_userCan($row->instance, 'read', $row->namespace) == 'true') {
                 $instance = Title::newFromText($row->instance, $row->namespace);
                 $results[] = $instance;
             }
             $row = $db->fetchObject($res);
         }
     }
     $db->freeResult($res);
     // drop virtual tables
     $db->query('DROP TEMPORARY TABLE smw_ob_instances');
     return $results;
 }
 public function getInstancesUsingProperty($p_array)
 {
     // param0 : property
     // param1 : limit
     // param2 : partitionNum
     $reqfilter = new SMWRequestOptions();
     $reqfilter->sort = true;
     $reqfilter->limit = intval($p_array[1]);
     $partitionNum = isset($p_array[2]) ? intval($p_array[2]) : 0;
     $reqfilter->offset = $partitionNum * $reqfilter->limit;
     $prop = Title::newFromText($p_array[0], SMW_NS_PROPERTY);
     if (smwf_om_userCan($p_array[0], 'propertyread', SMW_NS_PROPERTY) === "true") {
         $attinstances = smwfGetStore()->getAllPropertySubjects(SMWPropertyValue::makeUserProperty($prop->getDBkey()), $reqfilter);
     } else {
         $attinstances = array();
     }
     //FIXME: create this data structure in the SemanticStore interface
     $instanceWithMetadata = array();
     foreach ($attinstances as $i) {
         if (is_array($i)) {
             $instanceWithMetadata[] = array(array($i, NULL), NULL);
         } else {
             $instanceWithMetadata[] = array(array($i, NULL), NULL);
         }
     }
     $propertyName_xml = str_replace(array('"'), array('"'), $prop->getDBkey());
     return SMWOntologyBrowserXMLGenerator::encapsulateAsInstancePartition($instanceWithMetadata, $reqfilter->limit, $partitionNum, 'getInstancesUsingProperty,' . $propertyName_xml);
 }
 protected function getNarySubjects(Title $object, $pos)
 {
     $db =& wfGetDB(DB_SLAVE);
     $smw_rels2 = $db->tableName('smw_rels2');
     $smw_ids = $db->tableName('smw_ids');
     $domainAndRange = $db->selectRow($db->tableName('smw_ids'), array('smw_id'), array('smw_title' => $this->domainRangeHintRelation->getDBkey()));
     if ($domainAndRange == NULL) {
         $domainAndRangeID = -1;
         // does never exist
     } else {
         $domainAndRangeID = $domainAndRange->smw_id;
     }
     $results = array();
     $pos = "_" . ($pos + 1);
     // FIX: SMW update 1.5
     $res = $db->query('SELECT q.smw_title AS subject_title, q.smw_namespace AS subject_namespace FROM ' . $smw_ids . ' q JOIN ' . $smw_rels2 . ' n ON q.smw_id = n.s_id JOIN ' . $smw_rels2 . ' m ON n.o_id = m.s_id JOIN ' . $smw_ids . ' r ON m.o_id = r.smw_id JOIN ' . $smw_ids . ' s ON m.p_id = s.smw_id' . ' WHERE n.p_id = ' . $domainAndRangeID . ' AND s.smw_sortkey = "' . mysql_real_escape_string($pos) . '" AND r.smw_title = ' . $db->addQuotes($object->getDBkey()) . ' AND r.smw_namespace = ' . NS_CATEGORY);
     if ($db->numRows($res) > 0) {
         while ($row = $db->fetchObject($res)) {
             if (smwf_om_userCan($row->subject_title, 'read', $row->subject_namespace) === "true") {
                 $results[] = Title::newFromText($row->subject_title, $row->subject_namespace);
             }
         }
     }
     $db->freeResult($res);
     return $results;
 }
Esempio n. 4
0
 /**
  * Executes a series of auto-completion commands and stops when it
  * has a found at least one result. Except when it matches local values.
  *
  * @param string $command
  * @param substring $userInput
  * @return array of Title, array of (Title, inferred) or array of (Title, inferred, extraContent)
  */
 public static function executeCommand($command, $userInput)
 {
     $parsedCommands = self::parseCommand($command);
     $acStore = smwfGetAutoCompletionStore();
     $result = array();
     foreach ($parsedCommands as $c) {
         list($commandText, $params) = $c;
         if ($commandText == 'values') {
             foreach ($params as $p) {
                 if (stripos($p, $userInput) !== false) {
                     $result[] = $p;
                 }
             }
             // continue to fill in results if possible
         } else {
             if ($commandText == 'fixvalues') {
                 foreach ($params as $p) {
                     $result[] = $p;
                 }
                 // continue to fill in results if possible
             } else {
                 if ($commandText == 'schema-property-domain') {
                     if (empty($params[0]) || is_null($params[0])) {
                         continue;
                     }
                     if (smwf_om_userCan($params[0], 'read') == 'true') {
                         $category = Title::newFromText($params[0]);
                         if (!is_null($category)) {
                             $result = self::mergeResults($result, $acStore->getPropertyForCategory($userInput, $category));
                         }
                     }
                     if (count($result) >= SMW_AC_MAX_RESULTS) {
                         break;
                     }
                 } else {
                     if ($commandText == 'schema-property-range-instance') {
                         if (empty($params[0]) || is_null($params[0])) {
                             continue;
                         }
                         if (smwf_om_userCan($params[0], 'read') == 'true') {
                             $instance = Title::newFromText($params[0]);
                             if (!is_null($instance)) {
                                 $result = self::mergeResults($result, $acStore->getPropertyForInstance($userInput, $instance, false));
                             }
                         }
                         if (count($result) >= SMW_AC_MAX_RESULTS) {
                             break;
                         }
                     } else {
                         if ($commandText == 'annotation-property') {
                             if (empty($params[0]) || is_null($params[0])) {
                                 continue;
                             }
                             if (smwf_om_userCan($params[0], 'read') == 'true') {
                                 $category = Title::newFromText($params[0]);
                                 if (!is_null($category)) {
                                     $result = self::mergeResults($result, $acStore->getPropertyForAnnotation($userInput, $category, false));
                                 }
                             }
                             if (count($result) >= SMW_AC_MAX_RESULTS) {
                                 break;
                             }
                         } else {
                             if ($commandText == 'annotation-value') {
                                 if (empty($params[0]) || is_null($params[0])) {
                                     continue;
                                 }
                                 if (smwf_om_userCan($params[0], 'read') == 'true') {
                                     $property = Title::newFromText($params[0]);
                                     if (!is_null($property)) {
                                         $result = self::mergeResults($result, $acStore->getValueForAnnotation($userInput, $property));
                                     }
                                 }
                                 if (count($result) >= SMW_AC_MAX_RESULTS) {
                                     break;
                                 }
                             } else {
                                 if ($commandText == 'instance-property-range') {
                                     if (empty($params[0]) || is_null($params[0])) {
                                         continue;
                                     }
                                     if (smwf_om_userCan($params[0], 'read') == 'true') {
                                         $property = Title::newFromText($params[0]);
                                         if (!is_null($property)) {
                                             $domainRangeAnnotations = smwfGetStore()->getPropertyValues($property, smwfGetSemanticStore()->domainRangeHintProp);
                                             $result = self::mergeResults($result, $acStore->getInstanceAsTarget($userInput, $domainRangeAnnotations));
                                         }
                                     }
                                     if (count($result) >= SMW_AC_MAX_RESULTS) {
                                         break;
                                     }
                                 } else {
                                     if ($commandText == 'namespace') {
                                         $result = self::mergeResults($result, smwfGetAutoCompletionStore()->getPages($userInput, $params));
                                         if (count($result) >= SMW_AC_MAX_RESULTS) {
                                             break;
                                         }
                                     } else {
                                         if ($commandText == 'lexical') {
                                             $result = self::mergeResults($result, smwfGetAutoCompletionStore()->getPages($userInput));
                                             if (count($result) >= SMW_AC_MAX_RESULTS) {
                                                 break;
                                             }
                                         } else {
                                             if ($commandText == 'schema-property-type') {
                                                 if (empty($params[0]) || is_null($params[0])) {
                                                     continue;
                                                 }
                                                 $datatype = $params[0];
                                                 $result = self::mergeResults($result, smwfGetAutoCompletionStore()->getPropertyWithType($userInput, $datatype));
                                                 if (count($result) >= SMW_AC_MAX_RESULTS) {
                                                     break;
                                                 }
                                                 global $smwgContLang;
                                                 $dtl = $smwgContLang->getDatatypeLabels();
                                                 $result = self::mergeResults($result, smwfGetAutoCompletionStore()->getPropertyWithType($userInput, $dtl['_str']));
                                                 if (count($result) >= SMW_AC_MAX_RESULTS) {
                                                     break;
                                                 }
                                             } else {
                                                 if ($commandText == 'ask') {
                                                     if (empty($params[0]) || is_null($params[0])) {
                                                         continue;
                                                     }
                                                     $query = $params[0];
                                                     if (!isset($params[1]) || $params[1] == 'main') {
                                                         $column = "_var0";
                                                     } else {
                                                         $column = strtoupper(substr($params[1], 0, 1)) . substr($params[1], 1);
                                                         $column = str_replace(" ", "_", $column);
                                                     }
                                                     $xmlResult = self::runASKQuery($query, $column, $userInput);
                                                     $dom = simplexml_load_string($xmlResult);
                                                     $queryResults = $dom->xpath('//binding[@name="' . $column . '"]');
                                                     // make titles but eliminate duplicates before
                                                     $textTitles = array();
                                                     foreach ($queryResults as $r) {
                                                         if (empty($userInput) || stripos((string) $r[0], $userInput) !== false) {
                                                             $textTitles[] = (string) $r[0];
                                                             if (count($textTitles) >= SMW_AC_MAX_RESULTS) {
                                                                 break;
                                                             }
                                                         }
                                                     }
                                                     $textTitles = array_unique($textTitles);
                                                     $titles = array();
                                                     foreach ($textTitles as $r) {
                                                         if (smwf_om_userCan($r, 'read') == 'true') {
                                                             $titles[] = Title::newFromText($r);
                                                         }
                                                     }
                                                     self::mergeResults($result, $titles);
                                                     if (count($result) >= SMW_AC_MAX_RESULTS) {
                                                         break;
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return $result;
 }
Esempio n. 5
0
 public function getRedirectPages(Title $title)
 {
     $db =& wfGetDB(DB_SLAVE);
     $page = $db->tableName('page');
     $redirect = $db->tableName('redirect');
     $res = $db->query('SELECT page_title, page_namespace FROM ' . $page . ', ' . $redirect . ' WHERE ' . $db->addQuotes($title->getDBkey()) . ' = rd_title AND ' . $title->getNamespace() . ' = rd_namespace AND page_id = rd_from');
     $result = array();
     if ($db->numRows($res) > 0) {
         while ($row = $db->fetchObject($res)) {
             if (smwf_om_userCan($row->page_title, 'read', $row->page_namespace) === "true") {
                 $result[] = Title::newFromText($row->page_title, $row->page_namespace);
             }
         }
     }
     $db->freeResult($res);
     return $result;
 }
/**
 * Moves a property to a new super property.
 * 
 * @param $draggedProperty Title of property to move (String)
 * @param $oldSuperProperty Title of old superproperty. (String) May be NULL
 * @param $newSuperProperty Title of new superproperty. (String) May be NULL
 */
function smwf_om_MoveProperty($draggedProperty, $oldSuperProperty, $newSuperProperty)
{
    $newSuperProperty = strip_tags($newSuperProperty);
    if ($newSuperProperty == '') {
        return "false";
    }
    if (smwf_om_userCan($draggedProperty, 'move') === "false") {
        return "false";
    }
    $draggedOnRootLevel = $oldSuperProperty == 'null' || $oldSuperProperty == NULL;
    $draggedPropertyTitle = Title::newFromText($draggedProperty, SMW_NS_PROPERTY);
    $oldSuperPropertyTitle = Title::newFromText($oldSuperProperty, SMW_NS_PROPERTY);
    $newSuperPropertyTitle = Title::newFromText($newSuperProperty, SMW_NS_PROPERTY);
    if ($draggedPropertyTitle == NULL || $newSuperPropertyTitle == NULL) {
        // invalid titles
        return "false";
    }
    $draggedPropertyRevision = Revision::newFromTitle($draggedPropertyTitle);
    $draggedPropertyArticle = new Article($draggedPropertyTitle);
    if ($draggedPropertyRevision == NULL || $draggedPropertyArticle == NULL) {
        // some problem occured.
        return "false";
    }
    $text = $draggedPropertyRevision->getText();
    global $smwgContLang, $wgParser;
    $options = new ParserOptions();
    $sp = $smwgContLang->getPropertyLabels();
    if ($newSuperProperty == NULL || $newSuperProperty == 'null') {
        $newText = preg_replace("/\\[\\[\\s*" . $sp["_SUBP"] . "\\s*:[:|=]\\s*" . preg_quote($oldSuperPropertyTitle->getPrefixedText()) . "\\s*\\]\\]/i", "", $text);
    } else {
        if ($draggedOnRootLevel) {
            // dragged property was on root level
            $newText = $text . "\n[[" . $sp["_SUBP"] . "::" . $newSuperPropertyTitle->getPrefixedText() . "]]";
        } else {
            // replace on article $draggedProperty [[Subproperty of::$oldSuperProperty]] with [[Subproperty of::$newSuperProperty]]
            $newText = preg_replace("/\\[\\[\\s*" . $sp["_SUBP"] . "\\s*:[:|=]\\s*" . preg_quote($oldSuperPropertyTitle->getPrefixedText()) . "\\s*\\]\\]/i", "[[" . $sp["_SUBP"] . "::" . $newSuperPropertyTitle->getPrefixedText() . "]]", $text);
        }
    }
    // save article
    $draggedPropertyArticle->doEdit($newText, $draggedPropertyRevision->getComment(), EDIT_UPDATE);
    $wgParser->parse($newText, $draggedPropertyTitle, $options, true, true, $draggedPropertyRevision->getID());
    //SMWFactbox::storeData(true);
    return "true";
}