| 1 |
<?php |
| 2 |
|
| 3 |
h |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Specials; |
| 24 |
|
| 25 |
use MediaWiki\Cache\LinkBatchFactory; |
| 26 |
use MediaWiki\Html\Html; |
| 27 |
use MediaWiki\Page\RedirectLookup; |
| 28 |
use MediaWiki\Page\WikiPageFactory; |
| 29 |
use MediaWiki\SpecialPage\QueryPage; |
| 30 |
use MediaWiki\Title\Title; |
| 31 |
use Skin; |
| 32 |
use stdClass; |
| 33 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 34 |
use Wikimedia\Rdbms\IDatabase; |
| 35 |
use Wikimedia\Rdbms\IResultWrapper; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class SpecialListRedirects extends QueryPage { |
| 44 |
|
| 45 |
private LinkBatchFactory $linkBatchFactory; |
| 46 |
private WikiPageFactory $wikiPageFactory; |
| 47 |
private RedirectLookup $redirectLookup; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function __construct( |
| 56 |
LinkBatchFactory $linkBatchFactory, |
| 57 |
IConnectionProvider $dbProvider, |
| 58 |
WikiPageFactory $wikiPageFactory, |
| 59 |
RedirectLookup $redirectLookup |
| 60 |
) { |
| 61 |
parent::__construct( 'Listredirects' ); |
| 62 |
$this->linkBatchFactory = $linkBatchFactory; |
| 63 |
$this->setDatabaseProvider( $dbProvider ); |
| 64 |
$this->wikiPageFactory = $wikiPageFactory; |
| 65 |
$this->redirectLookup = $redirectLookup; |
| 66 |
} |
| 67 |
|
| 68 |
public function isExpensive() { |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
public function isSyndicated() { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
protected function sortDescending() { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
public function getQueryInfo() { |
| 81 |
return [ |
| 82 |
'tables' => [ 'page', 'redirect' ], |
| 83 |
'fields' => [ 'namespace' => 'page_namespace', |
| 84 |
'title' => 'page_title', |
| 85 |
'rd_namespace', |
| 86 |
'rd_title', |
| 87 |
'rd_fragment', |
| 88 |
'rd_interwiki', |
| 89 |
], |
| 90 |
'conds' => [ 'page_is_redirect' => 1 ], |
| 91 |
'join_conds' => [ 'redirect' => [ |
| 92 |
'LEFT JOIN', 'rd_from=page_id' ], |
| 93 |
] |
| 94 |
]; |
| 95 |
} |
| 96 |
|
| 97 |
protected function getOrderFields() { |
| 98 |
return [ 'page_namespace', 'page_title' ]; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public function preprocessResults( $db, $res ) { |
| 108 |
if ( !$res->numRows() ) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
$batch = $this->linkBatchFactory->newLinkBatch(); |
| 113 |
foreach ( $res as $row ) { |
| 114 |
$batch->add( $row->namespace, $row->title ); |
| 115 |
$redirTarget = $this->getRedirectTarget( $row ); |
| 116 |
if ( $redirTarget ) { |
| 117 |
$batch->addObj( $redirTarget ); |
| 118 |
} |
| 119 |
} |
| 120 |
$batch->execute(); |
| 121 |
|
| 122 |
|
| 123 |
$res->seek( 0 ); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
protected function getRedirectTarget( $row ) { |
| 131 |
if ( isset( $row->rd_title ) ) { |
| 132 |
return Title::makeTitle( |
| 133 |
$row->rd_namespace, |
| 134 |
$row->rd_title, |
| 135 |
$row->rd_fragment, |
| 136 |
$row->rd_interwiki |
| 137 |
); |
| 138 |
} else { |
| 139 |
$title = Title::makeTitle( $row->namespace, $row->title ); |
| 140 |
if ( !$title->canExist() ) { |
| 141 |
return null; |
| 142 |
} |
| 143 |
|
| 144 |
return Title::castFromLinkTarget( |
| 145 |
$this->redirectLookup->getRedirectTarget( $title ) |
| 146 |
); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function formatResult( $skin, $result ) { |
| 156 |
$linkRenderer = $this->getLinkRenderer(); |
| 157 |
|
| 158 |
$rd_title = Title::makeTitle( $result->namespace, $result->title ); |
| 159 |
$rd_link = $linkRenderer->makeLink( |
| 160 |
$rd_title, |
| 161 |
null, |
| 162 |
[], |
| 163 |
[ 'redirect' => 'no' ] |
| 164 |
); |
| 165 |
|
| 166 |
|
| 167 |
$target = $this->getRedirectTarget( $result ); |
| 168 |
if ( $target ) { |
| 169 |
|
| 170 |
$lang = $this->getLanguage(); |
| 171 |
$arr = $lang->getArrow(); |
| 172 |
$rd_link = Html::rawElement( 'bdi', [ 'dir' => $lang->getDir() ], $rd_link ); |
| 173 |
$targetLink = $linkRenderer->makeLink( $target, $target->getFullText() ); |
| 174 |
$targetLink = Html::rawElement( 'bdi', [ 'dir' => $lang->getDir() ], $targetLink ); |
| 175 |
|
| 176 |
return "$rd_link $arr $targetLink"; |
| 177 |
} else { |
| 178 |
return "<del>$rd_link</del>"; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
public function execute( $par ) { |
| 183 |
$this->addHelpLink( 'Help:Redirects' ); |
| 184 |
parent::execute( $par ); |
| 185 |
} |
| 186 |
|
| 187 |
protected function getGroupName() { |
| 188 |
return 'pages'; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
class_alias( SpecialListRedirects::class, 'SpecialListRedirects' ); |