Merge "Rewrite pref cleanup script"
[lhc/web/wiklou.git] / includes / specials / SpecialProtectedtitles.php
1 <?php
2 /**
3 * Implements Special:Protectedtitles
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 * @ingroup SpecialPage
22 */
23
24 /**
25 * A special page that list protected titles from creation
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialProtectedtitles extends SpecialPage {
30 protected $IdLevel = 'level';
31 protected $IdType = 'type';
32
33 public function __construct() {
34 parent::__construct( 'Protectedtitles' );
35 }
36
37 function execute( $par ) {
38 $this->setHeaders();
39 $this->outputHeader();
40
41 $request = $this->getRequest();
42 $type = $request->getVal( $this->IdType );
43 $level = $request->getVal( $this->IdLevel );
44 $sizetype = $request->getVal( 'sizetype' );
45 $size = $request->getIntOrNull( 'size' );
46 $NS = $request->getIntOrNull( 'namespace' );
47
48 $pager = new ProtectedTitlesPager( $this, [], $type, $level, $NS, $sizetype, $size );
49
50 $this->getOutput()->addHTML( $this->showOptions( $NS, $type, $level ) );
51
52 if ( $pager->getNumRows() ) {
53 $this->getOutput()->addHTML(
54 $pager->getNavigationBar() .
55 '<ul>' . $pager->getBody() . '</ul>' .
56 $pager->getNavigationBar()
57 );
58 } else {
59 $this->getOutput()->addWikiMsg( 'protectedtitlesempty' );
60 }
61 }
62
63 /**
64 * Callback function to output a restriction
65 *
66 * @param object $row Database row
67 * @return string
68 */
69 function formatRow( $row ) {
70 $title = Title::makeTitleSafe( $row->pt_namespace, $row->pt_title );
71 if ( !$title ) {
72 return Html::rawElement(
73 'li',
74 [],
75 Html::element(
76 'span',
77 [ 'class' => 'mw-invalidtitle' ],
78 Linker::getInvalidTitleDescription(
79 $this->getContext(),
80 $row->pt_namespace,
81 $row->pt_title
82 )
83 )
84 ) . "\n";
85 }
86
87 $link = $this->getLinkRenderer()->makeLink( $title );
88 // Messages: restriction-level-sysop, restriction-level-autoconfirmed
89 $description = $this->msg( 'restriction-level-' . $row->pt_create_perm )->escaped();
90 $lang = $this->getLanguage();
91 $expiry = strlen( $row->pt_expiry ) ?
92 $lang->formatExpiry( $row->pt_expiry, TS_MW ) :
93 'infinity';
94
95 if ( $expiry !== 'infinity' ) {
96 $user = $this->getUser();
97 $description .= $this->msg( 'comma-separator' )->escaped() . $this->msg(
98 'protect-expiring-local',
99 $lang->userTimeAndDate( $expiry, $user ),
100 $lang->userDate( $expiry, $user ),
101 $lang->userTime( $expiry, $user )
102 )->escaped();
103 }
104
105 return '<li>' . $lang->specialList( $link, $description ) . "</li>\n";
106 }
107
108 /**
109 * @param int $namespace
110 * @param string $type
111 * @param string $level
112 * @return string
113 * @private
114 */
115 function showOptions( $namespace, $type = 'edit', $level ) {
116 $action = htmlspecialchars( wfScript() );
117 $title = $this->getPageTitle();
118 $special = htmlspecialchars( $title->getPrefixedDBkey() );
119
120 return "<form action=\"$action\" method=\"get\">\n" .
121 '<fieldset>' .
122 Xml::element( 'legend', [], $this->msg( 'protectedtitles' )->text() ) .
123 Html::hidden( 'title', $special ) . "&#160;\n" .
124 $this->getNamespaceMenu( $namespace ) . "&#160;\n" .
125 $this->getLevelMenu( $level ) . "&#160;\n" .
126 "&#160;" . Xml::submitButton( $this->msg( 'protectedtitles-submit' )->text() ) . "\n" .
127 "</fieldset></form>";
128 }
129
130 /**
131 * Prepare the namespace filter drop-down; standard namespace
132 * selector, sans the MediaWiki namespace
133 *
134 * @param string|null $namespace Pre-select namespace
135 * @return string
136 */
137 function getNamespaceMenu( $namespace = null ) {
138 return Html::namespaceSelector(
139 [
140 'selected' => $namespace,
141 'all' => '',
142 'label' => $this->msg( 'namespace' )->text()
143 ], [
144 'name' => 'namespace',
145 'id' => 'namespace',
146 'class' => 'namespaceselector',
147 ]
148 );
149 }
150
151 /**
152 * @param string $pr_level Determines which option is selected as default
153 * @return string Formatted HTML
154 * @private
155 */
156 function getLevelMenu( $pr_level ) {
157 // Temporary array
158 $m = [ $this->msg( 'restriction-level-all' )->text() => 0 ];
159 $options = [];
160
161 // First pass to load the log names
162 foreach ( $this->getConfig()->get( 'RestrictionLevels' ) as $type ) {
163 if ( $type != '' && $type != '*' ) {
164 // Messages: restriction-level-sysop, restriction-level-autoconfirmed
165 $text = $this->msg( "restriction-level-$type" )->text();
166 $m[$text] = $type;
167 }
168 }
169
170 // Is there only one level (aside from "all")?
171 if ( count( $m ) <= 2 ) {
172 return '';
173 }
174 // Third pass generates sorted XHTML content
175 foreach ( $m as $text => $type ) {
176 $selected = ( $type == $pr_level );
177 $options[] = Xml::option( $text, $type, $selected );
178 }
179
180 return Xml::label( $this->msg( 'restriction-level' )->text(), $this->IdLevel ) . '&#160;' .
181 Xml::tags( 'select',
182 [ 'id' => $this->IdLevel, 'name' => $this->IdLevel ],
183 implode( "\n", $options ) );
184 }
185
186 protected function getGroupName() {
187 return 'maintenance';
188 }
189 }