Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Wikitext Parser

Convert wikitext to HTML or readable plain text through the current MediaWiki Action API.

Installation

composer require jord-jd/php-wikitext-parser

Usage

use JordJD\WikitextParser\Parser;

$parser = (new Parser())
    ->setWikitext("'''Hello''' [[world]]!");

$plainText = $parser->parse();

JordJD\WikitextParser\WikitextParser is also provided as a descriptive alias for compatibility with examples from older documentation.

HTML output

use JordJD\WikitextParser\Enums\Format;

$html = $parser
    ->setFormat(Format::HTML)
    ->parse();

Only Format::PLAIN_TEXT and Format::HTML are accepted. Plain text preserves useful block and line-break whitespace while removing markup, scripts, and styles.

Page context and other MediaWiki installations

Templates and magic words can depend on a page title. Supply it with setTitle(). A trusted alternate MediaWiki Action API can be selected with setEndpoint():

$result = (new Parser())
    ->setEndpoint('https://de.wikipedia.org/w/api.php')
    ->setTitle('Beispiel')
    ->setWikitext($wikitext)
    ->parse();

The parser sends wikitext with POST, so large inputs are not constrained by URL length. API, HTTP, JSON, and malformed-response failures throw a RuntimeException with a clear reason.

Caching

Parsed output is cached indefinitely in a system temporary directory by default, avoiding writes inside vendor/. Supply any PSR-6 pool, configure a TTL, or disable caching:

$parser = (new Parser($yourPsr6Cache))
    ->setCacheTtl(3600);

$uncached = (new Parser())
    ->disableCache();

Custom HTTP transport

The optional second constructor argument is a callable receiving the endpoint, POST body, headers, and timeout. It must return the response body as a string:

$parser = new Parser(
    null,
    function (string $endpoint, string $body, array $headers, int $timeout): string {
        return $yourHttpClient->post($endpoint, $body, $headers, $timeout);
    },
    10,
    'your-application/1.0 (https://example.com/contact)'
);

The package supports PHP 7.1 through current PHP 8.x releases and accepts both v3 and v4 of jord-jd/do-file-cache-psr-6.

About

Parse Wikitext in PHP

Topics

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages