ProtectedPagesPager.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. * @ingroup Pager
  20. */
  21. use MediaWiki\Linker\LinkRenderer;
  22. use MediaWiki\MediaWikiServices;
  23. class ProtectedPagesPager extends TablePager {
  24. public $mConds;
  25. private $type, $level, $namespace, $sizetype, $size, $indefonly, $cascadeonly, $noredirect;
  26. /**
  27. * @param SpecialPage $form
  28. * @param array $conds
  29. * @param string $type
  30. * @param string $level
  31. * @param int $namespace
  32. * @param string $sizetype
  33. * @param int $size
  34. * @param bool $indefonly
  35. * @param bool $cascadeonly
  36. * @param bool $noredirect
  37. * @param LinkRenderer $linkRenderer
  38. */
  39. public function __construct( $form, $conds, $type, $level, $namespace,
  40. $sizetype, $size, $indefonly, $cascadeonly, $noredirect,
  41. LinkRenderer $linkRenderer
  42. ) {
  43. parent::__construct( $form->getContext(), $linkRenderer );
  44. $this->mConds = $conds;
  45. $this->type = $type ?: 'edit';
  46. $this->level = $level;
  47. $this->namespace = $namespace;
  48. $this->sizetype = $sizetype;
  49. $this->size = intval( $size );
  50. $this->indefonly = (bool)$indefonly;
  51. $this->cascadeonly = (bool)$cascadeonly;
  52. $this->noredirect = (bool)$noredirect;
  53. }
  54. function preprocessResults( $result ) {
  55. # Do a link batch query
  56. $lb = new LinkBatch;
  57. $userids = [];
  58. foreach ( $result as $row ) {
  59. $lb->add( $row->page_namespace, $row->page_title );
  60. // field is nullable, maybe null on old protections
  61. if ( $row->log_user !== null ) {
  62. $userids[] = $row->log_user;
  63. }
  64. }
  65. // fill LinkBatch with user page and user talk
  66. if ( count( $userids ) ) {
  67. $userCache = UserCache::singleton();
  68. $userCache->doQuery( $userids, [], __METHOD__ );
  69. foreach ( $userids as $userid ) {
  70. $name = $userCache->getProp( $userid, 'name' );
  71. if ( $name !== false ) {
  72. $lb->add( NS_USER, $name );
  73. $lb->add( NS_USER_TALK, $name );
  74. }
  75. }
  76. }
  77. $lb->execute();
  78. }
  79. function getFieldNames() {
  80. static $headers = null;
  81. if ( $headers == [] ) {
  82. $headers = [
  83. 'log_timestamp' => 'protectedpages-timestamp',
  84. 'pr_page' => 'protectedpages-page',
  85. 'pr_expiry' => 'protectedpages-expiry',
  86. 'log_user' => 'protectedpages-performer',
  87. 'pr_params' => 'protectedpages-params',
  88. 'log_comment' => 'protectedpages-reason',
  89. ];
  90. foreach ( $headers as $key => $val ) {
  91. $headers[$key] = $this->msg( $val )->text();
  92. }
  93. }
  94. return $headers;
  95. }
  96. /**
  97. * @param string $field
  98. * @param string $value
  99. * @return string HTML
  100. * @throws MWException
  101. */
  102. function formatValue( $field, $value ) {
  103. /** @var object $row */
  104. $row = $this->mCurrentRow;
  105. $linkRenderer = $this->getLinkRenderer();
  106. switch ( $field ) {
  107. case 'log_timestamp':
  108. // when timestamp is null, this is a old protection row
  109. if ( $value === null ) {
  110. $formatted = Html::rawElement(
  111. 'span',
  112. [ 'class' => 'mw-protectedpages-unknown' ],
  113. $this->msg( 'protectedpages-unknown-timestamp' )->escaped()
  114. );
  115. } else {
  116. $formatted = htmlspecialchars( $this->getLanguage()->userTimeAndDate(
  117. $value, $this->getUser() ) );
  118. }
  119. break;
  120. case 'pr_page':
  121. $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
  122. if ( !$title ) {
  123. $formatted = Html::element(
  124. 'span',
  125. [ 'class' => 'mw-invalidtitle' ],
  126. Linker::getInvalidTitleDescription(
  127. $this->getContext(),
  128. $row->page_namespace,
  129. $row->page_title
  130. )
  131. );
  132. } else {
  133. $formatted = $linkRenderer->makeLink( $title );
  134. }
  135. if ( !is_null( $row->page_len ) ) {
  136. $formatted .= $this->getLanguage()->getDirMark() .
  137. ' ' . Html::rawElement(
  138. 'span',
  139. [ 'class' => 'mw-protectedpages-length' ],
  140. Linker::formatRevisionSize( $row->page_len )
  141. );
  142. }
  143. break;
  144. case 'pr_expiry':
  145. $formatted = htmlspecialchars( $this->getLanguage()->formatExpiry(
  146. $value, /* User preference timezone */true ) );
  147. $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
  148. if ( $title && MediaWikiServices::getInstance()
  149. ->getPermissionManager()
  150. ->userHasRight( $this->getUser(), 'protect' )
  151. ) {
  152. $changeProtection = $linkRenderer->makeKnownLink(
  153. $title,
  154. $this->msg( 'protect_change' )->text(),
  155. [],
  156. [ 'action' => 'unprotect' ]
  157. );
  158. $formatted .= ' ' . Html::rawElement(
  159. 'span',
  160. [ 'class' => 'mw-protectedpages-actions' ],
  161. $this->msg( 'parentheses' )->rawParams( $changeProtection )->escaped()
  162. );
  163. }
  164. break;
  165. case 'log_user':
  166. // when timestamp is null, this is a old protection row
  167. if ( $row->log_timestamp === null ) {
  168. $formatted = Html::rawElement(
  169. 'span',
  170. [ 'class' => 'mw-protectedpages-unknown' ],
  171. $this->msg( 'protectedpages-unknown-performer' )->escaped()
  172. );
  173. } else {
  174. $username = UserCache::singleton()->getProp( $value, 'name' );
  175. if ( LogEventsList::userCanBitfield(
  176. $row->log_deleted,
  177. LogPage::DELETED_USER,
  178. $this->getUser()
  179. ) ) {
  180. if ( $username === false ) {
  181. $formatted = htmlspecialchars( $value );
  182. } else {
  183. $formatted = Linker::userLink( $value, $username )
  184. . Linker::userToolLinks( $value, $username );
  185. }
  186. } else {
  187. $formatted = $this->msg( 'rev-deleted-user' )->escaped();
  188. }
  189. if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
  190. $formatted = '<span class="history-deleted">' . $formatted . '</span>';
  191. }
  192. }
  193. break;
  194. case 'pr_params':
  195. $params = [];
  196. // Messages: restriction-level-sysop, restriction-level-autoconfirmed
  197. $params[] = $this->msg( 'restriction-level-' . $row->pr_level )->escaped();
  198. if ( $row->pr_cascade ) {
  199. $params[] = $this->msg( 'protect-summary-cascade' )->escaped();
  200. }
  201. $formatted = $this->getLanguage()->commaList( $params );
  202. break;
  203. case 'log_comment':
  204. // when timestamp is null, this is an old protection row
  205. if ( $row->log_timestamp === null ) {
  206. $formatted = Html::rawElement(
  207. 'span',
  208. [ 'class' => 'mw-protectedpages-unknown' ],
  209. $this->msg( 'protectedpages-unknown-reason' )->escaped()
  210. );
  211. } else {
  212. if ( LogEventsList::userCanBitfield(
  213. $row->log_deleted,
  214. LogPage::DELETED_COMMENT,
  215. $this->getUser()
  216. ) ) {
  217. $value = CommentStore::getStore()->getComment( 'log_comment', $row )->text;
  218. $formatted = Linker::formatComment( $value ?? '' );
  219. } else {
  220. $formatted = $this->msg( 'rev-deleted-comment' )->escaped();
  221. }
  222. if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
  223. $formatted = '<span class="history-deleted">' . $formatted . '</span>';
  224. }
  225. }
  226. break;
  227. default:
  228. throw new MWException( "Unknown field '$field'" );
  229. }
  230. return $formatted;
  231. }
  232. function getQueryInfo() {
  233. $conds = $this->mConds;
  234. $conds[] = 'pr_expiry > ' . $this->mDb->addQuotes( $this->mDb->timestamp() ) .
  235. ' OR pr_expiry IS NULL';
  236. $conds[] = 'page_id=pr_page';
  237. $conds[] = 'pr_type=' . $this->mDb->addQuotes( $this->type );
  238. if ( $this->sizetype == 'min' ) {
  239. $conds[] = 'page_len>=' . $this->size;
  240. } elseif ( $this->sizetype == 'max' ) {
  241. $conds[] = 'page_len<=' . $this->size;
  242. }
  243. if ( $this->indefonly ) {
  244. $infinity = $this->mDb->addQuotes( $this->mDb->getInfinity() );
  245. $conds[] = "pr_expiry = $infinity OR pr_expiry IS NULL";
  246. }
  247. if ( $this->cascadeonly ) {
  248. $conds[] = 'pr_cascade = 1';
  249. }
  250. if ( $this->noredirect ) {
  251. $conds[] = 'page_is_redirect = 0';
  252. }
  253. if ( $this->level ) {
  254. $conds[] = 'pr_level=' . $this->mDb->addQuotes( $this->level );
  255. }
  256. if ( !is_null( $this->namespace ) ) {
  257. $conds[] = 'page_namespace=' . $this->mDb->addQuotes( $this->namespace );
  258. }
  259. $commentQuery = CommentStore::getStore()->getJoin( 'log_comment' );
  260. $actorQuery = ActorMigration::newMigration()->getJoin( 'log_user' );
  261. return [
  262. 'tables' => [
  263. 'page', 'page_restrictions', 'log_search',
  264. 'logparen' => [ 'logging' ] + $commentQuery['tables'] + $actorQuery['tables'],
  265. ],
  266. 'fields' => [
  267. 'pr_id',
  268. 'page_namespace',
  269. 'page_title',
  270. 'page_len',
  271. 'pr_type',
  272. 'pr_level',
  273. 'pr_expiry',
  274. 'pr_cascade',
  275. 'log_timestamp',
  276. 'log_deleted',
  277. ] + $commentQuery['fields'] + $actorQuery['fields'],
  278. 'conds' => $conds,
  279. 'join_conds' => [
  280. 'log_search' => [
  281. 'LEFT JOIN', [
  282. 'ls_field' => 'pr_id', 'ls_value = ' . $this->mDb->buildStringCast( 'pr_id' )
  283. ]
  284. ],
  285. 'logparen' => [
  286. 'LEFT JOIN', [
  287. 'ls_log_id = log_id'
  288. ]
  289. ]
  290. ] + $commentQuery['joins'] + $actorQuery['joins']
  291. ];
  292. }
  293. protected function getTableClass() {
  294. return parent::getTableClass() . ' mw-protectedpages';
  295. }
  296. function getIndexField() {
  297. return 'pr_id';
  298. }
  299. function getDefaultSort() {
  300. return 'pr_id';
  301. }
  302. function isFieldSortable( $field ) {
  303. // no index for sorting exists
  304. return false;
  305. }
  306. }