wiki.techinc.nl/includes/user/TempUser/Pattern.php
Tim Starling e8dbf5f80c TempUser infrastructure and services
Add services and utilities for automatic creation of temporary user
accounts on page save, in order to avoid exposing the user's IP
address.

* Add $wgAutoCreateTempUser, for configuring the system
* Add TempUserConfig service, which interprets the config.
* Add TempUserCreator service, which creates users during page save as
  requested by EditPage. With proxy methods to TempUserConfig for
  convenience.
* Add table user_autocreate_serial. Table creation is necessary before
  the feature is enabled but is not necessary before deployment of this
  commit.

Bug: T300263
Change-Id: Ib14a352490fc42039106523118e8d021844e3dfb
2022-04-14 09:23:55 +10:00

73 lines
1.7 KiB
PHP

<?php
namespace MediaWiki\User\TempUser;
/**
* Helper for TempUserConfig representing string patterns with "$1" indicating
* variable substitution.
*
* @internal
*/
class Pattern {
/** @var string */
private $debugName;
/** @var string */
private $pattern;
/** @var string */
private $prefix;
/** @var string */
private $suffix;
/**
* @param string $debugName The name of the pattern, for use in error messages
* @param string $pattern The pattern itself
*/
public function __construct( string $debugName, string $pattern ) {
$this->debugName = $debugName;
$this->pattern = $pattern;
}
/**
* Does the pattern match the given name?
* @param string $name
* @return bool
*/
public function isMatch( string $name ) {
$this->init();
$match = true;
if ( $this->prefix !== '' ) {
$match = str_starts_with( $name, $this->prefix );
}
if ( $match && $this->suffix !== '' ) {
$match = str_ends_with( $name, $this->suffix )
&& strlen( $name ) >= strlen( $this->prefix ) + strlen( $this->suffix );
}
return $match;
}
/**
* Substitute the serial number into the pattern.
*
* @param string $mappedSerial
* @return string
*/
public function generate( $mappedSerial ) {
$this->init();
return $this->prefix . $mappedSerial . $this->suffix;
}
/**
* Initialise the prefix and suffix
*/
private function init() {
if ( $this->prefix === null ) {
$varPos = strpos( $this->pattern, '$1' );
if ( $varPos === false ) {
throw new \MWException( __CLASS__ .
"pattern {$this->debugName} must be of the form \"prefix \$1 suffix\"" );
}
$this->prefix = substr( $this->pattern, 0, $varPos );
$this->suffix = substr( $this->pattern, $varPos + strlen( '$1' ) );
}
}
}