Correct the address of the FSF in some of the GPL headers
[lhc/web/wiklou.git] / includes / specials / SpecialProtectedtitles.php
1 <?php
2 /**
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19
20 /**
21 * @file
22 * @ingroup SpecialPage
23 */
24
25 /**
26 * @todo document
27 * @ingroup SpecialPage
28 */
29 class ProtectedTitlesForm {
30
31 protected $IdLevel = 'level';
32 protected $IdType = 'type';
33
34 function showList( $msg = '' ) {
35 global $wgOut, $wgRequest;
36
37 if ( $msg != "" ) {
38 $wgOut->setSubtitle( $msg );
39 }
40
41 // Purge expired entries on one in every 10 queries
42 if ( !mt_rand( 0, 10 ) ) {
43 Title::purgeExpiredRestrictions();
44 }
45
46 $type = $wgRequest->getVal( $this->IdType );
47 $level = $wgRequest->getVal( $this->IdLevel );
48 $sizetype = $wgRequest->getVal( 'sizetype' );
49 $size = $wgRequest->getIntOrNull( 'size' );
50 $NS = $wgRequest->getIntOrNull( 'namespace' );
51
52 $pager = new ProtectedTitlesPager( $this, array(), $type, $level, $NS, $sizetype, $size );
53
54 $wgOut->addHTML( $this->showOptions( $NS, $type, $level, $sizetype, $size ) );
55
56 if ( $pager->getNumRows() ) {
57 $s = $pager->getNavigationBar();
58 $s .= "<ul>" .
59 $pager->getBody() .
60 "</ul>";
61 $s .= $pager->getNavigationBar();
62 } else {
63 $s = '<p>' . wfMsgHtml( 'protectedtitlesempty' ) . '</p>';
64 }
65 $wgOut->addHTML( $s );
66 }
67
68 /**
69 * Callback function to output a restriction
70 */
71 function formatRow( $row ) {
72 global $wgUser, $wgLang, $wgContLang;
73
74 wfProfileIn( __METHOD__ );
75
76 static $skin=null;
77
78 if( is_null( $skin ) )
79 $skin = $wgUser->getSkin();
80
81 $title = Title::makeTitleSafe( $row->pt_namespace, $row->pt_title );
82 $link = $skin->link( $title );
83
84 $description_items = array ();
85
86 $protType = wfMsgHtml( 'restriction-level-' . $row->pt_create_perm );
87
88 $description_items[] = $protType;
89
90 $expiry_description = ''; $stxt = '';
91
92 if ( $row->pt_expiry != 'infinity' && strlen($row->pt_expiry) ) {
93 $expiry = Block::decodeExpiry( $row->pt_expiry );
94
95 $expiry_description = wfMsg( 'protect-expiring', $wgLang->timeanddate( $expiry ) , $wgLang->date( $expiry ) , $wgLang->time( $expiry ) );
96
97 $description_items[] = $expiry_description;
98 }
99
100 wfProfileOut( __METHOD__ );
101
102 return '<li>' . wfSpecialList( $link . $stxt, implode( $description_items, ', ' ) ) . "</li>\n";
103 }
104
105 /**
106 * @param $namespace Integer:
107 * @param $type string
108 * @param $level string
109 * @param $sizetype Unused
110 * @param $size Unused
111 * @private
112 */
113 function showOptions( $namespace, $type='edit', $level, $sizetype, $size ) {
114 global $wgScript;
115 $action = htmlspecialchars( $wgScript );
116 $title = SpecialPage::getTitleFor( 'Protectedtitles' );
117 $special = htmlspecialchars( $title->getPrefixedDBkey() );
118 return "<form action=\"$action\" method=\"get\">\n" .
119 '<fieldset>' .
120 Xml::element( 'legend', array(), wfMsg( 'protectedtitles' ) ) .
121 Xml::hidden( 'title', $special ) . "&#160;\n" .
122 $this->getNamespaceMenu( $namespace ) . "&#160;\n" .
123 $this->getLevelMenu( $level ) . "&#160;\n" .
124 "&#160;" . Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
125 "</fieldset></form>";
126 }
127
128 /**
129 * Prepare the namespace filter drop-down; standard namespace
130 * selector, sans the MediaWiki namespace
131 *
132 * @param $namespace Mixed: pre-select namespace
133 * @return string
134 */
135 function getNamespaceMenu( $namespace = null ) {
136 return Xml::label( wfMsg( 'namespace' ), 'namespace' )
137 . '&#160;'
138 . Xml::namespaceSelector( $namespace, '' );
139 }
140
141 /**
142 * @return string Formatted HTML
143 * @private
144 */
145 function getLevelMenu( $pr_level ) {
146 global $wgRestrictionLevels;
147
148 $m = array( wfMsg('restriction-level-all') => 0 ); // Temporary array
149 $options = array();
150
151 // First pass to load the log names
152 foreach( $wgRestrictionLevels as $type ) {
153 if ( $type !='' && $type !='*') {
154 $text = wfMsg("restriction-level-$type");
155 $m[$text] = $type;
156 }
157 }
158 // Is there only one level (aside from "all")?
159 if( count($m) <= 2 ) {
160 return '';
161 }
162 // Third pass generates sorted XHTML content
163 foreach( $m as $text => $type ) {
164 $selected = ($type == $pr_level );
165 $options[] = Xml::option( $text, $type, $selected );
166 }
167
168 return
169 Xml::label( wfMsg('restriction-level') , $this->IdLevel ) . '&#160;' .
170 Xml::tags( 'select',
171 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
172 implode( "\n", $options ) );
173 }
174 }
175
176 /**
177 * @todo document
178 * @ingroup Pager
179 */
180 class ProtectedtitlesPager extends AlphabeticPager {
181 public $mForm, $mConds;
182
183 function __construct( $form, $conds = array(), $type, $level, $namespace, $sizetype='', $size=0 ) {
184 $this->mForm = $form;
185 $this->mConds = $conds;
186 $this->level = $level;
187 $this->namespace = $namespace;
188 $this->size = intval($size);
189 parent::__construct();
190 }
191
192 function getStartBody() {
193 wfProfileIn( __METHOD__ );
194 # Do a link batch query
195 $this->mResult->seek( 0 );
196 $lb = new LinkBatch;
197
198 while ( $row = $this->mResult->fetchObject() ) {
199 $lb->add( $row->pt_namespace, $row->pt_title );
200 }
201
202 $lb->execute();
203 wfProfileOut( __METHOD__ );
204 return '';
205 }
206
207 function formatRow( $row ) {
208 return $this->mForm->formatRow( $row );
209 }
210
211 function getQueryInfo() {
212 $conds = $this->mConds;
213 $conds[] = 'pt_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
214 if( $this->level )
215 $conds['pt_create_perm'] = $this->level;
216 if( !is_null($this->namespace) )
217 $conds[] = 'pt_namespace=' . $this->mDb->addQuotes( $this->namespace );
218 return array(
219 'tables' => 'protected_titles',
220 'fields' => 'pt_namespace,pt_title,pt_create_perm,pt_expiry,pt_timestamp',
221 'conds' => $conds
222 );
223 }
224
225 function getIndexField() {
226 return 'pt_timestamp';
227 }
228 }
229
230 /**
231 * Constructor
232 */
233 function wfSpecialProtectedtitles() {
234
235 $ppForm = new ProtectedTitlesForm();
236
237 $ppForm->showList();
238 }