lucasdavies / wikitextbuilder
A fluent PHP library for programmatically building MediaWiki wikitext markup, including tables, lists, and templates
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 headings with the appropriate number of equals signs:
$builder->heading('Section Title', 2); // ==Section Title== $builder->heading('Subsection', 3); // ===Subsection===
Templates
Create 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(); // {{Citation needed}}
Tables
Build 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
|}
Styles
When applying styles to components, predefined styles can be used:
use LucasDavies\WikitextBuilder\Components\Table\TableCell; $cell = new TableCell('value')->textCenter(); echo $cell->render(); // |style="text-align:center"|value
Currently only text alignment styles are predefined: textLeft(), textCenter(), and textRight().
These can be used in conjunction with custom styles:
use LucasDavies\WikitextBuilder\Components\Table\TableCell; $cell = new TableCell('value')->textCenter()->styles(['color' => 'red']); echo $cell->render(); // |style="text-align:center;color:red"|value
Note that styles will overwrite each other depending on the order in which they are applied.
use LucasDavies\WikitextBuilder\Components\Table\TableCell; // Custom style applied after predefined style $cell = new TableCell('value')->textCenter()->styles(['color' => 'red', 'text-align' => 'left']); echo $cell->render(); // |style="color:red;text-align:left"|value // Predefined style applied after custom style $cell = new TableCell('value')->styles(['text-align' => 'left', 'color' => 'green'])->textRight(); echo $cell->render(); // |style="color:red;text-align:right"|value
Testing
Run the test suite:
composer test
Or with PHPUnit directly:
./vendor/bin/phpunit
License
MIT License. See LICENSE for details.