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 getHelppages($namespace, $action) { $helppages = array(); $db =& wfGetDB(DB_SLAVE); $namespace = mysql_real_escape_string($namespace); $action = mysql_real_escape_string($action); $discourseState1 = $namespace . ":" . $action; $discourseState2 = "ALL:" . $action; $discourseState3 = $namespace . ":ALL"; $discourseState4 = "ALL:ALL"; $smw_ids = $db->tableName('smw_ids'); $smw_atts2 = $db->tableName('smw_atts2'); $page = $db->tableName('page'); $discourseStateID = $db->selectRow($smw_ids, array('smw_id'), array('smw_title' => 'DiscourseState', 'smw_namespace' => SMW_NS_PROPERTY)); if ($discourseStateID != null) { $discourseStateID = $discourseStateID->smw_id; } else { return array(); } $res = $db->query('SELECT page_id FROM ' . $smw_atts2 . ' JOIN ' . $smw_ids . ' ON smw_id = s_id ' . ' JOIN ' . $page . ' ON page_title = smw_title AND page_namespace = smw_namespace ' . ' WHERE p_id = ' . $discourseStateID . ' AND ' . '(UPPER(' . DBHelper::convertColumn('value_xsd') . ') = UPPER(' . $db->addQuotes($discourseState1) . ') ' . 'OR UPPER(' . DBHelper::convertColumn('value_xsd') . ') = UPPER(' . $db->addQuotes($discourseState2) . ') ' . 'OR UPPER(' . DBHelper::convertColumn('value_xsd') . ') = UPPER(' . $db->addQuotes($discourseState3) . ') ' . 'OR UPPER(' . DBHelper::convertColumn('value_xsd') . ') = UPPER(' . $db->addQuotes($discourseState4) . ')) ' . 'AND page_namespace = ' . NS_HELP . ' ORDER BY RAND() LIMIT 5'); while ($row = $db->fetchObject($res)) { $helppages[] = $row->page_id; } $db->freeResult($res); return $helppages; }