lucasdavies / wikitextbuilder
A fluent PHP library for programmatically building MediaWiki wikitext markup, including tables, lists, and templates
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/lucasdavies/wikitextbuilder
Requires
- php: ^8.4
Requires (Dev)
- phpunit/phpunit: ^12.5
- symfony/var-dumper: ^8.0
README
A PHP library for building MediaWiki wikitext markup programmatically.
Requirements
- PHP 8.4 or higher
Installation
Install via Composer:
composer require lucasdavies/wikitextbuilder
Usage
Basic Setup
use LucasDavies\WikitextBuilder\Builder; $builder = new Builder();
Headings
Generate MediaWiki headings with the appropriate number of equals signs:
$builder->heading('Section Title', 2); // ==Section Title== $builder->heading('Subsection', 3); // ===Subsection===
Templates
Create MediaWiki templates with various formatting options:
// Basic template $builder->template('Citation needed'); // {{Citation needed}} // Template with unnamed parameters $builder->template('Convert')->params(['100', 'km', 'mi']); // {{Convert|100|km|mi}} // Template with named parameters $builder->template('Infobox') ->params([ 'name' => 'Example', 'type' => 'Sample' ]); // {{Infobox|name=Example|type=Sample}} // Multiline template $builder->template('Infobox') ->multiline() ->params([ 'name' => 'Example', 'type' => 'Sample' ]); // {{Infobox // |name=Example // |type=Sample // }} // Multiline with spacing $builder->template('Infobox') ->multiline() ->spaced() ->params([ 'name' => 'Example', 'type' => 'Sample' ]); // {{Infobox // |name = Example // |type = Sample // }} // Multiline with alignment $builder->template('Infobox') ->multiline() ->aligned() ->params([ 'name' => 'Example', 'description' => 'A longer parameter' ]); // {{Infobox // |name = Example // |description = A longer parameter // }}
Alternatively you can build templates by using the component classes directly and calling the render() method:
use LucasDavies\WikitextBuilder\Components\Template; $template = new Template('Citation needed'); echo $template->render();
Outputs:
{{Citation needed}}
Tables
Build MediaWiki tables with headers, body rows, captions, and styling:
$builder->table(function (Table $table) { return $table ->caption('Example Table') ->headerRow(['Column 1', 'Column 2', 'Column 3']) ->bodyRow(['Value 1', 'Value 2', 'Value 3']) ->bodyRow(['Value 4', 'Value 5', 'Value 6']); });
Outputs:
{| class="wikitable"
|+ Example Table
|-
!Column 1!!Column 2!!Column 3
|-
|Value 1||Value 2||Value 3
|-
|Value 4||Value 5||Value 6
|}
Table Styling
$builder->table(function (Table $table) { return $table ->styles(['width' => '100%', 'margin' => 'auto']) ->classes(['sortable', 'collapsible']) ->headerRow(['Name', 'Age', 'City']) ->bodyRow(['Alice', '30', 'New York']); });
Outputs:
{| class="wikitable sortable collapsible" style="width:100%;margin:auto"
|-
!Name!!Age!!City
|-
|Alice||30||New York
|}
Alternatively, like templates, you can build tables by using the component classes directly and calling the render() method:
use LucasDavies\WikitextBuilder\Components\Table; echo new Table() ->styles(['width' => '100%', 'margin' => 'auto']) ->classes(['sortable', 'collapsible']) ->headerRow(new TableHeaderRow(['Name', 'Age', 'City'])) ->bodyRow(new TableBodyRow(['Alice', '30', 'New York'])) ->render();
Outputs:
{| class="wikitable sortable collapsible" style="width:100%;margin:auto"
|-
!Name!!Age!!City
|-
|Alice||30||New York
|}
Testing
Run the test suite:
composer test
Or with PHPUnit directly:
./vendor/bin/phpunit
License
MIT License. See LICENSE for details.