RightsLogFormatter.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * Formatter for user rights log entries.
  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. * @author Alexandre Emsenhuber
  22. * @license GPL-2.0-or-later
  23. * @since 1.22
  24. */
  25. use MediaWiki\MediaWikiServices;
  26. /**
  27. * This class formats rights log entries.
  28. *
  29. * @since 1.21
  30. */
  31. class RightsLogFormatter extends LogFormatter {
  32. protected function makePageLink( Title $title = null, $parameters = [], $html = null ) {
  33. global $wgUserrightsInterwikiDelimiter;
  34. if ( !$this->plaintext ) {
  35. $text = MediaWikiServices::getInstance()->getContentLanguage()->
  36. ucfirst( $title->getDBkey() );
  37. $parts = explode( $wgUserrightsInterwikiDelimiter, $text, 2 );
  38. if ( count( $parts ) === 2 ) {
  39. $titleLink = WikiMap::foreignUserLink(
  40. $parts[1],
  41. $parts[0],
  42. htmlspecialchars(
  43. strtr( $parts[0], '_', ' ' ) .
  44. $wgUserrightsInterwikiDelimiter .
  45. $parts[1]
  46. )
  47. );
  48. if ( $titleLink !== false ) {
  49. return $titleLink;
  50. }
  51. }
  52. }
  53. return parent::makePageLink( $title, $parameters, $title ? $title->getText() : null );
  54. }
  55. protected function getMessageKey() {
  56. $key = parent::getMessageKey();
  57. $params = $this->getMessageParameters();
  58. if ( !isset( $params[3] ) && !isset( $params[4] ) ) {
  59. // Messages: logentry-rights-rights-legacy
  60. $key .= '-legacy';
  61. }
  62. return $key;
  63. }
  64. protected function getMessageParameters() {
  65. $params = parent::getMessageParameters();
  66. // Really old entries that lack old/new groups
  67. if ( !isset( $params[3] ) && !isset( $params[4] ) ) {
  68. return $params;
  69. }
  70. $oldGroups = $this->makeGroupArray( $params[3] );
  71. $newGroups = $this->makeGroupArray( $params[4] );
  72. $userName = $this->entry->getTarget()->getText();
  73. if ( !$this->plaintext && count( $oldGroups ) ) {
  74. foreach ( $oldGroups as &$group ) {
  75. $group = UserGroupMembership::getGroupMemberName( $group, $userName );
  76. }
  77. }
  78. if ( !$this->plaintext && count( $newGroups ) ) {
  79. foreach ( $newGroups as &$group ) {
  80. $group = UserGroupMembership::getGroupMemberName( $group, $userName );
  81. }
  82. }
  83. // fetch the metadata about each group membership
  84. $allParams = $this->entry->getParameters();
  85. if ( count( $oldGroups ) ) {
  86. $params[3] = [ 'raw' => $this->formatRightsList( $oldGroups,
  87. $allParams['oldmetadata'] ?? [] ) ];
  88. } else {
  89. $params[3] = $this->msg( 'rightsnone' )->text();
  90. }
  91. if ( count( $newGroups ) ) {
  92. // Array_values is used here because of T44211
  93. // see use of array_unique in UserrightsPage::doSaveUserGroups on $newGroups.
  94. $params[4] = [ 'raw' => $this->formatRightsList( array_values( $newGroups ),
  95. $allParams['newmetadata'] ?? [] ) ];
  96. } else {
  97. $params[4] = $this->msg( 'rightsnone' )->text();
  98. }
  99. $params[5] = $userName;
  100. return $params;
  101. }
  102. protected function formatRightsList( $groups, $serializedUGMs = [] ) {
  103. $uiLanguage = $this->context->getLanguage();
  104. $uiUser = $this->context->getUser();
  105. // separate arrays of temporary and permanent memberships
  106. $tempList = $permList = [];
  107. reset( $groups );
  108. reset( $serializedUGMs );
  109. while ( current( $groups ) ) {
  110. $group = current( $groups );
  111. if ( current( $serializedUGMs ) &&
  112. isset( current( $serializedUGMs )['expiry'] ) &&
  113. current( $serializedUGMs )['expiry']
  114. ) {
  115. // there is an expiry date; format the group and expiry into a friendly string
  116. $expiry = current( $serializedUGMs )['expiry'];
  117. $expiryFormatted = $uiLanguage->userTimeAndDate( $expiry, $uiUser );
  118. $expiryFormattedD = $uiLanguage->userDate( $expiry, $uiUser );
  119. $expiryFormattedT = $uiLanguage->userTime( $expiry, $uiUser );
  120. $tempList[] = $this->msg( 'rightslogentry-temporary-group' )->params( $group,
  121. $expiryFormatted, $expiryFormattedD, $expiryFormattedT )->parse();
  122. } else {
  123. // the right does not expire; just insert the group name
  124. $permList[] = $group;
  125. }
  126. next( $groups );
  127. next( $serializedUGMs );
  128. }
  129. // place all temporary memberships first, to avoid the ambiguity of
  130. // "adinistrator, bureaucrat and importer (temporary, until X time)"
  131. return $uiLanguage->listToText( array_merge( $tempList, $permList ) );
  132. }
  133. protected function getParametersForApi() {
  134. $entry = $this->entry;
  135. $params = $entry->getParameters();
  136. static $map = [
  137. '4:array:oldgroups',
  138. '5:array:newgroups',
  139. '4::oldgroups' => '4:array:oldgroups',
  140. '5::newgroups' => '5:array:newgroups',
  141. ];
  142. foreach ( $map as $index => $key ) {
  143. if ( isset( $params[$index] ) ) {
  144. $params[$key] = $params[$index];
  145. unset( $params[$index] );
  146. }
  147. }
  148. // Really old entries do not have log params, so form them from whatever info
  149. // we have.
  150. // Also walk through the parallel arrays of groups and metadata, combining each
  151. // metadata array with the name of the group it pertains to
  152. if ( isset( $params['4:array:oldgroups'] ) ) {
  153. $params['4:array:oldgroups'] = $this->makeGroupArray( $params['4:array:oldgroups'] );
  154. $oldmetadata =& $params['oldmetadata'];
  155. // unset old metadata entry to ensure metadata goes at the end of the params array
  156. unset( $params['oldmetadata'] );
  157. $params['oldmetadata'] = array_map( function ( $index ) use ( $params, $oldmetadata ) {
  158. $result = [ 'group' => $params['4:array:oldgroups'][$index] ];
  159. if ( isset( $oldmetadata[$index] ) ) {
  160. $result += $oldmetadata[$index];
  161. }
  162. $result['expiry'] = ApiResult::formatExpiry( $result['expiry'] ?? null );
  163. return $result;
  164. }, array_keys( $params['4:array:oldgroups'] ) );
  165. }
  166. if ( isset( $params['5:array:newgroups'] ) ) {
  167. $params['5:array:newgroups'] = $this->makeGroupArray( $params['5:array:newgroups'] );
  168. $newmetadata =& $params['newmetadata'];
  169. // unset old metadata entry to ensure metadata goes at the end of the params array
  170. unset( $params['newmetadata'] );
  171. $params['newmetadata'] = array_map( function ( $index ) use ( $params, $newmetadata ) {
  172. $result = [ 'group' => $params['5:array:newgroups'][$index] ];
  173. if ( isset( $newmetadata[$index] ) ) {
  174. $result += $newmetadata[$index];
  175. }
  176. $result['expiry'] = ApiResult::formatExpiry( $result['expiry'] ?? null );
  177. return $result;
  178. }, array_keys( $params['5:array:newgroups'] ) );
  179. }
  180. return $params;
  181. }
  182. public function formatParametersForApi() {
  183. $ret = parent::formatParametersForApi();
  184. if ( isset( $ret['oldgroups'] ) ) {
  185. ApiResult::setIndexedTagName( $ret['oldgroups'], 'g' );
  186. }
  187. if ( isset( $ret['newgroups'] ) ) {
  188. ApiResult::setIndexedTagName( $ret['newgroups'], 'g' );
  189. }
  190. if ( isset( $ret['oldmetadata'] ) ) {
  191. ApiResult::setArrayType( $ret['oldmetadata'], 'array' );
  192. ApiResult::setIndexedTagName( $ret['oldmetadata'], 'g' );
  193. }
  194. if ( isset( $ret['newmetadata'] ) ) {
  195. ApiResult::setArrayType( $ret['newmetadata'], 'array' );
  196. ApiResult::setIndexedTagName( $ret['newmetadata'], 'g' );
  197. }
  198. return $ret;
  199. }
  200. private function makeGroupArray( $group ) {
  201. // Migrate old group params from string to array
  202. if ( $group === '' ) {
  203. $group = [];
  204. } elseif ( is_string( $group ) ) {
  205. $group = array_map( 'trim', explode( ',', $group ) );
  206. }
  207. return $group;
  208. }
  209. }