MediaWiki  master
NewPagesPager.php
Go to the documentation of this file.
1 <?php
26 
28 
32  protected $opts;
33 
37  protected $mForm;
38 
43  public function __construct( $form, FormOptions $opts ) {
44  parent::__construct( $form->getContext() );
45  $this->mForm = $form;
46  $this->opts = $opts;
47  }
48 
49  public function getQueryInfo() {
50  $rcQuery = RecentChange::getQueryInfo();
51 
52  $conds = [];
53  $conds['rc_new'] = 1;
54 
55  $username = $this->opts->getValue( 'username' );
56  $user = Title::makeTitleSafe( NS_USER, $username );
57 
58  $size = abs( intval( $this->opts->getValue( 'size' ) ) );
59  if ( $size > 0 ) {
60  if ( $this->opts->getValue( 'size-mode' ) === 'max' ) {
61  $conds[] = 'page_len <= ' . $size;
62  } else {
63  $conds[] = 'page_len >= ' . $size;
64  }
65  }
66 
67  if ( $user ) {
68  $conds[] = ActorMigration::newMigration()->getWhere(
69  $this->mDb, 'rc_user', User::newFromName( $user->getText(), false ), false
70  )['conds'];
71  } elseif ( $this->canAnonymousUsersCreatePages() && $this->opts->getValue( 'hideliu' ) ) {
72  # If anons cannot make new pages, don't "exclude logged in users"!
73  $conds[] = ActorMigration::newMigration()->isAnon( $rcQuery['fields']['rc_user'] );
74  }
75 
76  $conds = array_merge( $conds, $this->getNamespaceCond() );
77 
78  # If this user cannot see patrolled edits or they are off, don't do dumb queries!
79  if ( $this->opts->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
80  $conds['rc_patrolled'] = RecentChange::PRC_UNPATROLLED;
81  }
82 
83  if ( $this->opts->getValue( 'hidebots' ) ) {
84  $conds['rc_bot'] = 0;
85  }
86 
87  if ( $this->opts->getValue( 'hideredirs' ) ) {
88  $conds['page_is_redirect'] = 0;
89  }
90 
91  // Allow changes to the New Pages query
92  $tables = array_merge( $rcQuery['tables'], [ 'page' ] );
93  $fields = array_merge( $rcQuery['fields'], [
94  'length' => 'page_len', 'rev_id' => 'page_latest', 'page_namespace', 'page_title',
95  'page_content_model',
96  ] );
97  $join_conds = [ 'page' => [ 'JOIN', 'page_id=rc_cur_id' ] ] + $rcQuery['joins'];
98 
99  // Avoid PHP 7.1 warning from passing $this by reference
100  $pager = $this;
101  Hooks::run( 'SpecialNewpagesConditions',
102  [ &$pager, $this->opts, &$conds, &$tables, &$fields, &$join_conds ] );
103 
104  $info = [
105  'tables' => $tables,
106  'fields' => $fields,
107  'conds' => $conds,
108  'options' => [],
109  'join_conds' => $join_conds
110  ];
111 
112  // Modify query for tags
114  $info['tables'],
115  $info['fields'],
116  $info['conds'],
117  $info['join_conds'],
118  $info['options'],
119  $this->opts['tagfilter']
120  );
121 
122  return $info;
123  }
124 
125  private function canAnonymousUsersCreatePages() {
126  $pm = MediaWikiServices::getInstance()->getPermissionManager();
127  return ( $pm->groupHasPermission( '*', 'createpage' ) ||
128  $pm->groupHasPermission( '*', 'createtalk' )
129  );
130  }
131 
132  // Based on ContribsPager.php
133  private function getNamespaceCond() {
134  $namespace = $this->opts->getValue( 'namespace' );
135  if ( $namespace === 'all' || $namespace === '' ) {
136  return [];
137  }
138 
139  $namespace = intval( $namespace );
140  $invert = $this->opts->getValue( 'invert' );
141  $associated = $this->opts->getValue( 'associated' );
142 
143  $eq_op = $invert ? '!=' : '=';
144  $bool_op = $invert ? 'AND' : 'OR';
145 
146  $selectedNS = $this->mDb->addQuotes( $namespace );
147  if ( !$associated ) {
148  return [ "rc_namespace $eq_op $selectedNS" ];
149  }
150 
151  $associatedNS = $this->mDb->addQuotes(
152  MediaWikiServices::getInstance()->getNamespaceInfo()->getAssociated( $namespace )
153  );
154  return [
155  "rc_namespace $eq_op $selectedNS " .
156  $bool_op .
157  " rc_namespace $eq_op $associatedNS"
158  ];
159  }
160 
161  public function getIndexField() {
162  return 'rc_timestamp';
163  }
164 
165  public function formatRow( $row ) {
166  return $this->mForm->formatRow( $row );
167  }
168 
169  protected function getStartBody() {
170  # Do a batch existence check on pages
171  $linkBatch = new LinkBatch();
172  foreach ( $this->mResult as $row ) {
173  $linkBatch->add( NS_USER, $row->rc_user_text );
174  $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
175  $linkBatch->add( $row->page_namespace, $row->page_title );
176  }
177  $linkBatch->execute();
178 
179  return '<ul>';
180  }
181 
182  protected function getEndBody() {
183  return '</ul>';
184  }
185 }
RecentChange\getQueryInfo
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new recentchanges object.
Definition: RecentChange.php:234
NewPagesPager\getIndexField
getIndexField()
Returns the name of the index field.
Definition: NewPagesPager.php:161
NewPagesPager\$opts
FormOptions $opts
Definition: NewPagesPager.php:32
LinkBatch
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:35
NewPagesPager\canAnonymousUsersCreatePages
canAnonymousUsersCreatePages()
Definition: NewPagesPager.php:125
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:144
NewPagesPager\getStartBody
getStartBody()
Hook into getBody(), allows text to be inserted at the start.
Definition: NewPagesPager.php:169
NewPagesPager\getNamespaceCond
getNamespaceCond()
Definition: NewPagesPager.php:133
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:533
NewPagesPager\getEndBody
getEndBody()
Hook into getBody() for the end of the list.
Definition: NewPagesPager.php:182
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:139
ChangeTags\modifyDisplayQuery
static modifyDisplayQuery(&$tables, &$fields, &$conds, &$join_conds, &$options, $filter_tag='')
Applies all tags-related changes to a query.
Definition: ChangeTags.php:781
NewPagesPager\getQueryInfo
getQueryInfo()
Provides all parameters needed for the main paged query.
Definition: NewPagesPager.php:49
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:621
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:72
NewPagesPager\formatRow
formatRow( $row)
Returns an HTML string representing the result row $row.
Definition: NewPagesPager.php:165
NewPagesPager
Definition: NewPagesPager.php:27
SpecialNewpages
A special page that list newly created pages.
Definition: SpecialNewpages.php:35
RecentChange\PRC_UNPATROLLED
const PRC_UNPATROLLED
Definition: RecentChange.php:80
NewPagesPager\__construct
__construct( $form, FormOptions $opts)
Definition: NewPagesPager.php:43
NS_USER
const NS_USER
Definition: Defines.php:71
ReverseChronologicalPager
Efficient paging for SQL queries.
Definition: ReverseChronologicalPager.php:28
FormOptions
Helper class to keep track of options when mixing links and form elements.
Definition: FormOptions.php:35
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:133
NewPagesPager\$mForm
SpecialNewpages $mForm
Definition: NewPagesPager.php:37