Exemplo n.º 1
0
    /**
     * Creates 'smw_ob_properties' and fills it with all properties (including inherited)
     * of a category.
     *
     * @param Title $category
     * @param & $db
     */
    protected function createVirtualTableWithPropertiesByCategory(Title $categoryTitle, &$db)
    {
        global $smwgDefaultCollation;
        $page = $db->tableName('page');
        $categorylinks = $db->tableName('categorylinks');
        $smw_nary = $db->tableName('smw_nary');
        $smw_nary_relations = $db->tableName('smw_nary_relations');
        if (!isset($smwgDefaultCollation)) {
            $collation = '';
        } else {
            $collation = 'COLLATE ' . $smwgDefaultCollation;
        }
        // create virtual tables
        $db->query('CREATE TEMPORARY TABLE smw_ob_properties (id INT(8) NOT NULL, property VARCHAR(255) ' . $collation . ')
		            TYPE=MEMORY', 'SMW::createVirtualTableWithPropertiesByCategory');
        $db->query('CREATE TEMPORARY TABLE smw_ob_properties_sub (category INT(8) NOT NULL)
		            TYPE=MEMORY', 'SMW::createVirtualTableWithPropertiesByCategory');
        $db->query('CREATE TEMPORARY TABLE smw_ob_properties_super (category INT(8) NOT NULL)
		            TYPE=MEMORY', 'SMW::createVirtualTableWithPropertiesByCategory');
        $db->query('INSERT INTO smw_ob_properties (SELECT n.subject_id AS id, n.subject_title AS property FROM ' . $smw_nary . ' n JOIN ' . $smw_nary_relations . ' r ON n.subject_id = r.subject_id JOIN ' . $page . ' p ON n.subject_id = p.page_id ' . ' WHERE r.nary_pos = 0 AND n.attribute_title = ' . $db->addQuotes($this->domainRangeHintRelation->getDBkey()) . ' AND r.object_title = ' . $db->addQuotes($categoryTitle->getDBkey()) . ' AND p.page_is_redirect = 0)');
        $db->query('INSERT INTO smw_ob_properties_sub VALUES (' . $db->addQuotes($categoryTitle->getArticleID()) . ')');
        $maxDepth = SMW_MAX_CATEGORY_GRAPH_DEPTH;
        // maximum iteration length is maximum category tree depth.
        do {
            $maxDepth--;
            // get next supercategory level
            $db->query('INSERT INTO smw_ob_properties_super (SELECT DISTINCT page_id AS category FROM ' . $categorylinks . ' JOIN ' . $page . ' ON page_title = cl_to WHERE page_namespace = ' . NS_CATEGORY . ' AND cl_from IN (SELECT * FROM smw_ob_properties_sub))');
            // insert direct properties of current supercategory level
            $db->query('INSERT INTO smw_ob_properties (SELECT n.subject_id AS id, n.subject_title AS property FROM ' . $smw_nary . ' n JOIN ' . $smw_nary_relations . ' r ON n.subject_id = r.subject_id JOIN ' . $page . ' p ON n.subject_id = p.page_id ' . ' WHERE r.nary_pos = 0 AND n.attribute_title = ' . $db->addQuotes($this->domainRangeHintRelation->getDBkey()) . ' AND p.page_is_redirect = 0 AND r.object_id IN (SELECT * FROM smw_ob_properties_super))');
            // copy supercatgegories to subcategories of next iteration
            $db->query('DELETE FROM smw_ob_properties_sub');
            $db->query('INSERT INTO smw_ob_properties_sub (SELECT * FROM smw_ob_properties_super)');
            // check if there was least one more supercategory. If not, all properties were found.
            $res = $db->query('SELECT COUNT(category) AS numOfSuperCats FROM smw_ob_properties_sub');
            $numOfSuperCats = $db->fetchObject($res)->numOfSuperCats;
            $db->freeResult($res);
            $db->query('DELETE FROM smw_ob_properties_super');
        } while ($numOfSuperCats > 0 && $maxDepth > 0);
        $db->query('DROP TEMPORARY TABLE smw_ob_properties_super');
        $db->query('DROP TEMPORARY TABLE smw_ob_properties_sub');
    }