3 * Formatter for protect log entries.
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.
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.
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
21 * @license GPL-2.0-or-later
25 use MediaWiki\MediaWikiServices
;
28 * This class formats protect log entries.
32 class ProtectLogFormatter
extends LogFormatter
{
33 public function getPreloadTitles() {
34 $subtype = $this->entry
->getSubtype();
35 if ( $subtype === 'move_prot' ) {
36 $params = $this->extractParameters();
37 return [ Title
::newFromText( $params[3] ) ];
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
53 protected function getMessageParameters() {
54 $params = parent
::getMessageParameters();
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];
65 // Very old way (nothing set)
69 if ( isset( $params[4] ) ) {
70 // handled in getMessageKey
73 } elseif ( $subtype === 'move_prot' ) {
74 $oldname = $this->makePageLink( Title
::newFromText( $params[3] ), [ 'redirect' => 'no' ] );
75 $params[3] = Message
::rawParam( $oldname );
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
90 // Show history link for all changes after the protection
91 $title = $this->entry
->getTarget();
93 $linkRenderer->makeLink( $title,
94 $this->msg( 'hist' )->text(),
97 'action' => 'history',
98 'offset' => $this->entry
->getTimestamp(),
103 // Show change protection link
104 if ( $this->context
->getUser()->isAllowed( 'protect' ) ) {
105 $links[] = $linkRenderer->makeKnownLink(
107 $this->msg( 'protect_change' )->text(),
109 [ 'action' => 'protect' ]
113 return $this->msg( 'parentheses' )->rawParams(
114 $this->context
->getLanguage()->pipeList( $links ) )->escaped();
117 protected function getParametersForApi() {
118 $entry = $this->entry
;
119 $subtype = $this->entry
->getSubtype();
120 $params = $entry->getParameters();
123 if ( $subtype === 'protect' ||
$subtype === 'modify' ) {
127 'details' => ':array:details',
129 } elseif ( $subtype === 'move_prot' ) {
132 '4::oldtitle' => '4:title:oldtitle',
135 foreach ( $map as $index => $key ) {
136 if ( isset( $params[$index] ) ) {
137 $params[$key] = $params[$index];
138 unset( $params[$index] );
142 // Change string to explicit boolean
143 if ( isset( $params['5:bool:cascade'] ) && is_string( $params['5:bool:cascade'] ) ) {
144 $params['5:bool:cascade'] = $params['5:bool:cascade'] === 'cascade';
150 public function formatParametersForApi() {
151 $ret = parent
::formatParametersForApi();
152 if ( isset( $ret['details'] ) && is_array( $ret['details'] ) ) {
153 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
154 foreach ( $ret['details'] as &$detail ) {
155 if ( isset( $detail['expiry'] ) ) {
156 $detail['expiry'] = $contLang->
157 formatExpiry( $detail['expiry'], TS_ISO_8601
, 'infinite' );
166 * Create the protect description to show in the log formatter
168 * @param array $details
171 public function createProtectDescription( array $details ) {
172 $protectDescription = '';
174 foreach ( $details as $param ) {
175 $expiryText = $this->formatExpiry( $param['expiry'] );
177 // Messages: restriction-edit, restriction-move, restriction-create,
178 // restriction-upload
179 $action = $this->context
->msg( 'restriction-' . $param['type'] )->escaped();
181 $protectionLevel = $param['level'];
182 // Messages: protect-level-autoconfirmed, protect-level-sysop
183 $message = $this->context
->msg( 'protect-level-' . $protectionLevel );
184 if ( $message->isDisabled() ) {
185 // Require "$1" permission
186 $restrictions = $this->context
->msg( "protect-fallback", $protectionLevel )->parse();
188 $restrictions = $message->escaped();
191 if ( $protectDescription !== '' ) {
192 $protectDescription .= $this->context
->msg( 'word-separator' )->escaped();
195 $protectDescription .= $this->context
->msg( 'protect-summary-desc' )
196 ->params( $action, $restrictions, $expiryText )->escaped();
199 return $protectDescription;
202 private function formatExpiry( $expiry ) {
203 if ( wfIsInfinity( $expiry ) ) {
204 return $this->context
->msg( 'protect-expiry-indefinite' )->text();
206 $lang = $this->context
->getLanguage();
207 $user = $this->context
->getUser();
208 return $this->context
->msg(
209 'protect-expiring-local',
210 $lang->userTimeAndDate( $expiry, $user ),
211 $lang->userDate( $expiry, $user ),
212 $lang->userTime( $expiry, $user )