MediaWiki master
ProtectLogFormatter.php
Go to the documentation of this file.
1<?php
25namespace MediaWiki\Logging;
26
31
38 private TitleParser $titleParser;
39
40 public function __construct(
42 TitleParser $titleParser
43 ) {
44 parent::__construct( $entry );
45 $this->titleParser = $titleParser;
46 }
47
48 public function getPreloadTitles() {
49 $subtype = $this->entry->getSubtype();
50 if ( $subtype === 'move_prot' ) {
51 $params = $this->extractParameters();
52 try {
53 return [ $this->titleParser->parseTitle( $params[3] ) ];
54 } catch ( MalformedTitleException $_ ) {
55 }
56 }
57 return [];
58 }
59
60 protected function getMessageKey() {
61 $key = parent::getMessageKey();
62 $params = $this->extractParameters();
63 if ( isset( $params[4] ) && $params[4] ) {
64 // Messages: logentry-protect-protect-cascade, logentry-protect-modify-cascade
65 $key .= '-cascade';
66 }
67
68 return $key;
69 }
70
71 protected function getMessageParameters() {
72 $params = parent::getMessageParameters();
73
74 $subtype = $this->entry->getSubtype();
75 if ( $subtype === 'protect' || $subtype === 'modify' ) {
76 $rawParams = $this->entry->getParameters();
77 if ( isset( $rawParams['details'] ) ) {
78 $params[3] = $this->createProtectDescription( $rawParams['details'] );
79 } elseif ( isset( $params[3] ) ) {
80 // Old way of Restrictions and expiries
81 $params[3] = $this->context->getLanguage()->getDirMark() . $params[3];
82 } else {
83 // Very old way (nothing set)
84 $params[3] = '';
85 }
86 // Cascading flag
87 if ( isset( $params[4] ) ) {
88 // handled in getMessageKey
89 unset( $params[4] );
90 }
91 } elseif ( $subtype === 'move_prot' ) {
92 $oldname = $this->makePageLink( Title::newFromText( $params[3] ), [ 'redirect' => 'no' ] );
93 $params[3] = Message::rawParam( $oldname );
94 }
95
96 return $params;
97 }
98
99 public function getActionLinks() {
100 $linkRenderer = $this->getLinkRenderer();
101 $subtype = $this->entry->getSubtype();
102 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) // Action is hidden
103 || $subtype === 'move_prot' // the move log entry has the right action link
104 ) {
105 return '';
106 }
107
108 // Show history link for pages that exist otherwise show nothing
109 $title = $this->entry->getTarget();
110 $links = [];
111 if ( $title->exists() ) {
112 $links[] = $linkRenderer->makeLink( $title,
113 $this->msg( 'hist' )->text(),
114 [],
115 [
116 'action' => 'history',
117 'offset' => $this->entry->getTimestamp(),
118 ]
119 );
120 }
121
122 // Show change protection link
123 if ( $this->context->getAuthority()->isAllowed( 'protect' ) ) {
124 $links[] = $linkRenderer->makeKnownLink(
125 $title,
126 $this->msg( 'protect_change' )->text(),
127 [],
128 [ 'action' => 'protect' ]
129 );
130 }
131
132 if ( !$links ) {
133 return '';
134 } else {
135 return $this->msg( 'parentheses' )->rawParams(
136 $this->context->getLanguage()->pipeList( $links )
137 )->escaped();
138 }
139 }
140
141 protected function getParametersForApi() {
143 $subtype = $this->entry->getSubtype();
144 $params = $entry->getParameters();
145
146 $map = [];
147 if ( $subtype === 'protect' || $subtype === 'modify' ) {
148 $map = [
149 '4::description',
150 '5:bool:cascade',
151 'details' => ':array:details',
152 ];
153 } elseif ( $subtype === 'move_prot' ) {
154 $map = [
155 '4:title:oldtitle',
156 '4::oldtitle' => '4:title:oldtitle',
157 ];
158 }
159 foreach ( $map as $index => $key ) {
160 if ( isset( $params[$index] ) ) {
161 $params[$key] = $params[$index];
162 unset( $params[$index] );
163 }
164 }
165
166 // Change string to explicit boolean
167 if ( isset( $params['5:bool:cascade'] ) && is_string( $params['5:bool:cascade'] ) ) {
168 $params['5:bool:cascade'] = $params['5:bool:cascade'] === 'cascade';
169 }
170
171 return $params;
172 }
173
174 public function formatParametersForApi() {
175 $ret = parent::formatParametersForApi();
176 if ( isset( $ret['details'] ) && is_array( $ret['details'] ) ) {
177 $contLang = $this->getContentLanguage();
178 foreach ( $ret['details'] as &$detail ) {
179 if ( isset( $detail['expiry'] ) ) {
180 $detail['expiry'] = $contLang->
181 formatExpiry( $detail['expiry'], TS_ISO_8601, 'infinite' );
182 }
183 }
184 }
185
186 return $ret;
187 }
188
195 public function createProtectDescription( array $details ) {
196 $protectDescription = '';
197
198 foreach ( $details as $param ) {
199 $expiryText = $this->formatExpiry( $param['expiry'] );
200
201 // Messages: restriction-edit, restriction-move, restriction-create,
202 // restriction-upload
203 $action = $this->context->msg( 'restriction-' . $param['type'] )->escaped();
204
205 $protectionLevel = $param['level'];
206 // Messages: protect-level-autoconfirmed, protect-level-sysop
207 $message = $this->context->msg( 'protect-level-' . $protectionLevel );
208 if ( $message->isDisabled() ) {
209 // Require "$1" permission
210 $restrictions = $this->context->msg( "protect-fallback", $protectionLevel )->parse();
211 } else {
212 $restrictions = $message->escaped();
213 }
214
215 if ( $protectDescription !== '' ) {
216 $protectDescription .= $this->context->msg( 'word-separator' )->escaped();
217 }
218
219 $protectDescription .= $this->context->msg( 'protect-summary-desc' )
220 ->params( $action, $restrictions, $expiryText )->escaped();
221 }
222
223 return $protectDescription;
224 }
225
226 private function formatExpiry( string $expiry ): string {
227 if ( wfIsInfinity( $expiry ) ) {
228 return $this->context->msg( 'protect-expiry-indefinite' )->text();
229 }
230 $lang = $this->context->getLanguage();
231 $user = $this->context->getUser();
232 return $this->context->msg(
233 'protect-expiring-local',
234 $lang->userTimeAndDate( $expiry, $user ),
235 $lang->userDate( $expiry, $user ),
236 $lang->userTime( $expiry, $user )
237 )->text();
238 }
239
240}
241
243class_alias( ProtectLogFormatter::class, 'ProtectLogFormatter' );
wfIsInfinity( $str)
Determine input string is represents as infinity.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:82
Implements the default log formatting.
makePageLink(?Title $title=null, $parameters=[], $html=null)
Helper to make a link to the page, taking the plaintext value in consideration.
extractParameters()
Extracts the optional extra parameters for use in action messages.
msg( $key,... $params)
Shortcut for wfMessage which honors local context.
This class formats protect log entries.
formatParametersForApi()
Format parameters for API output.
getMessageParameters()
Formats parameters intended for action message from array of all parameters.
getParametersForApi()
Get the array of parameters, converted from legacy format if necessary.
getActionLinks()
Returns extra links that comes after the action text, like "revert", etc.
__construct(LogEntry $entry, TitleParser $titleParser)
getMessageKey()
Returns a key to be used for formatting the action sentence.
createProtectDescription(array $details)
Create the protect description to show in the log formatter.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:157
MalformedTitleException is thrown when a TitleParser is unable to parse a title string.
A title parser service for MediaWiki.
Represents a title within MediaWiki.
Definition Title.php:78
An individual log entry.
Definition LogEntry.php:37
getParameters()
Get the extra parameters stored for this message.
getSubtype()
The log subtype.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...