Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
GoogleCoopFilter
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 8
306
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 register
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 writeOpenStream
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 writeCloseStream
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 writeOpenPage
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
 writeClosePage
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
30
 removeBrackets
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 threeLines
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace MediaWiki\Extension\ActiveAbstract;
4
5use BackupDumper;
6use ExportProgressFilter;
7use MediaWiki\Title\Title;
8use stdClass;
9use Xml;
10
11/**
12 * Dump filter for creation of a Google Coop 'Subscribed Links' file
13 *
14 * Usage:
15 *
16 * HOSTNAME=kamelopedia.mormo.org php dumpBackup.php \
17 *    --plugin=GoogleCoopFilter \
18 *    --current --output=file:coop3.xml --filter=namespace:NS_MAIN \
19 *    --filter=noredirect --filter=googlecoop
20 *
21 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
22 * Copyright (C) 2006 Jens Frank < JeLuF (at) mormo org >
23 * https://www.mediawiki.org/
24 *
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
29 *
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License along
36 * with this program; if not, write to the Free Software Foundation, Inc.,
37 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
38 * http://www.gnu.org/copyleft/gpl.html
39 *
40 * @file
41 * @author Brion Vibber <brion@pobox.com>
42 * @author Jens Frank < JeLuF (at) mormo org >
43 * @ingroup Maintenance
44 *
45 */
46class GoogleCoopFilter extends AbstractFilter {
47    /**
48     * @param ExportProgressFilter &$sink
49     */
50    public function __construct( &$sink ) {
51        $this->sink =& $sink;
52    }
53
54    /**
55     * Register the filter function with the dump manager
56     * @param BackupDumper $dumper
57     */
58    public static function register( $dumper ) {
59        $dumper->registerFilter( 'googlecoop', self::class );
60        parent::register( $dumper );
61    }
62
63    /**
64     * @param string $string
65     */
66    public function writeOpenStream( $string ) {
67        $this->sink->writeOpenStream(
68            "<Results>\n  <AuthorInfo description=\"MediaWiki autogenerated Google Coop output\" />\n"
69        );
70    }
71
72    /**
73     * @param string $string
74     */
75    public function writeCloseStream( $string ) {
76        $this->sink->writeCloseStream( "</Results>\n" );
77    }
78
79    /**
80     * @param stdClass $page
81     * @param string $string
82     */
83    public function writeOpenPage( $page, $string ) {
84        global $wgSitename;
85        static $n = 0;
86        $n++;
87        $this->title = Title::makeTitle( $page->page_namespace, $page->page_title );
88
89        $xml = "  <ResultSpec id=\"mw{$n}\">\n";
90        $xml .= '    ' . Xml::element( 'Query', null, $this->title->getPrefixedText() ) . "\n";
91        $xml .= "    <Response>\n";
92        $xml .= '      ' . Xml::element( 'Output', [ 'name' => 'title' ],
93                $wgSitename . ':' . $this->title->getPrefixedText() ) . "\n";
94        $xml .= '      ' . Xml::element( 'Output', [ 'name' => 'more_url' ],
95                $this->title->getCanonicalUrl() ) . "\n";
96
97        // add abstract and links when we have revision data...
98        $this->revision = null;
99
100        $this->sink->writeOpenPage( $page, $xml );
101    }
102
103    /**
104     * @param string $string
105     */
106    public function writeClosePage( $string ) {
107        $xml = '';
108        if ( $this->revision ) {
109            $text = $this->removeBrackets( $this->extractAbstract( $this->revision ) );
110            if ( $text == '' ) {
111                $text = '-';
112            }
113            $lines = $this->threeLines( $text );
114            for ( $i = 1; $i < 4; $i++ ) {
115                if ( $lines[$i] != '' ) {
116                    $xml .= '      ' . Xml::element( 'Output', [ 'name' => 'text' . $i ], $lines[$i] ) . "\n";
117                }
118            }
119        }
120        $xml .= "    </Response>\n  </ResultSpec>\n";
121        $this->sink->writeClosePage( $xml );
122        $this->title = null;
123        $this->revision = null;
124    }
125
126    /**
127     * @param string $string
128     * @return string
129     */
130    private function removeBrackets( $string ) {
131        return preg_replace( '#[\[\]]#', '', $string );
132    }
133
134    /**
135     * Returns an array of three strings, each string of the array has no more than
136     * 79 characters. The three strings are the first three 'lines' of the text
137     * given in $str.
138     *
139     * Lines are split at the last blank before position 79.
140     * If there's no blank before position, the entire string is returned as first
141     * element of the result array.
142     *
143     * This code needs a cleanup, it became rather ugly after adding exception
144     * handling :-(
145     *
146     * @param string $str
147     * @return array|string
148     */
149    private function threeLines( $str ) {
150        $s = [];
151
152        $slen = strlen( $str );
153
154        if ( $slen < 79 ) {
155            return [ 1 => $str, 2 => '', 3 => '' ];
156        }
157
158        $a = strrchr( substr( $str, 0, 79 ), ' ' );
159        $s1len = 79 - strlen( $a );
160        if ( $s1len == 79 ) {
161            return [ 1 => $str, 2 => '', 3 => '' ];
162        }
163        $s[1] = substr( $str, 0, $s1len );
164
165        if ( $slen < $s1len + 79 ) {
166            return [ 1 => $s[1], 2 => substr( $str, $s1len + 1 ), 3 => '' ];
167        }
168
169        $b = strrchr( substr( $str, $s1len + 1, 79 ), ' ' );
170        $s2len = 79 - strlen( $b );
171        $s[2] = substr( $str, $s1len + 1, $s2len );
172
173        if ( $slen < $s1len + $s2len + 79 ) {
174            return [ 1 => $s[1], 2 => $s[2], 3 => substr( $str, $s1len + $s2len + 1 ) ];
175        }
176
177        $c = strrchr( substr( $str, $s1len + $s2len + 2, 76 ), ' ' );
178        $s3len = 76 - strlen( $c );
179        $s[3] = substr( $str, $s1len + $s2len + 2, $s3len );
180        if ( strlen( $str ) > $s1len + $s2len + $s3len + 2 ) {
181            $s[3] .= '...';
182        }
183
184        return $s;
185    }
186}
187
188class_alias( GoogleCoopFilter::class, 'GoogleCoopFilter' );