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 
56  private $actorMigration;
57 
68  public function __construct(
69  $form,
72  HookContainer $hookContainer,
74  ILoadBalancer $loadBalancer,
77  ) {
78  // Set database before parent constructor to avoid setting it there with wfGetDB
79  $this->mDb = $loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
80  parent::__construct( $form->getContext() );
81  $this->mForm = $form;
82  $this->opts = $opts;
83  $this->linkBatchFactory = $linkBatchFactory;
84  $this->hookRunner = new HookRunner( $hookContainer );
85  $this->permissionManager = $permissionManager;
86  $this->namespaceInfo = $namespaceInfo;
87  $this->actorMigration = $actorMigration;
88  }
89 
90  public function getQueryInfo() {
91  $rcQuery = RecentChange::getQueryInfo();
92 
93  $conds = [];
94  $conds['rc_new'] = 1;
95 
96  $username = $this->opts->getValue( 'username' );
97  $user = Title::makeTitleSafe( NS_USER, $username );
98 
99  $size = abs( intval( $this->opts->getValue( 'size' ) ) );
100  if ( $size > 0 ) {
101  if ( $this->opts->getValue( 'size-mode' ) === 'max' ) {
102  $conds[] = 'page_len <= ' . $size;
103  } else {
104  $conds[] = 'page_len >= ' . $size;
105  }
106  }
107 
108  if ( $user ) {
109  $conds[] = $this->actorMigration->getWhere(
110  $this->getDatabase(), 'rc_user', User::newFromName( $user->getText(), false ), false
111  )['conds'];
112  } elseif ( $this->canAnonymousUsersCreatePages() && $this->opts->getValue( 'hideliu' ) ) {
113  # If anons cannot make new pages, don't "exclude logged in users"!
114  $conds[] = $this->actorMigration->isAnon( $rcQuery['fields']['rc_user'] );
115  }
116 
117  $conds = array_merge( $conds, $this->getNamespaceCond() );
118 
119  # If this user cannot see patrolled edits or they are off, don't do dumb queries!
120  if ( $this->opts->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
121  $conds['rc_patrolled'] = RecentChange::PRC_UNPATROLLED;
122  }
123 
124  if ( $this->opts->getValue( 'hidebots' ) ) {
125  $conds['rc_bot'] = 0;
126  }
127 
128  if ( $this->opts->getValue( 'hideredirs' ) ) {
129  $conds['page_is_redirect'] = 0;
130  }
131 
132  // Allow changes to the New Pages query
133  $tables = array_merge( $rcQuery['tables'], [ 'page' ] );
134  $fields = array_merge( $rcQuery['fields'], [
135  'length' => 'page_len', 'rev_id' => 'page_latest', 'page_namespace', 'page_title',
136  'page_content_model',
137  ] );
138  $join_conds = [ 'page' => [ 'JOIN', 'page_id=rc_cur_id' ] ] + $rcQuery['joins'];
139 
140  $this->hookRunner->onSpecialNewpagesConditions(
141  $this, $this->opts, $conds, $tables, $fields, $join_conds );
142 
143  $info = [
144  'tables' => $tables,
145  'fields' => $fields,
146  'conds' => $conds,
147  'options' => [],
148  'join_conds' => $join_conds
149  ];
150 
151  // Modify query for tags
153  $info['tables'],
154  $info['fields'],
155  $info['conds'],
156  $info['join_conds'],
157  $info['options'],
158  $this->opts['tagfilter']
159  );
160 
161  return $info;
162  }
163 
164  private function canAnonymousUsersCreatePages() {
165  return $this->permissionManager->groupHasPermission( '*', 'createpage' ) ||
166  $this->permissionManager->groupHasPermission( '*', 'createtalk' );
167  }
168 
169  // Based on ContribsPager.php
170  private function getNamespaceCond() {
171  $namespace = $this->opts->getValue( 'namespace' );
172  if ( $namespace === 'all' || $namespace === '' ) {
173  return [];
174  }
175 
176  $namespace = intval( $namespace );
177  if ( $namespace < NS_MAIN ) {
178  // Negative namespaces are invalid
179  return [];
180  }
181 
182  $invert = $this->opts->getValue( 'invert' );
183  $associated = $this->opts->getValue( 'associated' );
184 
185  $eq_op = $invert ? '!=' : '=';
186  $bool_op = $invert ? 'AND' : 'OR';
187 
188  $dbr = $this->getDatabase();
189  $selectedNS = $dbr->addQuotes( $namespace );
190  if ( !$associated ) {
191  return [ "rc_namespace $eq_op $selectedNS" ];
192  }
193 
194  $associatedNS = $dbr->addQuotes(
195  $this->namespaceInfo->getAssociated( $namespace )
196  );
197  return [
198  "rc_namespace $eq_op $selectedNS " .
199  $bool_op .
200  " rc_namespace $eq_op $associatedNS"
201  ];
202  }
203 
204  public function getIndexField() {
205  return 'rc_timestamp';
206  }
207 
208  public function formatRow( $row ) {
209  return $this->mForm->formatRow( $row );
210  }
211 
212  protected function getStartBody() {
213  # Do a batch existence check on pages
214  $linkBatch = $this->linkBatchFactory->newLinkBatch();
215  foreach ( $this->mResult as $row ) {
216  $linkBatch->add( NS_USER, $row->rc_user_text );
217  $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
218  $linkBatch->add( $row->page_namespace, $row->page_title );
219  }
220  $linkBatch->execute();
221 
222  return '<ul>';
223  }
224 
225  protected function getEndBody() {
226  return '</ul>';
227  }
228 }
RecentChange\getQueryInfo
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new recentchanges object.
Definition: RecentChange.php:246
NewPagesPager\getIndexField
getIndexField()
Returns the name of the index field.
Definition: NewPagesPager.php:204
NewPagesPager\$opts
FormOptions $opts
Definition: NewPagesPager.php:36
NewPagesPager\canAnonymousUsersCreatePages
canAnonymousUsersCreatePages()
Definition: NewPagesPager.php:164
NewPagesPager\getStartBody
getStartBody()
Hook into getBody(), allows text to be inserted at the start.
Definition: NewPagesPager.php:212
IndexPager\getDatabase
getDatabase()
Get the Database object in use.
Definition: IndexPager.php:244
NewPagesPager\getNamespaceCond
getNamespaceCond()
Definition: NewPagesPager.php:170
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:545
ActorMigration
This class handles the logic for the actor table migration and should always be used in lieu of direc...
Definition: ActorMigration.php:41
NewPagesPager\getEndBody
getEndBody()
Hook into getBody() for the end of the list.
Definition: NewPagesPager.php:225
Wikimedia\Rdbms\ILoadBalancer\getConnectionRef
getConnectionRef( $i, $groups=[], $domain=false, $flags=0)
Get a live database handle reference for a real or virtual (DB_MASTER/DB_REPLICA) server index.
$dbr
$dbr
Definition: testCompression.php:54
NS_MAIN
const NS_MAIN
Definition: Defines.php:69
ChangeTags\modifyDisplayQuery
static modifyDisplayQuery(&$tables, &$fields, &$conds, &$join_conds, &$options, $filter_tag='')
Applies all tags-related changes to a query.
Definition: ChangeTags.php:773
NewPagesPager\$actorMigration
ActorMigration $actorMigration
Definition: NewPagesPager.php:56
NewPagesPager\getQueryInfo
getQueryInfo()
Provides all parameters needed for the main paged query.
Definition: NewPagesPager.php:90
NewPagesPager\__construct
__construct( $form, FormOptions $opts, LinkBatchFactory $linkBatchFactory, HookContainer $hookContainer, PermissionManager $permissionManager, ILoadBalancer $loadBalancer, NamespaceInfo $namespaceInfo, ActorMigration $actorMigration)
Definition: NewPagesPager.php:68
NewPagesPager\$linkBatchFactory
LinkBatchFactory $linkBatchFactory
Definition: NewPagesPager.php:44
NewPagesPager\$namespaceInfo
NamespaceInfo $namespaceInfo
Definition: NewPagesPager.php:53
MediaWiki\Cache\LinkBatchFactory
Definition: LinkBatchFactory.php:38
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:617
MediaWiki\Permissions\PermissionManager
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Definition: PermissionManager.php:49
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:72
NewPagesPager\$permissionManager
PermissionManager $permissionManager
Definition: NewPagesPager.php:50
NewPagesPager\formatRow
formatRow( $row)
Returns an HTML string representing the result row $row.
Definition: NewPagesPager.php:208
NewPagesPager
Definition: NewPagesPager.php:31
SpecialNewpages
A special page that list newly created pages.
Definition: SpecialNewpages.php:40
RecentChange\PRC_UNPATROLLED
const PRC_UNPATROLLED
Definition: RecentChange.php:82
NS_USER
const NS_USER
Definition: Defines.php:71
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:562
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