/** * @param EntityId $id * @param array[] $clusteredTerms * @param Suggestion $suggestion * @return array */ private function buildEntry(EntityId $id, array $clusteredTerms, Suggestion $suggestion) { $entry = array(); $entry['id'] = $id->getSerialization(); $entry['url'] = $this->entityTitleLookup->getTitleForId($id)->getFullUrl(); $entry['rating'] = $suggestion->getProbability(); /** @var TermIndexEntry[] $matchingTerms */ if (isset($clusteredTerms[$id->getSerialization()])) { $matchingTerms = $clusteredTerms[$id->getSerialization()]; } else { $matchingTerms = array(); } foreach ($matchingTerms as $term) { switch ($term->getType()) { case TermIndexEntry::TYPE_LABEL: $entry['label'] = $term->getText(); break; case TermIndexEntry::TYPE_DESCRIPTION: $entry['description'] = $term->getText(); break; case TermIndexEntry::TYPE_ALIAS: $this->checkAndSetAlias($entry, $term); break; } } if (!isset($entry['label'])) { $entry['label'] = $id->getSerialization(); } elseif (preg_match($this->searchPattern, $entry['label'])) { // No aliases needed in the output when the label already is a successful match. unset($entry['aliases']); } return $entry; }
/** * @see EntityIdFormatter::formatEntityId * * @param EntityId $entityId * * @return string HTML */ public function formatEntityId(EntityId $entityId) { $title = $this->entityTitleLookup->getTitleForId($entityId); $term = $this->lookupEntityLabel($entityId); if ($term) { return $this->getHtmlForTerm($title->getLocalURL(), $term, $title->getPrefixedText()); } elseif (!$title->exists()) { return $this->getHtmlForNonExistent($entityId); } $attributes = array('title' => $title->getPrefixedText(), 'href' => $title->getLocalURL()); $html = Html::element('a', $attributes, $entityId->getSerialization()); return $html; }
/** * @see EntityRedirectLookup::getRedirectForEntityId * * @since 0.5 * * @param EntityId $entityId * @param string $forUpdate * * @return EntityId|null The ID of the redirect target, or null if $entityId * does not refer to a redirect * @throws EntityRedirectLookupException */ public function getRedirectForEntityId(EntityId $entityId, $forUpdate = '') { try { $title = $this->entityTitleLookup->getTitleForId($entityId); } catch (\Exception $ex) { // TODO: catch more specific type of exception once EntityTitleLookup contract is clarified throw new EntityRedirectLookupException($entityId, null, $ex); } $forUpdate = $forUpdate === 'for update' ? DB_MASTER : DB_SLAVE; try { $db = $this->loadBalancer->getConnection($forUpdate); } catch (MWException $ex) { throw new EntityRedirectLookupException($entityId, null, $ex); } $row = $db->selectRow(array('page', 'redirect'), array('page_id', 'rd_namespace', 'rd_title'), array('page_title' => $title->getDBkey(), 'page_namespace' => $title->getNamespace()), __METHOD__, array(), array('redirect' => array('LEFT JOIN', 'rd_from=page_id'))); try { $this->loadBalancer->reuseConnection($db); } catch (MWException $ex) { throw new EntityRedirectLookupException($entityId, null, $ex); } if (!$row) { throw new EntityRedirectLookupException($entityId); } if ($row->rd_namespace === null) { return null; } $title = Title::makeTitle($row->rd_namespace, $row->rd_title); return $this->entityIdLookup->getEntityIdForTitle($title); }
/** * Get serialized entity for the EntityRevision and add it to the result * * @param string|null $sourceEntityIdSerialization EntityId used to retreive $entityRevision * Used as the key for the entity in the 'entities' structure and for adding redirect * info Will default to the entity's serialized ID if null. If given this must be the * entity id before any redirects were resolved. * @param EntityRevision $entityRevision * @param string[]|string $props a list of fields to include, or "all" * @param string[]|null $filterSiteIds A list of site IDs to filter by * @param string[] $filterLangCodes A list of language codes to filter by * @param LanguageFallbackChain[] $fallbackChains with keys of the origional language * * @since 0.5 */ public function addEntityRevision($sourceEntityIdSerialization, EntityRevision $entityRevision, $props = 'all', array $filterSiteIds = null, array $filterLangCodes = array(), array $fallbackChains = array()) { $entity = $entityRevision->getEntity(); $entityId = $entity->getId(); if ($sourceEntityIdSerialization === null) { $sourceEntityIdSerialization = $entityId->getSerialization(); } $record = array(); //if there are no props defined only return type and id.. if ($props === array()) { $record['id'] = $entityId->getSerialization(); $record['type'] = $entityId->getEntityType(); } else { if ($props == 'all' || in_array('info', $props)) { $title = $this->entityTitleLookup->getTitleForId($entityId); $record['pageid'] = $title->getArticleID(); $record['ns'] = $title->getNamespace(); $record['title'] = $title->getPrefixedText(); $record['lastrevid'] = $entityRevision->getRevisionId(); $record['modified'] = wfTimestamp(TS_ISO_8601, $entityRevision->getTimestamp()); } if ($sourceEntityIdSerialization !== $entityId->getSerialization()) { $record['redirects'] = array('from' => $sourceEntityIdSerialization, 'to' => $entityId->getSerialization()); } $entitySerialization = $this->getEntityArray($entity, $props, $filterSiteIds, $filterLangCodes, $fallbackChains); $record = array_merge($record, $entitySerialization); } $this->appendValue(array('entities'), $sourceEntityIdSerialization, $record, 'entity'); if ($this->addMetaData) { $this->result->addArrayType(array('entities'), 'kvp', 'id'); $this->result->addValue(array('entities'), ApiResult::META_KVP_MERGE, true, ApiResult::OVERRIDE); } }
/** * @param TermSearchResult $match * * @return array */ private function buildTermSearchMatchEntry($match) { // TODO: use EntityInfoBuilder, EntityInfoTermLookup $title = $this->titleLookup->getTitleForId($match->getEntityId()); $entry = array('id' => $match->getEntityId()->getSerialization(), 'concepturi' => $this->conceptBaseUri . $match->getEntityId()->getSerialization(), 'url' => $title->getFullUrl(), 'title' => $title->getPrefixedText(), 'pageid' => $title->getArticleID()); $displayLabel = $match->getDisplayLabel(); if (!is_null($displayLabel)) { $entry['label'] = $displayLabel->getText(); } $displayDescription = $match->getDisplayDescription(); if (!is_null($displayDescription)) { $entry['description'] = $displayDescription->getText(); } $entry['match']['type'] = $match->getMatchedTermType(); // Special handling for 'entityId's as these are not actually Term objects if ($entry['match']['type'] === 'entityId') { $entry['match']['text'] = $entry['id']; $entry['aliases'] = array($entry['id']); } else { $matchedTerm = $match->getMatchedTerm(); $matchedTermText = $matchedTerm->getText(); $entry['match']['language'] = $matchedTerm->getLanguageCode(); $entry['match']['text'] = $matchedTermText; /** * Add matched terms to the aliases key in the result to give some context * for the matched Term if the matched term is different to the alias. * XXX: This appears odd but is used in the UI / Entity suggesters */ if (!array_key_exists('label', $entry) || $matchedTermText != $entry['label']) { $entry['aliases'] = array($matchedTerm->getText()); } } return $entry; }
/** * @param ParserOutput $parserOutput * @param SiteLinkList $siteLinkList */ private function addBadgesToParserOutput(ParserOutput $parserOutput, SiteLinkList $siteLinkList) { /** @var SiteLink $siteLink */ foreach ($siteLinkList as $siteLink) { foreach ($siteLink->getBadges() as $badge) { $parserOutput->addLink($this->entityTitleLookup->getTitleForId($badge)); } } }
/** * Formats a row for display. * * @param PropertyId $propertyId * * @return string */ protected function formatRow($propertyId) { $title = $this->titleLookup->getTitleForId($propertyId); if (!$title->exists()) { return $this->entityIdFormatter->formatEntityId($propertyId); } $labelTerm = $this->labelDescriptionLookup->getLabel($propertyId); $row = Html::rawElement('a', array('title' => $title ? $title->getPrefixedText() : $propertyId->getSerialization(), 'href' => $title ? $title->getLocalURL() : ''), Html::rawElement('span', array('class' => 'wb-itemlink'), Html::element('span', array('class' => 'wb-itemlink-label', 'lang' => $labelTerm ? $labelTerm->getLanguageCode() : ''), $labelTerm ? $labelTerm->getText() : '') . ($labelTerm ? ' ' : '') . Html::element('span', array('class' => 'wb-itemlink-id'), '(' . $propertyId->getSerialization() . ')'))); return $row; }
/** * Returns the Title of the page holding the entity that is being edited. * * @return Title|null */ private function getTitle() { if ($this->title === null) { $id = $this->newEntity->getId(); if ($id !== null) { $this->title = $this->titleLookup->getTitleForId($id); } } return $this->title; }
/** * @param ApiPageSet $resultPageSet */ public function executeGenerator($resultPageSet) { $params = $this->extractRequestParams(); $searchResults = $this->getSearchResults($params); $titles = array(); foreach ($searchResults as $match) { $title = $this->titleLookup->getTitleForId($match->getEntityId()); $titles[] = $title; $resultPageSet->setGeneratorData($title, array('displaytext' => $match->getMatchedTerm()->getText())); } $resultPageSet->populateFromTitles($titles); }
/** * Gets exact match for the search term as an EntityId if it can be found. * * @param string $term * @param string $entityType * * @return EntityId|null */ private function getExactMatchForEntityId($term, $entityType) { try { $entityId = $this->idParser->parse($term); $title = $this->titleLookup->getTitleForId($entityId); if ($title && $title->exists() && $entityId->getEntityType() === $entityType) { return $entityId; } } catch (EntityIdParsingException $ex) { // never mind, doesn't look like an ID. } return null; }
/** * @see SpecialWikibasePage::execute * * @since 0.1 * * @param string|null $subPage */ public function execute($subPage) { parent::execute($subPage); // Setup $request = $this->getRequest(); $parts = $subPage === '' ? array() : explode('/', $subPage, 2); $site = trim($request->getVal('site', isset($parts[0]) ? $parts[0] : '')); $page = trim($request->getVal('page', isset($parts[1]) ? $parts[1] : '')); $itemContent = null; // If there are enough data, then try to lookup the item content if ($site !== '' && $page !== '') { // FIXME: This code is duplicated in ItemByTitleHelper::getItemId! // Try to get a item content $siteId = $this->stringNormalizer->trimToNFC($site); // no stripping of underscores here! $pageName = $this->stringNormalizer->trimToNFC($page); if (!$this->sites->getSite($siteId)) { // HACK: If the site ID isn't known, add "wiki" to it; this allows the wikipedia // subdomains to be used to refer to wikipedias, instead of requiring their // full global id to be used. // @todo: Ideally, if the site can't be looked up by global ID, we // should try to look it up by local navigation ID. // Support for this depends on bug T50934. $siteId .= 'wiki'; } /* @var ItemHandler $itemHandler */ $itemId = $this->siteLinkLookup->getItemIdForLink($siteId, $pageName); // Do we have an item content, and if not can we try harder? if ($itemId === null) { // Try harder by requesting normalization on the external site $siteObj = $this->sites->getSite($siteId); if ($siteObj instanceof Site) { $pageName = $siteObj->normalizePageName($page); $itemId = $this->siteLinkLookup->getItemIdForLink($siteId, $pageName); } } // Redirect to the item page if we found its content if ($itemId !== null) { $title = $this->titleLookup->getTitleForId($itemId); $query = $request->getValues(); unset($query['title']); unset($query['site']); unset($query['page']); $itemUrl = $title->getFullUrl($query); $this->getOutput()->redirect($itemUrl); return; } } // If there is no item content post the switch form $this->switchForm($site, $page); }
/** * Returns a Title representing the given document. * * @param EntityId $id The entity * @param string|null $format The (normalized) format name, or '' * * @return Title */ public function getDocTitle(EntityId $id, $format = '') { if ($format === 'html') { $title = $this->entityTitleLookup->getTitleForId($id); } else { $doc = $this->getDocName($id, $format); $name = $this->interfaceTitle->getPrefixedText(); if ($doc !== null && $doc !== '') { $name .= '/' . $doc; } $title = Title::newFromText($name); } return $title; }
/** * @param ParserOutput $parserOutput */ private function addLinksToParserOutput(ParserOutput $parserOutput) { $linkBatch = new LinkBatch(); foreach ($this->entityIds as $entityId) { $linkBatch->addObj($this->entityTitleLookup->getTitleForId($entityId)); } $pages = $linkBatch->doQuery(); if ($pages === false) { return; } foreach ($pages as $page) { $title = Title::makeTitle($page->page_namespace, $page->page_title); $parserOutput->addLink($title, $page->page_id); } }
/** * @param EntityRedirect $redirect * @param Summary $summary * @param bool $bot Whether the edit should be marked as bot * * @throws RedirectCreationException */ private function saveRedirect(EntityRedirect $redirect, Summary $summary, $bot) { $summary = $this->summaryFormatter->formatSummary($summary); $flags = 0; if ($bot) { $flags = $flags | EDIT_FORCE_BOT; } if ($this->entityTitleLookup->getTitleForId($redirect->getEntityId())->isDeleted()) { $flags = $flags | EDIT_NEW; } else { $flags = $flags | EDIT_UPDATE; } $hookStatus = $this->editFilterHookRunner->run($redirect, $this->user, $summary); if (!$hookStatus->isOK()) { throw new RedirectCreationException('EditFilterHook stopped redirect creation', 'cant-redirect'); } try { $this->entityStore->saveRedirect($redirect, $summary, $this->user, $flags); } catch (StorageException $ex) { throw new RedirectCreationException($ex->getMessage(), 'cant-redirect', $ex); } }
/** * @param EntityId|null $entityId * @param string $entityType * * @return MutableContext */ private function getContextForEditFilter(EntityId $entityId = null, $entityType) { if ($entityId !== null) { $title = $this->titleLookup->getTitleForId($entityId); $context = clone $this->context; } else { $context = $this->context; // This constructs a "fake" title of the form Property:NewProperty, // where the title text is assumed to be name of the special page used // to create entities of the given type. This is used by the // LinkBeginHookHandler::doOnLinkBegin to replace the link to the // fake title with a link to the respective special page. // The effect is that e.g. the AbuseFilter log will show a link to // "Special:NewProperty" instead of "Property:NewProperty", while // the AbuseFilter itself will get a Title object with the correct // namespace IDs for Property entities. $namespace = $this->titleLookup->getNamespaceForType($entityType); $title = Title::makeTitle($namespace, 'New' . ucfirst($entityType)); } $context->setTitle($title); $context->setWikiPage(new WikiPage($title)); return $context; }
/** * @see EntityIdFormatter::formatEntityId * * @param EntityId $entityId * * @return string Plain text */ public function formatEntityId(EntityId $entityId) { $title = $this->titleLookup->getTitleForId($entityId); return $title->getFullText(); }
/** * Formats a row for display. * * @since 0.4 (as abstract function with same interface in 0.3) * * @param EntityId $entityId * * @return string HTML */ protected function formatRow($entityId) { $title = $this->entityTitleLookup->getTitleForId($entityId); return Linker::linkKnown($title); }
/** * @since 0.5 * * @param EntityId $id * * @throws MWException * @return null|Title */ protected function getEntityTitle(EntityId $id) { return $this->entityTitleLookup->getTitleForId($id); }
/** * Returns HTML representing the label in the display language (or an appropriate fallback). * * @param EntityId|null $entityId * * @return string HTML */ private function getIdHtml(EntityId $entityId = null) { $title = $this->titleLookup->getTitleForId($entityId); $idElement = Html::element('a', array('title' => $title ? $title->getPrefixedText() : '', 'href' => $title ? $title->getLocalURL() : '', 'class' => 'wb-itemlink-id'), $entityId->getSerialization()); return $idElement; }
/** * @param ItemId $fromId * @param ItemId $toId */ private function updateWatchlistEntries(ItemId $fromId, ItemId $toId) { $fromTitle = $this->entityTitleLookup->getTitleForId($fromId); $toTitle = $this->entityTitleLookup->getTitleForId($toId); WatchedItem::duplicateEntries($fromTitle, $toTitle); }