Merge "SpecialPages: Add ul { margin-top: 0; margin-bottom: 0 } for multicolumn"
[lhc/web/wiklou.git] / includes / specials / SpecialRecentchangeslinked.php
1 <?php
2 /**
3 * Implements Special:Recentchangeslinked
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * This is to display changes made to all articles linked in an article.
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialRecentChangesLinked extends SpecialRecentChanges {
30 /** @var bool|Title */
31 protected $rclTargetTitle;
32
33 protected $rclTarget;
34
35 function __construct() {
36 parent::__construct( 'Recentchangeslinked' );
37 }
38
39 public function getDefaultOptions() {
40 $opts = parent::getDefaultOptions();
41 $opts->add( 'target', '' );
42 $opts->add( 'showlinkedto', false );
43
44 return $opts;
45 }
46
47 public function parseParameters( $par, FormOptions $opts ) {
48 $opts['target'] = $par;
49 $this->rclTarget = $par;
50 }
51
52 /**
53 * @inheritDoc
54 */
55 protected function doMainQuery( $tables, $select, $conds, $query_options,
56 $join_conds, FormOptions $opts
57 ) {
58 $target = $opts['target'];
59 $showlinkedto = $opts['showlinkedto'];
60 $limit = $opts['limit'];
61
62 if ( $target === '' ) {
63 return false;
64 }
65 $outputPage = $this->getOutput();
66 $title = Title::newFromText( $target );
67 if ( !$title || $title->isExternal() ) {
68 $outputPage->addHTML(
69 Html::errorBox( $this->msg( 'allpagesbadtitle' )->parse() )
70 );
71
72 return false;
73 }
74
75 $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
76
77 /*
78 * Ordinary links are in the pagelinks table, while transclusions are
79 * in the templatelinks table, categorizations in categorylinks and
80 * image use in imagelinks. We need to somehow combine all these.
81 * Special:Whatlinkshere does this by firing multiple queries and
82 * merging the results, but the code we inherit from our parent class
83 * expects only one result set so we use UNION instead.
84 */
85
86 $dbr = wfGetDB( DB_REPLICA, 'recentchangeslinked' );
87 $id = $title->getArticleID();
88 $ns = $title->getNamespace();
89 $dbkey = $title->getDBkey();
90
91 $rcQuery = RecentChange::getQueryInfo();
92 $tables = array_merge( $tables, $rcQuery['tables'] );
93 $select = array_merge( $rcQuery['fields'], $select );
94 $join_conds = array_merge( $join_conds, $rcQuery['joins'] );
95
96 // left join with watchlist table to highlight watched rows
97 $uid = $this->getUser()->getId();
98 if ( $uid && $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
99 $tables[] = 'watchlist';
100 $select[] = 'wl_user';
101 $join_conds['watchlist'] = [ 'LEFT JOIN', [
102 'wl_user' => $uid,
103 'wl_title=rc_title',
104 'wl_namespace=rc_namespace'
105 ] ];
106 }
107
108 // JOIN on page, used for 'last revision' filter highlight
109 $tables[] = 'page';
110 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
111 $select[] = 'page_latest';
112
113 $tagFilter = $opts['tagfilter'] ? explode( '|', $opts['tagfilter'] ) : [];
114 ChangeTags::modifyDisplayQuery(
115 $tables,
116 $select,
117 $conds,
118 $join_conds,
119 $query_options,
120 $tagFilter
121 );
122
123 if ( $dbr->unionSupportsOrderAndLimit() ) {
124 if ( count( $tagFilter ) > 1 ) {
125 // ChangeTags::modifyDisplayQuery() will have added DISTINCT.
126 // To prevent this from causing query performance problems, we need to add
127 // a GROUP BY, and add rc_id to the ORDER BY.
128 $order = [
129 'GROUP BY' => 'rc_timestamp, rc_id',
130 'ORDER BY' => 'rc_timestamp DESC, rc_id DESC'
131 ];
132 } else {
133 $order = [ 'ORDER BY' => 'rc_timestamp DESC' ];
134 }
135 } else {
136 $order = [];
137 }
138
139 if ( !$this->runMainQueryHook( $tables, $select, $conds, $query_options, $join_conds,
140 $opts )
141 ) {
142 return false;
143 }
144
145 if ( $ns == NS_CATEGORY && !$showlinkedto ) {
146 // special handling for categories
147 // XXX: should try to make this less kludgy
148 $link_tables = [ 'categorylinks' ];
149 $showlinkedto = true;
150 } else {
151 // for now, always join on these tables; really should be configurable as in whatlinkshere
152 $link_tables = [ 'pagelinks', 'templatelinks' ];
153 // imagelinks only contains links to pages in NS_FILE
154 if ( $ns == NS_FILE || !$showlinkedto ) {
155 $link_tables[] = 'imagelinks';
156 }
157 }
158
159 if ( $id == 0 && !$showlinkedto ) {
160 return false; // nonexistent pages can't link to any pages
161 }
162
163 // field name prefixes for all the various tables we might want to join with
164 $prefix = [
165 'pagelinks' => 'pl',
166 'templatelinks' => 'tl',
167 'categorylinks' => 'cl',
168 'imagelinks' => 'il'
169 ];
170
171 $subsql = []; // SELECT statements to combine with UNION
172
173 foreach ( $link_tables as $link_table ) {
174 $pfx = $prefix[$link_table];
175
176 // imagelinks and categorylinks tables have no xx_namespace field,
177 // and have xx_to instead of xx_title
178 if ( $link_table == 'imagelinks' ) {
179 $link_ns = NS_FILE;
180 } elseif ( $link_table == 'categorylinks' ) {
181 $link_ns = NS_CATEGORY;
182 } else {
183 $link_ns = 0;
184 }
185
186 if ( $showlinkedto ) {
187 // find changes to pages linking to this page
188 if ( $link_ns ) {
189 if ( $ns != $link_ns ) {
190 continue;
191 } // should never happen, but check anyway
192 $subconds = [ "{$pfx}_to" => $dbkey ];
193 } else {
194 $subconds = [ "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey ];
195 }
196 $subjoin = "rc_cur_id = {$pfx}_from";
197 } else {
198 // find changes to pages linked from this page
199 $subconds = [ "{$pfx}_from" => $id ];
200 if ( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) {
201 $subconds["rc_namespace"] = $link_ns;
202 $subjoin = "rc_title = {$pfx}_to";
203 } else {
204 $subjoin = [ "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" ];
205 }
206 }
207
208 $query = $dbr->selectSQLText(
209 array_merge( $tables, [ $link_table ] ),
210 $select,
211 $conds + $subconds,
212 __METHOD__,
213 $order + $query_options,
214 $join_conds + [ $link_table => [ 'INNER JOIN', $subjoin ] ]
215 );
216
217 if ( $dbr->unionSupportsOrderAndLimit() ) {
218 $query = $dbr->limitResult( $query, $limit );
219 }
220
221 $subsql[] = $query;
222 }
223
224 if ( count( $subsql ) == 0 ) {
225 return false; // should never happen
226 }
227 if ( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
228 $sql = $subsql[0];
229 } else {
230 // need to resort and relimit after union
231 $sql = $dbr->unionQueries( $subsql, false ) . ' ORDER BY rc_timestamp DESC';
232 $sql = $dbr->limitResult( $sql, $limit, false );
233 }
234
235 $res = $dbr->query( $sql, __METHOD__ );
236
237 if ( $res->numRows() == 0 ) {
238 $this->mResultEmpty = true;
239 }
240
241 return $res;
242 }
243
244 function setTopText( FormOptions $opts ) {
245 $target = $this->getTargetTitle();
246 if ( $target ) {
247 $this->getOutput()->addBacklinkSubtitle( $target );
248 $this->getSkin()->setRelevantTitle( $target );
249 }
250 }
251
252 /**
253 * Get options to be displayed in a form
254 *
255 * @param FormOptions $opts
256 * @return array
257 */
258 function getExtraOptions( $opts ) {
259 $extraOpts = parent::getExtraOptions( $opts );
260
261 $opts->consumeValues( [ 'showlinkedto', 'target' ] );
262
263 $extraOpts['target'] = [ $this->msg( 'recentchangeslinked-page' )->escaped(),
264 Xml::input( 'target', 40, str_replace( '_', ' ', $opts['target'] ) ) .
265 Xml::check( 'showlinkedto', $opts['showlinkedto'], [ 'id' => 'showlinkedto' ] ) . ' ' .
266 Xml::label( $this->msg( 'recentchangeslinked-to' )->text(), 'showlinkedto' ) ];
267
268 $this->addHelpLink( 'Help:Related changes' );
269 return $extraOpts;
270 }
271
272 /**
273 * @return Title
274 */
275 function getTargetTitle() {
276 if ( $this->rclTargetTitle === null ) {
277 $opts = $this->getOptions();
278 if ( isset( $opts['target'] ) && $opts['target'] !== '' ) {
279 $this->rclTargetTitle = Title::newFromText( $opts['target'] );
280 } else {
281 $this->rclTargetTitle = false;
282 }
283 }
284
285 return $this->rclTargetTitle;
286 }
287
288 /**
289 * Return an array of subpages beginning with $search that this special page will accept.
290 *
291 * @param string $search Prefix to search for
292 * @param int $limit Maximum number of results to return (usually 10)
293 * @param int $offset Number of results to skip (usually 0)
294 * @return string[] Matching subpages
295 */
296 public function prefixSearchSubpages( $search, $limit, $offset ) {
297 return $this->prefixSearchString( $search, $limit, $offset );
298 }
299
300 /**
301 * Get a self-referential title object
302 * with consideration to the given subpage.
303 *
304 * @param string|bool $subpage
305 * @return Title
306 * @since 1.23
307 */
308 public function getPageTitle( $subpage = false ) {
309 $subpage = $subpage ? $subpage : $this->rclTarget;
310
311 return parent::getPageTitle( $subpage );
312 }
313
314 protected function outputNoResults() {
315 if ( $this->getTargetTitle() === false ) {
316 $this->getOutput()->addHTML(
317 '<div class="mw-changeslist-notargetpage">' .
318 $this->msg( 'recentchanges-notargetpage' )->parse() .
319 '</div>'
320 );
321 } else {
322 parent::outputNoResults();
323 }
324 }
325 }