MediaWiki master
SpecialListRedirects.php
Go to the documentation of this file.
1<?php
8
9namespace MediaWiki\Specials;
10
11use MediaWiki\Html\Html;
12use MediaWiki\Page\LinkBatchFactory;
13use MediaWiki\Page\RedirectLookup;
14use MediaWiki\Page\WikiPageFactory;
15use MediaWiki\Skin\Skin;
16use MediaWiki\SpecialPage\QueryPage;
17use MediaWiki\Title\Title;
18use stdClass;
19use Wikimedia\Rdbms\IConnectionProvider;
20use Wikimedia\Rdbms\IReadableDatabase;
21use Wikimedia\Rdbms\IResultWrapper;
22
30
31 public function __construct(
32 private readonly LinkBatchFactory $linkBatchFactory,
33 IConnectionProvider $dbProvider,
34 private readonly WikiPageFactory $wikiPageFactory,
35 private readonly RedirectLookup $redirectLookup,
36 ) {
37 parent::__construct( 'Listredirects' );
38 $this->setDatabaseProvider( $dbProvider );
39 }
40
42 public function isExpensive() {
43 return true;
44 }
45
47 public function isSyndicated() {
48 return false;
49 }
50
52 protected function sortDescending() {
53 return false;
54 }
55
57 public function getQueryInfo() {
58 return [
59 'tables' => [ 'page', 'redirect' ],
60 'fields' => [ 'namespace' => 'page_namespace',
61 'title' => 'page_title',
62 'rd_namespace',
63 'rd_title',
64 'rd_fragment',
65 'rd_interwiki',
66 ],
67 'conds' => [ 'page_is_redirect' => 1 ],
68 'join_conds' => [ 'redirect' => [
69 'LEFT JOIN', 'rd_from=page_id' ],
70 ]
71 ];
72 }
73
75 protected function getOrderFields() {
76 return [ 'page_namespace', 'page_title' ];
77 }
78
85 public function preprocessResults( $db, $res ) {
86 if ( !$res->numRows() ) {
87 return;
88 }
89
90 $batch = $this->linkBatchFactory->newLinkBatch()->setCaller( __METHOD__ );
91 foreach ( $res as $row ) {
92 $batch->add( $row->namespace, $row->title );
93 $redirTarget = $this->getRedirectTarget( $row );
94 if ( $redirTarget ) {
95 $batch->addObj( $redirTarget );
96 }
97 }
98 $batch->execute();
99
100 // Back to start for display
101 $res->seek( 0 );
102 }
103
104 protected function getRedirectTarget( stdClass $row ): ?Title {
105 if ( isset( $row->rd_title ) ) {
106 return Title::makeTitle(
107 $row->rd_namespace,
108 $row->rd_title,
109 $row->rd_fragment,
110 $row->rd_interwiki
111 );
112 } else {
113 $title = Title::makeTitle( $row->namespace, $row->title );
114 if ( !$title->canExist() ) {
115 return null;
116 }
117
118 return Title::castFromLinkTarget(
119 $this->redirectLookup->getRedirectTarget( $title )
120 );
121 }
122 }
123
129 public function formatResult( $skin, $result ) {
130 $linkRenderer = $this->getLinkRenderer();
131 # Make a link to the redirect itself
132 $rd_title = Title::makeTitle( $result->namespace, $result->title );
133 $rd_link = $linkRenderer->makeLink(
134 $rd_title,
135 null,
136 [],
137 [ 'redirect' => 'no' ]
138 );
139
140 # Find out where the redirect leads
141 $target = $this->getRedirectTarget( $result );
142 if ( $target ) {
143 # Make a link to the destination page
144 $lang = $this->getLanguage();
145 $arr = $lang->getArrow();
146 $rd_link = Html::rawElement( 'bdi', [ 'dir' => $lang->getDir() ], $rd_link );
147 $targetLink = $linkRenderer->makeLink( $target, $target->getFullText() );
148 $targetLink = Html::rawElement( 'bdi', [ 'dir' => $lang->getDir() ], $targetLink );
149
150 return "$rd_link $arr $targetLink";
151 } else {
152 return "<del>$rd_link</del>";
153 }
154 }
155
157 public function execute( $par ) {
158 $this->addHelpLink( 'Help:Redirects' );
159 parent::execute( $par );
160 }
161
163 protected function getGroupName() {
164 return 'pages';
165 }
166}
167
168// @codeCoverageIgnoreStart
170class_alias( SpecialListRedirects::class, 'SpecialListRedirects' );
171// @codeCoverageIgnoreEnd
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:72
makeTitle( $linkId)
Convert a link ID to a Title.to override Title
Factory for LinkBatch objects to batch query page metadata.
Service for creating WikiPage objects.
This is a class for doing query pages; since they're almost all the same, we factor out some of the f...
Definition QueryPage.php:77
setDatabaseProvider(IConnectionProvider $databaseProvider)
getLanguage()
Shortcut to get user's language.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Lists all the redirecting pages on the wiki.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
getOrderFields()
Subclasses return an array of fields to order by here.Don't append DESC to the field names,...
sortDescending()
Override to sort by increasing values.to override bool
isSyndicated()
Sometimes we don't want to build rss / atom feeds.to override bool
execute( $par)
This is the actual workhorse.It does everything needed to make a real, honest-to-gosh query page....
isExpensive()
Should this query page only be updated offline on large wikis?If the query for this page is considere...
__construct(private readonly LinkBatchFactory $linkBatchFactory, IConnectionProvider $dbProvider, private readonly WikiPageFactory $wikiPageFactory, private readonly RedirectLookup $redirectLookup,)
preprocessResults( $db, $res)
Cache page existence for performance.
getQueryInfo()
Subclasses return an SQL query here, formatted as an array with the following keys: tables => Table(s...
Represents a title within MediaWiki.
Definition Title.php:69
Service for resolving a wiki page redirect.
Provide primary and replica IDatabase connections.