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