Split logging classes to individual files
[lhc/web/wiklou.git] / includes / logging / LegacyLogFormatter.php
1 <?php
2 /**
3 * Contains a class for formatting log legacy entries
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Niklas Laxström
22 * @license GPL-2.0-or-later
23 * @since 1.19
24 */
25
26 /**
27 * This class formats all log entries for log types
28 * which have not been converted to the new system.
29 * This is not about old log entries which store
30 * parameters in a different format - the new
31 * LogFormatter classes have code to support formatting
32 * those too.
33 * @since 1.19
34 */
35 class LegacyLogFormatter extends LogFormatter {
36 /**
37 * Backward compatibility for extension changing the comment from
38 * the LogLine hook. This will be set by the first call on getComment(),
39 * then it might be modified by the hook when calling getActionLinks(),
40 * so that the modified value will be returned when calling getComment()
41 * a second time.
42 *
43 * @var string|null
44 */
45 private $comment = null;
46
47 /**
48 * Cache for the result of getActionLinks() so that it does not need to
49 * run multiple times depending on the order that getComment() and
50 * getActionLinks() are called.
51 *
52 * @var string|null
53 */
54 private $revert = null;
55
56 public function getComment() {
57 if ( $this->comment === null ) {
58 $this->comment = parent::getComment();
59 }
60
61 // Make sure we execute the LogLine hook so that we immediately return
62 // the correct value.
63 if ( $this->revert === null ) {
64 $this->getActionLinks();
65 }
66
67 return $this->comment;
68 }
69
70 /**
71 * @return string
72 * @return-taint onlysafefor_html
73 */
74 protected function getActionMessage() {
75 $entry = $this->entry;
76 $action = LogPage::actionText(
77 $entry->getType(),
78 $entry->getSubtype(),
79 $entry->getTarget(),
80 $this->plaintext ? null : $this->context->getSkin(),
81 (array)$entry->getParameters(),
82 !$this->plaintext // whether to filter [[]] links
83 );
84
85 $performer = $this->getPerformerElement();
86 if ( !$this->irctext ) {
87 $sep = $this->msg( 'word-separator' );
88 $sep = $this->plaintext ? $sep->text() : $sep->escaped();
89 $action = $performer . $sep . $action;
90 }
91
92 return $action;
93 }
94
95 public function getActionLinks() {
96 if ( $this->revert !== null ) {
97 return $this->revert;
98 }
99
100 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) ) {
101 $this->revert = '';
102 return $this->revert;
103 }
104
105 $title = $this->entry->getTarget();
106 $type = $this->entry->getType();
107 $subtype = $this->entry->getSubtype();
108
109 // Do nothing. The implementation is handled by the hook modifiying the
110 // passed-by-ref parameters. This also changes the default value so that
111 // getComment() and getActionLinks() do not call them indefinitely.
112 $this->revert = '';
113
114 // This is to populate the $comment member of this instance so that it
115 // can be modified when calling the hook just below.
116 if ( $this->comment === null ) {
117 $this->getComment();
118 }
119
120 $params = $this->entry->getParameters();
121
122 Hooks::run( 'LogLine', [ $type, $subtype, $title, $params,
123 &$this->comment, &$this->revert, $this->entry->getTimestamp() ] );
124
125 return $this->revert;
126 }
127 }