SpecialProtectedpages.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /**
  3. * @file
  4. * @ingroup SpecialPage
  5. */
  6. /**
  7. * @todo document
  8. * @ingroup SpecialPage
  9. */
  10. class ProtectedPagesForm {
  11. protected $IdLevel = 'level';
  12. protected $IdType = 'type';
  13. public function showList( $msg = '' ) {
  14. global $wgOut, $wgRequest;
  15. if( "" != $msg ) {
  16. $wgOut->setSubtitle( $msg );
  17. }
  18. // Purge expired entries on one in every 10 queries
  19. if( !mt_rand( 0, 10 ) ) {
  20. Title::purgeExpiredRestrictions();
  21. }
  22. $type = $wgRequest->getVal( $this->IdType );
  23. $level = $wgRequest->getVal( $this->IdLevel );
  24. $sizetype = $wgRequest->getVal( 'sizetype' );
  25. $size = $wgRequest->getIntOrNull( 'size' );
  26. $NS = $wgRequest->getIntOrNull( 'namespace' );
  27. $indefOnly = $wgRequest->getBool( 'indefonly' ) ? 1 : 0;
  28. $cascadeOnly = $wgRequest->getBool('cascadeonly') ? 1 : 0;
  29. $pager = new ProtectedPagesPager( $this, array(), $type, $level, $NS, $sizetype, $size, $indefOnly, $cascadeOnly );
  30. $wgOut->addHTML( $this->showOptions( $NS, $type, $level, $sizetype, $size, $indefOnly, $cascadeOnly ) );
  31. if( $pager->getNumRows() ) {
  32. $s = $pager->getNavigationBar();
  33. $s .= "<ul>" .
  34. $pager->getBody() .
  35. "</ul>";
  36. $s .= $pager->getNavigationBar();
  37. } else {
  38. $s = '<p>' . wfMsgHtml( 'protectedpagesempty' ) . '</p>';
  39. }
  40. $wgOut->addHTML( $s );
  41. }
  42. /**
  43. * Callback function to output a restriction
  44. * @param $row object Protected title
  45. * @return string Formatted <li> element
  46. */
  47. public function formatRow( $row ) {
  48. global $wgUser, $wgLang, $wgContLang;
  49. wfProfileIn( __METHOD__ );
  50. static $skin=null;
  51. if( is_null( $skin ) )
  52. $skin = $wgUser->getSkin();
  53. $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
  54. $link = $skin->makeLinkObj( $title );
  55. $description_items = array ();
  56. $protType = wfMsgHtml( 'restriction-level-' . $row->pr_level );
  57. $description_items[] = $protType;
  58. if( $row->pr_cascade ) {
  59. $description_items[] = wfMsg( 'protect-summary-cascade' );
  60. }
  61. $expiry_description = '';
  62. $stxt = '';
  63. if( $row->pr_expiry != 'infinity' && strlen($row->pr_expiry) ) {
  64. $expiry = Block::decodeExpiry( $row->pr_expiry );
  65. $expiry_description = wfMsg( 'protect-expiring' , $wgLang->timeanddate( $expiry ) ,
  66. $wgLang->date( $expiry ) , $wgLang->time( $expiry ) );
  67. $description_items[] = $expiry_description;
  68. }
  69. if(!is_null($size = $row->page_len)) {
  70. $stxt = $wgContLang->getDirMark() . ' ' . $skin->formatRevisionSize( $size );
  71. }
  72. # Show a link to the change protection form for allowed users otherwise a link to the protection log
  73. if( $wgUser->isAllowed( 'protect' ) ) {
  74. $changeProtection = ' (' . $skin->makeKnownLinkObj( $title, wfMsgHtml( 'protect_change' ),
  75. 'action=unprotect' ) . ')';
  76. } else {
  77. $ltitle = SpecialPage::getTitleFor( 'Log' );
  78. $changeProtection = ' (' . $skin->makeKnownLinkObj( $ltitle, wfMsgHtml( 'protectlogpage' ),
  79. 'type=protect&page=' . $title->getPrefixedUrl() ) . ')';
  80. }
  81. wfProfileOut( __METHOD__ );
  82. return '<li>' . wfSpecialList( $link . $stxt, implode( $description_items, ', ' ) ) . $changeProtection . "</li>\n";
  83. }
  84. /**
  85. * @param $namespace int
  86. * @param $type string
  87. * @param $level string
  88. * @param $minsize int
  89. * @param $indefOnly bool
  90. * @param $cascadeOnly bool
  91. * @return string Input form
  92. * @private
  93. */
  94. protected function showOptions( $namespace, $type='edit', $level, $sizetype, $size, $indefOnly, $cascadeOnly ) {
  95. global $wgScript;
  96. $title = SpecialPage::getTitleFor( 'ProtectedPages' );
  97. return Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
  98. Xml::openElement( 'fieldset' ) .
  99. Xml::element( 'legend', array(), wfMsg( 'protectedpages' ) ) .
  100. Xml::hidden( 'title', $title->getPrefixedDBkey() ) . "\n" .
  101. $this->getNamespaceMenu( $namespace ) . "&nbsp;\n" .
  102. $this->getTypeMenu( $type ) . "&nbsp;\n" .
  103. $this->getLevelMenu( $level ) . "&nbsp;\n" .
  104. "<br/><span style='white-space: nowrap'>" .
  105. $this->getExpiryCheck( $indefOnly ) . "&nbsp;\n" .
  106. $this->getCascadeCheck( $cascadeOnly ) . "&nbsp;\n" .
  107. "</span><br/><span style='white-space: nowrap'>" .
  108. $this->getSizeLimit( $sizetype, $size ) . "&nbsp;\n" .
  109. "</span>" .
  110. "&nbsp;" . Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
  111. Xml::closeElement( 'fieldset' ) .
  112. Xml::closeElement( 'form' );
  113. }
  114. /**
  115. * Prepare the namespace filter drop-down; standard namespace
  116. * selector, sans the MediaWiki namespace
  117. *
  118. * @param mixed $namespace Pre-select namespace
  119. * @return string
  120. */
  121. protected function getNamespaceMenu( $namespace = null ) {
  122. return "<span style='white-space: nowrap'>" .
  123. Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&nbsp;'
  124. . Xml::namespaceSelector( $namespace, '' ) . "</span>";
  125. }
  126. /**
  127. * @return string Formatted HTML
  128. */
  129. protected function getExpiryCheck( $indefOnly ) {
  130. return
  131. Xml::checkLabel( wfMsg('protectedpages-indef'), 'indefonly', 'indefonly', $indefOnly ) . "\n";
  132. }
  133. /**
  134. * @return string Formatted HTML
  135. */
  136. protected function getCascadeCheck( $cascadeOnly ) {
  137. return
  138. Xml::checkLabel( wfMsg('protectedpages-cascade'), 'cascadeonly', 'cascadeonly', $cascadeOnly ) . "\n";
  139. }
  140. /**
  141. * @return string Formatted HTML
  142. */
  143. protected function getSizeLimit( $sizetype, $size ) {
  144. $max = $sizetype === 'max';
  145. return
  146. Xml::radioLabel( wfMsg('minimum-size'), 'sizetype', 'min', 'wpmin', !$max ) .
  147. '&nbsp;' .
  148. Xml::radioLabel( wfMsg('maximum-size'), 'sizetype', 'max', 'wpmax', $max ) .
  149. '&nbsp;' .
  150. Xml::input( 'size', 9, $size, array( 'id' => 'wpsize' ) ) .
  151. '&nbsp;' .
  152. Xml::label( wfMsg('pagesize'), 'wpsize' );
  153. }
  154. /**
  155. * @return string Formatted HTML
  156. */
  157. protected function getTypeMenu( $pr_type ) {
  158. global $wgRestrictionTypes;
  159. $m = array(); // Temporary array
  160. $options = array();
  161. // First pass to load the log names
  162. foreach( $wgRestrictionTypes as $type ) {
  163. $text = wfMsg("restriction-$type");
  164. $m[$text] = $type;
  165. }
  166. // Third pass generates sorted XHTML content
  167. foreach( $m as $text => $type ) {
  168. $selected = ($type == $pr_type );
  169. $options[] = Xml::option( $text, $type, $selected ) . "\n";
  170. }
  171. return "<span style='white-space: nowrap'>" .
  172. Xml::label( wfMsg('restriction-type') , $this->IdType ) . '&nbsp;' .
  173. Xml::tags( 'select',
  174. array( 'id' => $this->IdType, 'name' => $this->IdType ),
  175. implode( "\n", $options ) ) . "</span>";
  176. }
  177. /**
  178. * @return string Formatted HTML
  179. */
  180. protected function getLevelMenu( $pr_level ) {
  181. global $wgRestrictionLevels;
  182. $m = array( wfMsg('restriction-level-all') => 0 ); // Temporary array
  183. $options = array();
  184. // First pass to load the log names
  185. foreach( $wgRestrictionLevels as $type ) {
  186. if( $type !='' && $type !='*') {
  187. $text = wfMsg("restriction-level-$type");
  188. $m[$text] = $type;
  189. }
  190. }
  191. // Third pass generates sorted XHTML content
  192. foreach( $m as $text => $type ) {
  193. $selected = ($type == $pr_level );
  194. $options[] = Xml::option( $text, $type, $selected );
  195. }
  196. return
  197. Xml::label( wfMsg('restriction-level') , $this->IdLevel ) . '&nbsp;' .
  198. Xml::tags( 'select',
  199. array( 'id' => $this->IdLevel, 'name' => $this->IdLevel ),
  200. implode( "\n", $options ) );
  201. }
  202. }
  203. /**
  204. * @todo document
  205. * @ingroup Pager
  206. */
  207. class ProtectedPagesPager extends AlphabeticPager {
  208. public $mForm, $mConds;
  209. private $type, $level, $namespace, $sizetype, $size, $indefonly;
  210. function __construct( $form, $conds = array(), $type, $level, $namespace, $sizetype='', $size=0,
  211. $indefonly = false, $cascadeonly = false )
  212. {
  213. $this->mForm = $form;
  214. $this->mConds = $conds;
  215. $this->type = ( $type ) ? $type : 'edit';
  216. $this->level = $level;
  217. $this->namespace = $namespace;
  218. $this->sizetype = $sizetype;
  219. $this->size = intval($size);
  220. $this->indefonly = (bool)$indefonly;
  221. $this->cascadeonly = (bool)$cascadeonly;
  222. parent::__construct();
  223. }
  224. function getStartBody() {
  225. # Do a link batch query
  226. $lb = new LinkBatch;
  227. while( $row = $this->mResult->fetchObject() ) {
  228. $lb->add( $row->page_namespace, $row->page_title );
  229. }
  230. $lb->execute();
  231. return '';
  232. }
  233. function formatRow( $row ) {
  234. return $this->mForm->formatRow( $row );
  235. }
  236. function getQueryInfo() {
  237. $conds = $this->mConds;
  238. $conds[] = '(pr_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() ) .
  239. 'OR pr_expiry IS NULL)';
  240. $conds[] = 'page_id=pr_page';
  241. $conds[] = 'pr_type=' . $this->mDb->addQuotes( $this->type );
  242. if( $this->sizetype=='min' ) {
  243. $conds[] = 'page_len>=' . $this->size;
  244. } else if( $this->sizetype=='max' ) {
  245. $conds[] = 'page_len<=' . $this->size;
  246. }
  247. if( $this->indefonly ) {
  248. $conds[] = "pr_expiry = 'infinity' OR pr_expiry IS NULL";
  249. }
  250. if( $this->cascadeonly ) {
  251. $conds[] = "pr_cascade = '1'";
  252. }
  253. if( $this->level )
  254. $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
  255. if( !is_null($this->namespace) )
  256. $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
  257. return array(
  258. 'tables' => array( 'page_restrictions', 'page' ),
  259. 'fields' => 'pr_id,page_namespace,page_title,page_len,pr_type,pr_level,pr_expiry,pr_cascade',
  260. 'conds' => $conds
  261. );
  262. }
  263. function getIndexField() {
  264. return 'pr_id';
  265. }
  266. }
  267. /**
  268. * Constructor
  269. */
  270. function wfSpecialProtectedpages() {
  271. $ppForm = new ProtectedPagesForm();
  272. $ppForm->showList();
  273. }