95f67d59b8fb4e32513426ee9ac6faa33b37ae40
[lhc/web/wiklou.git] / includes / specials / pagers / NewPagesPager.php
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 /**
23 * @ingroup Pager
24 */
25 use MediaWiki\MediaWikiServices;
26
27 class NewPagesPager extends ReverseChronologicalPager {
28
29 /**
30 * @var FormOptions
31 */
32 protected $opts;
33
34 /**
35 * @var SpecialNewpages
36 */
37 protected $mForm;
38
39 /**
40 * @param SpecialNewpages $form
41 * @param FormOptions $opts
42 */
43 public function __construct( $form, FormOptions $opts ) {
44 parent::__construct( $form->getContext() );
45 $this->mForm = $form;
46 $this->opts = $opts;
47 }
48
49 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 ( MediaWikiServices::getInstance()
72 ->getPermissionManager()
73 ->groupHasPermission( '*', 'createpage' ) &&
74 $this->opts->getValue( 'hideliu' )
75 ) {
76 # If anons cannot make new pages, don't "exclude logged in users"!
77 $conds[] = ActorMigration::newMigration()->isAnon( $rcQuery['fields']['rc_user'] );
78 }
79
80 $conds = array_merge( $conds, $this->getNamespaceCond() );
81
82 # If this user cannot see patrolled edits or they are off, don't do dumb queries!
83 if ( $this->opts->getValue( 'hidepatrolled' ) && $this->getUser()->useNPPatrol() ) {
84 $conds['rc_patrolled'] = RecentChange::PRC_UNPATROLLED;
85 }
86
87 if ( $this->opts->getValue( 'hidebots' ) ) {
88 $conds['rc_bot'] = 0;
89 }
90
91 if ( $this->opts->getValue( 'hideredirs' ) ) {
92 $conds['page_is_redirect'] = 0;
93 }
94
95 // Allow changes to the New Pages query
96 $tables = array_merge( $rcQuery['tables'], [ 'page' ] );
97 $fields = array_merge( $rcQuery['fields'], [
98 'length' => 'page_len', 'rev_id' => 'page_latest', 'page_namespace', 'page_title'
99 ] );
100 $join_conds = [ 'page' => [ 'JOIN', 'page_id=rc_cur_id' ] ] + $rcQuery['joins'];
101
102 // Avoid PHP 7.1 warning from passing $this by reference
103 $pager = $this;
104 Hooks::run( 'SpecialNewpagesConditions',
105 [ &$pager, $this->opts, &$conds, &$tables, &$fields, &$join_conds ] );
106
107 $info = [
108 'tables' => $tables,
109 'fields' => $fields,
110 'conds' => $conds,
111 'options' => [],
112 'join_conds' => $join_conds
113 ];
114
115 // Modify query for tags
116 ChangeTags::modifyDisplayQuery(
117 $info['tables'],
118 $info['fields'],
119 $info['conds'],
120 $info['join_conds'],
121 $info['options'],
122 $this->opts['tagfilter']
123 );
124
125 return $info;
126 }
127
128 // Based on ContribsPager.php
129 function getNamespaceCond() {
130 $namespace = $this->opts->getValue( 'namespace' );
131 if ( $namespace === 'all' || $namespace === '' ) {
132 return [];
133 }
134
135 $namespace = intval( $namespace );
136 $invert = $this->opts->getValue( 'invert' );
137 $associated = $this->opts->getValue( 'associated' );
138
139 $eq_op = $invert ? '!=' : '=';
140 $bool_op = $invert ? 'AND' : 'OR';
141
142 $selectedNS = $this->mDb->addQuotes( $namespace );
143 if ( !$associated ) {
144 return [ "rc_namespace $eq_op $selectedNS" ];
145 }
146
147 $associatedNS = $this->mDb->addQuotes(
148 MediaWikiServices::getInstance()->getNamespaceInfo()->getAssociated( $namespace )
149 );
150 return [
151 "rc_namespace $eq_op $selectedNS " .
152 $bool_op .
153 " rc_namespace $eq_op $associatedNS"
154 ];
155 }
156
157 function getIndexField() {
158 return 'rc_timestamp';
159 }
160
161 function formatRow( $row ) {
162 return $this->mForm->formatRow( $row );
163 }
164
165 protected function getStartBody() {
166 # Do a batch existence check on pages
167 $linkBatch = new LinkBatch();
168 foreach ( $this->mResult as $row ) {
169 $linkBatch->add( NS_USER, $row->rc_user_text );
170 $linkBatch->add( NS_USER_TALK, $row->rc_user_text );
171 $linkBatch->add( $row->page_namespace, $row->page_title );
172 }
173 $linkBatch->execute();
174
175 return '<ul>';
176 }
177
178 protected function getEndBody() {
179 return '</ul>';
180 }
181 }