DynamicPageList3/includes/Hooks.php

591 lines
16 KiB
PHP
Raw Normal View History

2020-11-22 19:56:23 +00:00
<?php
namespace MediaWiki\Extension\DynamicPageList3;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
use DatabaseUpdater;
use MediaWiki\Extension\DynamicPageList3\Maintenance\CreateTemplate;
2021-10-01 22:52:30 +00:00
use Parser;
use PPFrame;
2020-11-22 19:56:23 +00:00
class Hooks {
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const FATAL_WRONGNS = 1001;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const FATAL_WRONGLINKSTO = 1002;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const FATAL_TOOMANYCATS = 1003;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const FATAL_TOOFEWCATS = 1004;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const FATAL_NOSELECTION = 1005;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const FATAL_CATDATEBUTNOINCLUDEDCATS = 1006;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const FATAL_CATDATEBUTMORETHAN1CAT = 1007;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const FATAL_MORETHAN1TYPEOFDATE = 1008;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const FATAL_WRONGORDERMETHOD = 1009;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const FATAL_DOMINANTSECTIONRANGE = 1010;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const FATAL_OPENREFERENCES = 1012;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const FATAL_MISSINGPARAMFUNCTION = 1022;
2020-11-22 19:56:23 +00:00
public const FATAL_POOLCOUNTER = 1023;
2020-11-22 19:56:23 +00:00
public const FATAL_NOTPROTECTED = 1024;
public const FATAL_SQLBUILDERROR = 1025;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const WARN_UNKNOWNPARAM = 2013;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const WARN_PARAMNOOPTION = 2022;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const WARN_WRONGPARAM = 2014;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const WARN_WRONGPARAM_INT = 2015;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const WARN_NORESULTS = 2016;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const WARN_CATOUTPUTBUTWRONGPARAMS = 2017;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const WARN_HEADINGBUTSIMPLEORDERMETHOD = 2018;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const WARN_DEBUGPARAMNOTFIRST = 2019;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const WARN_TRANSCLUSIONLOOP = 2020;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
public const DEBUG_QUERY = 3021;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
/** @var array */
2021-02-22 23:48:01 +00:00
public static $fixedCategories = [];
2020-11-22 19:56:23 +00:00
2021-10-02 21:55:24 +00:00
/**
* @var array
* @phan-var array<mixed,mixed>
*/
2021-10-01 22:52:30 +00:00
public static $createdLinks;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
/** @var bool */
2021-02-22 23:48:01 +00:00
private static $likeIntersection = false;
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
/** @var int */
2021-02-22 23:48:01 +00:00
private static $debugLevel = 0;
2020-11-22 19:56:23 +00:00
/**
2021-10-01 22:52:30 +00:00
* @return string
2020-11-22 19:56:23 +00:00
*/
2021-10-01 22:52:30 +00:00
public static function getVersion(): string {
static $version = null;
if ( $version === null ) {
$version = json_decode( file_get_contents( __DIR__ . '/../extension.json' ) )->version;
2020-11-22 19:56:23 +00:00
}
2021-10-01 22:52:30 +00:00
return $version;
2020-11-22 19:56:23 +00:00
}
/**
* Sets up this extension's parser functions.
*
2021-10-01 22:52:30 +00:00
* @param Parser $parser
2020-11-22 19:56:23 +00:00
*/
2021-10-01 22:52:30 +00:00
public static function onParserFirstCallInit( Parser $parser ) {
2020-11-22 19:56:23 +00:00
self::init();
2021-10-01 22:52:30 +00:00
// DPL offers the same functionality as Intersection. So we register the <DynamicPageList> tag in case LabeledSection Extension is not installed so that the section markers are removed.
if ( Config::getSetting( 'handleSectionTag' ) ) {
2021-02-22 23:48:01 +00:00
$parser->setHook( 'section', [ __CLASS__, 'dplTag' ] );
2020-11-22 19:56:23 +00:00
}
2021-02-23 00:17:04 +00:00
2021-02-22 23:48:01 +00:00
$parser->setHook( 'DPL', [ __CLASS__, 'dplTag' ] );
$parser->setHook( 'DynamicPageList', [ __CLASS__, 'intersectionTag' ] );
2020-11-22 19:56:23 +00:00
2021-02-22 23:48:01 +00:00
$parser->setFunctionHook( 'dpl', [ __CLASS__, 'dplParserFunction' ] );
$parser->setFunctionHook( 'dplnum', [ __CLASS__, 'dplNumParserFunction' ] );
$parser->setFunctionHook( 'dplvar', [ __CLASS__, 'dplVarParserFunction' ] );
$parser->setFunctionHook( 'dplreplace', [ __CLASS__, 'dplReplaceParserFunction' ] );
$parser->setFunctionHook( 'dplchapter', [ __CLASS__, 'dplChapterParserFunction' ] );
$parser->setFunctionHook( 'dplmatrix', [ __CLASS__, 'dplMatrixParserFunction' ] );
2020-11-22 19:56:23 +00:00
}
/**
* Sets up this extension's parser functions for migration from Intersection.
*
2021-10-01 22:52:30 +00:00
* @param Parser $parser
2020-11-22 19:56:23 +00:00
*/
2021-10-01 22:52:30 +00:00
public static function setupMigration( Parser $parser ) {
2021-02-22 23:48:01 +00:00
$parser->setHook( 'Intersection', [ __CLASS__, 'intersectionTag' ] );
$parser->addTrackingCategory( 'dpl-intersection-tracking-category' );
2020-11-22 19:56:23 +00:00
self::init();
}
/**
* Common initializer for usage from parser entry points.
*/
private static function init() {
2021-10-01 22:52:30 +00:00
Config::init();
2020-11-22 19:56:23 +00:00
2021-02-22 23:48:01 +00:00
if ( !isset( self::$createdLinks ) ) {
2020-11-22 19:56:23 +00:00
self::$createdLinks = [
2021-02-18 21:47:20 +00:00
'resetLinks' => false,
'resetTemplates' => false,
'resetCategories' => false,
'resetImages' => false,
'resetdone' => false,
'elimdone' => false
2020-11-22 19:56:23 +00:00
];
}
}
/**
* Set to behave like intersection.
*
2021-10-01 22:52:30 +00:00
* @param bool $mode
2020-11-22 19:56:23 +00:00
*/
2021-02-22 23:48:01 +00:00
private static function setLikeIntersection( $mode = false ) {
2020-11-22 19:56:23 +00:00
self::$likeIntersection = $mode;
}
/**
* Is like intersection?
*
2021-10-01 22:52:30 +00:00
* @return bool
2020-11-22 19:56:23 +00:00
*/
public static function isLikeIntersection() {
return (bool)self::$likeIntersection;
}
/**
* Tag <section> entry point.
*
2021-10-01 22:52:30 +00:00
* @param string $input
* @param array $args
* @param Parser $parser
* @param PPFrame $frame
* @return string
2020-11-22 19:56:23 +00:00
*/
2021-02-22 23:48:01 +00:00
public static function intersectionTag( $input, array $args, Parser $parser, PPFrame $frame ) {
self::setLikeIntersection( true );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
return self::executeTag( $input, $args, $parser, $frame );
2020-11-22 19:56:23 +00:00
}
/**
* Tag <dpl> entry point.
*
2021-10-01 22:52:30 +00:00
* @param string $input
* @param array $args
* @param Parser $parser
* @param PPFrame $frame
* @return string
2020-11-22 19:56:23 +00:00
*/
2021-02-22 23:48:01 +00:00
public static function dplTag( $input, array $args, Parser $parser, PPFrame $frame ) {
self::setLikeIntersection( false );
$parser->addTrackingCategory( 'dpl-tag-tracking-category' );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
return self::executeTag( $input, $args, $parser, $frame );
2020-11-22 19:56:23 +00:00
}
/**
* The callback function wrapper for converting the input text to HTML output
*
2021-10-01 22:52:30 +00:00
* @param string $input
* @param array $args
* @param Parser $parser
* @param PPFrame $frame
* @return string
2020-11-22 19:56:23 +00:00
*/
2021-02-22 23:48:01 +00:00
private static function executeTag( $input, array $args, Parser $parser, PPFrame $frame ) {
2021-10-01 22:52:30 +00:00
// entry point for user tag <dpl> or <DynamicPageList>
2020-11-22 19:56:23 +00:00
// create list and do a recursive parse of the output
2021-10-01 22:52:30 +00:00
$parse = new Parse();
if ( Config::getSetting( 'recursiveTagParse' ) ) {
2021-02-22 23:48:01 +00:00
$input = $parser->recursiveTagParse( $input, $frame );
2020-11-22 19:56:23 +00:00
}
2021-02-23 00:17:04 +00:00
2021-02-22 23:48:01 +00:00
$text = $parse->parse( $input, $parser, $reset, $eliminate, true );
2020-11-22 19:56:23 +00:00
2021-12-15 20:22:38 +00:00
// we can remove the templates by save/restore
if ( $reset['templates'] ?? false ) {
2021-05-31 10:55:33 +00:00
$saveTemplates = $parser->getOutput()->mTemplates;
2020-11-22 19:56:23 +00:00
}
2021-10-01 22:52:30 +00:00
2021-12-15 20:22:38 +00:00
// we can remove the categories by save/restore
if ( $reset['categories'] ?? false ) {
2021-05-31 10:55:33 +00:00
$saveCategories = $parser->getOutput()->mCategories;
2020-11-22 19:56:23 +00:00
}
2021-10-01 22:52:30 +00:00
2021-12-15 20:22:38 +00:00
// we can remove the images by save/restore
if ( $reset['images'] ?? false ) {
2021-05-31 10:55:33 +00:00
$saveImages = $parser->getOutput()->mImages;
2020-11-22 19:56:23 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$parsedDPL = $parser->recursiveTagParse( $text );
2021-10-01 22:52:30 +00:00
if ( $reset['templates'] ?? false ) {
$parser->getOutput()->mTemplates = $saveTemplates ?? [];
2020-11-22 19:56:23 +00:00
}
2021-10-01 22:52:30 +00:00
if ( $reset['categories'] ?? false ) {
$parser->getOutput()->mCategories = $saveCategories ?? [];
2020-11-22 19:56:23 +00:00
}
2021-10-01 22:52:30 +00:00
if ( $reset['images'] ?? false ) {
$parser->getOutput()->mImages = $saveImages ?? [];
2020-11-22 19:56:23 +00:00
}
return $parsedDPL;
}
/**
* The #dpl parser tag entry point.
*
2021-10-01 22:52:30 +00:00
* @param Parser $parser
* @return array|string
2020-11-22 19:56:23 +00:00
*/
2021-10-01 22:52:30 +00:00
public static function dplParserFunction( $parser ) {
2021-02-22 23:48:01 +00:00
self::setLikeIntersection( false );
2020-11-22 19:56:23 +00:00
2021-02-22 23:48:01 +00:00
$parser->addTrackingCategory( 'dpl-parserfunc-tracking-category' );
2020-11-22 19:56:23 +00:00
2021-10-01 22:52:30 +00:00
// callback for the parser function {{#dpl: or {{DynamicPageList::
2020-11-22 19:56:23 +00:00
$input = "";
$numargs = func_num_args();
2021-02-22 23:48:01 +00:00
if ( $numargs < 2 ) {
2020-11-22 19:56:23 +00:00
$input = "#dpl: no arguments specified";
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
return str_replace( '§', '<', '§pre>§nowiki>' . $input . '§/nowiki>§/pre>' );
2020-11-22 19:56:23 +00:00
}
// fetch all user-provided arguments (skipping $parser)
$arg_list = func_get_args();
2021-02-22 23:48:01 +00:00
for ( $i = 1; $i < $numargs; $i++ ) {
2020-11-22 19:56:23 +00:00
$p1 = $arg_list[$i];
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$input .= str_replace( "\n", "", $p1 ) . "\n";
2020-11-22 19:56:23 +00:00
}
2021-10-01 22:52:30 +00:00
$parse = new Parse();
2021-02-22 23:48:01 +00:00
$dplresult = $parse->parse( $input, $parser, $reset, $eliminate, false );
2021-10-01 22:52:30 +00:00
return [
// @phan-suppress-next-line PhanPluginMixedKeyNoKey
$parser->getPreprocessor()->preprocessToObj( $dplresult, 1 ),
2020-11-22 19:56:23 +00:00
'isLocalObj' => true,
'title' => $parser->getTitle()
];
}
/**
* The #dplnum parser tag entry point.
2021-10-01 22:52:30 +00:00
* From the old documentation: "Tries to guess a number that is buried in the text. Uses a set of heuristic rules which may work or not. The idea is to extract the number so that it can be used as a sorting value in the column of a DPL table output."
2020-11-22 19:56:23 +00:00
*
2021-10-01 22:52:30 +00:00
* @param Parser $parser
* @param string $text
* @return string
2020-11-22 19:56:23 +00:00
*/
2021-10-01 22:52:30 +00:00
public static function dplNumParserFunction( $parser, $text = '' ) {
2021-02-22 23:48:01 +00:00
$parser->addTrackingCategory( 'dplnum-parserfunc-tracking-category' );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$num = str_replace( '&#160;', ' ', $text );
$num = str_replace( '&nbsp;', ' ', $text );
$num = preg_replace( '/([0-9])([.])([0-9][0-9]?[^0-9,])/', '\1,\3', $num );
$num = preg_replace( '/([0-9.]+),([0-9][0-9][0-9])\s*Mrd/', '\1\2 000000 ', $num );
$num = preg_replace( '/([0-9.]+),([0-9][0-9])\s*Mrd/', '\1\2 0000000 ', $num );
$num = preg_replace( '/([0-9.]+),([0-9])\s*Mrd/', '\1\2 00000000 ', $num );
$num = preg_replace( '/\s*Mrd/', '000000000 ', $num );
$num = preg_replace( '/([0-9.]+),([0-9][0-9][0-9])\s*Mio/', '\1\2 000 ', $num );
$num = preg_replace( '/([0-9.]+),([0-9][0-9])\s*Mio/', '\1\2 0000 ', $num );
$num = preg_replace( '/([0-9.]+),([0-9])\s*Mio/', '\1\2 00000 ', $num );
$num = preg_replace( '/\s*Mio/', '000000 ', $num );
$num = preg_replace( '/[. ]/', '', $num );
$num = preg_replace( '/^[^0-9]+/', '', $num );
$num = preg_replace( '/[^0-9].*/', '', $num );
2021-10-01 22:52:30 +00:00
2020-11-22 19:56:23 +00:00
return $num;
}
2021-02-22 23:48:01 +00:00
public static function dplVarParserFunction( &$parser, $cmd ) {
$parser->addTrackingCategory( 'dplvar-parserfunc-tracking-category' );
2020-11-22 19:56:23 +00:00
$args = func_get_args();
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $cmd == 'set' ) {
2021-10-01 22:52:30 +00:00
return Variables::setVar( $args );
2021-02-22 23:48:01 +00:00
} elseif ( $cmd == 'default' ) {
2021-10-01 22:52:30 +00:00
return Variables::setVarDefault( $args );
2020-11-22 19:56:23 +00:00
}
2021-10-01 22:52:30 +00:00
return Variables::getVar( $cmd );
2020-11-22 19:56:23 +00:00
}
2021-02-22 23:48:01 +00:00
private static function isRegexp( $needle ) {
if ( strlen( $needle ) < 3 ) {
2020-11-22 19:56:23 +00:00
return false;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( ctype_alnum( $needle[0] ) ) {
2020-11-22 19:56:23 +00:00
return false;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
$nettoNeedle = preg_replace( '/[ismu]*$/', '', $needle );
if ( strlen( $nettoNeedle ) < 2 ) {
2020-11-22 19:56:23 +00:00
return false;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $needle[0] == $nettoNeedle[strlen( $nettoNeedle ) - 1] ) {
2020-11-22 19:56:23 +00:00
return true;
}
2021-10-01 22:52:30 +00:00
2020-11-22 19:56:23 +00:00
return false;
}
2021-02-22 23:48:01 +00:00
public static function dplReplaceParserFunction( &$parser, $text, $pat = '', $repl = '' ) {
$parser->addTrackingCategory( 'dplreplace-parserfunc-tracking-category' );
if ( $text == '' || $pat == '' ) {
2020-11-22 19:56:23 +00:00
return '';
}
2021-10-01 22:52:30 +00:00
// convert \n to a real newline character
2021-02-22 23:48:01 +00:00
$repl = str_replace( '\n', "\n", $repl );
2020-11-22 19:56:23 +00:00
# replace
2021-02-22 23:48:01 +00:00
if ( !self::isRegexp( $pat ) ) {
$pat = '`' . str_replace( '`', '\`', $pat ) . '`';
2020-11-22 19:56:23 +00:00
}
2021-10-01 22:52:30 +00:00
// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
2021-02-22 23:48:01 +00:00
return @preg_replace( $pat, $repl, $text );
2020-11-22 19:56:23 +00:00
}
2021-02-22 23:48:01 +00:00
public static function dplChapterParserFunction( &$parser, $text = '', $heading = ' ', $maxLength = -1, $page = '?page?', $link = 'default', $trim = false ) {
$parser->addTrackingCategory( 'dplchapter-parserfunc-tracking-category' );
2021-10-01 22:52:30 +00:00
$output = LST::extractHeadingFromText( $parser, $page, '?title?', $text, $heading, '', $sectionHeading, true, $maxLength, $link, $trim );
2020-11-22 19:56:23 +00:00
return $output[0];
}
2021-02-22 23:48:01 +00:00
public static function dplMatrixParserFunction( &$parser, $name = '', $yes = '', $no = '', $flip = '', $matrix = '' ) {
$parser->addTrackingCategory( 'dplmatrix-parserfunc-tracking-category' );
2021-10-01 22:52:30 +00:00
$lines = explode( "\n", $matrix );
$m = [];
2020-11-22 19:56:23 +00:00
$sources = [];
$targets = [];
2021-10-01 22:52:30 +00:00
$from = '';
$to = '';
if ( $flip == '' || $flip == 'normal' ) {
2020-11-22 19:56:23 +00:00
$flip = false;
} else {
$flip = true;
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $name == '' ) {
2020-11-22 19:56:23 +00:00
$name = '&#160;';
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $yes == '' ) {
2020-11-22 19:56:23 +00:00
$yes = ' x ';
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $no == '' ) {
2020-11-22 19:56:23 +00:00
$no = '&#160;';
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( $no[0] == '-' ) {
2020-11-22 19:56:23 +00:00
$no = " $no ";
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $lines as $line ) {
if ( strlen( $line ) <= 0 ) {
2020-11-22 19:56:23 +00:00
continue;
}
2021-02-23 00:17:04 +00:00
2021-02-22 23:48:01 +00:00
if ( $line[0] != ' ' ) {
$from = preg_split( ' *\~\~ *', trim( $line ), 2 );
if ( !array_key_exists( $from[0], $sources ) ) {
if ( count( $from ) < 2 || $from[1] == '' ) {
2020-11-22 19:56:23 +00:00
$sources[$from[0]] = $from[0];
} else {
$sources[$from[0]] = $from[1];
}
2021-10-01 22:52:30 +00:00
2020-11-22 19:56:23 +00:00
$m[$from[0]] = [];
}
2021-02-22 23:48:01 +00:00
} elseif ( trim( $line ) != '' ) {
$to = preg_split( ' *\~\~ *', trim( $line ), 2 );
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( count( $to ) < 2 || $to[1] == '' ) {
2020-11-22 19:56:23 +00:00
$targets[$to[0]] = $to[0];
} else {
$targets[$to[0]] = $to[1];
}
2021-10-01 22:52:30 +00:00
// @phan-suppress-next-line PhanTypeInvalidDimOffset
2020-11-22 19:56:23 +00:00
$m[$from[0]][$to[0]] = true;
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
ksort( $targets );
2020-11-22 19:56:23 +00:00
$header = "\n";
2021-02-22 23:48:01 +00:00
if ( $flip ) {
foreach ( $sources as $from => $fromName ) {
2020-11-22 19:56:23 +00:00
$header .= "![[$from|" . $fromName . "]]\n";
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $targets as $to => $toName ) {
2020-11-22 19:56:23 +00:00
$targets[$to] = "[[$to|$toName]]";
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $sources as $from => $fromName ) {
if ( array_key_exists( $to, $m[$from] ) ) {
2020-11-22 19:56:23 +00:00
$targets[$to] .= "\n|$yes";
} else {
$targets[$to] .= "\n|$no";
}
}
2021-10-01 22:52:30 +00:00
2020-11-22 19:56:23 +00:00
$targets[$to] .= "\n|--\n";
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
return "{|class=dplmatrix\n|$name" . "\n" . $header . "|--\n!" . implode( "\n!", $targets ) . "\n|}";
2020-11-22 19:56:23 +00:00
} else {
2021-02-22 23:48:01 +00:00
foreach ( $targets as $to => $toName ) {
2020-11-22 19:56:23 +00:00
$header .= "![[$to|" . $toName . "]]\n";
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $sources as $from => $fromName ) {
2020-11-22 19:56:23 +00:00
$sources[$from] = "[[$from|$fromName]]";
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
foreach ( $targets as $to => $toName ) {
if ( array_key_exists( $to, $m[$from] ) ) {
2020-11-22 19:56:23 +00:00
$sources[$from] .= "\n|$yes";
} else {
$sources[$from] .= "\n|$no";
}
}
2021-10-01 22:52:30 +00:00
2020-11-22 19:56:23 +00:00
$sources[$from] .= "\n|--\n";
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
return "{|class=dplmatrix\n|$name" . "\n" . $header . "|--\n!" . implode( "\n!", $sources ) . "\n|}";
2020-11-22 19:56:23 +00:00
}
}
2021-02-22 23:48:01 +00:00
public static function fixCategory( $cat ) {
if ( $cat != '' ) {
2020-11-22 19:56:23 +00:00
self::$fixedCategories[$cat] = 1;
}
}
/**
* Set Debugging Level
*
2021-10-01 22:52:30 +00:00
* @param int|string $level
2020-11-22 19:56:23 +00:00
*/
2021-02-22 23:48:01 +00:00
public static function setDebugLevel( $level ) {
self::$debugLevel = intval( $level );
2020-11-22 19:56:23 +00:00
}
/**
* Return Debugging Level
*
2021-10-01 22:52:30 +00:00
* @return int
2020-11-22 19:56:23 +00:00
*/
public static function getDebugLevel() {
return self::$debugLevel;
}
2021-10-01 22:52:30 +00:00
// reset everything; some categories may have been fixed, however via fixcategory=
public static function endReset( $parser, $text ) {
2021-02-22 23:48:01 +00:00
if ( !self::$createdLinks['resetdone'] ) {
2020-11-22 19:56:23 +00:00
self::$createdLinks['resetdone'] = true;
2021-10-01 22:52:30 +00:00
2021-05-31 10:55:33 +00:00
foreach ( $parser->getOutput()->mCategories as $key => $val ) {
2021-02-22 23:48:01 +00:00
if ( array_key_exists( $key, self::$fixedCategories ) ) {
2020-11-22 19:56:23 +00:00
self::$fixedCategories[$key] = $val;
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( self::$createdLinks['resetLinks'] ) {
2021-05-31 10:55:33 +00:00
$parser->getOutput()->mLinks = [];
2020-11-22 19:56:23 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( self::$createdLinks['resetCategories'] ) {
2021-05-31 10:55:33 +00:00
$parser->getOutput()->mCategories = self::$fixedCategories;
2020-11-22 19:56:23 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( self::$createdLinks['resetTemplates'] ) {
2021-05-31 10:55:33 +00:00
$parser->getOutput()->mTemplates = [];
2020-11-22 19:56:23 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( self::$createdLinks['resetImages'] ) {
2021-05-31 10:55:33 +00:00
$parser->getOutput()->mImages = [];
2020-11-22 19:56:23 +00:00
}
2021-10-01 22:52:30 +00:00
2020-11-22 19:56:23 +00:00
self::$fixedCategories = [];
}
}
2021-10-01 22:52:30 +00:00
public static function endEliminate( $parser, &$text ) {
2020-11-22 19:56:23 +00:00
// called during the final output phase; removes links created by DPL
2021-02-22 23:48:01 +00:00
if ( isset( self::$createdLinks ) ) {
if ( array_key_exists( 0, self::$createdLinks ) ) {
2021-05-31 10:55:33 +00:00
foreach ( $parser->getOutput()->getLinks() as $nsp => $link ) {
2021-02-22 23:48:01 +00:00
if ( !array_key_exists( $nsp, self::$createdLinks[0] ) ) {
2020-11-22 19:56:23 +00:00
continue;
}
2021-10-01 22:52:30 +00:00
2021-05-31 10:55:33 +00:00
$parser->getOutput()->mLinks[$nsp] = array_diff_assoc( $parser->getOutput()->mLinks[$nsp], self::$createdLinks[0][$nsp] );
2021-10-01 22:52:30 +00:00
2021-05-31 10:55:33 +00:00
if ( count( $parser->getOutput()->mLinks[$nsp] ) == 0 ) {
unset( $parser->getOutput()->mLinks[$nsp] );
2020-11-22 19:56:23 +00:00
}
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( isset( self::$createdLinks ) && array_key_exists( 1, self::$createdLinks ) ) {
2021-05-31 10:55:33 +00:00
foreach ( $parser->getOutput()->mTemplates as $nsp => $tpl ) {
2021-02-22 23:48:01 +00:00
if ( !array_key_exists( $nsp, self::$createdLinks[1] ) ) {
2020-11-22 19:56:23 +00:00
continue;
}
2021-10-01 22:52:30 +00:00
2021-05-31 10:55:33 +00:00
$parser->getOutput()->mTemplates[$nsp] = array_diff_assoc( $parser->getOutput()->mTemplates[$nsp], self::$createdLinks[1][$nsp] );
2021-10-01 22:52:30 +00:00
2021-05-31 10:55:33 +00:00
if ( count( $parser->getOutput()->mTemplates[$nsp] ) == 0 ) {
unset( $parser->getOutput()->mTemplates[$nsp] );
2020-11-22 19:56:23 +00:00
}
}
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( isset( self::$createdLinks ) && array_key_exists( 2, self::$createdLinks ) ) {
2021-05-31 10:55:33 +00:00
$parser->getOutput()->mCategories = array_diff_assoc( $parser->getOutput()->mCategories, self::$createdLinks[2] );
2020-11-22 19:56:23 +00:00
}
2021-10-01 22:52:30 +00:00
2021-02-22 23:48:01 +00:00
if ( isset( self::$createdLinks ) && array_key_exists( 3, self::$createdLinks ) ) {
2021-05-31 10:55:33 +00:00
$parser->getOutput()->mImages = array_diff_assoc( $parser->getOutput()->mImages, self::$createdLinks[3] );
2020-11-22 19:56:23 +00:00
}
}
}
/**
* Setups and Modifies Database Information
*
2021-02-23 00:17:04 +00:00
* @param DatabaseUpdater $updater
2020-11-22 19:56:23 +00:00
*/
2021-02-22 23:48:01 +00:00
public static function onLoadExtensionSchemaUpdates( DatabaseUpdater $updater ) {
$updater->addPostDatabaseUpdateMaintenance( CreateTemplate::class );
2020-11-22 19:56:23 +00:00
$db = $updater->getDB();
2021-02-22 23:48:01 +00:00
if ( !$db->tableExists( 'dpl_clview' ) ) {
2020-11-22 19:56:23 +00:00
// PostgreSQL doesn't have IFNULL, so use COALESCE instead
2021-02-22 23:48:01 +00:00
$sqlNullMethod = ( $db->getType() === 'postgres' ? 'COALESCE' : 'IFNULL' );
$db->query( "CREATE VIEW {$db->tablePrefix()}dpl_clview AS SELECT $sqlNullMethod(cl_from, page_id) AS cl_from, $sqlNullMethod(cl_to, '') AS cl_to, cl_sortkey FROM {$db->tablePrefix()}page LEFT OUTER JOIN {$db->tablePrefix()}categorylinks ON {$db->tablePrefix()}page.page_id=cl_from;" );
2020-11-22 19:56:23 +00:00
}
}
}