Merge "Use new log system when create log entry for revision delete"
[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, array(), $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
71 static $infinity = null;
72
73 if ( is_null( $infinity ) ) {
74 $infinity = wfGetDB( DB_SLAVE )->getInfinity();
75 }
76
77 $title = Title::makeTitleSafe( $row->pt_namespace, $row->pt_title );
78 if ( !$title ) {
79
80 return Html::rawElement(
81 'li',
82 array(),
83 Html::element(
84 'span',
85 array( 'class' => 'mw-invalidtitle' ),
86 Linker::getInvalidTitleDescription(
87 $this->getContext(),
88 $row->pt_namespace,
89 $row->pt_title
90 )
91 )
92 ) . "\n";
93 }
94
95 $link = Linker::link( $title );
96 $description_items = array();
97 // Messages: restriction-level-sysop, restriction-level-autoconfirmed
98 $protType = $this->msg( 'restriction-level-' . $row->pt_create_perm )->escaped();
99 $description_items[] = $protType;
100 $lang = $this->getLanguage();
101 $expiry = strlen( $row->pt_expiry ) ?
102 $lang->formatExpiry( $row->pt_expiry, TS_MW ) :
103 $infinity;
104
105 if ( $expiry != $infinity ) {
106 $user = $this->getUser();
107 $description_items[] = $this->msg(
108 'protect-expiring-local',
109 $lang->userTimeAndDate( $expiry, $user ),
110 $lang->userDate( $expiry, $user ),
111 $lang->userTime( $expiry, $user )
112 )->escaped();
113 }
114
115 // @todo i18n: This should use a comma separator instead of a hard coded comma, right?
116 return '<li>' . $lang->specialList( $link, implode( $description_items, ', ' ) ) . "</li>\n";
117 }
118
119 /**
120 * @param int $namespace
121 * @param string $type
122 * @param string $level
123 * @return string
124 * @private
125 */
126 function showOptions( $namespace, $type = 'edit', $level ) {
127 $action = htmlspecialchars( wfScript() );
128 $title = $this->getPageTitle();
129 $special = htmlspecialchars( $title->getPrefixedDBkey() );
130
131 return "<form action=\"$action\" method=\"get\">\n" .
132 '<fieldset>' .
133 Xml::element( 'legend', array(), $this->msg( 'protectedtitles' )->text() ) .
134 Html::hidden( 'title', $special ) . "&#160;\n" .
135 $this->getNamespaceMenu( $namespace ) . "&#160;\n" .
136 $this->getLevelMenu( $level ) . "&#160;\n" .
137 "&#160;" . Xml::submitButton( $this->msg( 'allpagessubmit' )->text() ) . "\n" .
138 "</fieldset></form>";
139 }
140
141 /**
142 * Prepare the namespace filter drop-down; standard namespace
143 * selector, sans the MediaWiki namespace
144 *
145 * @param string|null $namespace Pre-select namespace
146 * @return string
147 */
148 function getNamespaceMenu( $namespace = null ) {
149 return Html::namespaceSelector(
150 array(
151 'selected' => $namespace,
152 'all' => '',
153 'label' => $this->msg( 'namespace' )->text()
154 ), array(
155 'name' => 'namespace',
156 'id' => 'namespace',
157 'class' => 'namespaceselector',
158 )
159 );
160 }
161
162 /**
163 * @param string $pr_level Determines which option is selected as default
164 * @return string Formatted HTML
165 * @private
166 */
167 function getLevelMenu( $pr_level ) {
168 // Temporary array
169 $m = array( $this->msg( 'restriction-level-all' )->text() => 0 );
170 $options = array();
171
172 // First pass to load the log names
173 foreach ( $this->getConfig()->get( 'RestrictionLevels' ) as $type ) {
174 if ( $type != '' && $type != '*' ) {
175 // Messages: restriction-level-sysop, restriction-level-autoconfirmed
176 $text = $this->msg( "restriction-level-$type" )->text();
177 $m[$text] = $type;
178 }
179 }
180
181 // Is there only one level (aside from "all")?
182 if ( count( $m ) <= 2 ) {
183 return '';
184 }
185 // Third pass generates sorted XHTML content
186 foreach ( $m as $text => $type ) {
187 $selected = ( $type == $pr_level );
188 $options[] = Xml::option( $text, $type, $selected );
189 }
190
191 return Xml::label( $this->msg( 'restriction-level' )->text(), $this->IdLevel ) . '&#160;' .
192 Xml::tags( 'select',
193 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
194 implode( "\n", $options ) );
195 }
196
197 protected function getGroupName() {
198 return 'maintenance';
199 }
200 }
201
202 /**
203 * @todo document
204 * @ingroup Pager
205 */
206 class ProtectedTitlesPager extends AlphabeticPager {
207 public $mForm, $mConds;
208
209 function __construct( $form, $conds = array(), $type, $level, $namespace,
210 $sizetype = '', $size = 0
211 ) {
212 $this->mForm = $form;
213 $this->mConds = $conds;
214 $this->level = $level;
215 $this->namespace = $namespace;
216 $this->size = intval( $size );
217 parent::__construct( $form->getContext() );
218 }
219
220 function getStartBody() {
221 # Do a link batch query
222 $this->mResult->seek( 0 );
223 $lb = new LinkBatch;
224
225 foreach ( $this->mResult as $row ) {
226 $lb->add( $row->pt_namespace, $row->pt_title );
227 }
228
229 $lb->execute();
230
231 return '';
232 }
233
234 /**
235 * @return Title
236 */
237 function getTitle() {
238 return $this->mForm->getTitle();
239 }
240
241 function formatRow( $row ) {
242 return $this->mForm->formatRow( $row );
243 }
244
245 /**
246 * @return array
247 */
248 function getQueryInfo() {
249 $conds = $this->mConds;
250 $conds[] = 'pt_expiry > ' . $this->mDb->addQuotes( $this->mDb->timestamp() ) .
251 ' OR pt_expiry IS NULL';
252 if ( $this->level ) {
253 $conds['pt_create_perm'] = $this->level;
254 }
255
256 if ( !is_null( $this->namespace ) ) {
257 $conds[] = 'pt_namespace=' . $this->mDb->addQuotes( $this->namespace );
258 }
259
260 return array(
261 'tables' => 'protected_titles',
262 'fields' => array( 'pt_namespace', 'pt_title', 'pt_create_perm',
263 'pt_expiry', 'pt_timestamp' ),
264 'conds' => $conds
265 );
266 }
267
268 function getIndexField() {
269 return 'pt_timestamp';
270 }
271 }