Completed
Push — master ( 85d1e0...3dc773 )
by
unknown
04:22
created

BasicBackend::next()   C

Complexity

Conditions 13
Paths 20

Size

Total Lines 50
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 50
ccs 0
cts 32
cp 0
rs 5.3808
cc 13
eloc 25
nc 20
nop 0
crap 182

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Page;
32
use Parser;
33
use ParserOptions;
34
use Revision;
35
use Title;
36
use User;
37
38
/**
39
 * The Lingo\BasicBackend class.
40
 *
41
 * @ingroup Lingo
42
 */
43
class BasicBackend extends Backend {
44
45
	protected $mArticleLines = array();
46
47
	/**
48
	 * Lingo\BasicBackend constructor.
49
	 * @param MessageLog|null $messages
50
	 */
51 1
	public function __construct( MessageLog &$messages = null ) {
52
53 1
		global $wgexLingoPage, $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...
54
55 1
		$page = $wgexLingoPage ? $wgexLingoPage : wfMessage( 'lingo-terminologypagename' )->inContentLanguage()->text();
56
57 1
		parent::__construct( $messages );
58
59
		// Get Terminology page
60 1
		$title = Title::newFromText( $page );
61 1
		if ( $title->getInterwiki() ) {
62
			$this->getMessageLog()->addError( wfMessage( 'lingo-terminologypagenotlocal', $page )->inContentLanguage()->text() );
63
			return;
64
		}
65
66
		// FIXME: This is a hack special-casing the submitting of the terminology
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task "This is a hack special-casing the submitting of the terminology"
Loading history...
67
		// page itself. In this case the Revision is not up to date when we get
68
		// here, i.e. $rev->getText() would return outdated Test.
69
		// This hack takes the text directly out of the data from the web request.
70 1
		if ( $wgRequest->getVal( 'action', 'view' ) === 'submit'
71 1
			&& Title::newFromText( $wgRequest->getVal( 'title' ) )->getArticleID() === $title->getArticleID()
72 1
		) {
73
74
			$content = $wgRequest->getVal( 'wpTextbox1' );
75
76
		} else {
77 1
			$rev = $this->getRevision( $title );
78 1
			if ( !$rev ) {
79 1
				$this->getMessageLog()->addWarning( wfMessage( 'lingo-noterminologypage', $page )->inContentLanguage()->text() );
80 1
				return false;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
81
			}
82
83
			$content = $rev->getText();
84
85
		}
86
87
		$parser = new Parser;
88
		// expand templates and variables in the text, producing valid, static wikitext
89
		// have to use a new anonymous user to avoid any leakage as Lingo is caching only one user-independant glossary
90
		$content = $parser->preprocess( $content, $title, new ParserOptions( new User() ) );
91
92
		$this->mArticleLines = array_reverse( explode( "\n", $content ) );
93
	}
94
95
	/**
96
	 * This function returns the next element. The element is an array of four
97
	 * strings: Term, Definition, Link, Source. For the Lingo\BasicBackend Link
98
	 * and Source are set to null. If there is no next element the function
99
	 * returns null.
100
	 *
101
	 * @return Array | null
102
	 */
103
	public function next() {
104
105
		wfProfileIn( __METHOD__ );
106
107
		static $term = null;
108
		static $definitions = array();
109
		static $ret = array();
110
111
		// find next valid line (yes, the assignation is intended)
112
		while ( ( count( $ret ) == 0 ) && ( $entry = each( $this->mArticleLines ) ) ) {
113
114
			if ( empty( $entry[ 1 ] ) || ( $entry[ 1 ][ 0 ] !== ';' && $entry[ 1 ][ 0 ] !== ':' ) ) {
115
				continue;
116
			}
117
118
			$chunks = explode( ':', $entry[ 1 ], 2 );
119
120
			// found a new definition?
121
			if ( count( $chunks ) == 2 ) {
122
123
				// wipe the data if its a totaly new term definition
124
				if ( !empty( $term ) && count( $definitions ) > 0 ) {
125
					$definitions = array();
126
					$term = null;
127
				}
128
129
				$definitions[] = trim( $chunks[ 1 ] );
130
			}
131
132
			// found a new term?
133
			if ( count( $chunks ) >= 1 && strlen( $chunks[ 0 ] ) >= 1 ) {
134
				$term = trim( substr( $chunks[ 0 ], 1 ) );
135
			}
136
137
			if ( $term !== null ) {
138
				foreach ( $definitions as $definition ) {
139
					$ret[] = array(
140
						Element::ELEMENT_TERM       => $term,
141
						Element::ELEMENT_DEFINITION => $definition,
142
						Element::ELEMENT_LINK       => null,
143
						Element::ELEMENT_SOURCE     => null
144
					);
145
				}
146
			}
147
		}
148
149
		wfProfileOut( __METHOD__ );
150
151
		return array_pop( $ret );
152
	}
153
154
	/**
155
	 * Returns revision of the terms page.
156
	 *
157
	 * @param Title $title
158
	 * @return Revision
159
	 */
160
	public function getRevision( $title ) {
161
		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...
162
163
		if ( $wgexLingoEnableApprovedRevs ) {
164
165
			if ( defined( 'APPROVED_REVS_VERSION' ) ) {
166
				$rev_id = ApprovedRevs::getApprovedRevID( $title );
167
				return Revision::newFromId( $rev_id );
168
			} else {
169
				wfDebug( 'Support for ApprovedRevs is enabled in Lingo. But ApprovedRevs was not found.\n' );
170
			}
171
		}
172
173
		return Revision::newFromTitle( $title );
174
	}
175
176
	/**
177
	 * Initiates the purging of the cache when the Terminology page was saved or purged.
178
	 *
179
	 * @param Page $wikipage
180
	 * @return Bool
181
	 */
182
	public static function purgeCache( &$wikipage ) {
183
184
		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...
185
		$page = $wgexLingoPage ? $wgexLingoPage : wfMessage( 'lingo-terminologypagename' )->inContentLanguage()->text();
186
187
		if ( !is_null( $wikipage ) && ( $wikipage->getTitle()->getText() === $page ) ) {
188
189
			LingoParser::purgeCache();
190
		}
191
192
		return true;
193
	}
194
195
	/**
196
	 * The basic backend is cache-enabled so this function returns true.
197
	 *
198
	 * Actual caching is done by the parser, the backend just calls
199
	 * Lingo\LingoParser::purgeCache when necessary.
200
	 *
201
	 * @return boolean
202
	 */
203
	public function useCache() {
204
		return true;
205
	}
206
}
207