diff --git "a/class-mw\\Wikitext.php" "b/class-mw\\Wikitext.php" index 92fbc32..e376182 100644 --- "a/class-mw\\Wikitext.php" +++ "b/class-mw\\Wikitext.php" @@ -1,114 +1,152 @@ . # MediaWiki namespace mw; class Wikitext { /** * MediaWiki site */ private $site; /** * Plain text wikitext * @var string */ private $wikitext; + private $appended = ''; + private $prepended = ''; + private $sobstitutions = []; + public function __construct( $site, $wikitext ) { $this->setWikitext( $wikitext ); $this->setSite( $site ); } public function getSite() { return $this->site; } public function getWikitext() { return $this->wikitext; } public function setSite( $site ) { $this->site = $site; return $this; } public function setWikitext( $wikitext ) { $this->wikitext = $wikitext; return $this; } public function appendWikitext( $wikitext ) { - $this->wikitext = $this->wikitext . $wikitext; + $this->appended .= $wikitext; + $this->setWikitext( $this->getWikitext() . $wikitext ); return $this; } public function prependWikitext( $wikitext ) { - $this->wikitext = $wikitext . $this->wikitext; + $this->prepended .= $wikitext; + $this->setWikitext( $wikitext . $this->getWikitext() ); return $this; } public function pregMatch( $pattern, & $matches = [], $flags = 0, $offset = 0 ) { return preg_match( $pattern, $this->getWikitext(), $matches, $flags, $offset ); } public function pregReplace( $pattern, $replacement, $limit = -1, &$count = 0 ) { + if( 1 === $this->pregMatch( $pattern, $matches ) ) { + $from = array_shift( $matches ); + $to = $replacement; + foreach( $matches as $i => $match ) { + $i++; + $to = str_replace( [ + "\\$i", + '${' . $i . '}' + ], $match, $to ); + } + $this->sobstitutions[] = [ $from, $to ]; + } $this->setWikitext( preg_replace( $pattern, $replacement, $this->getWikitext(), $limit, $count ) ); return $this; } + public function strReplace( $a, $b, & $count = 0 ) { + $this->setWikitext( str_replace( $a, $b, $this->getWikitext(), $count ) ); + for( $i = 0; $i < $count; $i++ ) { + $this->sobstitutions[] = [ $a, $b ]; + } + return $this; + } + public function hasCategory( $category_name ) { $category_name_regex = Title::factory( $category_name )->getRegexFirstCaseInsensitive(); $category_name_regex = \regex\Generic::spaceBurger( $category_name_regex ); $category_ns_titleparts = $this->getSite()->getNamespace( 14 )->getAllTitlePartsCapitalized(); $category_ns_regexes = []; foreach( $category_ns_titleparts as $category_ns_titlepart ) { $category_ns_regexes[] = $category_ns_titlepart->getRegexFirstCaseInsensitive(); } $whatever_sortkey = '(\|.*?)?'; foreach( $category_ns_regexes as $category_ns_regex ) { $category_ns_regex = \regex\Generic::spaceBurger( $category_ns_regex ); $pattern = sprintf( '/\[\[%s\]\]/', - \regex\Generic::spaceBurger( $category_ns_regex . ':' . $category_name_regex . $whatever_sortkey ) + $category_ns_regex . ':' . $category_name_regex . $whatever_sortkey ); if( 1 === $this->pregMatch( $pattern ) ) { return true; } } return false; } public function addCategory( $category_name, $sortkey = null ) { if( $this->hasCategory( $category_name ) ) { return false; } $category_namespace = $this->getSite()->getNamespace( 14 )->getName(); $this->appendWikitext( sprintf( "\n[[%s:%s%s]]", $category_namespace, $category_name, $sortkey ? "|$sortkey" : '' ) ); return true; } + + public function getPrepended() { + return $this->prepended; + } + + public function getAppended() { + return $this->appended; + } + + public function getSobstitutions() { + return $this->sobstitutions; + } }