Completed
Pull Request — master (#77)
by mw
02:46 queued 01:34
created

addExternalHelpLinkFor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

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