Merge "mw.rcfilters.ui.SaveFiltersPopupButtonWidget: Remove pointless option"
[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 function __construct() {
34 parent::__construct( 'Recentchangeslinked' );
35 }
36
37 public function getDefaultOptions() {
38 $opts = parent::getDefaultOptions();
39 $opts->add( 'target', '' );
40 $opts->add( 'showlinkedto', false );
41
42 return $opts;
43 }
44
45 public function parseParameters( $par, FormOptions $opts ) {
46 $opts['target'] = $par;
47 }
48
49 /**
50 * @inheritDoc
51 */
52 protected function doMainQuery( $tables, $select, $conds, $query_options,
53 $join_conds, FormOptions $opts
54 ) {
55 $target = $opts['target'];
56 $showlinkedto = $opts['showlinkedto'];
57 $limit = $opts['limit'];
58
59 if ( $target === '' ) {
60 return false;
61 }
62 $outputPage = $this->getOutput();
63 $title = Title::newFromText( $target );
64 if ( !$title || $title->isExternal() ) {
65 $outputPage->addHTML(
66 Html::errorBox( $this->msg( 'allpagesbadtitle' )->parse() )
67 );
68
69 return false;
70 }
71
72 $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
73
74 /*
75 * Ordinary links are in the pagelinks table, while transclusions are
76 * in the templatelinks table, categorizations in categorylinks and
77 * image use in imagelinks. We need to somehow combine all these.
78 * Special:Whatlinkshere does this by firing multiple queries and
79 * merging the results, but the code we inherit from our parent class
80 * expects only one result set so we use UNION instead.
81 */
82
83 $dbr = wfGetDB( DB_REPLICA, 'recentchangeslinked' );
84 $id = $title->getArticleID();
85 $ns = $title->getNamespace();
86 $dbkey = $title->getDBkey();
87
88 $rcQuery = RecentChange::getQueryInfo();
89 $tables = array_merge( $tables, $rcQuery['tables'] );
90 $select = array_merge( $rcQuery['fields'], $select );
91 $join_conds = array_merge( $join_conds, $rcQuery['joins'] );
92
93 // left join with watchlist table to highlight watched rows
94 $uid = $this->getUser()->getId();
95 if ( $uid && $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
96 $tables[] = 'watchlist';
97 $select[] = 'wl_user';
98 $join_conds['watchlist'] = [ 'LEFT JOIN', [
99 'wl_user' => $uid,
100 'wl_title=rc_title',
101 'wl_namespace=rc_namespace'
102 ] ];
103 }
104
105 // JOIN on page, used for 'last revision' filter highlight
106 $tables[] = 'page';
107 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
108 $select[] = 'page_latest';
109
110 $tagFilter = $opts['tagfilter'] ? explode( '|', $opts['tagfilter'] ) : [];
111 ChangeTags::modifyDisplayQuery(
112 $tables,
113 $select,
114 $conds,
115 $join_conds,
116 $query_options,
117 $tagFilter
118 );
119
120 if ( $dbr->unionSupportsOrderAndLimit() ) {
121 if ( count( $tagFilter ) > 1 ) {
122 // ChangeTags::modifyDisplayQuery() will have added DISTINCT.
123 // To prevent this from causing query performance problems, we need to add
124 // a GROUP BY, and add rc_id to the ORDER BY.
125 $order = [
126 'GROUP BY' => 'rc_timestamp, rc_id',
127 'ORDER BY' => 'rc_timestamp DESC, rc_id DESC'
128 ];
129 } else {
130 $order = [ 'ORDER BY' => 'rc_timestamp DESC' ];
131 }
132 } else {
133 $order = [];
134 }
135
136 if ( !$this->runMainQueryHook( $tables, $select, $conds, $query_options, $join_conds,
137 $opts )
138 ) {
139 return false;
140 }
141
142 if ( $ns == NS_CATEGORY && !$showlinkedto ) {
143 // special handling for categories
144 // XXX: should try to make this less kludgy
145 $link_tables = [ 'categorylinks' ];
146 $showlinkedto = true;
147 } else {
148 // for now, always join on these tables; really should be configurable as in whatlinkshere
149 $link_tables = [ 'pagelinks', 'templatelinks' ];
150 // imagelinks only contains links to pages in NS_FILE
151 if ( $ns == NS_FILE || !$showlinkedto ) {
152 $link_tables[] = 'imagelinks';
153 }
154 }
155
156 if ( $id == 0 && !$showlinkedto ) {
157 return false; // nonexistent pages can't link to any pages
158 }
159
160 // field name prefixes for all the various tables we might want to join with
161 $prefix = [
162 'pagelinks' => 'pl',
163 'templatelinks' => 'tl',
164 'categorylinks' => 'cl',
165 'imagelinks' => 'il'
166 ];
167
168 $subsql = []; // SELECT statements to combine with UNION
169
170 foreach ( $link_tables as $link_table ) {
171 $pfx = $prefix[$link_table];
172
173 // imagelinks and categorylinks tables have no xx_namespace field,
174 // and have xx_to instead of xx_title
175 if ( $link_table == 'imagelinks' ) {
176 $link_ns = NS_FILE;
177 } elseif ( $link_table == 'categorylinks' ) {
178 $link_ns = NS_CATEGORY;
179 } else {
180 $link_ns = 0;
181 }
182
183 if ( $showlinkedto ) {
184 // find changes to pages linking to this page
185 if ( $link_ns ) {
186 if ( $ns != $link_ns ) {
187 continue;
188 } // should never happen, but check anyway
189 $subconds = [ "{$pfx}_to" => $dbkey ];
190 } else {
191 $subconds = [ "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey ];
192 }
193 $subjoin = "rc_cur_id = {$pfx}_from";
194 } else {
195 // find changes to pages linked from this page
196 $subconds = [ "{$pfx}_from" => $id ];
197 if ( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) {
198 $subconds["rc_namespace"] = $link_ns;
199 $subjoin = "rc_title = {$pfx}_to";
200 } else {
201 $subjoin = [ "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" ];
202 }
203 }
204
205 $query = $dbr->selectSQLText(
206 array_merge( $tables, [ $link_table ] ),
207 $select,
208 $conds + $subconds,
209 __METHOD__,
210 $order + $query_options,
211 $join_conds + [ $link_table => [ 'INNER JOIN', $subjoin ] ]
212 );
213
214 if ( $dbr->unionSupportsOrderAndLimit() ) {
215 $query = $dbr->limitResult( $query, $limit );
216 }
217
218 $subsql[] = $query;
219 }
220
221 if ( count( $subsql ) == 0 ) {
222 return false; // should never happen
223 }
224 if ( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
225 $sql = $subsql[0];
226 } else {
227 // need to resort and relimit after union
228 $sql = $dbr->unionQueries( $subsql, false ) . ' ORDER BY rc_timestamp DESC';
229 $sql = $dbr->limitResult( $sql, $limit, false );
230 }
231
232 $res = $dbr->query( $sql, __METHOD__ );
233
234 if ( $res->numRows() == 0 ) {
235 $this->mResultEmpty = true;
236 }
237
238 return $res;
239 }
240
241 function setTopText( FormOptions $opts ) {
242 $target = $this->getTargetTitle();
243 if ( $target ) {
244 $this->getOutput()->addBacklinkSubtitle( $target );
245 $this->getSkin()->setRelevantTitle( $target );
246 }
247 }
248
249 /**
250 * Get options to be displayed in a form
251 *
252 * @param FormOptions $opts
253 * @return array
254 */
255 function getExtraOptions( $opts ) {
256 $extraOpts = parent::getExtraOptions( $opts );
257
258 $opts->consumeValues( [ 'showlinkedto', 'target' ] );
259
260 $extraOpts['target'] = [ $this->msg( 'recentchangeslinked-page' )->escaped(),
261 Xml::input( 'target', 40, str_replace( '_', ' ', $opts['target'] ) ) .
262 Xml::check( 'showlinkedto', $opts['showlinkedto'], [ 'id' => 'showlinkedto' ] ) . ' ' .
263 Xml::label( $this->msg( 'recentchangeslinked-to' )->text(), 'showlinkedto' ) ];
264
265 $this->addHelpLink( 'Help:Related changes' );
266 return $extraOpts;
267 }
268
269 /**
270 * @return Title
271 */
272 function getTargetTitle() {
273 if ( $this->rclTargetTitle === null ) {
274 $opts = $this->getOptions();
275 if ( isset( $opts['target'] ) && $opts['target'] !== '' ) {
276 $this->rclTargetTitle = Title::newFromText( $opts['target'] );
277 } else {
278 $this->rclTargetTitle = false;
279 }
280 }
281
282 return $this->rclTargetTitle;
283 }
284
285 /**
286 * Return an array of subpages beginning with $search that this special page will accept.
287 *
288 * @param string $search Prefix to search for
289 * @param int $limit Maximum number of results to return (usually 10)
290 * @param int $offset Number of results to skip (usually 0)
291 * @return string[] Matching subpages
292 */
293 public function prefixSearchSubpages( $search, $limit, $offset ) {
294 return $this->prefixSearchString( $search, $limit, $offset );
295 }
296
297 protected function outputNoResults() {
298 if ( $this->getTargetTitle() === false ) {
299 $this->getOutput()->addHTML(
300 '<div class="mw-changeslist-notargetpage">' .
301 $this->msg( 'recentchanges-notargetpage' )->parse() .
302 '</div>'
303 );
304 } else {
305 parent::outputNoResults();
306 }
307 }
308 }