MediaWiki  master
ProtectLogFormatter.php
Go to the documentation of this file.
1 <?php
26 
33  public function getPreloadTitles() {
34  $subtype = $this->entry->getSubtype();
35  if ( $subtype === 'move_prot' ) {
36  $params = $this->extractParameters();
37  return [ Title::newFromText( $params[3] ) ];
38  }
39  return [];
40  }
41 
42  protected function getMessageKey() {
43  $key = parent::getMessageKey();
44  $params = $this->extractParameters();
45  if ( isset( $params[4] ) && $params[4] ) {
46  // Messages: logentry-protect-protect-cascade, logentry-protect-modify-cascade
47  $key .= '-cascade';
48  }
49 
50  return $key;
51  }
52 
53  protected function getMessageParameters() {
54  $params = parent::getMessageParameters();
55 
56  $subtype = $this->entry->getSubtype();
57  if ( $subtype === 'protect' || $subtype === 'modify' ) {
58  $rawParams = $this->entry->getParameters();
59  if ( isset( $rawParams['details'] ) ) {
60  $params[3] = $this->createProtectDescription( $rawParams['details'] );
61  } elseif ( isset( $params[3] ) ) {
62  // Old way of Restrictions and expiries
63  $params[3] = $this->context->getLanguage()->getDirMark() . $params[3];
64  } else {
65  // Very old way (nothing set)
66  $params[3] = '';
67  }
68  // Cascading flag
69  if ( isset( $params[4] ) ) {
70  // handled in getMessageKey
71  unset( $params[4] );
72  }
73  } elseif ( $subtype === 'move_prot' ) {
74  $oldname = $this->makePageLink( Title::newFromText( $params[3] ), [ 'redirect' => 'no' ] );
75  $params[3] = Message::rawParam( $oldname );
76  }
77 
78  return $params;
79  }
80 
81  public function getActionLinks() {
82  $linkRenderer = $this->getLinkRenderer();
83  $subtype = $this->entry->getSubtype();
84  if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) // Action is hidden
85  || $subtype === 'move_prot' // the move log entry has the right action link
86  ) {
87  return '';
88  }
89 
90  // Show history link for pages that exist otherwise show nothing
91  $title = $this->entry->getTarget();
92  $links = [];
93  if ( $title->exists() ) {
94  $links [] = $linkRenderer->makeLink( $title,
95  $this->msg( 'hist' )->text(),
96  [],
97  [
98  'action' => 'history',
99  'offset' => $this->entry->getTimestamp(),
100  ]
101  );
102  }
103 
104  // Show change protection link
105  if ( MediaWikiServices::getInstance()
107  ->userHasRight( $this->context->getUser(), 'protect' )
108  ) {
109  $links[] = $linkRenderer->makeKnownLink(
110  $title,
111  $this->msg( 'protect_change' )->text(),
112  [],
113  [ 'action' => 'protect' ]
114  );
115  }
116 
117  if ( empty( $links ) ) {
118  return '';
119  } else {
120  return $this->msg( 'parentheses' )->rawParams(
121  $this->context->getLanguage()->pipeList( $links )
122  )->escaped();
123  }
124  }
125 
126  protected function getParametersForApi() {
128  $subtype = $this->entry->getSubtype();
129  $params = $entry->getParameters();
130 
131  $map = [];
132  if ( $subtype === 'protect' || $subtype === 'modify' ) {
133  $map = [
134  '4::description',
135  '5:bool:cascade',
136  'details' => ':array:details',
137  ];
138  } elseif ( $subtype === 'move_prot' ) {
139  $map = [
140  '4:title:oldtitle',
141  '4::oldtitle' => '4:title:oldtitle',
142  ];
143  }
144  foreach ( $map as $index => $key ) {
145  if ( isset( $params[$index] ) ) {
146  $params[$key] = $params[$index];
147  unset( $params[$index] );
148  }
149  }
150 
151  // Change string to explicit boolean
152  if ( isset( $params['5:bool:cascade'] ) && is_string( $params['5:bool:cascade'] ) ) {
153  $params['5:bool:cascade'] = $params['5:bool:cascade'] === 'cascade';
154  }
155 
156  return $params;
157  }
158 
159  public function formatParametersForApi() {
160  $ret = parent::formatParametersForApi();
161  if ( isset( $ret['details'] ) && is_array( $ret['details'] ) ) {
162  $contLang = MediaWikiServices::getInstance()->getContentLanguage();
163  foreach ( $ret['details'] as &$detail ) {
164  if ( isset( $detail['expiry'] ) ) {
165  $detail['expiry'] = $contLang->
166  formatExpiry( $detail['expiry'], TS_ISO_8601, 'infinite' );
167  }
168  }
169  }
170 
171  return $ret;
172  }
173 
180  public function createProtectDescription( array $details ) {
181  $protectDescription = '';
182 
183  foreach ( $details as $param ) {
184  $expiryText = $this->formatExpiry( $param['expiry'] );
185 
186  // Messages: restriction-edit, restriction-move, restriction-create,
187  // restriction-upload
188  $action = $this->context->msg( 'restriction-' . $param['type'] )->escaped();
189 
190  $protectionLevel = $param['level'];
191  // Messages: protect-level-autoconfirmed, protect-level-sysop
192  $message = $this->context->msg( 'protect-level-' . $protectionLevel );
193  if ( $message->isDisabled() ) {
194  // Require "$1" permission
195  $restrictions = $this->context->msg( "protect-fallback", $protectionLevel )->parse();
196  } else {
197  $restrictions = $message->escaped();
198  }
199 
200  if ( $protectDescription !== '' ) {
201  $protectDescription .= $this->context->msg( 'word-separator' )->escaped();
202  }
203 
204  $protectDescription .= $this->context->msg( 'protect-summary-desc' )
205  ->params( $action, $restrictions, $expiryText )->escaped();
206  }
207 
208  return $protectDescription;
209  }
210 
211  private function formatExpiry( $expiry ) {
212  if ( wfIsInfinity( $expiry ) ) {
213  return $this->context->msg( 'protect-expiry-indefinite' )->text();
214  }
215  $lang = $this->context->getLanguage();
216  $user = $this->context->getUser();
217  return $this->context->msg(
218  'protect-expiring-local',
219  $lang->userTimeAndDate( $expiry, $user ),
220  $lang->userDate( $expiry, $user ),
221  $lang->userTime( $expiry, $user )
222  )->text();
223  }
224 
225 }
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:329
ProtectLogFormatter\createProtectDescription
createProtectDescription(array $details)
Create the protect description to show in the log formatter.
Definition: ProtectLogFormatter.php:180
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
LogEntry\getParameters
getParameters()
Get the extra parameters stored for this message.
ProtectLogFormatter\formatParametersForApi
formatParametersForApi()
Format parameters for API output.
Definition: ProtectLogFormatter.php:159
LogFormatter\$entry
LogEntryBase $entry
Definition: LogFormatter.php:79
ProtectLogFormatter\formatExpiry
formatExpiry( $expiry)
Definition: ProtectLogFormatter.php:211
ProtectLogFormatter\getPreloadTitles
getPreloadTitles()
Stable to override.
Definition: ProtectLogFormatter.php:33
ProtectLogFormatter
This class formats protect log entries.
Definition: ProtectLogFormatter.php:32
getPermissionManager
getPermissionManager()
$title
$title
Definition: testCompression.php:38
LogPage\DELETED_ACTION
const DELETED_ACTION
Definition: LogPage.php:38
Message\rawParam
static rawParam( $raw)
Definition: Message.php:1037
ProtectLogFormatter\getMessageKey
getMessageKey()
Returns a key to be used for formatting the action sentence.
Definition: ProtectLogFormatter.php:42
wfIsInfinity
wfIsInfinity( $str)
Determine input string is represents as infinity.
Definition: GlobalFunctions.php:2785
LogEntry\getSubtype
getSubtype()
The log subtype.
LogFormatter
Implements the default log formatting.
Definition: LogFormatter.php:40
ProtectLogFormatter\getMessageParameters
getMessageParameters()
Formats parameters intented for action message from array of all parameters.
Definition: ProtectLogFormatter.php:53
LogFormatter\$linkRenderer
LinkRenderer null $linkRenderer
Definition: LogFormatter.php:105
LogFormatter\msg
msg( $key,... $params)
Shortcut for wfMessage which honors local context.
Definition: LogFormatter.php:775
LogFormatter\getLinkRenderer
getLinkRenderer()
Definition: LogFormatter.php:143
ProtectLogFormatter\getActionLinks
getActionLinks()
Returns extra links that comes after the action text, like "revert", etc.
Definition: ProtectLogFormatter.php:81
LogFormatter\extractParameters
extractParameters()
Extracts the optional extra parameters for use in action messages.
Definition: LogFormatter.php:525
LogFormatter\makePageLink
makePageLink(Title $title=null, $parameters=[], $html=null)
Helper to make a link to the page, taking the plaintext value in consideration.
Definition: LogFormatter.php:678
ProtectLogFormatter\getParametersForApi
getParametersForApi()
Get the array of parameters, converted from legacy format if necessary.
Definition: ProtectLogFormatter.php:126