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 all changes after the protection
91  $title = $this->entry->getTarget();
92  $links = [
93  $linkRenderer->makeLink( $title,
94  $this->msg( 'hist' )->text(),
95  [],
96  [
97  'action' => 'history',
98  'offset' => $this->entry->getTimestamp(),
99  ]
100  )
101  ];
102 
103  // Show change protection link
104  if ( MediaWikiServices::getInstance()
105  ->getPermissionManager()
106  ->userHasRight( $this->context->getUser(), 'protect' )
107  ) {
108  $links[] = $linkRenderer->makeKnownLink(
109  $title,
110  $this->msg( 'protect_change' )->text(),
111  [],
112  [ 'action' => 'protect' ]
113  );
114  }
115 
116  return $this->msg( 'parentheses' )->rawParams(
117  $this->context->getLanguage()->pipeList( $links ) )->escaped();
118  }
119 
120  protected function getParametersForApi() {
122  $subtype = $this->entry->getSubtype();
123  $params = $entry->getParameters();
124 
125  $map = [];
126  if ( $subtype === 'protect' || $subtype === 'modify' ) {
127  $map = [
128  '4::description',
129  '5:bool:cascade',
130  'details' => ':array:details',
131  ];
132  } elseif ( $subtype === 'move_prot' ) {
133  $map = [
134  '4:title:oldtitle',
135  '4::oldtitle' => '4:title:oldtitle',
136  ];
137  }
138  foreach ( $map as $index => $key ) {
139  if ( isset( $params[$index] ) ) {
140  $params[$key] = $params[$index];
141  unset( $params[$index] );
142  }
143  }
144 
145  // Change string to explicit boolean
146  if ( isset( $params['5:bool:cascade'] ) && is_string( $params['5:bool:cascade'] ) ) {
147  $params['5:bool:cascade'] = $params['5:bool:cascade'] === 'cascade';
148  }
149 
150  return $params;
151  }
152 
153  public function formatParametersForApi() {
154  $ret = parent::formatParametersForApi();
155  if ( isset( $ret['details'] ) && is_array( $ret['details'] ) ) {
156  $contLang = MediaWikiServices::getInstance()->getContentLanguage();
157  foreach ( $ret['details'] as &$detail ) {
158  if ( isset( $detail['expiry'] ) ) {
159  $detail['expiry'] = $contLang->
160  formatExpiry( $detail['expiry'], TS_ISO_8601, 'infinite' );
161  }
162  }
163  }
164 
165  return $ret;
166  }
167 
174  public function createProtectDescription( array $details ) {
175  $protectDescription = '';
176 
177  foreach ( $details as $param ) {
178  $expiryText = $this->formatExpiry( $param['expiry'] );
179 
180  // Messages: restriction-edit, restriction-move, restriction-create,
181  // restriction-upload
182  $action = $this->context->msg( 'restriction-' . $param['type'] )->escaped();
183 
184  $protectionLevel = $param['level'];
185  // Messages: protect-level-autoconfirmed, protect-level-sysop
186  $message = $this->context->msg( 'protect-level-' . $protectionLevel );
187  if ( $message->isDisabled() ) {
188  // Require "$1" permission
189  $restrictions = $this->context->msg( "protect-fallback", $protectionLevel )->parse();
190  } else {
191  $restrictions = $message->escaped();
192  }
193 
194  if ( $protectDescription !== '' ) {
195  $protectDescription .= $this->context->msg( 'word-separator' )->escaped();
196  }
197 
198  $protectDescription .= $this->context->msg( 'protect-summary-desc' )
199  ->params( $action, $restrictions, $expiryText )->escaped();
200  }
201 
202  return $protectDescription;
203  }
204 
205  private function formatExpiry( $expiry ) {
206  if ( wfIsInfinity( $expiry ) ) {
207  return $this->context->msg( 'protect-expiry-indefinite' )->text();
208  }
209  $lang = $this->context->getLanguage();
210  $user = $this->context->getUser();
211  return $this->context->msg(
212  'protect-expiring-local',
213  $lang->userTimeAndDate( $expiry, $user ),
214  $lang->userDate( $expiry, $user ),
215  $lang->userTime( $expiry, $user )
216  )->text();
217  }
218 
219 }
static rawParam( $raw)
Definition: Message.php:1031
LogEntryBase $entry
if(!isset( $args[0])) $lang
This class formats protect log entries.
wfIsInfinity( $str)
Determine input string is represents as infinity.
getSubtype()
The log subtype.
LinkRenderer null $linkRenderer
msg( $key,... $params)
Shortcut for wfMessage which honors local context.
createProtectDescription(array $details)
Create the protect description to show in the log formatter.
Implements the default log formatting.
getParameters()
Get the extra parameters stored for this message.
extractParameters()
Extracts the optional extra parameters for use in action messages.
makePageLink(Title $title=null, $parameters=[], $html=null)
Helper to make a link to the page, taking the plaintext value in consideration.
const DELETED_ACTION
Definition: LogPage.php:34
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:317