jord-jd / php-wikitext-parser
Parse Wikitext in PHP
Fund package maintenance!
Requires
- php: >=7.1
- ext-json: *
- jord-jd/do-file-cache-psr-6: ^3.0||^4.0
Requires (Dev)
- phpunit/phpunit: ^7.5||^9.6||^10.5||^11.5||^12.5
Replaces
- divineomega/php-wikitext-parser: v3.1.0
This package is auto-updated.
Last update: 2026-07-18 05:07:09 UTC
README
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.