Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
FetchText
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 4
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 finalSetup
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getBlobStore
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2/**
3 * Communications protocol.
4 * This is used by dumpTextPass.php when the --spawn option is present.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Maintenance
23 */
24
25require_once __DIR__ . '/Maintenance.php';
26
27use MediaWiki\MainConfigNames;
28use MediaWiki\Settings\SettingsBuilder;
29use MediaWiki\Storage\BlobAccessException;
30use MediaWiki\Storage\BlobStore;
31use MediaWiki\Storage\SqlBlobStore;
32
33/**
34 * Maintenance script used to fetch page text in a subprocess.
35 *
36 * @ingroup Maintenance
37 */
38class FetchText extends Maintenance {
39
40    public function __construct() {
41        parent::__construct();
42        $this->addDescription( "Fetch the raw revision blob from a blob address.\n" .
43            "Integer IDs are interpreted as referring to text.old_id for backwards compatibility.\n" .
44            "NOTE: Export transformations are NOT applied. " .
45            "This is left to dumpTextPass.php"
46        );
47    }
48
49    public function finalSetup( SettingsBuilder $settingsBuilder ) {
50        // This script should always try to run all db queries in the 'dump' group if such
51        // a group exists, just like the BackupDumper and TextPassDumper modules.
52        // To account for parts of MediaWiki that get their own db connection outside of
53        // Maintenance::getDB(), we set this global variable so that they will attempt
54        // to use this group.
55        $settingsBuilder->putConfigValue( MainConfigNames::DBDefaultGroup, 'dump' );
56        // do this last so that options can override
57
58        parent::finalSetup( $settingsBuilder );
59    }
60
61    /**
62     * @return BlobStore
63     */
64    private function getBlobStore() {
65        return $this->getServiceContainer()->getBlobStore();
66    }
67
68    /**
69     * returns a string containing the following in order:
70     *   textid
71     *   \n
72     *   length of text (-1 on error = failure to retrieve/unserialize/gunzip/etc)
73     *   \n
74     *   text  (may be empty)
75     *
76     * note that the text string itself is *not* followed by newline
77     */
78    public function execute() {
79        $stdin = $this->getStdin();
80        while ( !feof( $stdin ) ) {
81            $line = fgets( $stdin );
82            if ( $line === false ) {
83                // We appear to have lost contact...
84                break;
85            }
86            $blobAddress = trim( $line );
87
88            // Plain integers are supported for backwards compatibility with pre-MCR dumps.
89            if ( strpos( $blobAddress, ':' ) === false && is_numeric( $blobAddress ) ) {
90                $blobAddress = SqlBlobStore::makeAddressFromTextId( intval( $blobAddress ) );
91            }
92
93            try {
94                $text = $this->getBlobStore()->getBlob( $blobAddress );
95                $textLen = strlen( $text );
96            } catch ( BlobAccessException | InvalidArgumentException $ex ) {
97                // XXX: log $ex to stderr?
98                $textLen = '-1';
99                $text = '';
100            }
101
102            $this->output( $blobAddress . "\n" . $textLen . "\n" . $text );
103        }
104    }
105
106}
107
108$maintClass = FetchText::class;
109require_once RUN_MAINTENANCE_IF_MAIN;