Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
DumpLinks
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2/**
3 * Quick demo hack to generate a plaintext link dump,
4 * per the proposed wiki link database standard:
5 * http://www.usemod.com/cgi-bin/mb.pl?LinkDatabase
6 *
7 * Includes all (live and broken) intra-wiki links.
8 * Does not include interwiki or URL links.
9 * Dumps ASCII text to stdout; command-line.
10 *
11 * Copyright © 2005 Brooke Vibber <bvibber@wikimedia.org>
12 * https://www.mediawiki.org/
13 *
14 * @license GPL-2.0-or-later
15 * @file
16 * @ingroup Maintenance
17 */
18
19// @codeCoverageIgnoreStart
20require_once __DIR__ . '/Maintenance.php';
21// @codeCoverageIgnoreEnd
22
23use MediaWiki\Deferred\LinksUpdate\PageLinksTable;
24use MediaWiki\Maintenance\Maintenance;
25use MediaWiki\Title\Title;
26
27/**
28 * Maintenance script that generates a plaintext link dump.
29 *
30 * @ingroup Maintenance
31 */
32class DumpLinks extends Maintenance {
33    public function __construct() {
34        parent::__construct();
35        $this->addDescription( 'Quick demo hack to generate a plaintext link dump' );
36    }
37
38    public function execute() {
39        $dbr = $this->getServiceContainer()
40            ->getConnectionProvider()
41            ->getReplicaDatabase( PageLinksTable::VIRTUAL_DOMAIN );
42        $linksMigration = $this->getServiceContainer()->getLinksMigration();
43        $queryInfo = $linksMigration->getQueryInfo( 'pagelinks' );
44        $queryInfo['tables'] = array_diff( $queryInfo['tables'], [ 'pagelinks' ] );
45        [ $blNamespace, $blTitle ] = $linksMigration->getTitleFields( 'pagelinks' );
46
47        $result = $dbr->newSelectQueryBuilder()
48            ->select( array_merge( [
49                'page_id',
50                'page_namespace',
51                'page_title',
52            ], $queryInfo['fields'] ) )
53            ->from( 'page' )
54            ->join( 'pagelinks', null, [ 'page_id=pl_from' ] )
55            ->joinConds( $queryInfo['joins'] )
56            ->tables( $queryInfo['tables'] )
57            ->orderBy( 'page_id' )
58            ->caller( __METHOD__ )
59            ->fetchResultSet();
60
61        $lastPage = null;
62        foreach ( $result as $row ) {
63            if ( $lastPage != $row->page_id ) {
64                if ( $lastPage !== null ) {
65                    $this->output( "\n" );
66                }
67                $page = Title::makeTitle( $row->page_namespace, $row->page_title );
68                $this->output( $page->getPrefixedURL() );
69                $lastPage = $row->page_id;
70            }
71            $link = Title::makeTitle( $row->$blNamespace, $row->$blTitle );
72            $this->output( " " . $link->getPrefixedURL() );
73        }
74        if ( $lastPage !== null ) {
75            $this->output( "\n" );
76        }
77    }
78}
79
80// @codeCoverageIgnoreStart
81$maintClass = DumpLinks::class;
82require_once RUN_MAINTENANCE_IF_MAIN;
83// @codeCoverageIgnoreEnd