ExternalUserNames.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Class to parse and build external user names
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. use MediaWiki\MediaWikiServices;
  23. /**
  24. * Class to parse and build external user names
  25. * @since 1.31
  26. */
  27. class ExternalUserNames {
  28. /**
  29. * @var string
  30. */
  31. private $usernamePrefix = 'imported';
  32. /**
  33. * @var bool
  34. */
  35. private $assignKnownUsers = false;
  36. /**
  37. * @var bool[]
  38. */
  39. private $triedCreations = [];
  40. /**
  41. * @param string $usernamePrefix Prefix to apply to unknown (and possibly also known) usernames
  42. * @param bool $assignKnownUsers Whether to apply the prefix to usernames that exist locally
  43. */
  44. public function __construct( $usernamePrefix, $assignKnownUsers ) {
  45. $this->usernamePrefix = rtrim( (string)$usernamePrefix, ':>' );
  46. $this->assignKnownUsers = (bool)$assignKnownUsers;
  47. }
  48. /**
  49. * Get a target Title to link a username.
  50. *
  51. * @param string $userName Username to link
  52. *
  53. * @return null|Title
  54. */
  55. public static function getUserLinkTitle( $userName ) {
  56. $pos = strpos( $userName, '>' );
  57. if ( $pos !== false ) {
  58. $iw = explode( ':', substr( $userName, 0, $pos ) );
  59. $firstIw = array_shift( $iw );
  60. $services = MediaWikiServices::getInstance();
  61. $interwikiLookup = $services->getInterwikiLookup();
  62. if ( $interwikiLookup->isValidInterwiki( $firstIw ) ) {
  63. $title = $services->getNamespaceInfo()->getCanonicalName( NS_USER ) .
  64. ':' . substr( $userName, $pos + 1 );
  65. if ( $iw ) {
  66. $title = implode( ':', $iw ) . ':' . $title;
  67. }
  68. return Title::makeTitle( NS_MAIN, $title, '', $firstIw );
  69. }
  70. return null;
  71. } else {
  72. return SpecialPage::getTitleFor( 'Contributions', $userName );
  73. }
  74. }
  75. /**
  76. * Add an interwiki prefix to the username, if appropriate
  77. *
  78. * This method does have a side-effect on SUL (single user login) wikis: Calling this calls the
  79. * ImportHandleUnknownUser hook from the CentralAuth extension, which assigns a local ID to the
  80. * global user name, if possible. No prefix is applied if this is successful.
  81. *
  82. * @see https://meta.wikimedia.org/wiki/Help:Unified_login
  83. * @see https://www.mediawiki.org/wiki/Manual:Hooks/ImportHandleUnknownUser
  84. *
  85. * @param string $name
  86. * @return string Either the unchanged username if it's a known local user (or not a valid
  87. * username), otherwise the name with the prefix prepended.
  88. */
  89. public function applyPrefix( $name ) {
  90. if ( !User::isUsableName( $name ) ) {
  91. return $name;
  92. }
  93. if ( $this->assignKnownUsers ) {
  94. if ( User::idFromName( $name ) ) {
  95. return $name;
  96. }
  97. // See if any extension wants to create it.
  98. if ( !isset( $this->triedCreations[$name] ) ) {
  99. $this->triedCreations[$name] = true;
  100. if ( !Hooks::run( 'ImportHandleUnknownUser', [ $name ] ) &&
  101. User::idFromName( $name, User::READ_LATEST )
  102. ) {
  103. return $name;
  104. }
  105. }
  106. }
  107. return $this->addPrefix( $name );
  108. }
  109. /**
  110. * Add an interwiki prefix to the username regardless of circumstances
  111. *
  112. * @param string $name
  113. * @return string Prefixed username, using ">" as separator
  114. */
  115. public function addPrefix( $name ) {
  116. return substr( $this->usernamePrefix . '>' . $name, 0, 255 );
  117. }
  118. /**
  119. * Tells whether the username is external or not
  120. *
  121. * @param string $username Username to check
  122. * @return bool true if it's external, false otherwise.
  123. */
  124. public static function isExternal( $username ) {
  125. return strpos( $username, '>' ) !== false;
  126. }
  127. /**
  128. * Get local part of the user name
  129. *
  130. * @param string $username Username to get
  131. * @return string
  132. */
  133. public static function getLocal( $username ) {
  134. if ( !self::isExternal( $username ) ) {
  135. return $username;
  136. }
  137. return substr( $username, strpos( $username, '>' ) + 1 );
  138. }
  139. }