Mediawiki/Erweiterung/Description

Aus Mikiwiki
Zur Navigation springen Zur Suche springen

Die Mediawiki-Erweiterung "Description" erzeugt zu jeder Seite eine automatische Beschreibung innerhalb des HTML-Tags "<meta>".

Üblicherweise scheint es sich bei dieser Beschreibung einfach um den ersten Satz der Seite zu handeln. Das sieht dann z. B. so aus:

<meta name="description" content="Die Extension matrix zeigt hunderte von Erweiterungen für Mediawiki." />

Dumm ist allerdings, wenn die Seite mit einer Tabelle anfängt, dann sieht die Beschreibung nämlich so aus:

<meta name="description" content="|-" />

Installation

1. Herunterladen der aktuellen Software unter http://www.mediawiki.org/wiki/Special:ExtensionDistributor/Description

Anlegen des Verzeichnisses "extensions/Description".

# mkdir extensions/Description
# chown wiki:wiki extensions/Description

2. Anlegen der Datei "extensions/Description/Description.php" und Einfügen des folgenden PHP-Codes: Description r36316 (für Mediawiki 13.3).

# vi extensions/Description/Description.php
<?php
/**
 * Description.php -- Adds meaningful description <meta> tag to MW pages
 * Copyright 2008 Vinismo, Inc. (http://vinismo.com/)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @author Evan Prodromou <evan@vinismo.com>
 * @addtogroup Extensions
 */

if (!defined('MEDIAWIKI')) {
  exit( 1 );
}

define('DESCRIPTION_VERSION', '0.1');

$wgExtensionFunctions[] = 'DescriptionSetup';
$wgExtensionCredits['other'][] = array('name'        => 'Description',
                                       'version'     => DESCRIPTION_VERSION,
                                       'author'      => 'Evan Prodromou',
                                       'url'         => 'http://www.mediawiki.org/wiki/Extension:Description',
                                       'description' => 'Adds a description meta-tag to MW pages');

function DescriptionSetup() {
  global $wgHooks;
  $wgHooks['ArticleViewHeader'][] = 'DescriptionArticleViewHeader';
}

function DescriptionArticleViewHeader(&$article, &$outputDone = null, &$pcache = null) {
  global $wgOut;
  $desc = DescriptionFromArticle($article);
  if (!is_null($desc)) {
    $wgOut->addMeta('description', htmlspecialchars($desc));
  }
  return TRUE;
}

function DescriptionFromArticle(&$article) {
  if (defined('MWRDF_VERSION')) {
    $desc = DescriptionFromRDF($article);
  }
  if (is_null($desc) || strlen($desc) == 0) {
    $desc = DescriptionFromText($article);
  }
  return $desc;
}

function DescriptionFromRDF(&$article) {
  $nt = $article->getTitle();
  $uri = $nt->getFullUrl();
  $model = MwRdfGetModel($article);
  $results = $model->rdqlQuery("SELECT ?description " .
    "WHERE (<$uri> dc:description ?description) " . "USING dc FOR http://purl.org/dc/elements/1.1/", FALSE);
  $desc = '';
  foreach ($results as $row) {
    $rowval = preg_replace("/^\"(.*?)\"$/", '\1', $row['?description']);
    $desc .= (($desc) ? ' ' : '') . $rowval;
  }
  return $desc;
}

function DescriptionFromText(&$article) {
  global $wgParser, $wgContLang;

  # Expand all templates
  $text = $article->getContent(true);
  $text = $wgParser->preprocess($text, $article->mTitle, new ParserOptions());

  # Find first non-ws, non-empty, non-image, non-table content
  $imageLabel = $wgContLang->getNsText(NS_IMAGE);
  $paragraphs = explode("\n", $text);
  $desc = '';

  foreach ($paragraphs as $paragraph) {
    if (preg_match("/^\s*(=|__|\[\[[Ii]mage:|\[\[$imageLabel:|\{\||\|#|\*)/", $paragraph)) {
      continue;
    } else if (preg_match("/^\s*$/", $paragraph)) {
      continue;
    } else {
      $desc = DescriptionStripParagraph($paragraph);
      break;
    }
  }
  return $desc;
}

function DescriptionStripParagraph($para) {
  $para = preg_replace("/'''''(.*?)'''''/", '\1', $para);
  $para = preg_replace("/'''(.*?)'''/", '\1', $para);
  $para = preg_replace("/''(.*?)''/", '\1', $para);
  $para = preg_replace("@<(.*?)>(.*?)</\1>@", '\2', $para);	
  $para = preg_replace("/\[\[([^\]]*?)\|([^\]]*?)\]\]/", '\2', $para);
  $para = preg_replace("/\[\[([^\]]*?)\]\]/", '\1', $para);	
  $para = preg_replace("/\[(\S*)\s+(.*?)\]/", '\2', $para);
  $para = preg_replace("/\[(\S*)\s*\]/", '\1', $para);
  return $para;
}
?>

4. Anpassung der Rechte.

# chown wiki:wiki extensions/Description/Description.php

5. Einfügen der folgenden Zeile in die Datei "LocalSettings.php".

## Extension: Description
require_once("$IP/extensions/Description/Description.php");

Weblinks