Completed
Push — master ( 3dc773...9cb031 )
by
unknown
04:27
created

BasicBackend::getLingoPage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 2
1
<?php
2
3
/**
4
 * File holding the Lingo\Backend class
5
 *
6
 * This file is part of the MediaWiki extension Lingo.
7
 *
8
 * @copyright 2011 - 2016, Stephan Gambke
9
 * @license   GNU General Public License, version 2 (or any later version)
10
 *
11
 * The Lingo extension is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by the Free
13
 * Software Foundation; either version 2 of the License, or (at your option) any
14
 * later version.
15
 *
16
 * The Lingo extension is distributed in the hope that it will be useful, but
17
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19
 * details.
20
 *
21
 * You should have received a copy of the GNU General Public License along
22
 * with this program. If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 * @author Stephan Gambke
25
 * @file
26
 * @ingroup Lingo
27
 */
28
namespace Lingo;
29
30
use ApprovedRevs;
31
use ContentHandler;
32
use Page;
33
use Parser;
34
use ParserOptions;
35
use Revision;
36
use Title;
37
use User;
38
39
/**
40
 * The Lingo\BasicBackend class.
41
 *
42
 * @ingroup Lingo
43
 */
44
class BasicBackend extends Backend {
45
46
	protected $mArticleLines = array();
47
48
	/**
49
	 * Lingo\BasicBackend constructor.
50
	 * @param MessageLog|null $messages
51
	 */
52 1
	public function __construct( MessageLog &$messages = null ) {
53
54 1
		global $wgRequest;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
55
56 1
		$page = self::getLingoPage();
57
58 1
		parent::__construct( $messages );
59
60
		// Get Terminology page
61 1
		$title = Title::newFromText( $page );
62 1
		if ( $title->getInterwiki() ) {
63
			$this->getMessageLog()->addError( wfMessage( 'lingo-terminologypagenotlocal', $page )->inContentLanguage()->text() );
64
			return;
65
		}
66
67
		// This is a hack special-casing the submitting of the terminology
68
		// page itself. In this case the Revision is not up to date when we get
69
		// here, i.e. $rev->getText() would return outdated Text.
70
		// This hack takes the text directly out of the data from the web request.
71 1
		if ( $wgRequest->getVal( 'action', 'view' ) === 'submit'
72 1
			&& Title::newFromText( $wgRequest->getVal( 'title' ) )->getArticleID() === $title->getArticleID()
73 1
		) {
74
75
			$content = $wgRequest->getVal( 'wpTextbox1' );
76
77
		} else {
78 1
			$rev = $this->getRevision( $title );
79 1
			if ( !$rev ) {
80 1
				$this->getMessageLog()->addWarning( wfMessage( 'lingo-noterminologypage', $page )->inContentLanguage()->text() );
81 1
				return;
82
			}
83
84
			$content = ContentHandler::getContentText( $rev->getContent() );
85
86
		}
87
88
		$parser = new Parser;
89
		// expand templates and variables in the text, producing valid, static
90
		// wikitext have to use a new anonymous user to avoid any leakage as
91
		// Lingo is caching only one user-independent glossary
92
		$content = $parser->preprocess( $content, $title, new ParserOptions( new User() ) );
93
94
		$this->mArticleLines = array_reverse( explode( "\n", $content ) );
95
	}
96
97
	/**
98
	 * @param $wgexLingoPage
99
	 * @return string
100
	 */
101 1
	private static function getLingoPage() {
102 1
		global $wgexLingoPage;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
103
104 1
		return $wgexLingoPage ? $wgexLingoPage : wfMessage( 'lingo-terminologypagename' )->inContentLanguage()->text();
105
	}
106
107
	/**
108
	 * This function returns the next element. The element is an array of four
109
	 * strings: Term, Definition, Link, Source. For the Lingo\BasicBackend Link
110
	 * and Source are set to null. If there is no next element the function
111
	 * returns null.
112
	 *
113
	 * @return Array | null
114
	 */
115
	public function next() {
116
117
		static $term = null;
118
		static $definitions = array();
119
		static $ret = array();
120
121
		// find next valid line (yes, the assignation is intended)
122
		while ( ( count( $ret ) == 0 ) && ( $entry = each( $this->mArticleLines ) ) ) {
123
124
			if ( empty( $entry[ 1 ] ) || ( $entry[ 1 ][ 0 ] !== ';' && $entry[ 1 ][ 0 ] !== ':' ) ) {
125
				continue;
126
			}
127
128
			$chunks = explode( ':', $entry[ 1 ], 2 );
129
130
			// found a new definition?
131
			if ( count( $chunks ) == 2 ) {
132
133
				// wipe the data if its a totaly new term definition
134
				if ( !empty( $term ) && count( $definitions ) > 0 ) {
135
					$definitions = array();
136
					$term = null;
137
				}
138
139
				$definitions[] = trim( $chunks[ 1 ] );
140
			}
141
142
			// found a new term?
143
			if ( count( $chunks ) >= 1 && strlen( $chunks[ 0 ] ) >= 1 ) {
144
				$term = trim( substr( $chunks[ 0 ], 1 ) );
145
			}
146
147
			if ( $term !== null ) {
148
				foreach ( $definitions as $definition ) {
149
					$ret[] = array(
150
						Element::ELEMENT_TERM       => $term,
151
						Element::ELEMENT_DEFINITION => $definition,
152
						Element::ELEMENT_LINK       => null,
153
						Element::ELEMENT_SOURCE     => null
154
					);
155
				}
156
			}
157
		}
158
159
		return array_pop( $ret );
160
	}
161
162
	/**
163
	 * Returns revision of the terms page.
164
	 *
165
	 * @param Title $title
166
	 * @return Revision
167
	 */
168
	public function getRevision( $title ) {
169
		global $wgexLingoEnableApprovedRevs;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
170
171
		if ( $wgexLingoEnableApprovedRevs ) {
172
173
			if ( defined( 'APPROVED_REVS_VERSION' ) ) {
174
				$rev_id = ApprovedRevs::getApprovedRevID( $title );
175
				return Revision::newFromId( $rev_id );
176
			} else {
177
				wfDebug( 'Support for ApprovedRevs is enabled in Lingo. But ApprovedRevs was not found.\n' );
178
			}
179
		}
180
181
		return Revision::newFromTitle( $title );
182
	}
183
184
	/**
185
	 * Initiates the purging of the cache when the Terminology page was saved or purged.
186
	 *
187
	 * @param Page $wikipage
188
	 * @return Bool
189
	 */
190
	public static function purgeCache( &$wikipage ) {
191
192
		$page = self::getLingoPage();
193
194
		if ( !is_null( $wikipage ) && ( $wikipage->getTitle()->getText() === $page ) ) {
195
196
			LingoParser::purgeCache();
197
		}
198
199
		return true;
200
	}
201
202
	/**
203
	 * The basic backend is cache-enabled so this function returns true.
204
	 *
205
	 * Actual caching is done by the parser, the backend just calls
206
	 * Lingo\LingoParser::purgeCache when necessary.
207
	 *
208
	 * @return boolean
209
	 */
210
	public function useCache() {
211
		return true;
212
	}
213
}
214