BlockLogFormatter.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Formatter for block 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. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
  22. * @since 1.25
  23. */
  24. use MediaWiki\MediaWikiServices;
  25. /**
  26. * This class formats block log entries.
  27. *
  28. * @since 1.25
  29. */
  30. class BlockLogFormatter extends LogFormatter {
  31. protected function getMessageParameters() {
  32. $params = parent::getMessageParameters();
  33. $title = $this->entry->getTarget();
  34. if ( substr( $title->getText(), 0, 1 ) === '#' ) {
  35. // autoblock - no user link possible
  36. $params[2] = $title->getText();
  37. $params[3] = ''; // no user name for gender use
  38. } else {
  39. // Create a user link for the blocked
  40. $username = $title->getText();
  41. // @todo Store the user identifier in the parameters
  42. // to make this faster for future log entries
  43. $targetUser = User::newFromName( $username, false );
  44. $params[2] = Message::rawParam( $this->makeUserLink( $targetUser, Linker::TOOL_LINKS_NOBLOCK ) );
  45. $params[3] = $username; // plain user name for gender use
  46. }
  47. $subtype = $this->entry->getSubtype();
  48. if ( $subtype === 'block' || $subtype === 'reblock' ) {
  49. if ( !isset( $params[4] ) ) {
  50. // Very old log entry without duration: means infinite
  51. $params[4] = 'infinite';
  52. }
  53. // Localize the duration, and add a tooltip
  54. // in English to help visitors from other wikis.
  55. // The lrm is needed to make sure that the number
  56. // is shown on the correct side of the tooltip text.
  57. $durationTooltip = '&lrm;' . htmlspecialchars( $params[4] );
  58. $params[4] = Message::rawParam(
  59. "<span class=\"blockExpiry\" title=\"$durationTooltip\">" .
  60. $this->context->getLanguage()->translateBlockExpiry(
  61. $params[4],
  62. $this->context->getUser(),
  63. wfTimestamp( TS_UNIX, $this->entry->getTimestamp() )
  64. ) .
  65. '</span>'
  66. );
  67. $params[5] = isset( $params[5] ) ?
  68. self::formatBlockFlags( $params[5], $this->context->getLanguage() ) : '';
  69. }
  70. return $params;
  71. }
  72. protected function extractParameters() {
  73. $params = parent::extractParameters();
  74. // Legacy log params returning the params in index 3 and 4, moved to 4 and 5
  75. if ( $this->entry->isLegacy() && isset( $params[3] ) ) {
  76. if ( isset( $params[4] ) ) {
  77. $params[5] = $params[4];
  78. }
  79. $params[4] = $params[3];
  80. $params[3] = '';
  81. }
  82. return $params;
  83. }
  84. public function getPreloadTitles() {
  85. $title = $this->entry->getTarget();
  86. // Preload user page for non-autoblocks
  87. if ( substr( $title->getText(), 0, 1 ) !== '#' ) {
  88. return [ $title->getTalkPage() ];
  89. }
  90. return [];
  91. }
  92. public function getActionLinks() {
  93. $subtype = $this->entry->getSubtype();
  94. $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
  95. if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) // Action is hidden
  96. || !( $subtype === 'block' || $subtype === 'reblock' )
  97. || !$this->context->getUser()->isAllowed( 'block' )
  98. ) {
  99. return '';
  100. }
  101. // Show unblock/change block link
  102. $title = $this->entry->getTarget();
  103. $links = [
  104. $linkRenderer->makeKnownLink(
  105. SpecialPage::getTitleFor( 'Unblock', $title->getDBkey() ),
  106. $this->msg( 'unblocklink' )->text()
  107. ),
  108. $linkRenderer->makeKnownLink(
  109. SpecialPage::getTitleFor( 'Block', $title->getDBkey() ),
  110. $this->msg( 'change-blocklink' )->text()
  111. )
  112. ];
  113. return $this->msg( 'parentheses' )->rawParams(
  114. $this->context->getLanguage()->pipeList( $links ) )->escaped();
  115. }
  116. /**
  117. * Convert a comma-delimited list of block log flags
  118. * into a more readable (and translated) form
  119. *
  120. * @param string $flags Flags to format
  121. * @param Language $lang
  122. * @return string
  123. */
  124. public static function formatBlockFlags( $flags, $lang ) {
  125. $flags = trim( $flags );
  126. if ( $flags === '' ) {
  127. return ''; // nothing to do
  128. }
  129. $flags = explode( ',', $flags );
  130. $flagsCount = count( $flags );
  131. for ( $i = 0; $i < $flagsCount; $i++ ) {
  132. $flags[$i] = self::formatBlockFlag( $flags[$i], $lang );
  133. }
  134. return wfMessage( 'parentheses' )->inLanguage( $lang )
  135. ->rawParams( $lang->commaList( $flags ) )->escaped();
  136. }
  137. /**
  138. * Translate a block log flag if possible
  139. *
  140. * @param int $flag Flag to translate
  141. * @param Language $lang Language object to use
  142. * @return string
  143. */
  144. public static function formatBlockFlag( $flag, $lang ) {
  145. static $messages = [];
  146. if ( !isset( $messages[$flag] ) ) {
  147. $messages[$flag] = htmlspecialchars( $flag ); // Fallback
  148. // For grepping. The following core messages can be used here:
  149. // * block-log-flags-angry-autoblock
  150. // * block-log-flags-anononly
  151. // * block-log-flags-hiddenname
  152. // * block-log-flags-noautoblock
  153. // * block-log-flags-nocreate
  154. // * block-log-flags-noemail
  155. // * block-log-flags-nousertalk
  156. $msg = wfMessage( 'block-log-flags-' . $flag )->inLanguage( $lang );
  157. if ( $msg->exists() ) {
  158. $messages[$flag] = $msg->escaped();
  159. }
  160. }
  161. return $messages[$flag];
  162. }
  163. protected function getParametersForApi() {
  164. $entry = $this->entry;
  165. $params = $entry->getParameters();
  166. static $map = [
  167. // While this looks wrong to be starting at 5 rather than 4, it's
  168. // because getMessageParameters uses $4 for its own purposes.
  169. '5::duration',
  170. '6:array:flags',
  171. '6::flags' => '6:array:flags',
  172. ];
  173. foreach ( $map as $index => $key ) {
  174. if ( isset( $params[$index] ) ) {
  175. $params[$key] = $params[$index];
  176. unset( $params[$index] );
  177. }
  178. }
  179. $subtype = $entry->getSubtype();
  180. if ( $subtype === 'block' || $subtype === 'reblock' ) {
  181. // Defaults for old log entries missing some fields
  182. $params += [
  183. '5::duration' => 'infinite',
  184. '6:array:flags' => [],
  185. ];
  186. if ( !is_array( $params['6:array:flags'] ) ) {
  187. $params['6:array:flags'] = $params['6:array:flags'] === ''
  188. ? []
  189. : explode( ',', $params['6:array:flags'] );
  190. }
  191. if ( !wfIsInfinity( $params['5::duration'] ) ) {
  192. $ts = wfTimestamp( TS_UNIX, $entry->getTimestamp() );
  193. $expiry = strtotime( $params['5::duration'], $ts );
  194. if ( $expiry !== false && $expiry > 0 ) {
  195. $params[':timestamp:expiry'] = $expiry;
  196. }
  197. }
  198. }
  199. return $params;
  200. }
  201. public function formatParametersForApi() {
  202. $ret = parent::formatParametersForApi();
  203. if ( isset( $ret['flags'] ) ) {
  204. ApiResult::setIndexedTagName( $ret['flags'], 'f' );
  205. }
  206. return $ret;
  207. }
  208. }