diff --git "a/include/class-mw\\Wikilink.php" "b/include/class-mw\\Wikilink.php" index 0a56347..0579dda 100644 --- "a/include/class-mw\\Wikilink.php" +++ "b/include/class-mw\\Wikilink.php" @@ -1,174 +1,178 @@ . # 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; - /** - * Legal characters for a title - * - * @see https://www.mediawiki.org/wiki/Manual:$wgLegalTitleChars - */ - const LEGAL_TITLE_CHARS = ' %!\"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+'; - - /** - * Legal characters for an alias - * - * @see https://www.mediawiki.org/wiki/Manual:$wgLegalTitleChars - */ - const LEGAL_ALIAS_CHARS = self::LEGAL_TITLE_CHARS . '#<>\[\]{}\n\t'; - /** * 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. * * @return string */ public function getRegexTitle() { return $this->title ? $this->title->getRegex() - : '[' . self::LEGAL_TITLE_CHARS . ']*'; + : '[' . 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, because of it can contains ']') - return '[' . self::LEGAL_ALIAS_CHARS . ']*?'; + return '[' . self::legalAliasCharset() . ']*?'; } /** * 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 */ public function getRegex( $args = [] ) { // default options $args = array_replace( [ 'title-group-name' => null, 'alias-group-name' => null, ], $args ); // regex matching the title $title_regex = \regex\Generic::groupNamed( $this->getRegexTitle(), $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 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'; + } + }