IfPageExists.php

From Knot Atlas
Jump to navigationJump to search

This is now deprecated, replaced by mediawiki's ParserFunction extension, in particular the parser function #ifexist:

<?php
# Example WikiMedia extension
# with WikiMedia's extension mechanism it is possible to define
# new tags of the form
# <TAGNAME> some text </TAGNAME>
# the function registered by the extension gets the text between the
# tags as input and can transform it into arbitrary HTML code.
# Note: The output is not interpreted as WikiText but directly
#       included in the HTML output. So Wiki markup is not supported.
# To activate the extension, include it from your LocalSettings.php
# with: include("extensions/YourExtensionName.php");

$wgExtensionFunctions[] = "wfIfPageExistsExtension";

function wfIfPageExistsExtension() {
    global $wgParser;
    # register the extension with the WikiText parser
    # the first parameter is the name of the new tag.
    # In this case it defines the tag <example> ... </example>
    # the second parameter is the callback function for
    # processing the text between the tags
    $wgParser->setHook( "ifpageexists", "ifPageExists" );
}

# The callback function for converting the input text to HTML output
function ifPageExists( $input ) {
    global $wgTitle;

    # first, let's replace {{PAGENAME}} wherever it appears.
    $input = str_replace("{{PAGENAME}}", $wgTitle->getText(), $input);

    # let's try to extract a transclusion
    if(ereg ("\{\{([^|}]*)", $input, $regs)) {
        # okay, we've found a likely looking target.
        $targetpage = $regs[1];
        # now ask the database if the page exists
        $article = & new Article( Title::newFromText($targetpage) );
        $content = $article->getContent( false );
        if( $content == "(There is currently no text in this page)" ) return "";
        if( $content == "" ) return "";
    }

     global $wgParser, $wgUser;
     $parserOptions = ParserOptions::newFromUser( $wgUser );
     $parser = & new Parser();
     $output = & $parser->parse($input, $wgTitle, $parserOptions);
     return $output->getText();
}
?>