function linkSuggestFormatResults($db, $res, $query, &$redirects, &$results, &$exactMatchRow) { global $wgLinkSuggestLimit; while (($row = $db->fetchObject($res)) && count($results) < $wgLinkSuggestLimit) { if (strtolower($row->page_title) == $query) { $exactMatchRow = $row; continue; } $titleFormatted = wfLinkSuggestFormatTitle($row->page_namespace, $row->page_title); if ($row->page_is_redirect == 0) { if (!in_array($titleFormatted, $results)) { $results[] = $titleFormatted; } $flippedRedirs = array_flip($redirects); if (isset($flippedRedirs[$titleFormatted])) { unset($redirects[$flippedRedirs[$titleFormatted]]); } } else { $redirTitleFormatted = wfLinkSuggestFormatTitle($row->page_namespace, $row->rd_title); if (!in_array($redirTitleFormatted, $results)) { $results[] = $redirTitleFormatted; $redirects[$redirTitleFormatted] = $titleFormatted; } } } }
/** * AJAX callback function * * @return $ar Array of link suggestions */ function getLinkSuggest() { global $wgRequest, $wgContLang, $wgContentNamespaces; // trim passed query and replace spaces by underscores // - this is how MediaWiki stores article titles in database $query = urldecode(trim($wgRequest->getText('query'))); $query = str_replace(' ', '_', $query); // explode passed query by ':' to get namespace and article title $queryParts = explode(':', $query, 2); if (count($queryParts) == 2) { $query = $queryParts[1]; $namespaceName = $queryParts[0]; // try to get the index by canonical name first $namespace = MWNamespace::getCanonicalIndex(strtolower($namespaceName)); if ($namespace == null) { // if we failed, try looking through localized namespace names $namespace = array_search(ucfirst($namespaceName), $wgContLang->getNamespaces()); if (empty($namespace)) { // getting here means our "namespace" is not real and can only // be a part of the title $query = $namespaceName . ':' . $query; } } } // list of namespaces to search in if (empty($namespace)) { // search only within content namespaces - default behaviour $namespaces = $wgContentNamespaces; } else { // search only within a namespace from query $namespaces = $namespace; } $results = array(); $dbr = wfGetDB(DB_SLAVE); $query = $dbr->strencode(mb_strtolower($query)); $res = $dbr->select(array('querycache', 'page'), array('qc_namespace', 'qc_title'), array('qc_title = page_title', 'qc_namespace = page_namespace', 'page_is_redirect = 0', 'qc_type' => 'Mostlinked', "LOWER(qc_title) LIKE '{$query}%'", 'qc_namespace' => $namespaces), __METHOD__, array('ORDER BY' => 'qc_value DESC', 'LIMIT' => 10)); foreach ($res as $row) { $results[] = wfLinkSuggestFormatTitle($row->qc_namespace, $row->qc_title); } $res = $dbr->select('page', array('page_namespace', 'page_title'), array("LOWER(page_title) LIKE '{$query}%'", 'page_is_redirect' => 0, 'page_namespace' => $namespaces), __METHOD__, array('ORDER BY' => 'page_title ASC', 'LIMIT' => 15 - count($results))); foreach ($res as $row) { $results[] = wfLinkSuggestFormatTitle($row->page_namespace, $row->page_title); } $results = array_unique($results); $format = $wgRequest->getText('format'); if ($format == 'json') { $out = json_encode(array('query' => $wgRequest->getText('query'), 'suggestions' => array_values($results))); } else { $out = implode("\n", $results); } $ar = new AjaxResponse($out); $ar->setCacheDuration(60 * 60); // cache results for one hour // set proper content type to ease development if ($format == 'json') { $ar->setContentType('application/json; charset=utf-8'); } else { $ar->setContentType('text/plain; charset=utf-8'); } return $ar; }