MediaWiki REL1_32
SpecialRecentchangeslinked.php
Go to the documentation of this file.
1<?php
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
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 return false;
69 }
70
71 $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
72
73 /*
74 * Ordinary links are in the pagelinks table, while transclusions are
75 * in the templatelinks table, categorizations in categorylinks and
76 * image use in imagelinks. We need to somehow combine all these.
77 * Special:Whatlinkshere does this by firing multiple queries and
78 * merging the results, but the code we inherit from our parent class
79 * expects only one result set so we use UNION instead.
80 */
81
82 $dbr = wfGetDB( DB_REPLICA, 'recentchangeslinked' );
83 $id = $title->getArticleID();
84 $ns = $title->getNamespace();
85 $dbkey = $title->getDBkey();
86
87 $rcQuery = RecentChange::getQueryInfo();
88 $tables = array_merge( $tables, $rcQuery['tables'] );
89 $select = array_merge( $rcQuery['fields'], $select );
90 $join_conds = array_merge( $join_conds, $rcQuery['joins'] );
91
92 // left join with watchlist table to highlight watched rows
93 $uid = $this->getUser()->getId();
94 if ( $uid && $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
95 $tables[] = 'watchlist';
96 $select[] = 'wl_user';
97 $join_conds['watchlist'] = [ 'LEFT JOIN', [
98 'wl_user' => $uid,
99 'wl_title=rc_title',
100 'wl_namespace=rc_namespace'
101 ] ];
102 }
103
104 // JOIN on page, used for 'last revision' filter highlight
105 $tables[] = 'page';
106 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
107 $select[] = 'page_latest';
108
109 $tagFilter = $opts['tagfilter'] ? explode( '|', $opts['tagfilter'] ) : [];
110 ChangeTags::modifyDisplayQuery(
111 $tables,
112 $select,
113 $conds,
114 $join_conds,
115 $query_options,
116 $tagFilter
117 );
118
119 if ( $dbr->unionSupportsOrderAndLimit() ) {
120 if ( count( $tagFilter ) > 1 ) {
121 // ChangeTags::modifyDisplayQuery() will have added DISTINCT.
122 // To prevent this from causing query performance problems, we need to add
123 // a GROUP BY, and add rc_id to the ORDER BY.
124 $order = [
125 'GROUP BY' => 'rc_timestamp, rc_id',
126 'ORDER BY' => 'rc_timestamp DESC, rc_id DESC'
127 ];
128 } else {
129 $order = [ 'ORDER BY' => 'rc_timestamp DESC' ];
130 }
131 } else {
132 $order = [];
133 }
134
135 if ( !$this->runMainQueryHook( $tables, $select, $conds, $query_options, $join_conds,
136 $opts )
137 ) {
138 return false;
139 }
140
141 if ( $ns == NS_CATEGORY && !$showlinkedto ) {
142 // special handling for categories
143 // XXX: should try to make this less kludgy
144 $link_tables = [ 'categorylinks' ];
145 $showlinkedto = true;
146 } else {
147 // for now, always join on these tables; really should be configurable as in whatlinkshere
148 $link_tables = [ 'pagelinks', 'templatelinks' ];
149 // imagelinks only contains links to pages in NS_FILE
150 if ( $ns == NS_FILE || !$showlinkedto ) {
151 $link_tables[] = 'imagelinks';
152 }
153 }
154
155 if ( $id == 0 && !$showlinkedto ) {
156 return false; // nonexistent pages can't link to any pages
157 }
158
159 // field name prefixes for all the various tables we might want to join with
160 $prefix = [
161 'pagelinks' => 'pl',
162 'templatelinks' => 'tl',
163 'categorylinks' => 'cl',
164 'imagelinks' => 'il'
165 ];
166
167 $subsql = []; // SELECT statements to combine with UNION
168
169 foreach ( $link_tables as $link_table ) {
170 $pfx = $prefix[$link_table];
171
172 // imagelinks and categorylinks tables have no xx_namespace field,
173 // and have xx_to instead of xx_title
174 if ( $link_table == 'imagelinks' ) {
175 $link_ns = NS_FILE;
176 } elseif ( $link_table == 'categorylinks' ) {
177 $link_ns = NS_CATEGORY;
178 } else {
179 $link_ns = 0;
180 }
181
182 if ( $showlinkedto ) {
183 // find changes to pages linking to this page
184 if ( $link_ns ) {
185 if ( $ns != $link_ns ) {
186 continue;
187 } // should never happen, but check anyway
188 $subconds = [ "{$pfx}_to" => $dbkey ];
189 } else {
190 $subconds = [ "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey ];
191 }
192 $subjoin = "rc_cur_id = {$pfx}_from";
193 } else {
194 // find changes to pages linked from this page
195 $subconds = [ "{$pfx}_from" => $id ];
196 if ( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) {
197 $subconds["rc_namespace"] = $link_ns;
198 $subjoin = "rc_title = {$pfx}_to";
199 } else {
200 $subjoin = [ "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" ];
201 }
202 }
203
204 $query = $dbr->selectSQLText(
205 array_merge( $tables, [ $link_table ] ),
206 $select,
207 $conds + $subconds,
208 __METHOD__,
209 $order + $query_options,
210 $join_conds + [ $link_table => [ 'INNER JOIN', $subjoin ] ]
211 );
212
213 if ( $dbr->unionSupportsOrderAndLimit() ) {
214 $query = $dbr->limitResult( $query, $limit );
215 }
216
217 $subsql[] = $query;
218 }
219
220 if ( count( $subsql ) == 0 ) {
221 return false; // should never happen
222 }
223 if ( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
224 $sql = $subsql[0];
225 } else {
226 // need to resort and relimit after union
227 $sql = $dbr->unionQueries( $subsql, false ) . ' ORDER BY rc_timestamp DESC';
228 $sql = $dbr->limitResult( $sql, $limit, false );
229 }
230
231 $res = $dbr->query( $sql, __METHOD__ );
232
233 if ( $res->numRows() == 0 ) {
234 $this->mResultEmpty = true;
235 }
236
237 return $res;
238 }
239
240 function setTopText( FormOptions $opts ) {
241 $target = $this->getTargetTitle();
242 if ( $target ) {
243 $this->getOutput()->addBacklinkSubtitle( $target );
244 $this->getSkin()->setRelevantTitle( $target );
245 }
246 }
247
254 function getExtraOptions( $opts ) {
255 $extraOpts = parent::getExtraOptions( $opts );
256
257 $opts->consumeValues( [ 'showlinkedto', 'target' ] );
258
259 $extraOpts['target'] = [ $this->msg( 'recentchangeslinked-page' )->escaped(),
260 Xml::input( 'target', 40, str_replace( '_', ' ', $opts['target'] ) ) .
261 Xml::check( 'showlinkedto', $opts['showlinkedto'], [ 'id' => 'showlinkedto' ] ) . ' ' .
262 Xml::label( $this->msg( 'recentchangeslinked-to' )->text(), 'showlinkedto' ) ];
263
264 $this->addHelpLink( 'Help:Related changes' );
265 return $extraOpts;
266 }
267
271 function getTargetTitle() {
272 if ( $this->rclTargetTitle === null ) {
273 $opts = $this->getOptions();
274 if ( isset( $opts['target'] ) && $opts['target'] !== '' ) {
275 $this->rclTargetTitle = Title::newFromText( $opts['target'] );
276 } else {
277 $this->rclTargetTitle = false;
278 }
279 }
280
282 }
283
292 public function prefixSearchSubpages( $search, $limit, $offset ) {
293 return $this->prefixSearchString( $search, $limit, $offset );
294 }
295
296 protected function outputNoResults() {
297 $targetTitle = $this->getTargetTitle();
298 if ( $targetTitle === false ) {
299 $this->getOutput()->addHTML(
300 '<div class="mw-changeslist-empty mw-changeslist-notargetpage">' .
301 $this->msg( 'recentchanges-notargetpage' )->parse() .
302 '</div>'
303 );
304 } elseif ( !$targetTitle || $targetTitle->isExternal() ) {
305 $this->getOutput()->addHTML(
306 '<div class="mw-changeslist-empty mw-changeslist-invalidtargetpage">' .
307 $this->msg( 'allpagesbadtitle' )->parse() .
308 '</div>'
309 );
310 } else {
311 parent::outputNoResults();
312 }
313 }
314}
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second as well as the first line of the second redirect text
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
runMainQueryHook(&$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts)
getOptions()
Get the current FormOptions for this request.
Helper class to keep track of options when mixing links and form elements.
getOutput()
Get the OutputPage being used for this instance.
getUser()
Shortcut to get the User executing this instance.
getSkin()
Shortcut to get the skin being used for this instance.
msg( $key)
Wrapper around wfMessage that sets the current context.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
prefixSearchString( $search, $limit, $offset)
Perform a regular substring search for prefixSearchSubpages.
This is to display changes made to all articles linked in an article.
setTopText(FormOptions $opts)
Send the text to be displayed above the options.
doMainQuery( $tables, $select, $conds, $query_options, $join_conds, FormOptions $opts)
@inheritDoc
parseParameters( $par, FormOptions $opts)
Process $par and put options found in $opts.
getDefaultOptions()
Get a FormOptions object containing the default options.
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
outputNoResults()
Add the "no results" message to the output.
getExtraOptions( $opts)
Get options to be displayed in a form.
A special page that lists last changes made to the wiki.
Represents a title within MediaWiki.
Definition Title.php:39
$res
Definition database.txt:21
this hook is for auditing only RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist & $tables
Definition hooks.txt:1035
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition hooks.txt:1656
const NS_FILE
Definition Defines.php:70
const NS_CATEGORY
Definition Defines.php:78
const DB_REPLICA
Definition defines.php:25