MediaWiki  master
DeleteLogFormatter.php
Go to the documentation of this file.
1 <?php
28 
37 
41  protected function getMessageKey() {
42  $key = parent::getMessageKey();
43  if ( in_array( $this->entry->getSubtype(), [ 'event', 'revision' ] ) ) {
44  if ( count( $this->getMessageParameters() ) < 5 ) {
45  // Messages: logentry-delete-event-legacy, logentry-delete-revision-legacy,
46  // logentry-suppress-event-legacy, logentry-suppress-revision-legacy
47  return "$key-legacy";
48  }
49  } elseif ( $this->entry->getSubtype() === 'restore' ) {
50  $rawParams = $this->entry->getParameters();
51  if ( !isset( $rawParams[':assoc:count'] ) ) {
52  // Message: logentry-delete-restore-nocount
53  return $key . '-nocount';
54  }
55  }
56 
57  return $key;
58  }
59 
63  protected function getMessageParameters() {
64  if ( $this->parsedParametersDeleteLog !== null ) {
66  }
67 
68  $params = parent::getMessageParameters();
69  $subtype = $this->entry->getSubtype();
70  if ( in_array( $subtype, [ 'event', 'revision' ] ) ) {
71  // $params[3] here is 'revision' or 'archive' for page revisions, 'oldimage' or
72  // 'filearchive' for file versions, or a comma-separated list of log_ids for log
73  // entries. $subtype here is 'revision' for page revisions and file
74  // versions, or 'event' for log entries.
75  if (
76  ( $subtype === 'event' && count( $params ) === 6 )
77  || (
78  $subtype === 'revision' && isset( $params[3] )
79  && in_array( $params[3], [ 'revision', 'archive', 'oldimage', 'filearchive' ] )
80  )
81  ) {
82  // See RevDelList::getLogParams()/RevDelLogList::getLogParams()
83  $paramStart = $subtype === 'revision' ? 4 : 3;
84 
85  $old = $this->parseBitField( $params[$paramStart + 1] );
86  $new = $this->parseBitField( $params[$paramStart + 2] );
87  list( $hid, $unhid, $extra ) = RevisionDeleter::getChanges( $new, $old );
88  $changes = [];
89  // messages used: revdelete-content-hid, revdelete-summary-hid, revdelete-uname-hid
90  foreach ( $hid as $v ) {
91  $changes[] = $this->msg( "$v-hid" )->plain();
92  }
93  // messages used: revdelete-content-unhid, revdelete-summary-unhid,
94  // revdelete-uname-unhid
95  foreach ( $unhid as $v ) {
96  $changes[] = $this->msg( "$v-unhid" )->plain();
97  }
98  foreach ( $extra as $v ) {
99  $changes[] = $this->msg( $v )->plain();
100  }
101  $changeText = $this->context->getLanguage()->listToText( $changes );
102 
103  $newParams = array_slice( $params, 0, 3 );
104  $newParams[3] = $changeText;
105  $ids = is_array( $params[$paramStart] )
106  ? $params[$paramStart]
107  : explode( ',', $params[$paramStart] );
108  $newParams[4] = $this->context->getLanguage()->formatNum( count( $ids ) );
109 
110  $this->parsedParametersDeleteLog = $newParams;
112  } else {
113  $this->parsedParametersDeleteLog = array_slice( $params, 0, 3 );
115  }
116  } elseif ( $subtype === 'restore' ) {
117  $rawParams = $this->entry->getParameters();
118  if ( isset( $rawParams[':assoc:count'] ) ) {
119  $countList = [];
120  foreach ( $rawParams[':assoc:count'] as $type => $count ) {
121  if ( $count ) {
122  // Messages: restore-count-revisions, restore-count-files
123  $countList[] = $this->context->msg( 'restore-count-' . $type )
124  ->numParams( $count )->plain();
125  }
126  }
127  $params[3] = $this->context->getLanguage()->listToText( $countList );
128  }
129  }
130 
131  $this->parsedParametersDeleteLog = $params;
133  }
134 
135  protected function parseBitField( $string ) {
136  // Input is like ofield=2134 or just the number
137  if ( strpos( $string, 'field=' ) === 1 ) {
138  list( , $field ) = explode( '=', $string );
139 
140  return (int)$field;
141  } else {
142  return (int)$string;
143  }
144  }
145 
146  public function getActionLinks() {
147  $user = $this->context->getUser();
148  $linkRenderer = $this->getLinkRenderer();
149  $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
150  if ( !$permissionManager->userHasRight( $user, 'deletedhistory' )
151  || $this->entry->isDeleted( LogPage::DELETED_ACTION )
152  ) {
153  return '';
154  }
155 
156  switch ( $this->entry->getSubtype() ) {
157  case 'delete': // Show undelete link
158  case 'delete_redir':
159  case 'delete_redir2':
160  if ( $permissionManager->userHasRight( $user, 'undelete' ) ) {
161  $message = 'undeletelink';
162  } else {
163  $message = 'undeleteviewlink';
164  }
165  $revert = $linkRenderer->makeKnownLink(
166  SpecialPage::getTitleFor( 'Undelete' ),
167  $this->msg( $message )->text(),
168  [],
169  [ 'target' => $this->entry->getTarget()->getPrefixedDBkey() ]
170  );
171 
172  return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
173 
174  case 'revision': // If an edit was hidden from a page give a review link to the history
175  $params = $this->extractParameters();
176  if ( !isset( $params[3] ) || !isset( $params[4] ) ) {
177  return '';
178  }
179 
180  // Different revision types use different URL params...
181  $key = $params[3];
182  // This is a array or CSV of the IDs
183  $ids = is_array( $params[4] )
184  ? $params[4]
185  : explode( ',', $params[4] );
186 
187  $links = [];
188 
189  // If there's only one item, we can show a diff link
190  if ( count( $ids ) == 1 ) {
191  // Live revision diffs...
192  if ( $key == 'oldid' || $key == 'revision' ) {
193  $links[] = $linkRenderer->makeKnownLink(
194  $this->entry->getTarget(),
195  $this->msg( 'diff' )->text(),
196  [],
197  [
198  'diff' => intval( $ids[0] ),
199  'unhide' => 1
200  ]
201  );
202  // Deleted revision diffs...
203  } elseif ( $key == 'artimestamp' || $key == 'archive' ) {
204  $links[] = $linkRenderer->makeKnownLink(
205  SpecialPage::getTitleFor( 'Undelete' ),
206  $this->msg( 'diff' )->text(),
207  [],
208  [
209  'target' => $this->entry->getTarget()->getPrefixedDBkey(),
210  'diff' => 'prev',
211  'timestamp' => $ids[0]
212  ]
213  );
214  }
215  }
216 
217  // View/modify link...
218  $links[] = $linkRenderer->makeKnownLink(
219  SpecialPage::getTitleFor( 'Revisiondelete' ),
220  $this->msg( 'revdel-restore' )->text(),
221  [],
222  [
223  'target' => $this->entry->getTarget()->getPrefixedText(),
224  'type' => $key,
225  'ids' => implode( ',', $ids ),
226  ]
227  );
228 
229  return $this->msg( 'parentheses' )->rawParams(
230  $this->context->getLanguage()->pipeList( $links ) )->escaped();
231 
232  case 'event': // Hidden log items, give review link
233  $params = $this->extractParameters();
234  if ( !isset( $params[3] ) ) {
235  return '';
236  }
237  // This is a CSV of the IDs
238  $query = $params[3];
239  if ( is_array( $query ) ) {
240  $query = implode( ',', $query );
241  }
242  // Link to each hidden object ID, $params[1] is the url param
243  $revert = $linkRenderer->makeKnownLink(
244  SpecialPage::getTitleFor( 'Revisiondelete' ),
245  $this->msg( 'revdel-restore' )->text(),
246  [],
247  [
248  'target' => $this->entry->getTarget()->getPrefixedText(),
249  'type' => 'logging',
250  'ids' => $query
251  ]
252  );
253 
254  return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
255  default:
256  return '';
257  }
258  }
259 
260  protected function getParametersForApi() {
262  $params = [];
263 
264  $subtype = $this->entry->getSubtype();
265  if ( in_array( $subtype, [ 'event', 'revision' ] ) ) {
266  $rawParams = $entry->getParameters();
267  if ( $subtype === 'event' ) {
268  array_unshift( $rawParams, 'logging' );
269  }
270 
271  static $map = [
272  '4::type',
273  '5::ids',
274  '6::ofield',
275  '7::nfield',
276  '4::ids' => '5::ids',
277  '5::ofield' => '6::ofield',
278  '6::nfield' => '7::nfield',
279  ];
280  foreach ( $map as $index => $key ) {
281  if ( isset( $rawParams[$index] ) ) {
282  $rawParams[$key] = $rawParams[$index];
283  unset( $rawParams[$index] );
284  }
285  }
286 
287  if ( !is_array( $rawParams['5::ids'] ) ) {
288  $rawParams['5::ids'] = explode( ',', $rawParams['5::ids'] );
289  }
290 
291  $params = [
292  '::type' => $rawParams['4::type'],
293  ':array:ids' => $rawParams['5::ids'],
294  ];
295 
296  static $fields = [
297  RevisionRecord::DELETED_TEXT => 'content',
298  RevisionRecord::DELETED_COMMENT => 'comment',
299  RevisionRecord::DELETED_USER => 'user',
300  RevisionRecord::DELETED_RESTRICTED => 'restricted',
301  ];
302 
303  if ( isset( $rawParams['6::ofield'] ) ) {
304  $old = $this->parseBitField( $rawParams['6::ofield'] );
305  $params[':assoc:old'] = [ 'bitmask' => $old ];
306  foreach ( $fields as $bit => $key ) {
307  $params[':assoc:old'][$key] = (bool)( $old & $bit );
308  }
309  }
310  if ( isset( $rawParams['7::nfield'] ) ) {
311  $new = $this->parseBitField( $rawParams['7::nfield'] );
312  $params[':assoc:new'] = [ 'bitmask' => $new ];
313  foreach ( $fields as $bit => $key ) {
314  $params[':assoc:new'][$key] = (bool)( $new & $bit );
315  }
316  }
317  } elseif ( $subtype === 'restore' ) {
318  $rawParams = $entry->getParameters();
319  if ( isset( $rawParams[':assoc:count'] ) ) {
320  $params[':assoc:count'] = $rawParams[':assoc:count'];
321  }
322  }
323 
324  return $params;
325  }
326 
327  public function formatParametersForApi() {
328  $ret = parent::formatParametersForApi();
329  if ( isset( $ret['ids'] ) ) {
330  ApiResult::setIndexedTagName( $ret['ids'], 'id' );
331  }
332  return $ret;
333  }
334 }
DeleteLogFormatter\getParametersForApi
getParametersForApi()
Get the array of parameters, converted from legacy format if necessary.
Definition: DeleteLogFormatter.php:260
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:163
LogEntry\getParameters
getParameters()
Get the extra parameters stored for this message.
LogFormatter\$entry
LogEntryBase $entry
Definition: LogFormatter.php:79
DeleteLogFormatter\parseBitField
parseBitField( $string)
Definition: DeleteLogFormatter.php:135
SpecialPage\getTitleFor
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
Definition: SpecialPage.php:92
DeleteLogFormatter\getMessageKey
getMessageKey()
Returns a key to be used for formatting the action sentence.Default is logentry-TYPE-SUBTYPE for mode...
Definition: DeleteLogFormatter.php:41
LogPage\DELETED_ACTION
const DELETED_ACTION
Definition: LogPage.php:38
ApiResult\setIndexedTagName
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:604
RevisionDeleter\getChanges
static getChanges( $n, $o)
Gets an array of message keys describing the changes made to the visibility of the revision.
Definition: RevisionDeleter.php:129
LogEntry\getSubtype
getSubtype()
The log subtype.
LogFormatter
Implements the default log formatting.
Definition: LogFormatter.php:40
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
DeleteLogFormatter\getMessageParameters
getMessageParameters()
Formats parameters intented for action message from array of all parameters.There are three hardcoded...
Definition: DeleteLogFormatter.php:63
DeleteLogFormatter\formatParametersForApi
formatParametersForApi()
Format parameters for API output.
Definition: DeleteLogFormatter.php:327
LogFormatter\getLinkRenderer
getLinkRenderer()
Definition: LogFormatter.php:143
DeleteLogFormatter\getActionLinks
getActionLinks()
Returns extra links that comes after the action text, like "revert", etc.
Definition: DeleteLogFormatter.php:146
LogFormatter\extractParameters
extractParameters()
Extracts the optional extra parameters for use in action messages.
Definition: LogFormatter.php:525
DeleteLogFormatter
This class formats delete log entries.
Definition: DeleteLogFormatter.php:34
DeleteLogFormatter\$parsedParametersDeleteLog
array null $parsedParametersDeleteLog
Definition: DeleteLogFormatter.php:36
$type
$type
Definition: testCompression.php:52