MediaWiki  1.34.0
SpecialRecentChangesLinked.php
Go to the documentation of this file.
1 <?php
25 
33  protected $rclTargetTitle;
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  }
50 
54  protected function doMainQuery( $tables, $select, $conds, $query_options,
55  $join_conds, FormOptions $opts
56  ) {
57  $target = $opts['target'];
58  $showlinkedto = $opts['showlinkedto'];
59  $limit = $opts['limit'];
60 
61  if ( $target === '' ) {
62  return false;
63  }
64  $outputPage = $this->getOutput();
65  $title = Title::newFromText( $target );
66  if ( !$title || $title->isExternal() ) {
67  $outputPage->addHTML(
68  Html::errorBox( $this->msg( 'allpagesbadtitle' )->parse() )
69  );
70  return false;
71  }
72 
73  $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
74 
75  /*
76  * Ordinary links are in the pagelinks table, while transclusions are
77  * in the templatelinks table, categorizations in categorylinks and
78  * image use in imagelinks. We need to somehow combine all these.
79  * Special:Whatlinkshere does this by firing multiple queries and
80  * merging the results, but the code we inherit from our parent class
81  * expects only one result set so we use UNION instead.
82  */
83 
84  $dbr = wfGetDB( DB_REPLICA, 'recentchangeslinked' );
85  $id = $title->getArticleID();
86  $ns = $title->getNamespace();
87  $dbkey = $title->getDBkey();
88 
89  $rcQuery = RecentChange::getQueryInfo();
90  $tables = array_merge( $tables, $rcQuery['tables'] );
91  $select = array_merge( $rcQuery['fields'], $select );
92  $join_conds = array_merge( $join_conds, $rcQuery['joins'] );
93 
94  // left join with watchlist table to highlight watched rows
95  $uid = $this->getUser()->getId();
96  if ( $uid && MediaWikiServices::getInstance()
98  ->userHasRight( $this->getUser(), 'viewmywatchlist' )
99  ) {
100  $tables[] = 'watchlist';
101  $select[] = 'wl_user';
102  $join_conds['watchlist'] = [ 'LEFT JOIN', [
103  'wl_user' => $uid,
104  'wl_title=rc_title',
105  'wl_namespace=rc_namespace'
106  ] ];
107  }
108 
109  // JOIN on page, used for 'last revision' filter highlight
110  $tables[] = 'page';
111  $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
112  $select[] = 'page_latest';
113 
114  $tagFilter = $opts['tagfilter'] ? explode( '|', $opts['tagfilter'] ) : [];
116  $tables,
117  $select,
118  $conds,
119  $join_conds,
120  $query_options,
121  $tagFilter
122  );
123 
124  if ( $dbr->unionSupportsOrderAndLimit() ) {
125  if ( count( $tagFilter ) > 1 ) {
126  // ChangeTags::modifyDisplayQuery() will have added DISTINCT.
127  // To prevent this from causing query performance problems, we need to add
128  // a GROUP BY, and add rc_id to the ORDER BY.
129  $order = [
130  'GROUP BY' => 'rc_timestamp, rc_id',
131  'ORDER BY' => 'rc_timestamp DESC, rc_id DESC'
132  ];
133  } else {
134  $order = [ 'ORDER BY' => 'rc_timestamp DESC' ];
135  }
136  } else {
137  $order = [];
138  }
139 
140  if ( !$this->runMainQueryHook( $tables, $select, $conds, $query_options, $join_conds,
141  $opts )
142  ) {
143  return false;
144  }
145 
146  if ( $ns == NS_CATEGORY && !$showlinkedto ) {
147  // special handling for categories
148  // XXX: should try to make this less kludgy
149  $link_tables = [ 'categorylinks' ];
150  $showlinkedto = true;
151  } else {
152  // for now, always join on these tables; really should be configurable as in whatlinkshere
153  $link_tables = [ 'pagelinks', 'templatelinks' ];
154  // imagelinks only contains links to pages in NS_FILE
155  if ( $ns == NS_FILE || !$showlinkedto ) {
156  $link_tables[] = 'imagelinks';
157  }
158  }
159 
160  if ( $id == 0 && !$showlinkedto ) {
161  return false; // nonexistent pages can't link to any pages
162  }
163 
164  // field name prefixes for all the various tables we might want to join with
165  $prefix = [
166  'pagelinks' => 'pl',
167  'templatelinks' => 'tl',
168  'categorylinks' => 'cl',
169  'imagelinks' => 'il'
170  ];
171 
172  $subsql = []; // SELECT statements to combine with UNION
173 
174  foreach ( $link_tables as $link_table ) {
175  $pfx = $prefix[$link_table];
176 
177  // imagelinks and categorylinks tables have no xx_namespace field,
178  // and have xx_to instead of xx_title
179  if ( $link_table == 'imagelinks' ) {
180  $link_ns = NS_FILE;
181  } elseif ( $link_table == 'categorylinks' ) {
182  $link_ns = NS_CATEGORY;
183  } else {
184  $link_ns = 0;
185  }
186 
187  if ( $showlinkedto ) {
188  // find changes to pages linking to this page
189  if ( $link_ns ) {
190  if ( $ns != $link_ns ) {
191  continue;
192  } // should never happen, but check anyway
193  $subconds = [ "{$pfx}_to" => $dbkey ];
194  } else {
195  $subconds = [ "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey ];
196  }
197  $subjoin = "rc_cur_id = {$pfx}_from";
198  } else {
199  // find changes to pages linked from this page
200  $subconds = [ "{$pfx}_from" => $id ];
201  if ( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) {
202  $subconds["rc_namespace"] = $link_ns;
203  $subjoin = "rc_title = {$pfx}_to";
204  } else {
205  $subjoin = [ "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" ];
206  }
207  }
208 
209  $query = $dbr->selectSQLText(
210  array_merge( $tables, [ $link_table ] ),
211  $select,
212  $conds + $subconds,
213  __METHOD__,
214  $order + $query_options,
215  $join_conds + [ $link_table => [ 'JOIN', $subjoin ] ]
216  );
217 
218  if ( $dbr->unionSupportsOrderAndLimit() ) {
219  $query = $dbr->limitResult( $query, $limit );
220  }
221 
222  $subsql[] = $query;
223  }
224 
225  if ( count( $subsql ) == 0 ) {
226  return false; // should never happen
227  }
228  if ( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
229  $sql = $subsql[0];
230  } else {
231  // need to resort and relimit after union
232  $sql = $dbr->unionQueries( $subsql, $dbr::UNION_DISTINCT ) .
233  ' ORDER BY rc_timestamp DESC';
234  $sql = $dbr->limitResult( $sql, $limit, false );
235  }
236 
237  return $dbr->query( $sql, __METHOD__ );
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 
281  return $this->rclTargetTitle;
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  Html::rawElement(
301  'div',
302  [ 'class' => 'mw-changeslist-empty mw-changeslist-notargetpage' ],
303  $this->msg( 'recentchanges-notargetpage' )->parse()
304  )
305  );
306  } elseif ( !$targetTitle || $targetTitle->isExternal() ) {
307  $this->getOutput()->addHTML(
308  Html::rawElement(
309  'div',
310  [ 'class' => 'mw-changeslist-empty mw-changeslist-invalidtargetpage' ],
311  $this->msg( 'allpagesbadtitle' )->parse()
312  )
313  );
314  } else {
315  parent::outputNoResults();
316  }
317  }
318 }
SpecialRecentChangesLinked\getExtraOptions
getExtraOptions( $opts)
Get options to be displayed in a form.
Definition: SpecialRecentChangesLinked.php:254
RecentChange\getQueryInfo
static getQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new recentchanges object.
Definition: RecentChange.php:233
SpecialRecentChangesLinked\outputNoResults
outputNoResults()
Add the "no results" message to the output.
Definition: SpecialRecentChangesLinked.php:296
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:792
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:316
SpecialRecentChangesLinked\parseParameters
parseParameters( $par, FormOptions $opts)
Process $par and put options found in $opts.
Definition: SpecialRecentChangesLinked.php:47
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:719
SpecialRecentChangesLinked
This is to display changes made to all articles linked in an article.
Definition: SpecialRecentChangesLinked.php:31
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
Xml\label
static label( $label, $id, $attribs=[])
Convenience function to build an HTML form label.
Definition: Xml.php:358
SpecialRecentChangesLinked\getTargetTitle
getTargetTitle()
Definition: SpecialRecentChangesLinked.php:271
SpecialRecentChangesLinked\setTopText
setTopText(FormOptions $opts)
Send the text to be displayed above the options.
Definition: SpecialRecentChangesLinked.php:240
NS_FILE
const NS_FILE
Definition: Defines.php:66
SpecialPage\getSkin
getSkin()
Shortcut to get the skin being used for this instance.
Definition: SpecialPage.php:739
$dbr
$dbr
Definition: testCompression.php:50
ChangeTags\modifyDisplayQuery
static modifyDisplayQuery(&$tables, &$fields, &$conds, &$join_conds, &$options, $filter_tag='')
Applies all tags-related changes to a query.
Definition: ChangeTags.php:772
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:828
SpecialPage\prefixSearchString
prefixSearchString( $search, $limit, $offset)
Perform a regular substring search for prefixSearchSubpages.
Definition: SpecialPage.php:501
getPermissionManager
getPermissionManager()
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2575
ChangesListSpecialPage\runMainQueryHook
runMainQueryHook(&$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts)
Definition: ChangesListSpecialPage.php:1620
Xml\check
static check( $name, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox.
Definition: Xml.php:323
$title
$title
Definition: testCompression.php:34
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:729
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:74
SpecialRecentChangesLinked\prefixSearchSubpages
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
Definition: SpecialRecentChangesLinked.php:292
SpecialRecentChangesLinked\__construct
__construct()
Definition: SpecialRecentChangesLinked.php:35
SpecialRecentChangesLinked\doMainQuery
doMainQuery( $tables, $select, $conds, $query_options, $join_conds, FormOptions $opts)
Process the query.Array of tables; see IDatabase::select $table Array of fields; see IDatabase::selec...
Definition: SpecialRecentChangesLinked.php:54
SpecialRecentChanges
A special page that lists last changes made to the wiki.
Definition: SpecialRecentChanges.php:33
Title
Represents a title within MediaWiki.
Definition: Title.php:42
SpecialRecentChangesLinked\getDefaultOptions
getDefaultOptions()
Get a FormOptions object containing the default options.
Definition: SpecialRecentChangesLinked.php:39
Xml\input
static input( $name, $size=false, $value=false, $attribs=[])
Convenience function to build an HTML text input field.
Definition: Xml.php:274
FormOptions
Helper class to keep track of options when mixing links and form elements.
Definition: FormOptions.php:35
SpecialRecentChangesLinked\$rclTargetTitle
bool Title $rclTargetTitle
Definition: SpecialRecentChangesLinked.php:33
ChangesListSpecialPage\getOptions
getOptions()
Get the current FormOptions for this request.
Definition: ChangesListSpecialPage.php:1031