Make Linker class instantiable.

This commit is contained in:
Daniel Kraus
2017-08-27 10:42:10 +02:00
parent 5fe5923bf7
commit 5f7c96459d
4 changed files with 112 additions and 89 deletions

View File

@ -44,7 +44,7 @@ else
} }
}; };
require_once( __DIR__ . "/includes/LinkTitles_Extension.php" ); require_once( __DIR__ . "/includes/Extension.php" );
/// Core class of the maintanance script. /// Core class of the maintanance script.
/// @note Note that the execution of maintenance scripts is prohibited for /// @note Note that the execution of maintenance scripts is prohibited for

View File

@ -133,6 +133,9 @@ class Config {
*/ */
public $parseHeadings; public $parseHeadings;
public $enableConsoleOutput;
public $enableDebugConsoleOutput;
/** /**
* Constructs a new Config object. * Constructs a new Config object.
* *
@ -166,6 +169,8 @@ class Config {
$this->wordEndOnly = $wgLinkTitlesWordEndOnly; $this->wordEndOnly = $wgLinkTitlesWordEndOnly;
$this->skipTemplates = $wgLinkTitlesSkipTemplates; $this->skipTemplates = $wgLinkTitlesSkipTemplates;
$this->parseHeadings = $wgLinkTitlesParseHeadings; $this->parseHeadings = $wgLinkTitlesParseHeadings;
$this->enableConsoleOutput = false;
$this->enableDebugConsoleOutput = false;
} }
} }

View File

@ -1,6 +1,6 @@
<?php <?php
/** /**
* The LinkTitles\Extension class provides entry points for the extension. * The LinkTitles\Extension class provides event handlers and entry points for the extension.
* *
* Copyright 2012-2017 Daniel Kraus <bovender@bovender.de> ('bovender') * Copyright 2012-2017 Daniel Kraus <bovender@bovender.de> ('bovender')
* *
@ -24,7 +24,7 @@
namespace LinkTitles; namespace LinkTitles;
/** /**
* Provides entry points for the extension. * Provides event handlers and entry points for the extension.
*/ */
class Extension { class Extension {
@ -33,7 +33,7 @@ class Extension {
$isMinor, $isWatch, $section, &$flags, &$status ) { $isMinor, $isWatch, $section, &$flags, &$status ) {
global $wgLinkTitlesParseOnEdit; global $wgLinkTitlesParseOnEdit;
global $wgLinkTitlesNamespaces; global $wgLinkTitlesNamespaces;
if ( !$wgLinkTitlesParseOnEdit ) return true; if ( !$wgLinkTitlesParseOnEdit ) return true; // TODO: refactor with following if
if ( !$isMinor ) { if ( !$isMinor ) {
$title = $wikiPage->getTitle(); $title = $wikiPage->getTitle();
@ -43,7 +43,9 @@ class Extension {
if ( in_array( $title->getNamespace(), $wgLinkTitlesNamespaces )) { if ( in_array( $title->getNamespace(), $wgLinkTitlesNamespaces )) {
$text = $content->getContentHandler()->serializeContent( $content ); $text = $content->getContentHandler()->serializeContent( $content );
if ( !\MagicWord::get( 'MAG_LINKTITLES_NOAUTOLINKS' )->match( $text ) ) { if ( !\MagicWord::get( 'MAG_LINKTITLES_NOAUTOLINKS' )->match( $text ) ) {
$newText = Linker::linkContent( $title, $text ); $config = new Config();
$linker = new Linker( $config );
$newText = $linker->linkContent( $title, $text );
if ( $newText != $text ) { if ( $newText != $text ) {
$content = $content->getContentHandler()->unserializeContent( $newText ); $content = $content->getContentHandler()->unserializeContent( $newText );
} }
@ -53,39 +55,43 @@ class Extension {
return true; return true;
} }
/// Event handler that is hooked to the InternalParseBeforeLinks event. /*
/// @param Parser $parser Parser that raised the event. * Event handler that is hooked to the InternalParseBeforeLinks event.
/// @param $text Preprocessed text of the page. * @param Parser $parser Parser that raised the event.
* @param $text Preprocessed text of the page
*/
public static function onInternalParseBeforeLinks( \Parser &$parser, &$text ) { public static function onInternalParseBeforeLinks( \Parser &$parser, &$text ) {
global $wgLinkTitlesParseOnRender; $config = new Config();
if (!$wgLinkTitlesParseOnRender) return true; if (!$config->parseOnRender) return true;
global $wgLinkTitlesNamespaces;
$title = $parser->getTitle(); $title = $parser->getTitle();
// If the page contains the magic word '__NOAUTOLINKS__', do not parse it. // If the page contains the magic word '__NOAUTOLINKS__', do not parse it.
// Only process if page is in one of our namespaces we want to link // Only process if page is in one of our namespaces we want to link
if ( !\MagicWord::get( 'MAG_LINKTITLES_NOAUTOLINKS' )->match( $text ) && if ( !\MagicWord::get( 'MAG_LINKTITLES_NOAUTOLINKS' )->match( $text ) &&
in_array( $title->getNamespace(), $wgLinkTitlesNamespaces ) ) { in_array( $title->getNamespace(), $config->nameSpaces ) ) {
$text = Linker::linkContent( $title, $text ); $linker = new Linker( $config );
$text = $linker->linkContent( $title, $text );
} }
return true; return true;
} }
/// Automatically processes a single page, given a $title Title object. /*
/// This function is called by the SpecialLinkTitles class and the * Automatically processes a single page, given a $title Title object.
/// LinkTitlesJob class. * This function is called by the SpecialLinkTitles class and the
/// @param Title $title Title object. * LinkTitlesJob class.
/// @param RequestContext $context Current request context. * @param Title $title Title object.
/// If in doubt, call MediaWiki's `RequestContext::getMain()` * @param RequestContext $context Current request context. If in doubt, call MediaWiki's `RequestContext::getMain()` to obtain such an object.
/// to obtain such an object. * @returns bool True if the page exists, false if the page does not exist
/// @returns boolean True if the page exists, false if the page does not exist */
public static function processPage( \Title $title, \RequestContext $context ) { public static function processPage( \Title $title, \RequestContext $context ) {
self::ltLog('Processing '. $title->getPrefixedText()); self::ltLog('Processing '. $title->getPrefixedText());
$page = \WikiPage::factory($title); $page = \WikiPage::factory($title);
$content = $page->getContent(); $content = $page->getContent();
if ( $content != null ) { if ( $content != null ) {
$text = $content->getContentHandler()->serializeContent($content); $text = $content->getContentHandler()->serializeContent($content);
$newText = Linker::linkContent($title, $text); $config = new Config();
$linker = new Linker( $config );
$newText = $linker->linkContent($title, $text);
if ( $text != $newText ) { if ( $text != $newText ) {
$content = $content->getContentHandler()->unserializeContent( $newText ); $content = $content->getContentHandler()->unserializeContent( $newText );
$page->doEditContent( $page->doEditContent(
@ -130,7 +136,9 @@ class Extension {
/// by simply returning the text between the tags (if any). /// by simply returning the text between the tags (if any).
/// See https://www.mediawiki.org/wiki/Manual:Tag_extensions#How_do_I_render_wikitext_in_my_extension.3F /// See https://www.mediawiki.org/wiki/Manual:Tag_extensions#How_do_I_render_wikitext_in_my_extension.3F
public static function doAutolinksTag( $input, array $args, \Parser $parser, \PPFrame $frame ) { public static function doAutolinksTag( $input, array $args, \Parser $parser, \PPFrame $frame ) {
$withLinks = Linker::linkContent( $parser->getTitle(), $input ); $config = new Config();
$linker = new Linker( $config );
$withLinks = $linker->linkContent( $parser->getTitle(), $input );
$output = $parser->recursiveTagParse( $withLinks, $frame ); $output = $parser->recursiveTagParse( $withLinks, $frame );
return $output; return $output;
} }

View File

@ -24,56 +24,66 @@
namespace LinkTitles; namespace LinkTitles;
/** /**
* Provides entry points for the extension. * Performs the actual linking of content to existing pages.
*/ */
class Linker { class Linker {
/// A Title object for the page that is being parsed. /// A Title object for the page that is being parsed.
private static $currentTitle; private $currentTitle;
/// A Title object for the target page currently being examined. /// A Title object for the target page currently being examined.
private static $targetTitle; private $targetTitle;
// The TitleValue object of the target page // The TitleValue object of the target page
private static $targetTitleValue; private $targetTitleValue;
/// The content object for the currently processed target page. /// The content object for the currently processed target page.
/// This variable is necessary to be able to prevent loading the target /// This variable is necessary to be able to prevent loading the target
/// content twice. /// content twice.
private static $targetContent; private $targetContent;
/// Holds the page title of the currently processed target page /// Holds the page title of the currently processed target page
/// as a string. /// as a string.
private static $targetTitleText; private $targetTitleText;
public static $ltConsoleOutput; /**
public static $ltConsoleOutputDebug; * LinkTitles configuration.
*
* @var Config $config
*/
public $config;
/// Core function of the extension, performs the actual parsing of the content. /**
/// @param Parser $parser Parser instance for the current page * Constructs a new instance of the Linker class.
/// @param $text String that holds the article content *
/// @returns string: parsed text with links added if needed * @param Config $config LinkTitles configuration object.
public static function linkContent( $title, &$text ) { */
public function __construct( Config &$config ) {
$this->config = $config;
}
// Configuration variables need to be defined here as globals. /*
global $wgLinkTitlesFirstOnly; * Core function of the extension, performs the actual parsing of the content.
global $wgLinkTitlesSmartMode; *
global $wgCapitalLinks; * @param Parser $parser Parser instance for the current page
* @param String $text String that holds the article content
* @returns String String with links to target pages
*/
public function linkContent( \Title &$title, &$text ) {
( $wgLinkTitlesFirstOnly ) ? $limit = 1 : $limit = -1; ( $this->config->firstOnly ) ? $limit = 1 : $limit = -1;
$limitReached = false; $limitReached = false;
self::$currentTitle = $title; $this->currentTitle = $title;
$newText = $text; $newText = $text;
$config = new Config(); $delimiters = Delimiters::default( $this->config );
$delimiters = Delimiters::default( $config ); $targets = Targets::default( $title, $this->config );
$targets = Targets::default( $title, $config );
// Iterate through the page titles // Iterate through the page titles
foreach( $targets->queryResult as $row ) { foreach( $targets->queryResult as $row ) {
self::newTarget( $row->page_namespace, $row->page_title ); $this->newTarget( $row->page_namespace, $row->page_title );
// Don't link current page // Don't link current page
if ( self::$targetTitle->equals( self::$currentTitle ) ) { continue; } if ( $this->targetTitle->equals( $this->currentTitle ) ) { continue; }
// split the page content by [[...]] groups // split the page content by [[...]] groups
// credits to inhan @ StackOverflow for suggesting preg_split // credits to inhan @ StackOverflow for suggesting preg_split
@ -82,17 +92,17 @@ class Linker {
// Escape certain special characters in the page title to prevent // Escape certain special characters in the page title to prevent
// regexp compilation errors // regexp compilation errors
self::$targetTitleText = self::$targetTitle->getText(); $this->targetTitleText = $this->targetTitle->getText();
$quotedTitle = preg_quote( self::$targetTitleText, '/' ); $quotedTitle = preg_quote( $this->targetTitleText, '/' );
self::ltDebugLog( 'TargetTitle='. self::$targetTitleText, 'private' ); $this->ltDebugLog( 'TargetTitle='. $this->targetTitleText, 'private' );
self::ltDebugLog( 'TargetTitleQuoted='. $quotedTitle, 'private' ); $this->ltDebugLog( 'TargetTitleQuoted='. $quotedTitle, 'private' );
// Depending on the global configuration setting $wgCapitalLinks, // Depending on the global configuration setting $wgCapitalLinks,
// the title has to be searched for either in a strictly case-sensitive // the title has to be searched for either in a strictly case-sensitive
// way, or in a 'fuzzy' way where the first letter of the title may // way, or in a 'fuzzy' way where the first letter of the title may
// be either case. // be either case.
if ( $config->capitalLinks && ( $quotedTitle[0] != '\\' )) { if ( $this->config->capitalLinks && ( $quotedTitle[0] != '\\' )) {
$searchTerm = '((?i)' . $quotedTitle[0] . '(?-i)' . $searchTerm = '((?i)' . $quotedTitle[0] . '(?-i)' .
substr($quotedTitle, 1) . ')'; substr($quotedTitle, 1) . ')';
} else { } else {
@ -103,8 +113,9 @@ class Linker {
for ( $i = 0; $i < count( $arr ); $i+=2 ) { for ( $i = 0; $i < count( $arr ); $i+=2 ) {
// even indexes will point to text that is not enclosed by brackets // even indexes will point to text that is not enclosed by brackets
$arr[$i] = preg_replace_callback( $regex, $arr[$i] = preg_replace_callback( $regex,
'LinkTitles\Linker::simpleModeCallback', $arr[$i], $limit, $count ); array( $this, 'simpleModeCallback'),
if ( $config->firstOnly && ( $count > 0 ) ) { $arr[$i], $limit, $count );
if ( $this->config->firstOnly && ( $count > 0 ) ) {
$limitReached = true; $limitReached = true;
break; break;
}; };
@ -114,16 +125,17 @@ class Linker {
// If smart mode is turned on, the extension will perform a second // If smart mode is turned on, the extension will perform a second
// pass on the page and add links with aliases where the case does // pass on the page and add links with aliases where the case does
// not match. // not match.
if ( $config->smartMode && !$limitReached ) { if ( $this->config->smartMode && !$limitReached ) {
$arr = preg_split( $delimiters->splitter, $newText, -1, PREG_SPLIT_DELIM_CAPTURE ); $arr = preg_split( $delimiters->splitter, $newText, -1, PREG_SPLIT_DELIM_CAPTURE );
for ( $i = 0; $i < count( $arr ); $i+=2 ) { for ( $i = 0; $i < count( $arr ); $i+=2 ) {
// even indexes will point to text that is not enclosed by brackets // even indexes will point to text that is not enclosed by brackets
$arr[$i] = preg_replace_callback( '/(?<![\:\.\@\/\?\&])' . $arr[$i] = preg_replace_callback( '/(?<![\:\.\@\/\?\&])' .
$delimiters->wordStart . '(' . $quotedTitle . ')' . $delimiters->wordStart . '(' . $quotedTitle . ')' .
$delimiters->wordEnd . '/iS', 'LinkTitles\Linker::smartModeCallback', $delimiters->wordEnd . '/iS',
array( $this, 'smartModeCallback'),
$arr[$i], $limit, $count ); $arr[$i], $limit, $count );
if ( $config->firstOnly && ( $count > 0 )) { if ( $this->config->firstOnly && ( $count > 0 )) {
break; break;
}; };
}; };
@ -134,9 +146,9 @@ class Linker {
} }
// Build an anonymous callback function to be used in simple mode. // Build an anonymous callback function to be used in simple mode.
private static function simpleModeCallback( array $matches ) { private function simpleModeCallback( array $matches ) {
if ( self::checkTargetPage() ) { if ( $this->checkTargetPage() ) {
self::ltLog( "Linking '$matches[0]' to '" . self::$targetTitle . "'" ); $this->ltLog( "Linking '$matches[0]' to '" . $this->targetTitle . "'" );
return '[[' . $matches[0] . ']]'; return '[[' . $matches[0] . ']]';
} }
else else
@ -152,22 +164,21 @@ class Linker {
// If $wgCapitalLinks is set to true, the case of the first // If $wgCapitalLinks is set to true, the case of the first
// letter is ignored by MediaWiki and we don't need to build a // letter is ignored by MediaWiki and we don't need to build a
// piped link if only the case of the first letter is different. // piped link if only the case of the first letter is different.
private static function smartModeCallback( array $matches ) { private function smartModeCallback( array $matches ) {
global $wgCapitalLinks;
if ( $wgCapitalLinks ) { if ( $this->config->capitalLinks ) {
// With $wgCapitalLinks set to true we have a slightly more // With $wgCapitalLinks set to true we have a slightly more
// complicated version of the callback than if it were false; // complicated version of the callback than if it were false;
// we need to ignore the first letter of the page titles, as // we need to ignore the first letter of the page titles, as
// it does not matter for linking. // it does not matter for linking.
if ( self::checkTargetPage() ) { if ( $this->checkTargetPage() ) {
self::ltLog( "Linking (smart) '$matches[0]' to '" . self::$targetTitle . "'" ); $this->ltLog( "Linking (smart) '$matches[0]' to '" . $this->targetTitle . "'" );
if ( strcmp(substr(self::$targetTitleText, 1), substr($matches[0], 1)) == 0 ) { if ( strcmp(substr($this->targetTitleText, 1), substr($matches[0], 1)) == 0 ) {
// Case-sensitive match: no need to bulid piped link. // Case-sensitive match: no need to bulid piped link.
return '[[' . $matches[0] . ']]'; return '[[' . $matches[0] . ']]';
} else { } else {
// Case-insensitive match: build piped link. // Case-insensitive match: build piped link.
return '[[' . self::$targetTitleText . '|' . $matches[0] . ']]'; return '[[' . $this->targetTitleText . '|' . $matches[0] . ']]';
} }
} }
else else
@ -177,14 +188,14 @@ class Linker {
} else { } else {
// If $wgCapitalLinks is false, we can use the simple variant // If $wgCapitalLinks is false, we can use the simple variant
// of the callback function. // of the callback function.
if ( self::checkTargetPage() ) { if ( $this->checkTargetPage() ) {
self::ltLog( "Linking (smart) '$matches[0]' to '" . self::$targetTitle . "'" ); $this->ltLog( "Linking (smart) '$matches[0]' to '" . $this->targetTitle . "'" );
if ( strcmp(self::$targetTitleText, $matches[0]) == 0 ) { if ( strcmp($this->targetTitleText, $matches[0]) == 0 ) {
// Case-sensitive match: no need to bulid piped link. // Case-sensitive match: no need to bulid piped link.
return '[[' . $matches[0] . ']]'; return '[[' . $matches[0] . ']]';
} else { } else {
// Case-insensitive match: build piped link. // Case-insensitive match: build piped link.
return '[[' . self::$targetTitleText . '|' . $matches[0] . ']]'; return '[[' . $this->targetTitleText . '|' . $matches[0] . ']]';
} }
} }
else else
@ -195,12 +206,12 @@ class Linker {
} }
/// Sets member variables for the current target page. /// Sets member variables for the current target page.
private static function newTarget( $ns, $title ) { private function newTarget( $ns, $title ) {
self::$targetTitle = \Title::makeTitleSafe( $ns, $title ); $this->targetTitle = \Title::makeTitleSafe( $ns, $title );
self::ltDebugLog( 'newtarget='. self::$targetTitle->getText(), "private" ); $this->ltDebugLog( 'newtarget='. $this->targetTitle->getText(), "private" );
self::$targetTitleValue = self::$targetTitle->getTitleValue(); $this->targetTitleValue = $this->targetTitle->getTitleValue();
self::ltDebugLog( 'altTarget='. self::$targetTitleValue->getText(), "private" ); $this->ltDebugLog( 'altTarget='. $this->targetTitleValue->getText(), "private" );
self::$targetContent = null; $this->targetContent = null;
} }
/// Returns the content of the current target page. /// Returns the content of the current target page.
@ -209,12 +220,11 @@ class Linker {
/// database only when needed. /// database only when needed.
/// @note It is absolutely necessary that the newTarget() /// @note It is absolutely necessary that the newTarget()
/// function is called for every new page. /// function is called for every new page.
private static function getTargetContent() { private function getTargetContent() {
if ( ! isset( $targetContent ) ) { if ( ! isset( $targetContent ) ) {
self::$targetContent = \WikiPage::factory( $this->targetContent = \WikiPage::factory( $this->targetTitle )->getContent();
self::$targetTitle)->getContent();
}; };
return self::$targetContent; return $this->targetContent;
} }
/// Examines the current target page. Returns true if it may be linked; /// Examines the current target page. Returns true if it may be linked;
@ -223,7 +233,7 @@ class Linker {
/// and whether the target page is a redirect or contains the /// and whether the target page is a redirect or contains the
/// __NOAUTOLINKTARGET__ magic word. /// __NOAUTOLINKTARGET__ magic word.
/// @returns boolean /// @returns boolean
private static function checkTargetPage() { private function checkTargetPage() {
global $wgLinkTitlesEnableNoTargetMagicWord; global $wgLinkTitlesEnableNoTargetMagicWord;
global $wgLinkTitlesCheckRedirect; global $wgLinkTitlesCheckRedirect;
@ -231,8 +241,8 @@ class Linker {
// indeed redirect to the current page, return the page title as-is // indeed redirect to the current page, return the page title as-is
// (unlinked). // (unlinked).
if ( $wgLinkTitlesCheckRedirect ) { if ( $wgLinkTitlesCheckRedirect ) {
$redirectTitle = self::getTargetContent()->getUltimateRedirectTarget(); $redirectTitle = $this->getTargetContent()->getUltimateRedirectTarget();
if ( $redirectTitle && $redirectTitle->equals(self::$currentTitle) ) { if ( $redirectTitle && $redirectTitle->equals($this->currentTitle) ) {
return false; return false;
} }
}; };
@ -241,7 +251,7 @@ class Linker {
// page does indeed contain this magic word, return the page title // page does indeed contain this magic word, return the page title
// as-is (unlinked). // as-is (unlinked).
if ( $wgLinkTitlesEnableNoTargetMagicWord ) { if ( $wgLinkTitlesEnableNoTargetMagicWord ) {
if ( self::getTargetContent()->matchMagicWord( if ( $this->getTargetContent()->matchMagicWord(
\MagicWord::get('MAG_LINKTITLES_NOTARGET') ) ) { \MagicWord::get('MAG_LINKTITLES_NOTARGET') ) ) {
return false; return false;
} }
@ -250,16 +260,16 @@ class Linker {
} }
/// Local Debugging output function which can send output to console as well /// Local Debugging output function which can send output to console as well
public static function ltDebugLog($text) { public function ltDebugLog($text) {
if ( self::$ltConsoleOutputDebug ) { if ( $this->config->enableDebugConsoleOutput ) {
print $text . "\n"; print $text . "\n";
} }
wfDebugLog( 'LinkTitles', $text , 'private' ); wfDebugLog( 'LinkTitles', $text , 'private' );
} }
/// Local Logging output function which can send output to console as well /// Local Logging output function which can send output to console as well
public static function ltLog($text) { public function ltLog($text) {
if (self::$ltConsoleOutput) { if ( $this->config->enableConsoleOutput) {
print $text . "\n"; print $text . "\n";
} }
wfDebugLog( 'LinkTitles', $text , 'private' ); wfDebugLog( 'LinkTitles', $text , 'private' );