Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[lhc/web/wiklou.git] / maintenance / fetchText.php
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
25 require_once __DIR__ . '/Maintenance.php';
26
27 use MediaWiki\MediaWikiServices;
28 use MediaWiki\Storage\BlobAccessException;
29 use MediaWiki\Storage\SqlBlobStore;
30
31 /**
32 * Maintenance script used to fetch page text in a subprocess.
33 *
34 * @ingroup Maintenance
35 */
36 class FetchText extends Maintenance {
37
38 public function __construct() {
39 parent::__construct();
40
41 $this->addDescription( "Fetch the raw revision blob from a blob address.\n" .
42 "Integer IDs are interpreted as referring to text.old_id for backwards compatibility.\n" .
43 "NOTE: Export transformations are NOT applied. " .
44 "This is left to dumpTextPass.php"
45 );
46 }
47
48 /**
49 * @return SqlBlobStore
50 */
51 private function getBlobStore() {
52 return MediaWikiServices::getInstance()->getBlobStore();
53 }
54
55 /**
56 * returns a string containing the following in order:
57 * textid
58 * \n
59 * length of text (-1 on error = failure to retrieve/unserialize/gunzip/etc)
60 * \n
61 * text (may be empty)
62 *
63 * note that the text string itself is *not* followed by newline
64 */
65 public function execute() {
66 $stdin = $this->getStdin();
67 while ( !feof( $stdin ) ) {
68 $line = fgets( $stdin );
69 if ( $line === false ) {
70 // We appear to have lost contact...
71 break;
72 }
73 $blobAddress = trim( $line );
74
75 // Plain integers are supported for backwards compatibility with pre-MCR dumps.
76 if ( strpos( $blobAddress, ':' ) === false && is_numeric( $blobAddress ) ) {
77 $blobAddress = SqlBlobStore::makeAddressFromTextId( intval( $blobAddress ) );
78 }
79
80 try {
81 $text = $this->getBlobStore()->getBlob( $blobAddress );
82 $textLen = strlen( $text );
83 } catch ( BlobAccessException $ex ) {
84 // XXX: log $ex to stderr?
85 $textLen = '-1';
86 $text = '';
87 } catch ( InvalidArgumentException $ex ) {
88 // XXX: log $ex to stderr?
89 $textLen = '-1';
90 $text = '';
91 }
92
93 $this->output( $blobAddress . "\n" . $textLen . "\n" . $text );
94 }
95 }
96
97 }
98
99 $maintClass = FetchText::class;
100 require_once RUN_MAINTENANCE_IF_MAIN;