/** mediawiki-extensions-ParserFunctions-REL1_37\includes\ParserFunctions.php
* {{#ifexist: page title | value if exists | value if doesn't exist }}
*
* @link https://www.mediawiki.org/wiki/Help:Extension:ParserFunctions##ifexist
*
* @param Parser $parser
* @param PPFrame $frame
* @param array $args
* @return string
*/
public static function ifexist( Parser $parser, PPFrame $frame, array $args ) {
$title = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
$then = $args[1] ?? null;
$else = $args[2] ?? null;
$result = self::ifexistInternal( $parser, $frame, $title, $then, $else );
if ( $result === null ) {
return '';
} else {
return trim( $frame->expand( $result ) );
}
}
/** mediawiki-extensions-ParserFunctions-REL1_37\includes\ParserFunctions.php
* @param Parser $parser
* @param PPFrame $frame
* @param string $titletext
* @param string $then
* @param string $else
*
* @return string
*/
private static function ifexistInternal(
Parser $parser, PPFrame $frame, $titletext = '', $then = '', $else = ''
) {
$title = Title::newFromText( $titletext );
self::getLanguageConverter( $parser->getContentLanguage() )
->findVariantLink( $titletext, $title, true );
if ( $title ) {
if ( $title->getNamespace() === NS_MEDIA ) {
/* If namespace is specified as NS_MEDIA, then we want to
* check the physical file, not the "description" page.
*/
if ( !$parser->incrementExpensiveFunctionCount() ) {
return $else;
}
$file = MediaWikiServices::getInstance()->getRepoGroup()->findFile( $title );
if ( !$file ) {
$parser->getOutput()->addImage(
$title->getDBKey(), false, false );
return $else;
}
$parser->getOutput()->addImage(
$file->getName(), $file->getTimestamp(), $file->getSha1() );
return $file->exists() ? $then : $else;
} elseif ( $title->isSpecialPage() ) {
/* Don't bother with the count for special pages,
* since their existence can be checked without
* accessing the database.
*/
return MediaWikiServices::getInstance()->getSpecialPageFactory()
->exists( $title->getDBkey() ) ? $then : $else;
} elseif ( $title->isExternal() ) {
/* Can't check the existence of pages on other sites,
* so just return $else. Makes a sort of sense, since
* they don't exist _locally_.
*/
return $else;
} else {
$pdbk = $title->getPrefixedDBkey();
$lc = MediaWikiServices::getInstance()->getLinkCache();
$id = $lc->getGoodLinkID( $pdbk );
if ( $id !== 0 ) {
$parser->getOutput()->addLink( $title, $id );
return $then;
} elseif ( $lc->isBadLink( $pdbk ) ) {
$parser->getOutput()->addLink( $title, 0 );
return $else;
}
if ( !$parser->incrementExpensiveFunctionCount() ) {
return $else;
}
$id = $title->getArticleID();
$parser->getOutput()->addLink( $title, $id );
// bug 70495: don't just check whether the ID != 0
if ( $title->exists() ) {
return $then;
}
}
}
return $else;
}