MediaWiki master
ProtectLogFormatter.php
Go to the documentation of this file.
1<?php
11namespace MediaWiki\Logging;
12
17use Wikimedia\Timestamp\TimestampFormat as TS;
18
25 private TitleParser $titleParser;
26
27 public function __construct(
29 TitleParser $titleParser
30 ) {
31 parent::__construct( $entry );
32 $this->titleParser = $titleParser;
33 }
34
36 public function getPreloadTitles() {
37 $subtype = $this->entry->getSubtype();
38 if ( $subtype === 'move_prot' ) {
39 $params = $this->extractParameters();
40 try {
41 return [ $this->titleParser->parseTitle( $params[3] ) ];
42 } catch ( MalformedTitleException ) {
43 }
44 }
45 return [];
46 }
47
49 protected function getMessageKey() {
50 $key = parent::getMessageKey();
51 $params = $this->extractParameters();
52 if ( isset( $params[4] ) && $params[4] ) {
53 // Messages: logentry-protect-protect-cascade, logentry-protect-modify-cascade
54 $key .= '-cascade';
55 }
56
57 return $key;
58 }
59
61 protected function getMessageParameters() {
62 $params = parent::getMessageParameters();
63
64 $subtype = $this->entry->getSubtype();
65 if ( $subtype === 'protect' || $subtype === 'modify' ) {
66 $rawParams = $this->entry->getParameters();
67 if ( isset( $rawParams['details'] ) ) {
68 $params[3] = $this->createProtectDescription( $rawParams['details'] );
69 } elseif ( isset( $params[3] ) ) {
70 // Old way of Restrictions and expiries
71 $params[3] = $this->context->getLanguage()->getDirMark() . $params[3];
72 } else {
73 // Very old way (nothing set)
74 $params[3] = '';
75 }
76 // Cascading flag
77 if ( isset( $params[4] ) ) {
78 // handled in getMessageKey
79 unset( $params[4] );
80 }
81 } elseif ( $subtype === 'move_prot' ) {
82 $oldname = $this->makePageLink( Title::newFromText( $params[3] ), [ 'redirect' => 'no' ] );
83 $params[3] = Message::rawParam( $oldname );
84 }
85
86 return $params;
87 }
88
90 public function getActionLinks() {
91 $linkRenderer = $this->getLinkRenderer();
92 $subtype = $this->entry->getSubtype();
93 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) // Action is hidden
94 || $subtype === 'move_prot' // the move log entry has the right action link
95 ) {
96 return '';
97 }
98
99 // Show history link for pages that exist otherwise show nothing
100 $title = $this->entry->getTarget();
101 $links = [];
102 if ( $title->exists() ) {
103 $links[] = $linkRenderer->makeLink( $title,
104 $this->msg( 'hist' )->text(),
105 [],
106 [
107 'action' => 'history',
108 'offset' => $this->entry->getTimestamp(),
109 ]
110 );
111 }
112
113 // Show change protection link
114 if ( $this->context->getAuthority()->isAllowed( 'protect' ) ) {
115 $links[] = $linkRenderer->makeKnownLink(
116 $title,
117 $this->msg( 'protect_change' )->text(),
118 [],
119 [ 'action' => 'protect' ]
120 );
121 }
122
123 if ( !$links ) {
124 return '';
125 } else {
126 return $this->msg( 'parentheses' )->rawParams(
127 $this->context->getLanguage()->pipeList( $links )
128 )->escaped();
129 }
130 }
131
133 protected function getParametersForApi() {
135 $subtype = $this->entry->getSubtype();
136 $params = $entry->getParameters();
137
138 $map = [];
139 if ( $subtype === 'protect' || $subtype === 'modify' ) {
140 $map = [
141 '4::description',
142 '5:bool:cascade',
143 'details' => ':array:details',
144 ];
145 } elseif ( $subtype === 'move_prot' ) {
146 $map = [
147 '4:title:oldtitle',
148 '4::oldtitle' => '4:title:oldtitle',
149 ];
150 }
151 foreach ( $map as $index => $key ) {
152 if ( isset( $params[$index] ) ) {
153 $params[$key] = $params[$index];
154 unset( $params[$index] );
155 }
156 }
157
158 // Change string to explicit boolean
159 if ( isset( $params['5:bool:cascade'] ) && is_string( $params['5:bool:cascade'] ) ) {
160 $params['5:bool:cascade'] = $params['5:bool:cascade'] === 'cascade';
161 }
162
163 return $params;
164 }
165
167 public function formatParametersForApi() {
168 $ret = parent::formatParametersForApi();
169 if ( isset( $ret['details'] ) && is_array( $ret['details'] ) ) {
170 $contLang = $this->getContentLanguage();
171 foreach ( $ret['details'] as &$detail ) {
172 if ( isset( $detail['expiry'] ) ) {
173 $detail['expiry'] = $contLang->
174 formatExpiry( $detail['expiry'], TS::ISO_8601, 'infinite' );
175 }
176 }
177 }
178
179 return $ret;
180 }
181
188 public function createProtectDescription( array $details ) {
189 $protectDescription = '';
190
191 foreach ( $details as $param ) {
192 $expiryText = $this->formatExpiry( $param['expiry'] );
193
194 // Messages: restriction-edit, restriction-move, restriction-create,
195 // restriction-upload
196 $action = $this->context->msg( 'restriction-' . $param['type'] )->escaped();
197
198 $protectionLevel = $param['level'];
199 // Messages: protect-level-autoconfirmed, protect-level-sysop
200 $message = $this->context->msg( 'protect-level-' . $protectionLevel );
201 if ( $message->isDisabled() ) {
202 // Require "$1" permission
203 $restrictions = $this->context->msg( "protect-fallback", $protectionLevel )->parse();
204 } else {
205 $restrictions = $message->escaped();
206 }
207
208 if ( $protectDescription !== '' ) {
209 $protectDescription .= $this->context->msg( 'word-separator' )->escaped();
210 }
211
212 $protectDescription .= $this->context->msg( 'protect-summary-desc' )
213 ->params( $action, $restrictions, $expiryText )->escaped();
214 }
215
216 return $protectDescription;
217 }
218
219 private function formatExpiry( string $expiry ): string {
220 if ( wfIsInfinity( $expiry ) ) {
221 return $this->context->msg( 'protect-expiry-indefinite' )->text();
222 }
223 $lang = $this->context->getLanguage();
224 $user = $this->context->getUser();
225 return $this->context->msg(
226 'protect-expiring-local',
227 $lang->userTimeAndDate( $expiry, $user ),
228 $lang->userDate( $expiry, $user ),
229 $lang->userTime( $expiry, $user )
230 )->text();
231 }
232
233}
234
236class_alias( ProtectLogFormatter::class, 'ProtectLogFormatter' );
wfIsInfinity( $str)
Determine input string is represents as infinity.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:68
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.The result array should generally map named keys to values....
getMessageParameters()
Formats parameters intended for action message from array of all parameters.There are three hardcoded...
getParametersForApi()
Get the array of parameters, converted from legacy format if necessary.1.25 to override array
getActionLinks()
Returns extra links that comes after the action text, like "revert", etc.to override string
__construct(LogEntry $entry, TitleParser $titleParser)
getMessageKey()
Returns a key to be used for formatting the action sentence.Default is logentry-TYPE-SUBTYPE for mode...
getPreloadTitles()
to override LinkTarget[] Array of titles that should be preloaded with LinkBatch
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:144
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:69
An individual log entry.
Definition LogEntry.php:23
getParameters()
Get the extra parameters stored for this message.
getSubtype()
The log subtype.