/** * Add an array of categories, with names in the keys */ public function addCategoryLinks($categories) { global $wgUser, $wgContLang; if (!is_array($categories) || count($categories) == 0) { return; } # Add the links to a LinkBatch $arr = array(NS_CATEGORY => $categories); $lb = new LinkBatch(); $lb->setArray($arr); # Fetch existence plus the hiddencat property $dbr = wfGetDB(DB_SLAVE); $pageTable = $dbr->tableName('page'); $where = $lb->constructSet('page', $dbr); $propsTable = $dbr->tableName('page_props'); $sql = "SELECT page_id, page_namespace, page_title, page_len, page_is_redirect, pp_value\n\t\t\tFROM {$pageTable} LEFT JOIN {$propsTable} ON pp_propname='hiddencat' AND pp_page=page_id WHERE {$where}"; $res = $dbr->query($sql, __METHOD__); # Add the results to the link cache $lb->addResultToCache(LinkCache::singleton(), $res); # Set all the values to 'normal'. This can be done with array_fill_keys in PHP 5.2.0+ $categories = array_combine(array_keys($categories), array_fill(0, count($categories), 'normal')); # Mark hidden categories foreach ($res as $row) { if (isset($row->pp_value)) { $categories[$row->page_title] = 'hidden'; } } # Add the remaining categories to the skin if (wfRunHooks('OutputPageMakeCategoryLinks', array(&$this, $categories, &$this->mCategoryLinks))) { $sk = $wgUser->getSkin(); foreach ($categories as $category => $type) { $title = Title::makeTitleSafe(NS_CATEGORY, $category); $text = $wgContLang->convertHtml($title->getText()); $this->mCategoryLinks[$type][] = $sk->makeLinkObj($title, $text); } } }
/** * Add an array of categories, with names in the keys * * @param array $categories Mapping category name => sort key */ public function addCategoryLinks(array $categories) { global $wgContLang; if (!is_array($categories) || count($categories) == 0) { return; } # Add the links to a LinkBatch $arr = array(NS_CATEGORY => $categories); $lb = new LinkBatch(); $lb->setArray($arr); # Fetch existence plus the hiddencat property $dbr = wfGetDB(DB_SLAVE); $fields = array('page_id', 'page_namespace', 'page_title', 'page_len', 'page_is_redirect', 'page_latest', 'pp_value'); if ($this->getConfig()->get('ContentHandlerUseDB')) { $fields[] = 'page_content_model'; } $res = $dbr->select(array('page', 'page_props'), $fields, $lb->constructSet('page', $dbr), __METHOD__, array(), array('page_props' => array('LEFT JOIN', array('pp_propname' => 'hiddencat', 'pp_page = page_id')))); # Add the results to the link cache $lb->addResultToCache(LinkCache::singleton(), $res); # Set all the values to 'normal'. $categories = array_fill_keys(array_keys($categories), 'normal'); # Mark hidden categories foreach ($res as $row) { if (isset($row->pp_value)) { $categories[$row->page_title] = 'hidden'; } } # Add the remaining categories to the skin if (Hooks::run('OutputPageMakeCategoryLinks', array(&$this, $categories, &$this->mCategoryLinks))) { foreach ($categories as $category => $type) { $origcategory = $category; $title = Title::makeTitleSafe(NS_CATEGORY, $category); if (!$title) { continue; } $wgContLang->findVariantLink($category, $title, true); if ($category != $origcategory && array_key_exists($category, $categories)) { continue; } $text = $wgContLang->convertHtml($title->getText()); $this->mCategories[] = $title->getText(); $this->mCategoryLinks[$type][] = Linker::link($title, $text); } } }
/** * Add an array of categories, with names in the keys * * @param array $categories Mapping category name => sort key */ public function addCategoryLinks(array $categories) { global $wgContLang; if (!is_array($categories) || count($categories) == 0) { return; } # Add the links to a LinkBatch $arr = [NS_CATEGORY => $categories]; $lb = new LinkBatch(); $lb->setArray($arr); # Fetch existence plus the hiddencat property $dbr = wfGetDB(DB_REPLICA); $fields = array_merge(LinkCache::getSelectFields(), ['page_namespace', 'page_title', 'pp_value']); $res = $dbr->select(['page', 'page_props'], $fields, $lb->constructSet('page', $dbr), __METHOD__, [], ['page_props' => ['LEFT JOIN', ['pp_propname' => 'hiddencat', 'pp_page = page_id']]]); # Add the results to the link cache $lb->addResultToCache(LinkCache::singleton(), $res); # Set all the values to 'normal'. $categories = array_fill_keys(array_keys($categories), 'normal'); # Mark hidden categories foreach ($res as $row) { if (isset($row->pp_value)) { $categories[$row->page_title] = 'hidden'; } } # Add the remaining categories to the skin if (Hooks::run('OutputPageMakeCategoryLinks', [&$this, $categories, &$this->mCategoryLinks])) { foreach ($categories as $category => $type) { // array keys will cast numeric category names to ints, so cast back to string $category = (string) $category; $origcategory = $category; $title = Title::makeTitleSafe(NS_CATEGORY, $category); if (!$title) { continue; } $wgContLang->findVariantLink($category, $title, true); if ($category != $origcategory && array_key_exists($category, $categories)) { continue; } $text = $wgContLang->convertHtml($title->getText()); $this->mCategories[] = $title->getText(); $this->mCategoryLinks[$type][] = Linker::link($title, $text); } } }