diff --git "a/include/class-mw\\Wikilink.php" "b/include/class-mw\\Wikilink.php" index 47d9eb6..102feba 100644 --- "a/include/class-mw\\Wikilink.php" +++ "b/include/class-mw\\Wikilink.php" @@ -1,181 +1,221 @@ . # MediaWiki namespace mw; /** * A MediaWiki wikilink * * Something like [[Wikipedia:Contatti|contatti]]. */ class Wikilink { /** * A valid alias value */ const WHATEVER_ALIAS = null; /** * A valid alias value */ const NO_ALIAS = false; /** * MediaWiki title * * When falsy, it means that can be whatever * * @var object|null */ private $title; /** * Displayed alias * * When NULL, it means that can be whatever, * When false, it means that there is no one. * * @var object */ private $alias; /** * Constructor * * @param $title object * @param $alias string|false|null (NULL: whatever, false: no one) */ public function __construct( CompleteTitle $title, $alias = null ) { $this->setTitle( $title ) ->setAlias( $alias ); } /** * Set the title (link) * * @param $title object * @return self */ public function setTitle( CompleteTitle $title ) { $this->title = $title; return $this; } /** * Set the alias * * @param $alias string|null|false (NULL: whatever, false: no one * @return self */ public function setAlias( $alias ) { $this->alias = $alias; return $this; } /** * Get a regex matching this wikilink's title * * If there is no title, match a generic one. * + * @param $args array Arguments + * 'wikilink' boolean If false, a category will categorize (default true) * @return string */ - public function getRegexTitle() { + public function getRegexTitle( $args = [] ) { return $this->title - ? $this->title->getRegex() + ? $this->title->getRegex( $args ) : '[' . self::legalTitleCharset() . ']*'; } /** * Get a regex matching this wikilink's alias * * If there is no alias, match a generic one. * * @return string */ public function getRegexAlias() { // match the alias exactly if present if( $this->alias ) { return preg_quote( $this->alias ); } // match whatever alias otherwise (non-greedy) return '.*?'; } + /** + * Get the wikitext that will point to this wikilink + * + * @param $args array + * @return string + */ + public function getWikitext( $args = [] ) { + + // default arguments + $args = array_replace( [ + 'wikilink' => true, + ], $args ); + + $completetitle = $this->title; + $ns = $completetitle->getNs(); + $ns_name = $ns->getName(); + $title = $completetitle->getTitle()->get(); + + // categories must me prefixed with ':' if you want a wikilink + $prefix = ''; + if( $ns->getID() === 14 && $args[ 'wikilink' ] ) { + $prefix = ':'; + } + + // the alias is the piped text + $alias = ''; + if( strlen( $this->alias ) > 1 ) { + $alias = "|$this->alias"; + } + + return "[[$prefix$ns_name:$title$alias]]"; + } + /** * Get a regex matching this wikilink * * @param $args array Arguments to be specified - * 'title-group-name': If specified, the title will be captured in a group with this name - * 'alias-group-name': If specified, the alias will be captured in a group with this name + * 'title-group-name' string If specified, the title will be captured in a group with this name + * 'alias-group-name' string If specified, the alias will be captured in a group with this name + * 'wikilink': bool If false, a category will categorize (default true) */ public function getRegex( $args = [] ) { // default options $args = array_replace( [ + 'wikilink' => true, 'title-group-name' => null, 'alias-group-name' => null, ], $args ); // regex matching the title - $title_regex = \regex\Generic::groupNamed( $this->getRegexTitle(), $args[ 'title-group-name' ] ); + $title_regex = $this->getRegexTitle( [ + 'wikilink' => $args[ 'wikilink' ], + ] ); + $title_regex = \regex\Generic::groupNamed( $title_regex, $args[ 'title-group-name' ] ); // regex matching the alias (if any) $alias_regex = false; if( $this->alias !== self::NO_ALIAS ) { $alias_regex = \regex\Generic::groupNamed( $this->getRegexAlias(), $args[ 'alias-group-name' ] ); // the alias part, when it's a captch-all, is optional if( $this->alias === self::WHATEVER_ALIAS ) { // note, do not try to create an atomic group, because atomic groups do not backreference past $alias_regex = "([ _]*\|$alias_regex)?"; } } // complete regex $regex = $title_regex; if( $alias_regex ) { $regex .= $alias_regex; } // surround with spaces $regex = \regex\Generic::spaceBurger( $regex ); // surround with brackets return "\[\[$regex\]\]"; } /** * Legal characters for a title * * @see https://www.mediawiki.org/wiki/Manual:$wgLegalTitleChars */ public static function legalTitleCharset() { return ' %!\"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+'; } /** * Legal characters for an alias * * @see https://www.mediawiki.org/wiki/Manual:$wgLegalTitleChars */ public static function legalAliasCharset() { return self::legalTitleCharset() . '#<>\[\]{}\n\t'; } } diff --git "a/include/class-mw\\Wikitext.php" "b/include/class-mw\\Wikitext.php" index fc77f16..a8ca3fd 100644 --- "a/include/class-mw\\Wikitext.php" +++ "b/include/class-mw\\Wikitext.php" @@ -1,386 +1,378 @@ . # MediaWiki namespace mw; /** * A MediaWiki wikitext */ class Wikitext { /** * MediaWiki site * * @var mw\Site */ private $site; /** * Complete plain text wikitext * * @var string */ private $wikitext; /** * String appended to the wikitext * * @var string */ private $appended = ''; /** * String prepended to the wikitext * * @var string */ private $prepended = ''; /** * Strings sobstituted from the wikitext * * @var array [ [ 'a', 'b' ], [ 'b', 'c' ] ] */ private $sobstitutions = []; /** * Constructor * * @param $site mw\Site MediaWiki site * @param $wikitext string Wikitext */ public function __construct( $site, $wikitext ) { $this->setWikitext( $wikitext ); $this->setSite( $site ); } /** * Get the MediaWiki site * * @return mw\Site */ public function getSite() { return $this->site; } /** * Get the complete wikitext * * @return string */ public function getWikitext() { return $this->wikitext; } /** * Set the MediaWiki site * * @return self */ public function setSite( $site ) { $this->site = $site; return $this; } /** * Set the wikitext * * @param $wikitext string * @return self */ public function setWikitext( $wikitext ) { $this->wikitext = $wikitext; return $this; } /** * Append some wikitext * * @param $wikitext string * @return self */ public function appendWikitext( $wikitext ) { $this->appended .= $wikitext; $this->setWikitext( $this->getWikitext() . $wikitext ); return $this; } /** * Prepend some wikitext * * @param $wikitext string * @return self */ public function prependWikitext( $wikitext ) { $this->prepended .= $wikitext; $this->setWikitext( $wikitext . $this->getWikitext() ); return $this; } /** * Run a preg_match() on the wikitext * * @param $pattern string Pattern * @param $matches array Matches * @param $flags int * @param $offset int * @see preg_match() */ public function pregMatch( $pattern, & $matches = [], $flags = 0, $offset = 0 ) { return preg_match( $pattern, $this->getWikitext(), $matches, $flags, $offset ); } /** * Run a preg_match_all() on the wikitext * * @param $pattern string Pattern * @param $matches array Matches * @param $flags int * @param $offset int * @see preg_match_all() * @return int|false */ public function pregMatchAll( $pattern, & $matches = [], $flags = 0, $offset = 0 ) { return preg_match_all( $pattern, $this->getWikitext(), $matches, $flags, $offset ); } /** * Run a preg_replace() on the wikitext * * @param $patterns string|array Pattern * @param $replacement mixed Replacement where you can use group placeholders such as $0, $1 or ${0}, ${1}, ecc. * Note that it does NOT support named parameter. * @param $limit int Sobstitution limit * @param $count int Sobstitution count * @see preg_replace() */ public function pregReplace( $patterns, $replacement, $limit = -1, &$count = 0 ) { // note that preg_replace() supports $pattern as array, but pregMatchAll() requires a string, so let's loop if( ! is_array( $patterns ) ) { $patterns = [ $patterns ]; } foreach( $patterns as $pattern ) { $n = $this->pregMatchAll( $pattern, $matches ); if( $n ) { $groups = count( $matches ); for( $i = 0; $i < $n; $i++ ) { $from = $matches[ 0 ][ $i ]; $to = $replacement; foreach( $matches as $group_name => $groups ) { $to = str_replace( [ '\\' . $group_name, // \\1 '$' . $group_name, // $1 '${' . $group_name . '}' // ${1} ], $groups[ $i ], $to ); } $this->sobstitutions[] = [ $from, $to ]; } } } if( $n ) { $this->setWikitext( preg_replace( $patterns, $replacement, $this->getWikitext(), $limit, $count ) ); } return $this; } /** * Run a preg_replace_callback() on the wikitext * * @param $patterns string|array Pattern * @param $callback callable * @param $limit int Sobstitution limit * @param $count int Sobstitution count * @see preg_replace() */ public function pregReplaceCallback( $patterns, $callback, $limit = -1, &$count = 0 ) { // note that preg_replace_callback() supports $pattern as array, but pregMatchAll() requires a string, so let's loop if( ! is_array( $patterns ) ) { $patterns = [ $patterns ]; } foreach( $patterns as $pattern ) { $n = $this->pregMatchAll( $pattern, $matches ); if( $n ) { for( $i = 0; $i < $n; $i++ ) { $groups = []; foreach( $matches as $key => $_ ) { $groups[ $key ] = $matches[ $key ][ $i ]; } $from = $matches[ 0 ][ $i ]; $to = $callback( $groups ); $this->sobstitutions[] = [ $from, $to ]; } } } if( $n ) { $this->setWikitext( preg_replace_callback( $patterns, $callback, $this->getWikitext(), $limit, $count ) ); } return $this; } /** * Search and replace all the occurrences of a string from the wikitext * * @param $a mixed Search * @param $b mixed Replace * @param $count int Sobstitution count * @return self */ 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; } /** * It has a Category? (it checks only the wikitext) * * @param $category_name string Category without namespace prefix * @return bool */ 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(); - } + // category with whatever sortkey + $category = $this->getSite() + ->createTitle( $category_name, 14 ) + ->createWikilink( Wikilink::WHATEVER_ALIAS ); - $whatever_sortkey = '(\|.*?)?'; - foreach( $category_ns_regexes as $category_ns_regex ) { - $category_ns_regex = \regex\Generic::spaceBurger( $category_ns_regex ); - $pattern = sprintf( - '/\[\[%s\]\]/', - $category_ns_regex . ':' . $category_name_regex . $whatever_sortkey - ); - if( 1 === $this->pregMatch( $pattern ) ) { - return true; - } - } + // category wiki syntax + $category_regex = $category->getRegex( [ + 'wikilink' => false, + ] ); - return false; + return 1 === $this->pregMatch( "/$category_regex/" ); } /** * Add a Category * * @param $category_name string Category name without category prefix - * @param $sortkey string + * @param $sortkey mixed * @return string */ - public function addCategory( $category_name, $sortkey = null ) { + public function addCategory( $category_name, $sortkey = false ) { 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" : '' - ) ); + + $category = $this->getSite() + ->createTitle( $category_name, 14 ) + ->createWikilink( $sortkey ); + + // [[Category:asd|sortkey]] + $category_wlink = $category->getWikitext( [ 'wikilink' => false ] ); + + $this->appendWikitext( "\n$category_wlink" ); return true; } /** * Get the prepended wikitext * * @return string */ public function getPrepended() { return $this->prepended; } /** * Get the appended wikitext * * @return string */ public function getAppended() { return $this->appended; } /** * Get the sobstituted wikitext * * @return array [ [ 'a', 'b' ], [ 'b', 'c' ] ] */ public function getSobstitutions() { return $this->sobstitutions; } /** * Count the sobstitutions in the wikitext * * @return int */ public function countSobstitutions() { return count( $this->getSobstitutions() ); } /** * Check if there were changes since creation * * @return bool */ public function isChanged() { return $this->countSobstitutions() || $this->prepended || $this->appended; } /** * Get the sobstituted wikitext without repetitions * * It also count the repetitions * * @return array [ [ 'a', 'b', 1 ], [ 'b', 'c', 2 ] ] */ public function getUniqueSobstitutions() { $seen = []; foreach( $this->getSobstitutions() as $sobstitution ) { list( $a, $b ) = $sobstitution; @$seen[ $a ][ $b ]++; } $sobstitutions = []; foreach( $seen as $a => $_ ) { foreach( $_ as $b => $n ) { $sobstitutions[] = [ $a, $b, $n ]; } } return $sobstitutions; } /** * Get a boring array of human sobstitution messages * * @return array */ public function getHumanUniqueSobstitutions() { $things = []; foreach( $this->getUniqueSobstitutions() as $abn ) { list( $a, $b, $n ) = $abn; $many = 1 === $n ? "1 time" : sprintf( "$n times", $n ); $things[] = sprintf( "%s → %s (%s)", $a, $b, $many ); } return $things; } }