MediaWiki  master
NewPagesPager.php
Go to the documentation of this file.
1 <?php
27 
32 
36  protected $opts;
37 
41  protected $mForm;
42 
45 
47  private $hookRunner;
48 
51 
53  private $namespaceInfo;
54 
64  public function __construct(
65  $form,
68  HookContainer $hookContainer,
70  ILoadBalancer $loadBalancer,
72  ) {
73  // Set database before parent constructor to avoid setting it there with wfGetDB
74  $this->mDb = $loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
75  parent::__construct( $form->getContext() );
76  $this->mForm = $form;
77  $this->opts = $opts;
78  $this->linkBatchFactory = $linkBatchFactory;
79  $this->hookRunner = new HookRunner( $hookContainer );
80  $this->groupPermissionsLookup = $groupPermissionsLookup;
81  $this->namespaceInfo = $namespaceInfo;
82  }
83 
84  public function getQueryInfo() {
85  $rcQuery = RecentChange::getQueryInfo();
86 
87  $conds = [];
88  $conds['rc_new'] = 1;
89 
90  $username = $this->opts->getValue( 'username' );
91  $user = Title::makeTitleSafe( NS_USER, $username );
92 
93  $size = abs( intval( $this->opts->getValue( 'size' ) ) );
94  if ( $size > 0 ) {
95  if ( $this->opts->getValue( 'size-mode' ) === 'max' ) {
96  $conds[] = 'page_len <= ' . $size;
97  } else {
98  $conds[] = 'page_len >= ' . $size;
99  }
100  }
101 
102  if ( $user ) {
103  $conds['actor_name'] = $user->getText();
104  } elseif ( $this->canAnonymousUsersCreatePages() && $this->opts->getValue( 'hideliu' ) ) {
105  # If anons cannot make new pages, don't "exclude logged in users"!
106  $conds['actor_user'] = null;
107  }
108 
109  $conds = array_merge( $conds, $this->getNamespaceCond() );
110 
111  # If this user cannot see patrolled edits or they are off, don't do dumb queries!
112  if ( $this->opts->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
113  $conds['rc_patrolled'] = RecentChange::PRC_UNPATROLLED;
114  }
115 
116  if ( $this->opts->getValue( 'hidebots' ) ) {
117  $conds['rc_bot'] = 0;
118  }
119 
120  if ( $this->opts->getValue( 'hideredirs' ) ) {
121  $conds['page_is_redirect'] = 0;
122  }
123 
124  // Allow changes to the New Pages query
125  $tables = array_merge( $rcQuery['tables'], [ 'page' ] );
126  $fields = array_merge( $rcQuery['fields'], [
127  'length' => 'page_len', 'rev_id' => 'page_latest', 'page_namespace', 'page_title',
128  'page_content_model',
129  ] );
130  $join_conds = [ 'page' => [ 'JOIN', 'page_id=rc_cur_id' ] ] + $rcQuery['joins'];
131 
132  $this->hookRunner->onSpecialNewpagesConditions(
133  $this, $this->opts, $conds, $tables, $fields, $join_conds );
134 
135  $info = [
136  'tables' => $tables,
137  'fields' => $fields,
138  'conds' => $conds,
139  'options' => [],
140  'join_conds' => $join_conds
141  ];
142 
143  // Modify query for tags
145  $info['tables'],
146  $info['fields'],
147  $info['conds'],
148  $info['join_conds'],
149  $info['options'],
150  $this->opts['tagfilter']
151  );
152 
153  return $info;
154  }
155 
156  private function canAnonymousUsersCreatePages() {
157  return $this->groupPermissionsLookup->groupHasPermission( '*', 'createpage' ) ||
158  $this->groupPermissionsLookup->groupHasPermission( '*', 'createtalk' );
159  }
160 
161  // Based on ContribsPager.php
162  private function getNamespaceCond() {
163  $namespace = $this->opts->getValue( 'namespace' );
164  if ( $namespace === 'all' || $namespace === '' ) {
165  return [];
166  }
167 
168  $namespace = intval( $namespace );
169  if ( $namespace < NS_MAIN ) {
170  // Negative namespaces are invalid
171  return [];
172  }
173 
174  $invert = $this->opts->getValue( 'invert' );
175  $associated = $this->opts->getValue( 'associated' );
176 
177  $eq_op = $invert ? '!=' : '=';
178  $bool_op = $invert ? 'AND' : 'OR';
179 
180  $dbr = $this->getDatabase();
181  $selectedNS = $dbr->addQuotes( $namespace );
182  if ( !$associated ) {
183  return [ "rc_namespace $eq_op $selectedNS" ];
184  }
185 
186  $associatedNS = $dbr->addQuotes(
187  $this->namespaceInfo->getAssociated( $namespace )
188  );
189  return [
190  "rc_namespace $eq_op $selectedNS " .
191  $bool_op .
192  " rc_namespace $eq_op $associatedNS"
193  ];
194  }
195 
196  public function getIndexField() {
197  return 'rc_timestamp';
198  }
199 
200  public function formatRow( $row ) {
201  return $this->mForm->formatRow( $row );
202  }
203 
204  protected function getStartBody() {
205  # Do a batch existence check on pages
206  $linkBatch = $this->linkBatchFactory->newLinkBatch();
207  foreach ( $this->mResult as $row ) {
208  $linkBatch->add( NS_USER, $row->rc_user_text );
209  $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
210  $linkBatch->add( $row->page_namespace, $row->page_title );
211  }
212  $linkBatch->execute();
213 
214  return '<ul>';
215  }
216 
217  protected function getEndBody() {
218  return '</ul>';
219  }
220 }
RecentChange\getQueryInfo
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new recentchanges object.
Definition: RecentChange.php:257
NewPagesPager\getIndexField
getIndexField()
Returns the name of the index field.
Definition: NewPagesPager.php:196
NewPagesPager\$opts
FormOptions $opts
Definition: NewPagesPager.php:36
NewPagesPager\canAnonymousUsersCreatePages
canAnonymousUsersCreatePages()
Definition: NewPagesPager.php:156
MediaWiki\Permissions\GroupPermissionsLookup
Definition: GroupPermissionsLookup.php:30
NewPagesPager\getStartBody
getStartBody()
Hook into getBody(), allows text to be inserted at the start.
Definition: NewPagesPager.php:204
IndexPager\getDatabase
getDatabase()
Get the Database object in use.
Definition: IndexPager.php:248
NewPagesPager\getNamespaceCond
getNamespaceCond()
Definition: NewPagesPager.php:162
NewPagesPager\getEndBody
getEndBody()
Hook into getBody() for the end of the list.
Definition: NewPagesPager.php:217
Wikimedia\Rdbms\ILoadBalancer\getConnectionRef
getConnectionRef( $i, $groups=[], $domain=false, $flags=0)
Get a live database handle reference for a server index.
NS_MAIN
const NS_MAIN
Definition: Defines.php:64
$dbr
$dbr
Definition: testCompression.php:54
ChangeTags\modifyDisplayQuery
static modifyDisplayQuery(&$tables, &$fields, &$conds, &$join_conds, &$options, $filter_tag='')
Applies all tags-related changes to a query.
Definition: ChangeTags.php:891
NewPagesPager\getQueryInfo
getQueryInfo()
Provides all parameters needed for the main paged query.
Definition: NewPagesPager.php:84
NewPagesPager\$linkBatchFactory
LinkBatchFactory $linkBatchFactory
Definition: NewPagesPager.php:44
NewPagesPager\$namespaceInfo
NamespaceInfo $namespaceInfo
Definition: NewPagesPager.php:53
MediaWiki\Cache\LinkBatchFactory
Definition: LinkBatchFactory.php:39
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:676
NewPagesPager\formatRow
formatRow( $row)
Returns an HTML string representing the result row $row.
Definition: NewPagesPager.php:200
NS_USER
const NS_USER
Definition: Defines.php:66
NewPagesPager
Definition: NewPagesPager.php:31
NewPagesPager\$groupPermissionsLookup
GroupPermissionsLookup $groupPermissionsLookup
Definition: NewPagesPager.php:50
SpecialNewpages
A special page that list newly created pages.
Definition: SpecialNewpages.php:40
RecentChange\PRC_UNPATROLLED
const PRC_UNPATROLLED
Definition: RecentChange.php:91
NewPagesPager\__construct
__construct( $form, FormOptions $opts, LinkBatchFactory $linkBatchFactory, HookContainer $hookContainer, GroupPermissionsLookup $groupPermissionsLookup, ILoadBalancer $loadBalancer, NamespaceInfo $namespaceInfo)
Definition: NewPagesPager.php:64
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:67
MediaWiki\HookContainer\HookContainer
HookContainer class.
Definition: HookContainer.php:45
ReverseChronologicalPager
Efficient paging for SQL queries.
Definition: ReverseChronologicalPager.php:29
NamespaceInfo
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Definition: NamespaceInfo.php:35
MediaWiki\HookContainer\HookRunner
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:557
FormOptions
Helper class to keep track of options when mixing links and form elements.
Definition: FormOptions.php:35
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81
NewPagesPager\$mForm
SpecialNewpages $mForm
Definition: NewPagesPager.php:41
NewPagesPager\$hookRunner
HookRunner $hookRunner
Definition: NewPagesPager.php:47