SpecialFindCitableMetadata   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 81.54%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 15
eloc 58
c 2
b 0
f 0
dl 0
loc 130
ccs 53
cts 65
cp 0.8154
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getRequestParameters() 0 15 5
A addExternalHelpLinkFor() 0 7 2
A callPageBuilderFor() 0 22 2
A execute() 0 28 3
A getGroupName() 0 2 1
A __construct() 0 2 1
A newPageBuilder() 0 28 1
1
<?php
2
3
namespace SCI\Specials;
4
5
use MediaWiki\MediaWikiServices;
0 ignored issues
show
Bug introduced by
The type MediaWiki\MediaWikiServices was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SpecialPage;
0 ignored issues
show
Bug introduced by
The type SpecialPage was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use SMW\Services\ServicesFactory as ApplicationFactory;
0 ignored issues
show
Bug introduced by
The type SMW\Services\ServicesFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Onoi\HttpRequest\HttpRequestFactory;
9
use SCI\FilteredMetadata\HttpResponseParserFactory;
10
use SCI\CitationResourceMatchFinder;
11
use SCI\Specials\CitableMetadata\PageBuilder;
12
13
/**
14
 * @license GNU GPL v2+
15
 * @since   1.0
16
 *
17
 * @author mwjames
18
 */
19
class SpecialFindCitableMetadata extends SpecialPage {
20
21
	/**
22
	 * @codeCoverageIgnore
23
	 */
24
	public function __construct() {
25
		parent::__construct( 'FindCitableMetadata', 'sci-metadatasearch' );
26
	}
27
28
	/**
29
	 * @see SpecialPage::getGroupName
30
	 */
31
	protected function getGroupName() {
32
		return 'wiki';
33
	}
34
35
	/**
36
	 * @see SpecialPage::execute
37 2
	 */
38
	public function execute( $query ) {
39 2
40
		if ( !$this->userCanExecute( $this->getUser() ) ) {
41
			$this->displayRestrictionError();
42
			return;
43
		}
44 2
45 2
		$output = $this->getOutput();
46 2
		$output->setPageTitle( $this->msg( 'findmetadatabyid' )->text() );
47
		$this->setHeaders();
48 2
49
		$output->addModuleStyles( 'ext.scite.styles' );
50 2
51
		$output->addModules(
52 2
			[
53
				'ext.scite.metadata'
54
			]
55
		);
56 2
57 2
		list( $type, $id, $format ) = $this->getRequestParameters(
58
			$query
59
		);
60 2
61
		if ( $type === 'pmid' ) {
62
			$type = 'pubmed';
63
		}
64 2
65 2
		$this->callPageBuilderFor( $type, $id, $format );
66
	}
67 2
68
	private function callPageBuilderFor( $type, $id, $format ) {
69 2
70
		$pageBuilder = $this->newPageBuilder();
71 2
72
		if ( $format === 'raw' ) {
73
			$this->getOutput()->disable();
74
			header( "Content-type: text/plain; charset=utf-8" );
75
			header( 'Cache-Control: private, no-cache, must-revalidate' );
76
77
			$html = $pageBuilder->getRawResponseFor(
78
				$type, $id
79
			);
80
81
			return print $html;
82
		}
83 2
84 2
		$html = $pageBuilder->getHtmlFor(
85
			$type, $id
86
		);
87 2
88 2
		$this->getOutput()->addHTML( $html );
89 2
		$this->addExternalHelpLinkFor( 'sci-specials-citablemetadata-helplink' );
90
	}
91 2
92
	private function getRequestParameters( $query ) {
93 2
94
		$request = $this->getRequest()->getValues();
95 2
96 1
		if ( strpos( $query, '/' ) !== false ) {
97 1
			list( $type, $id ) = explode( '/', $query, 2 );
98 1
			$request['type'] = $type;
99
			$request['id'] = $id;
100
		}
101 2
102 2
		$id = isset( $request['id'] ) ? trim( $request['id'] ) : '';
103 2
		$type = isset( $request['type'] ) ? strtolower( $request['type'] ) : '';
104
		$format = isset( $request['format'] ) ? strtolower( $request['format'] ) : '';
105 2
106
		return [ $type, $id, $format ];
107
	}
108 2
109
	private function newPageBuilder() {
110 2
111 2
		$applicationFactory = ApplicationFactory::getInstance();
112
		$mwCollaboratorFactory = $applicationFactory->newMwCollaboratorFactory();
113 2
114 2
		$htmlFormRenderer = $mwCollaboratorFactory->newHtmlFormRenderer(
115 2
			$this->getContext()->getTitle(),
116
			$this->getLanguage()
117
		);
118 2
119 2
		$httpRequestFactory = new HttpRequestFactory(
120
			$applicationFactory->newCacheFactory()->newMediaWikiCompositeCache( $GLOBALS['scigMetadataResponseCacheType'] )
121
		);
122 2
123 2
		$httpRequest = $httpRequestFactory->newCachedCurlRequest();
124 2
		$httpRequest->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_TTL, $GLOBALS['scigMetadataResponseCacheLifetime'] );
125
		$httpRequest->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_PREFIX, $GLOBALS['scigCachePrefix'] . ':sci:meta:' );
126 2
127 2
		$pageBuilder = new PageBuilder(
128 2
			$htmlFormRenderer,
129 2
			$mwCollaboratorFactory->newHtmlColumnListRenderer(),
130 2
			new CitationResourceMatchFinder( $applicationFactory->getStore() ),
131
			new HttpResponseParserFactory( $httpRequest )
132
		);
133 2
134
		$pageBuilder->isReadOnly( MediaWikiServices::getInstance()->getReadOnlyMode()->isReadOnly() );
135 2
136
		return $pageBuilder;
137
	}
138
139
	/**
140
	 * FIXME MW 1.25
141 2
	 */
142
	private function addExternalHelpLinkFor( $key ) {
143 2
144
		if ( !method_exists( $this, 'addHelpLink' ) ) {
145
			return null;
146
		}
147 2
148 2
		$this->getOutput()->addHelpLink( wfMessage( $key )->escaped(), true );
0 ignored issues
show
Bug introduced by
The function wfMessage was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

148
		$this->getOutput()->addHelpLink( /** @scrutinizer ignore-call */ wfMessage( $key )->escaped(), true );
Loading history...
149
	}
150
151
}
152