NewPagesPager.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. /**
  22. * @ingroup Pager
  23. */
  24. use MediaWiki\MediaWikiServices;
  25. class NewPagesPager extends ReverseChronologicalPager {
  26. /**
  27. * @var FormOptions
  28. */
  29. protected $opts;
  30. /**
  31. * @var SpecialNewpages
  32. */
  33. protected $mForm;
  34. /**
  35. * @param SpecialNewpages $form
  36. * @param FormOptions $opts
  37. */
  38. public function __construct( $form, FormOptions $opts ) {
  39. parent::__construct( $form->getContext() );
  40. $this->mForm = $form;
  41. $this->opts = $opts;
  42. }
  43. function getQueryInfo() {
  44. $rcQuery = RecentChange::getQueryInfo();
  45. $conds = [];
  46. $conds['rc_new'] = 1;
  47. $username = $this->opts->getValue( 'username' );
  48. $user = Title::makeTitleSafe( NS_USER, $username );
  49. $size = abs( intval( $this->opts->getValue( 'size' ) ) );
  50. if ( $size > 0 ) {
  51. if ( $this->opts->getValue( 'size-mode' ) === 'max' ) {
  52. $conds[] = 'page_len <= ' . $size;
  53. } else {
  54. $conds[] = 'page_len >= ' . $size;
  55. }
  56. }
  57. if ( $user ) {
  58. $conds[] = ActorMigration::newMigration()->getWhere(
  59. $this->mDb, 'rc_user', User::newFromName( $user->getText(), false ), false
  60. )['conds'];
  61. } elseif ( MediaWikiServices::getInstance()
  62. ->getPermissionManager()
  63. ->groupHasPermission( '*', 'createpage' ) &&
  64. $this->opts->getValue( 'hideliu' )
  65. ) {
  66. # If anons cannot make new pages, don't "exclude logged in users"!
  67. $conds[] = ActorMigration::newMigration()->isAnon( $rcQuery['fields']['rc_user'] );
  68. }
  69. $conds = array_merge( $conds, $this->getNamespaceCond() );
  70. # If this user cannot see patrolled edits or they are off, don't do dumb queries!
  71. if ( $this->opts->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
  72. $conds['rc_patrolled'] = RecentChange::PRC_UNPATROLLED;
  73. }
  74. if ( $this->opts->getValue( 'hidebots' ) ) {
  75. $conds['rc_bot'] = 0;
  76. }
  77. if ( $this->opts->getValue( 'hideredirs' ) ) {
  78. $conds['page_is_redirect'] = 0;
  79. }
  80. // Allow changes to the New Pages query
  81. $tables = array_merge( $rcQuery['tables'], [ 'page' ] );
  82. $fields = array_merge( $rcQuery['fields'], [
  83. 'length' => 'page_len', 'rev_id' => 'page_latest', 'page_namespace', 'page_title'
  84. ] );
  85. $join_conds = [ 'page' => [ 'JOIN', 'page_id=rc_cur_id' ] ] + $rcQuery['joins'];
  86. // Avoid PHP 7.1 warning from passing $this by reference
  87. $pager = $this;
  88. Hooks::run( 'SpecialNewpagesConditions',
  89. [ &$pager, $this->opts, &$conds, &$tables, &$fields, &$join_conds ] );
  90. $info = [
  91. 'tables' => $tables,
  92. 'fields' => $fields,
  93. 'conds' => $conds,
  94. 'options' => [],
  95. 'join_conds' => $join_conds
  96. ];
  97. // Modify query for tags
  98. ChangeTags::modifyDisplayQuery(
  99. $info['tables'],
  100. $info['fields'],
  101. $info['conds'],
  102. $info['join_conds'],
  103. $info['options'],
  104. $this->opts['tagfilter']
  105. );
  106. return $info;
  107. }
  108. // Based on ContribsPager.php
  109. function getNamespaceCond() {
  110. $namespace = $this->opts->getValue( 'namespace' );
  111. if ( $namespace === 'all' || $namespace === '' ) {
  112. return [];
  113. }
  114. $namespace = intval( $namespace );
  115. $invert = $this->opts->getValue( 'invert' );
  116. $associated = $this->opts->getValue( 'associated' );
  117. $eq_op = $invert ? '!=' : '=';
  118. $bool_op = $invert ? 'AND' : 'OR';
  119. $selectedNS = $this->mDb->addQuotes( $namespace );
  120. if ( !$associated ) {
  121. return [ "rc_namespace $eq_op $selectedNS" ];
  122. }
  123. $associatedNS = $this->mDb->addQuotes(
  124. MediaWikiServices::getInstance()->getNamespaceInfo()->getAssociated( $namespace )
  125. );
  126. return [
  127. "rc_namespace $eq_op $selectedNS " .
  128. $bool_op .
  129. " rc_namespace $eq_op $associatedNS"
  130. ];
  131. }
  132. function getIndexField() {
  133. return 'rc_timestamp';
  134. }
  135. function formatRow( $row ) {
  136. return $this->mForm->formatRow( $row );
  137. }
  138. protected function getStartBody() {
  139. # Do a batch existence check on pages
  140. $linkBatch = new LinkBatch();
  141. foreach ( $this->mResult as $row ) {
  142. $linkBatch->add( NS_USER, $row->rc_user_text );
  143. $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
  144. $linkBatch->add( $row->page_namespace, $row->page_title );
  145. }
  146. $linkBatch->execute();
  147. return '<ul>';
  148. }
  149. protected function getEndBody() {
  150. return '</ul>';
  151. }
  152. }