Merge "docs: Fix typo"
[mediawiki.git] / maintenance / dumpLinks.php
blobdc8c4d81fcfde2ecd0fb1b09392230638945cf27
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
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.
11 * Copyright © 2005 Brooke Vibber <bvibber@wikimedia.org>
12 * https://www.mediawiki.org/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 * http://www.gnu.org/copyleft/gpl.html
29 * @file
30 * @ingroup Maintenance
33 // @codeCoverageIgnoreStart
34 require_once __DIR__ . '/Maintenance.php';
35 // @codeCoverageIgnoreEnd
37 use MediaWiki\Maintenance\Maintenance;
38 use MediaWiki\Title\Title;
40 /**
41 * Maintenance script that generates a plaintext link dump.
43 * @ingroup Maintenance
45 class DumpLinks extends Maintenance {
46 public function __construct() {
47 parent::__construct();
48 $this->addDescription( 'Quick demo hack to generate a plaintext link dump' );
51 public function execute() {
52 $dbr = $this->getReplicaDB();
53 $linksMigration = $this->getServiceContainer()->getLinksMigration();
54 $queryInfo = $linksMigration->getQueryInfo( 'pagelinks' );
55 $queryInfo['tables'] = array_diff( $queryInfo['tables'], [ 'pagelinks' ] );
56 [ $blNamespace, $blTitle ] = $linksMigration->getTitleFields( 'pagelinks' );
58 $result = $dbr->newSelectQueryBuilder()
59 ->select( array_merge( [
60 'page_id',
61 'page_namespace',
62 'page_title',
63 ], $queryInfo['fields'] ) )
64 ->from( 'page' )
65 ->join( 'pagelinks', null, [ 'page_id=pl_from' ] )
66 ->joinConds( $queryInfo['joins'] )
67 ->tables( $queryInfo['tables'] )
68 ->orderBy( 'page_id' )
69 ->caller( __METHOD__ )
70 ->fetchResultSet();
72 $lastPage = null;
73 foreach ( $result as $row ) {
74 if ( $lastPage != $row->page_id ) {
75 if ( $lastPage !== null ) {
76 $this->output( "\n" );
78 $page = Title::makeTitle( $row->page_namespace, $row->page_title );
79 $this->output( $page->getPrefixedURL() );
80 $lastPage = $row->page_id;
82 $link = Title::makeTitle( $row->$blNamespace, $row->$blTitle );
83 $this->output( " " . $link->getPrefixedURL() );
85 if ( $lastPage !== null ) {
86 $this->output( "\n" );
91 // @codeCoverageIgnoreStart
92 $maintClass = DumpLinks::class;
93 require_once RUN_MAINTENANCE_IF_MAIN;
94 // @codeCoverageIgnoreEnd