How do i write my own pre-save transform?

134 views Asked by At

As described here, the mediawiki parser allows for a pre-save transform which will automatically replace wikitext with something else.

How do I create my own?

I did find this, but I can't even be sure it's relevant anymore. http://mediawiki.sourcearchive.com/documentation/1.13.3/classArticle_a0d27b9b92f688ea124b1f1c4c0b60018.html

2

There are 2 answers

0
Ilmari Karonen On

In modern MediaWiki versions (v1.21+), one way to do this could be to:

  1. Write your own ContentHandler classes, extending WikiTextContent and WikitextContentHandler. These could be as simple as:

    class MyContentHandler extends WikitextContentHandler {
        protected function getContentClass() {
            return 'MyContent';
        }
    }
    class MyContent extends WikitextContent {
        // TODO: override preSaveTransform() here
    }
    
  2. In the Content subclass, override the preSaveTransform() method, e.g. like this (if you want to modify the wikitext after the normal PST pass):

    public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
        $content = parent::preSaveTransform( $title, $user, $popts );
        $orig = $text = $content->getNativeData();
        // ...modify $text here...
        return ( $orig === $text ) ? $content : new static( $text );
    }
    
  3. Register your new ContentHandler as the handler for ordinary wiki pages using $wgContentHandlers in LocalSettings.php:

    $wgContentHandlers[CONTENT_MODEL_WIKITEXT] = 'MyContentHandler';
    

(Warning: I believe this method should work, but I have not actually tested it! Use at your own risk. Improvements and bug reports welcome.)

1
Florian On

The replacement of the signature is hard-coded in mediawiki/core.

I don't know any way to do the same thing without changing core files (which, btw, isn't a good idea). You could use a parser function or create a tag extension to achieve what you want to do in a similar way.