Completed
Push — master ( 314506...335380 )
by mw
100:54 queued 62:54
created

includes/queryprinters/EmbeddedResultPrinter.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SMW;
4
5
use SMWQueryResult;
6
use Title;
7
8
/**
9
 * Printer for embedded data.
10
 *
11
 * Embeds in the page output the contents of the pages in the query result set.
12
 * Printouts are ignored: it only matters which pages were returned by the query.
13
 * The optional "titlestyle" formatting parameter can be used to apply a format to
14
 * the headings for the page titles. If "titlestyle" is not specified, a <h1> tag is
15
 * used.
16
 *
17
 * @license GNU GPL v2+
18
 * @since 1.7
19
 *
20
 * @author Fernando Correia
21
 * @author Markus Krötzsch
22
 */
23
class EmbeddedResultPrinter extends ResultPrinter {
24
25
	protected $m_showhead;
26
	protected $m_embedformat;
27
28
	/**
29
	 * @see SMWResultPrinter::handleParameters
30
	 *
31
	 * @since 1.7
32
	 *
33
	 * @param array $params
34
	 * @param $outputmode
35
	 */
36 2
	protected function handleParameters( array $params, $outputmode ) {
37 2
		parent::handleParameters( $params, $outputmode );
38
39 2
		$this->m_showhead = !$params['embedonly'];
40 2
		$this->m_embedformat = $params['embedformat'];
41 2
	}
42
43 1
	public function getName() {
44 1
		return wfMessage( 'smw_printername_embedded' )->text();
45
	}
46
47 2
	protected function getResultText( SMWQueryResult $res, $outputMode ) {
48 2
		global $wgParser;
49
		// No page should embed itself, find out who we are:
50 2
		if ( $wgParser->getTitle() instanceof Title ) {
51 2
			$title = $wgParser->getTitle()->getPrefixedText();
52
		} else { // this is likely to be in vain -- this case is typical if we run on special pages
53
			global $wgTitle;
54
			$title = $wgTitle->getPrefixedText();
55
		}
56
57
		// print header
58 2
		$result = '';
59 2
		$footer = '';
60 2
		$embstart = '';
61 2
		$embend = '';
62 2
		$headstart = '';
63 2
		$headend = '';
64 2
		$this->hasTemplates = true;
65
66 2
		switch ( $this->m_embedformat ) {
67 2
			case 'h1': case 'h2': case 'h3': case 'h4': case 'h5': case 'h6':
68 2
				$headstart = '<' . $this->m_embedformat . '>';
69 2
				$headend = '</' . $this->m_embedformat . ">\n";
70 2
			break;
71
			case 'ul': case 'ol':
72
				$result .= '<' . $this->m_embedformat . '>';
73
				$footer = '</' . $this->m_embedformat . '>';
74
				$embstart = '<li>';
75
				$headend = "<br />\n";
76
				$embend = "</li>\n";
77
			break;
78
		}
79
80
		// Print all result rows:
81 2
		foreach ( $res->getResults() as $diWikiPage ) {
82 2
			if ( $diWikiPage instanceof DIWikiPage  ) { // ensure that we deal with title-likes
83 2
				$dvWikiPage = DataValueFactory::getInstance()->newDataValueByItem( $diWikiPage, null );
84 2
				$result .= $embstart;
85
86 2
				if ( $this->m_showhead ) {
87 2
					$result .= $headstart . $dvWikiPage->getLongWikiText( $this->mLinker ) . $headend;
88
				}
89
90 2
				if ( $dvWikiPage->getLongWikiText() != $title ) {
91 2
					if ( $diWikiPage->getNamespace() == NS_MAIN ) {
92 2
						$result .= '{{:' . $diWikiPage->getDBkey() . '}}';
93
					} else {
94 2
						$result .= '{{' . $dvWikiPage->getLongWikiText() . '}}';
95
					}
96
				} else { // block recursion
97 1
					$result .= '<b>' . $dvWikiPage->getLongWikiText() . '</b>';
98
				}
99
100 2
				$result .= $embend;
101
			}
102
		}
103
104
		// show link to more results
105 2
		if ( $this->linkFurtherResults( $res ) ) {
106
			$result .= $embstart
107
				. $this->getFurtherResultsLink( $res, $outputMode )->getText( SMW_OUTPUT_WIKI, $this->mLinker )
108
				. $embend;
109
		}
110
111 2
		$result .= $footer;
112
113 2
		return $result;
114
	}
115
116 2
	public function getParameters() {
117 2
		$params = parent::getParameters();
0 ignored issues
show
Deprecated Code introduced by
The method SMW\ResultPrinter::getParameters() has been deprecated with message: since 1.8, use getParamDefinitions instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
118
119 2
		$params[] = array(
120
			'name' => 'embedformat',
121
			'message' => 'smw-paramdesc-embedformat',
122
			'default' => 'h1',
123
			'values' => array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ol', 'ul' ),
124
		);
125
126 2
		$params[] = array(
127
			'name' => 'embedonly',
128
			'type' => 'boolean',
129
			'message' => 'smw-paramdesc-embedonly',
130
			'default' => false,
131
		);
132
133 2
		return $params;
134
	}
135
136
}
137