MediaWiki  master
SpecialListRedirects.php
Go to the documentation of this file.
1 <?php
27 namespace MediaWiki\Specials;
28 
34 use Skin;
35 use stdClass;
39 
45 
46  private LinkBatchFactory $linkBatchFactory;
47  private WikiPageFactory $wikiPageFactory;
48  private RedirectLookup $redirectLookup;
49 
56  public function __construct(
57  LinkBatchFactory $linkBatchFactory,
58  IConnectionProvider $dbProvider,
59  WikiPageFactory $wikiPageFactory,
60  RedirectLookup $redirectLookup
61  ) {
62  parent::__construct( 'Listredirects' );
63  $this->linkBatchFactory = $linkBatchFactory;
64  $this->setDatabaseProvider( $dbProvider );
65  $this->wikiPageFactory = $wikiPageFactory;
66  $this->redirectLookup = $redirectLookup;
67  }
68 
69  public function isExpensive() {
70  return true;
71  }
72 
73  public function isSyndicated() {
74  return false;
75  }
76 
77  protected function sortDescending() {
78  return false;
79  }
80 
81  public function getQueryInfo() {
82  return [
83  'tables' => [ 'page', 'redirect' ],
84  'fields' => [ 'namespace' => 'page_namespace',
85  'title' => 'page_title',
86  'rd_namespace',
87  'rd_title',
88  'rd_fragment',
89  'rd_interwiki',
90  ],
91  'conds' => [ 'page_is_redirect' => 1 ],
92  'join_conds' => [ 'redirect' => [
93  'LEFT JOIN', 'rd_from=page_id' ],
94  ]
95  ];
96  }
97 
98  protected function getOrderFields() {
99  return [ 'page_namespace', 'page_title' ];
100  }
101 
108  public function preprocessResults( $db, $res ) {
109  if ( !$res->numRows() ) {
110  return;
111  }
112 
113  $batch = $this->linkBatchFactory->newLinkBatch();
114  foreach ( $res as $row ) {
115  $batch->add( $row->namespace, $row->title );
116  $redirTarget = $this->getRedirectTarget( $row );
117  if ( $redirTarget ) {
118  $batch->addObj( $redirTarget );
119  }
120  }
121  $batch->execute();
122 
123  // Back to start for display
124  $res->seek( 0 );
125  }
126 
131  protected function getRedirectTarget( $row ) {
132  if ( isset( $row->rd_title ) ) {
133  return Title::makeTitle(
134  $row->rd_namespace,
135  $row->rd_title,
136  $row->rd_fragment ?? '',
137  $row->rd_interwiki ?? ''
138  );
139  } else {
140  $title = Title::makeTitle( $row->namespace, $row->title );
141  if ( !$title->canExist() ) {
142  return null;
143  }
144 
146  $this->redirectLookup->getRedirectTarget( $title )
147  );
148  }
149  }
150 
156  public function formatResult( $skin, $result ) {
157  $linkRenderer = $this->getLinkRenderer();
158  # Make a link to the redirect itself
159  $rd_title = Title::makeTitle( $result->namespace, $result->title );
160  $rd_link = $linkRenderer->makeLink(
161  $rd_title,
162  null,
163  [],
164  [ 'redirect' => 'no' ]
165  );
166 
167  # Find out where the redirect leads
168  $target = $this->getRedirectTarget( $result );
169  if ( $target ) {
170  # Make a link to the destination page
171  $lang = $this->getLanguage();
172  $arr = $lang->getArrow() . $lang->getDirMark();
173  $targetLink = $linkRenderer->makeLink( $target, $target->getFullText() );
174 
175  return "$rd_link $arr $targetLink";
176  } else {
177  return "<del>$rd_link</del>";
178  }
179  }
180 
181  public function execute( $par ) {
182  $this->addHelpLink( 'Help:Redirects' );
183  parent::execute( $par );
184  }
185 
186  protected function getGroupName() {
187  return 'pages';
188  }
189 }
190 
194 class_alias( SpecialListRedirects::class, 'SpecialListRedirects' );
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:88
setDatabaseProvider(IConnectionProvider $databaseProvider)
Definition: QueryPage.php:985
getLanguage()
Shortcut to get user's language.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Special:Listredirects - Lists all the redirects 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.
sortDescending()
Override to sort by increasing values.
isSyndicated()
Sometimes we don't want to build rss / atom feeds.
execute( $par)
This is the actual workhorse.
__construct(LinkBatchFactory $linkBatchFactory, IConnectionProvider $dbProvider, WikiPageFactory $wikiPageFactory, RedirectLookup $redirectLookup)
isExpensive()
Should this query page only be updated offline on large wikis?
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:76
static castFromLinkTarget(?LinkTarget $linkTarget)
Same as newFromLinkTarget(), but if passed null, returns null.
Definition: Title.php:314
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:624
The base class for all skins.
Definition: Skin.php:60
Service for resolving a wiki page redirect.
Provide primary and replica IDatabase connections.
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:36
Result wrapper for grabbing data queried from an IDatabase object.