/** * Computes rules for attribute of id <code>$attributeId</code> * * @param int $attributeId Attribute * @return void * @throws SQLException */ public static function computeRules($attributeId) { global $wgFreqPatternTagCloudMinSupport, $wgFreqPatternTagCloudMinConfidence; // Configuration if (isset($wgFreqPatternTagCloudMinSupport)) { FrequentPattern::$min_support = $wgFreqPatternTagCloudMinSupport; } if (isset($wgFreqPatternTagCloudMinConfidence)) { FrequentPattern::$min_confidence = $wgFreqPatternTagCloudMinConfidence; } $dbr =& wfGetDB(DB_SLAVE); $dbw =& wfGetDB(DB_MASTER); // Compile items = all possible o_ids if (!$attributeId) { $res = $dbr->query("SELECT GROUP_CONCAT(smw_id)\n\t\t\t\t\t\tFROM " . $dbr->tableName("smw_ids") . "\n\t\t\t\t\t\tWHERE smw_namespace = 14\n\t\t\t\t\t\tAND LENGTH(smw_iw) = 0\n\t\t\t\t\t\tGROUP BY smw_namespace"); } else { $res = $dbr->query("SELECT GROUP_CONCAT(DISTINCT o_id)\n\t\t\t\t\t\tFROM " . $dbr->tableName("smw_rels2") . "\n\t\t\t\t\t\tWHERE p_id = " . mysql_real_escape_string($attributeId) . "\n\t\t\t\t\t\tGROUP BY p_id"); } $row = $res->fetchRow(); $items = explode(",", $row[0]); $res->free(); // Compile transactions = all corelated o_ids (by s_id) if (!$attributeId) { $res = $dbr->query("SELECT GROUP_CONCAT(smw_id)\n\t\t\t\t\t\tFROM " . $dbr->tableName("smw_ids") . " ids, " . $dbr->tableName("categorylinks") . " catlinks\n\t\t\t\t\t\tWHERE ids.smw_title = catlinks.cl_to\n\t\t\t\t\t\tAND ids.smw_namespace = 14\n\t\t\t\t\t\tGROUP BY catlinks.cl_from"); } else { $res = $dbr->query("SELECT GROUP_CONCAT(o_id)\n\t\t\t\t\t\tFROM " . $dbr->tableName("smw_rels2") . "\n\t\t\t\t\t\tWHERE p_id = " . mysql_real_escape_string($attributeId) . "\n\t\t\t\t\t\tGROUP BY s_id"); } $transactions = array(); while ($row = $res->fetchRow()) { $transactions[] = explode(",", $row[0]); } $res->free(); // Run algorithm $algorithm = new FrequentPatternApriori(); $rules = $algorithm->computeRules($items, $transactions, self::$min_support, self::$min_confidence); foreach ($rules as $rule) { // Push rules to db $dbw->query("INSERT INTO " . $dbw->tableName("fptc_associationrules") . " (p_id, rule_support, rule_confidence)\n\t\t\t\t\t\tVALUES (" . mysql_real_escape_string($attributeId) . ", " . mysql_real_escape_string($rule->getSupport()) . ", " . mysql_real_escape_string($rule->getConfidence()) . ")"); $ruleId = $dbw->insertId(); foreach ($rule->getAssumption() as $item) { $dbw->query("INSERT INTO " . $dbw->tableName("fptc_items") . " (o_id, rule_id, item_order)\n\t\t\t\t\t\t\tVALUES (" . mysql_real_escape_string($item) . ", " . mysql_real_escape_string($ruleId) . ", 0)"); } foreach ($rule->getConclusion() as $item) { $dbw->query("INSERT INTO " . $dbw->tableName("fptc_items") . " (o_id, rule_id, item_order)\n\t\t\t\t\t\t\tVALUES (" . mysql_real_escape_string($item) . ", " . mysql_real_escape_string($ruleId) . ", 1)"); } } $dbw->commit(); }
/** * Executes special page (will be called when accessing special page) * * @param $par Mixed: parameter passed to the special page or null */ public function execute($par) { global $wgOut, $wgUser; $this->setHeaders(); if (!$wgUser->isAllowed('protect')) { // No admin $wgOut->addWikiMsg('fptc-insufficient-rights-for-maintenance'); } else { // Refresh frequent pattern rules include_once "includes/FrequentPattern.php"; FrequentPattern::deleteAllRules(); FrequentPattern::computeAllRules(); // Notify user $wgOut->addWikiMsg('fptc-refreshed-frequent-patterns'); } }
/** * Gets suggestions * * @param $attribute String: attribute * @param $value String: chosen value * @return string */ public static function getSuggestions($attribute, $value) { // Get similar tags, sorted by priority $tags = FrequentPattern::getConclusions($attribute, $value); if (!count($tags)) { return '<li class="no_entries">-</li>'; } else { $suggestions = array(); foreach ($tags as $number => $tag) { $suggestions[] = sprintf('<li class="similar_tag"><a href="#browse_similar_tag" title="%2$s">%1$d. %2$s</a></li>', $number + 1, $tag); } return implode("\n", $suggestions); } }