You are here

class Wiki in Freelinking 8.3

Same name and namespace in other branches
  1. 4.0.x src/Plugin/freelinking/Wiki.php \Drupal\freelinking\Plugin\freelinking\Wiki

Freelinking wiki plugin implements wikipedia amongst other plugins.

Plugin annotation


@Freelinking(
  id = "wiki",
  title = @Translation("Wiki"),
  weight = 0,
  hidden = false,
  settings = {  }
)

Hierarchy

Expanded class hierarchy of Wiki

1 file declares its use of Wiki
WikiTest.php in tests/src/Unit/Plugin/freelinking/WikiTest.php
1 string reference to 'Wiki'
WikiTest::getPlugin in tests/src/Unit/Plugin/freelinking/WikiTest.php
Get the plugin instance.

File

src/Plugin/freelinking/Wiki.php, line 19

Namespace

Drupal\freelinking\Plugin\freelinking
View source
class Wiki extends FreelinkingPluginBase {

  /**
   * A map of wiki abbreviations to full names.
   *
   * @var array
   */
  protected $wikiMap = [
    'wp' => 'wikipedia',
    'wq' => 'wikiquote',
    'wt' => 'wiktionary',
    'wn' => 'wikinews',
    'ws' => 'wikisource',
    'wb' => 'wikibooks',
  ];

  /**
   * {@inheritdoc}
   */
  public function getTip() {

    // @todo Decide to split into multiple plugins for Wikipedia/Wikiquote here.
    return $this
      ->t('Click to view a wiki page.');
  }

  /**
   * {@inheritdoc}
   */
  public function getIndicator() {
    return '/w((ikipedia|p)|(ikiquote|q)|(iktionary|t)|(ikinews|n)|(ikisource|s)|(ikibooks|b))?$/A';
  }

  /**
   * {@inheritdoc}
   */
  public function buildLink(array $target) {
    $wikiname = $this
      ->getWikiFromIndicator($target['indicator']);
    $langcode = $target['language']
      ->getId();
    $dest = str_replace(' ', '_', $target['dest']);
    $wikiurl = 'https://' . $langcode . '.' . $wikiname . '.org/wiki/' . $dest;
    return [
      '#type' => 'link',
      '#title' => isset($target['text']) ? $target['text'] : ucwords($wikiname),
      '#url' => Url::fromUri($wikiurl, [
        'language' => $target['language'],
        'absolute' => TRUE,
      ]),
      '#attributes' => [
        'title' => isset($target['tooltip']) ? $target['tooltip'] : $this
          ->getTip(),
      ],
    ];
  }

  /**
   * Get the wiki name from the indicator string.
   *
   * @param string $indicator
   *   The indicator string used.
   *
   * @return string
   *   The full wiki name.
   */
  protected function getWikiFromIndicator($indicator) {
    $ret = '';
    if (preg_match($this
      ->getIndicator(), $indicator, $matches)) {
      if (strlen($matches[0]) === 2) {
        $ret = $this->wikiMap[$matches[0]];
      }
      else {

        // The indicator is the full wiki name.
        $ret = $matches[0];
      }
    }
    return $ret;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FreelinkingPluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
FreelinkingPluginBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 6
FreelinkingPluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration 1
FreelinkingPluginBase::getFailoverPluginId public function Get the failover plugin ID (if applicable). Overrides FreelinkingPluginInterface::getFailoverPluginId 1
FreelinkingPluginBase::isHidden public function Determine if the plugin is built-in (always on). Overrides FreelinkingPluginInterface::isHidden
FreelinkingPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
FreelinkingPluginBase::settingsForm public function Plugin configuration form. Overrides FreelinkingPluginInterface::settingsForm 6
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
Wiki::$wikiMap protected property A map of wiki abbreviations to full names.
Wiki::buildLink public function Build a link with the plugin. Overrides FreelinkingPluginInterface::buildLink
Wiki::getIndicator public function A regular expression string to indicate what to replace for this plugin. Overrides FreelinkingPluginInterface::getIndicator
Wiki::getTip public function Provides tips for this freelinking plugin. Overrides FreelinkingPluginInterface::getTip
Wiki::getWikiFromIndicator protected function Get the wiki name from the indicator string.