*Default selected type to 'edit'
[lhc/web/wiklou.git] / includes / SpecialProtectedpages.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 *
9 * @addtogroup SpecialPage
10 */
11 class ProtectedPagesForm {
12 function showList( $msg = '' ) {
13 global $wgOut, $wgRequest;
14
15 $wgOut->setPagetitle( wfMsg( "protectedpages" ) );
16 if ( "" != $msg ) {
17 $wgOut->setSubtitle( $msg );
18 }
19
20 // Purge expired entries on one in every 10 queries
21 if ( !mt_rand( 0, 10 ) ) {
22 Title::purgeExpiredRestrictions();
23 }
24
25 $type = $wgRequest->getVal( 'type' );
26 $level = $wgRequest->getVal( 'level' );
27 $NS = $wgRequest->getIntOrNull( 'namespace' );
28
29 $pager = new ProtectedPagesPager( $this, array(), $type, $level, $NS );
30
31 $wgOut->addHTML( $this->showOptions( $NS, $type, $level ) );
32
33 if ( $pager->getNumRows() ) {
34 $s = $pager->getNavigationBar();
35 $s .= "<ul>" .
36 $pager->getBody() .
37 "</ul>";
38 $s .= $pager->getNavigationBar();
39 } else {
40 $s = '<p>' . wfMsgHTML( 'protectedpagesempty' ) . '</p>';
41 }
42 $wgOut->addHTML( $s );
43 }
44
45 /**
46 * Callback function to output a restriction
47 */
48 function formatRow( $row ) {
49 global $wgUser, $wgLang;
50
51 wfProfileIn( __METHOD__ );
52
53 static $skin=null;
54
55 if( is_null( $skin ) )
56 $skin = $wgUser->getSkin();
57
58 $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
59 $link = $skin->makeLinkObj( $title );
60
61 $description_items = array ();
62
63 $protType = wfMsgHtml( 'restriction-level-' . $row->pr_level );
64
65 $description_items[] = $protType;
66
67 $expiry_description = ''; $stxt = '';
68
69 if ( $row->pr_expiry != 'infinity' && strlen($row->pr_expiry) ) {
70 $expiry = Block::decodeExpiry( $row->pr_expiry );
71
72 $expiry_description = wfMsgForContent( 'protect-expiring', $wgLang->timeanddate( $expiry ) );
73
74 $description_items[] = $expiry_description;
75 }
76
77 if (!is_null($size = $row->page_len)) {
78 if ($size == 0)
79 $stxt = ' <small>' . wfMsgHtml('historyempty') . '</small>';
80 else
81 $stxt = ' <small>' . wfMsgHtml('historysize', $wgLang->formatNum( $size ) ) . '</small>';
82 }
83 wfProfileOut( __METHOD__ );
84
85 return '<li>' . wfSpecialList( $link . $stxt, implode( $description_items, ', ' ) ) . "</li>\n";
86 }
87
88 /**
89 * @param $namespace int
90 * @param $type string
91 * @param $level string
92 * @private
93 */
94 function showOptions( $namespace, $type, $level ) {
95 global $wgScript;
96 $type = ( $type ) ? $type : 'edit';
97 $action = htmlspecialchars( $wgScript );
98 $title = SpecialPage::getTitleFor( 'ProtectedPages' );
99 $special = htmlspecialchars( $title->getPrefixedDBkey() );
100 return "<form action=\"$action\" method=\"get\">\n" .
101 '<fieldset>' .
102 Xml::element( 'legend', array(), wfMsg( 'protectedpages' ) ) .
103 Xml::hidden( 'title', $special ) . "\n" .
104 $this->getNamespaceMenu( $namespace ) . "\n" .
105 $this->getTypeMenu( $type ) . "\n" .
106 $this->getLevelMenu( $level ) . "\n" .
107 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
108 "</fieldset></form>";
109 }
110
111 function getNamespaceMenu( $namespace=NULL ) {
112 return "<label for='namespace'>" . wfMsgHtml('namespace') . "</label>" . HTMLnamespaceselector($namespace, '');
113 }
114
115 /**
116 * @return string Formatted HTML
117 * @private
118 */
119 function getTypeMenu( $pr_type ) {
120 global $wgRestrictionTypes, $wgUser;
121
122 $out = "<select name='type'>\n";
123 $m = array(); // Temporary array
124
125 // First pass to load the log names
126 foreach( $wgRestrictionTypes as $type ) {
127 $text = wfMsgHtml("restriction-$type");
128 $m[$text] = $type;
129 }
130
131 // Second pass to sort by name
132 ksort($m);
133
134 // Third pass generates sorted XHTML content
135 foreach( $m as $text => $type ) {
136 $selected = ($type == $pr_type );
137 $out .= Xml::option( $text, $type, $selected ) . "\n";
138 }
139
140 $out .= '</select>';
141 return "<label for='type'>" . wfMsgHtml('restriction-type') . "</label>: " . $out;
142 }
143
144 /**
145 * @return string Formatted HTML
146 * @private
147 */
148 function getLevelMenu( $pr_level ) {
149 global $wgRestrictionLevels, $wgUser;
150
151 $out = "<select name='level'>\n";
152 $m = array( wfMsgHtml('restriction-level-all') => 0 ); // Temporary array
153
154 // First pass to load the log names
155 foreach( $wgRestrictionLevels as $type ) {
156 if ( $type !='' && $type !='*') {
157 $text = wfMsgHtml("restriction-level-$type");
158 $m[$text] = $type;
159 }
160 }
161
162 // Second pass to sort by name
163 ksort($m);
164
165 // Third pass generates sorted XHTML content
166 foreach( $m as $text => $type ) {
167 $selected = ($type == $pr_level );
168 $out .= Xml::option( $text, $type, $selected ) . "\n";
169 }
170
171 $out .= '</select>';
172 return "<label for='level'>" . wfMsgHtml('restriction-level') . "</label>: " . $out;
173 }
174 }
175
176 /**
177 *
178 *
179 */
180 class ProtectedPagesPager extends ReverseChronologicalPager {
181 public $mForm, $mConds;
182
183 function __construct( $form, $conds = array(), $type, $level, $namespace ) {
184 $this->mForm = $form;
185 $this->mConds = $conds;
186 $this->type = ( $type ) ? $type : 'edit';
187 $this->level = $level;
188 $this->namespace = $namespace;
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 $name = str_replace( ' ', '_', $row->page_title );
200 $lb->add( $row->page_namespace, $name );
201 }
202
203 $lb->execute();
204 wfProfileOut( __METHOD__ );
205 return '';
206 }
207
208 function formatRow( $row ) {
209 $block = new Block;
210 return $this->mForm->formatRow( $row );
211 }
212
213 function getQueryInfo() {
214 $conds = $this->mConds;
215 $conds[] = 'pr_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
216 $conds[] = 'page_id=pr_page';
217 $conds[] = 'pr_type=' . $this->mDb->addQuotes( $this->type );
218 if ( $this->level )
219 $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
220 if ( !is_null($this->namespace) )
221 $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
222 return array(
223 'tables' => array( 'page_restrictions', 'page' ),
224 'fields' => 'max(pr_id) AS pr_id,page_namespace,page_title,page_len,pr_type,pr_level,pr_expiry',
225 'conds' => $conds,
226 'options' => array( 'GROUP BY' => 'page_namespace,page_title,pr_level,pr_expiry,page_len,pr_type' ),
227 );
228 }
229
230 function getIndexField() {
231 return 'page_len';
232 }
233 }
234
235 /**
236 * Constructor
237 */
238 function wfSpecialProtectedpages() {
239
240 list( $limit, $offset ) = wfCheckLimits();
241
242 $ppForm = new ProtectedPagesForm();
243
244 $ppForm->showList();
245 }
246
247 ?>