Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / maintenance / fetchText.php
blobff236c1dd29b199b9801b0d391994325a76cb412
1 <?php
2 /**
3 * Communications protocol.
4 * This is used by dumpTextPass.php when the --spawn option is present.
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.
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.
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
21 * @file
22 * @ingroup Maintenance
25 // @codeCoverageIgnoreStart
26 require_once __DIR__ . '/Maintenance.php';
27 // @codeCoverageIgnoreEnd
29 use MediaWiki\MainConfigNames;
30 use MediaWiki\Maintenance\Maintenance;
31 use MediaWiki\Settings\SettingsBuilder;
32 use MediaWiki\Storage\BlobAccessException;
33 use MediaWiki\Storage\BlobStore;
34 use MediaWiki\Storage\SqlBlobStore;
36 /**
37 * Maintenance script used to fetch page text in a subprocess.
39 * @ingroup Maintenance
41 class FetchText extends Maintenance {
43 public function __construct() {
44 parent::__construct();
45 $this->addDescription( "Fetch the raw revision blob from a blob address.\n" .
46 "Integer IDs are interpreted as referring to text.old_id for backwards compatibility.\n" .
47 "NOTE: Export transformations are NOT applied. " .
48 "This is left to dumpTextPass.php"
52 public function finalSetup( SettingsBuilder $settingsBuilder ) {
53 // This script should always try to run all db queries in the 'dump' group if such
54 // a group exists, just like the BackupDumper and TextPassDumper modules.
55 // To account for parts of MediaWiki that get their own db connection outside of
56 // Maintenance::getDB(), we set this global variable so that they will attempt
57 // to use this group.
58 $settingsBuilder->putConfigValue( MainConfigNames::DBDefaultGroup, 'dump' );
59 // do this last so that options can override
61 parent::finalSetup( $settingsBuilder );
64 /**
65 * @return BlobStore
67 private function getBlobStore() {
68 return $this->getServiceContainer()->getBlobStore();
71 /**
72 * returns a string containing the following in order:
73 * textid
74 * \n
75 * length of text (-1 on error = failure to retrieve/unserialize/gunzip/etc)
76 * \n
77 * text (may be empty)
79 * note that the text string itself is *not* followed by newline
81 public function execute() {
82 $stdin = $this->getStdin();
83 while ( !feof( $stdin ) ) {
84 $line = fgets( $stdin );
85 if ( $line === false ) {
86 // We appear to have lost contact...
87 break;
89 $blobAddress = trim( $line );
91 // Plain integers are supported for backwards compatibility with pre-MCR dumps.
92 if ( strpos( $blobAddress, ':' ) === false && is_numeric( $blobAddress ) ) {
93 $blobAddress = SqlBlobStore::makeAddressFromTextId( intval( $blobAddress ) );
96 try {
97 $text = $this->getBlobStore()->getBlob( $blobAddress );
98 $textLen = strlen( $text );
99 } catch ( BlobAccessException | InvalidArgumentException $ex ) {
100 // XXX: log $ex to stderr?
101 $textLen = '-1';
102 $text = '';
105 $this->output( $blobAddress . "\n" . $textLen . "\n" . $text );
111 // @codeCoverageIgnoreStart
112 $maintClass = FetchText::class;
113 require_once RUN_MAINTENANCE_IF_MAIN;
114 // @codeCoverageIgnoreEnd