Use Language::userTimeAndDate() instead of Language::timeanddate() to display timesta...
[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
31 protected $IdLevel = 'level';
32 protected $IdType = 'type';
33
34 public function __construct() {
35 parent::__construct( 'Protectedtitles' );
36 }
37
38 function execute( $par ) {
39 $this->setHeaders();
40 $this->outputHeader();
41
42 // Purge expired entries on one in every 10 queries
43 if ( !mt_rand( 0, 10 ) ) {
44 Title::purgeExpiredRestrictions();
45 }
46
47 $request = $this->getRequest();
48 $type = $request->getVal( $this->IdType );
49 $level = $request->getVal( $this->IdLevel );
50 $sizetype = $request->getVal( 'sizetype' );
51 $size = $request->getIntOrNull( 'size' );
52 $NS = $request->getIntOrNull( 'namespace' );
53
54 $pager = new ProtectedTitlesPager( $this, array(), $type, $level, $NS, $sizetype, $size );
55
56 $this->getOutput()->addHTML( $this->showOptions( $NS, $type, $level ) );
57
58 if ( $pager->getNumRows() ) {
59 $s = $pager->getNavigationBar();
60 $s .= "<ul>" .
61 $pager->getBody() .
62 "</ul>";
63 $s .= $pager->getNavigationBar();
64 } else {
65 $s = '<p>' . wfMsgHtml( 'protectedtitlesempty' ) . '</p>';
66 }
67 $this->getOutput()->addHTML( $s );
68 }
69
70 /**
71 * Callback function to output a restriction
72 *
73 * @return string
74 */
75 function formatRow( $row ) {
76 wfProfileIn( __METHOD__ );
77
78 static $infinity = null;
79
80 if( is_null( $infinity ) ){
81 $infinity = wfGetDB( DB_SLAVE )->getInfinity();
82 }
83
84 $title = Title::makeTitleSafe( $row->pt_namespace, $row->pt_title );
85 $link = Linker::link( $title );
86
87 $description_items = array ();
88
89 $protType = wfMsgHtml( 'restriction-level-' . $row->pt_create_perm );
90
91 $description_items[] = $protType;
92
93 $lang = $this->getLanguage();
94 $expiry = strlen( $row->pt_expiry ) ? $lang->formatExpiry( $row->pt_expiry, TS_MW ) : $infinity;
95 if( $expiry != $infinity ) {
96 $expiry_description = wfMsg(
97 'protect-expiring-local',
98 $lang->timeanddate( $expiry, true ),
99 $lang->date( $expiry, true ),
100 $lang->time( $expiry, true )
101 );
102
103 $description_items[] = htmlspecialchars($expiry_description);
104 }
105
106 wfProfileOut( __METHOD__ );
107
108 return '<li>' . $lang->specialList( $link, implode( $description_items, ', ' ) ) . "</li>\n";
109 }
110
111 /**
112 * @param $namespace Integer:
113 * @param $type string
114 * @param $level string
115 * @private
116 */
117 function showOptions( $namespace, $type='edit', $level ) {
118 global $wgScript;
119 $action = htmlspecialchars( $wgScript );
120 $title = $this->getTitle();
121 $special = htmlspecialchars( $title->getPrefixedDBkey() );
122 return "<form action=\"$action\" method=\"get\">\n" .
123 '<fieldset>' .
124 Xml::element( 'legend', array(), wfMsg( 'protectedtitles' ) ) .
125 Html::hidden( 'title', $special ) . "&#160;\n" .
126 $this->getNamespaceMenu( $namespace ) . "&#160;\n" .
127 $this->getLevelMenu( $level ) . "&#160;\n" .
128 "&#160;" . Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
129 "</fieldset></form>";
130 }
131
132 /**
133 * Prepare the namespace filter drop-down; standard namespace
134 * selector, sans the MediaWiki namespace
135 *
136 * @param $namespace Mixed: pre-select namespace
137 * @return string
138 */
139 function getNamespaceMenu( $namespace = null ) {
140 return Xml::label( wfMsg( 'namespace' ), 'namespace' )
141 . '&#160;'
142 . Xml::namespaceSelector( $namespace, '' );
143 }
144
145 /**
146 * @return string Formatted HTML
147 * @private
148 */
149 function getLevelMenu( $pr_level ) {
150 global $wgRestrictionLevels;
151
152 $m = array( wfMsg('restriction-level-all') => 0 ); // Temporary array
153 $options = array();
154
155 // First pass to load the log names
156 foreach( $wgRestrictionLevels as $type ) {
157 if ( $type !='' && $type !='*') {
158 $text = wfMsg("restriction-level-$type");
159 $m[$text] = $type;
160 }
161 }
162 // Is there only one level (aside from "all")?
163 if( count($m) <= 2 ) {
164 return '';
165 }
166 // Third pass generates sorted XHTML content
167 foreach( $m as $text => $type ) {
168 $selected = ($type == $pr_level );
169 $options[] = Xml::option( $text, $type, $selected );
170 }
171
172 return
173 Xml::label( wfMsg('restriction-level') , $this->IdLevel ) . '&#160;' .
174 Xml::tags( 'select',
175 array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
176 implode( "\n", $options ) );
177 }
178 }
179
180 /**
181 * @todo document
182 * @ingroup Pager
183 */
184 class ProtectedTitlesPager extends AlphabeticPager {
185 public $mForm, $mConds;
186
187 function __construct( $form, $conds = array(), $type, $level, $namespace, $sizetype='', $size=0 ) {
188 $this->mForm = $form;
189 $this->mConds = $conds;
190 $this->level = $level;
191 $this->namespace = $namespace;
192 $this->size = intval($size);
193 parent::__construct( $form->getContext() );
194 }
195
196 function getStartBody() {
197 wfProfileIn( __METHOD__ );
198 # Do a link batch query
199 $this->mResult->seek( 0 );
200 $lb = new LinkBatch;
201
202 foreach ( $this->mResult as $row ) {
203 $lb->add( $row->pt_namespace, $row->pt_title );
204 }
205
206 $lb->execute();
207 wfProfileOut( __METHOD__ );
208 return '';
209 }
210
211 /**
212 * @return Title
213 */
214 function getTitle() {
215 return SpecialPage::getTitleFor( 'Protectedtitles' );
216 }
217
218 function formatRow( $row ) {
219 return $this->mForm->formatRow( $row );
220 }
221
222 /**
223 * @return array
224 */
225 function getQueryInfo() {
226 $conds = $this->mConds;
227 $conds[] = 'pt_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
228 if( $this->level )
229 $conds['pt_create_perm'] = $this->level;
230 if( !is_null($this->namespace) )
231 $conds[] = 'pt_namespace=' . $this->mDb->addQuotes( $this->namespace );
232 return array(
233 'tables' => 'protected_titles',
234 'fields' => 'pt_namespace,pt_title,pt_create_perm,pt_expiry,pt_timestamp',
235 'conds' => $conds
236 );
237 }
238
239 function getIndexField() {
240 return 'pt_timestamp';
241 }
242 }
243