\n to eof
[lhc/web/wiklou.git] / maintenance / fetchText.php
1 <?php
2 /**
3 * Communications protocol...
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @ingroup Maintenance
21 */
22
23 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
24
25 class FetchText extends Maintenance {
26 public function __construct() {
27 parent::__construct();
28 $this->mDescription = "Fetch the revision text from an old_id";
29 }
30
31 /*
32 * returns a string containing the following in order:
33 * textid
34 * \n
35 * length of text (-1 on error = failure to retrieve/unserialize/gunzip/etc)
36 * \n
37 * text (may be empty)
38 *
39 * note that that the text string itself is *not* followed by newline
40 */
41 public function execute() {
42 $db = wfGetDB( DB_SLAVE );
43 $stdin = $this->getStdin();
44 while ( !feof( $stdin ) ) {
45 $line = fgets( $stdin );
46 if ( $line === false ) {
47 // We appear to have lost contact...
48 break;
49 }
50 $textId = intval( $line );
51 $text = $this->doGetText( $db, $textId );
52 if ($text === false) {
53 # actual error, not zero-length text
54 $textLen = "-1";
55 }
56 else {
57 $textLen = strlen($text);
58 }
59 $this->output( $textId . "\n" . $textLen . "\n" . $text );
60 }
61 }
62
63 /**
64 * May throw a database error if, say, the server dies during query.
65 * @param $db Database object
66 * @param $id int The old_id
67 * @return String
68 */
69 private function doGetText( $db, $id ) {
70 $id = intval( $id );
71 $row = $db->selectRow( 'text',
72 array( 'old_text', 'old_flags' ),
73 array( 'old_id' => $id ),
74 'TextPassDumper::getText' );
75 $text = Revision::getRevisionText( $row );
76 if ( $text === false ) {
77 return false;
78 }
79 return $text;
80 }
81 }
82
83 $maintClass = "FetchText";
84 require_once( DO_MAINTENANCE );