summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Description2/Description2.class.php
blob: c3a66837108186a800b5dee3ad0c6f9db548791d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
 * Description2 – Adds meaningful description <meta> tag to MW pages and into the parser output
 *
 * @file
 * @ingroup Extensions
 * @author Daniel Friesen (http://danf.ca/mw/)
 * @copyright Copyright 2010 – Daniel Friesen
 * @license https://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 * @link https://www.mediawiki.org/wiki/Extension:Description2 Documentation
 */

class Description2 {

	/**
	 * @param Parser $parser
	 * @param string $desc
	 */
	public static function setDescription( Parser $parser, $desc ) {
		$parserOutput = $parser->getOutput();
		if ( $parserOutput->getProperty( 'description' ) !== false ) {
			return;
		}
		$parserOutput->setProperty( 'description', $desc );
	}

	/**
	 * @param Parser $parser
	 * @param string $text
	 * @return bool
	 */
	public static function onParserAfterTidy( Parser &$parser, &$text ) {
		$desc = '';

		$myText = preg_replace( '%<table\b[^>]*+>(?:(?R)|[^<]*+(?:(?!</?table\b)<[^<]*+)*+)*+</table>%i', '', $text );

		$paragraphs = array();
		if ( preg_match_all( '#<p>.*?</p>#is', $myText, $paragraphs ) ) {
			foreach ( $paragraphs[0] as $paragraph ) {
				$paragraph = trim( strip_tags( $paragraph ) );
				if ( !$paragraph ) {
					continue;
				}
				$desc = $paragraph;
				break;
			}
		}

		if ( $desc ) {
			self::setDescription( $parser, $desc );
		}

		return true;
	}

	/**
	 * @param Parser $parser
	 * @return bool
	 */
	public static function onParserFirstCallInit( Parser &$parser ) {
		global $wgEnableMetaDescriptionFunctions;
		if ( !$wgEnableMetaDescriptionFunctions ) {
			// Functions and tags are disabled
			return true;
		}
		$parser->setFunctionHook( 'description2', array( 'Description2', 'parserFunctionCallback' ), Parser::SFH_OBJECT_ARGS );
		$parser->setFunctionTagHook( 'metadesc', array( 'Description2', 'tagCallback' ), Parser::SFH_OBJECT_ARGS );
		return true;
	}

	/**
	 * @param Parser $parser
	 * @param $frame
	 * @param $args
	 * @return string
	 */
	public static function parserFunctionCallback( Parser $parser, $frame, $args ) {
		$desc = isset( $args[0] ) ? $frame->expand( $args[0] ) : '';
		self::setDescription( $parser, $desc );
		return '';
	}

	/**
	 * @param Parser $parser
	 * @param $frame
	 * @param $content
	 * @param $attributes
	 * @return string
	 */
	public static function tagCallback( Parser $parser, $frame, $content, $attributes ) {
		$desc = ( isset( $content ) ? $content : ( isset( $attributes['content'] ) ? $attributes['content'] : null ) );
		if ( isset( $desc ) ) {
			self::setDescription( $parser, $desc );
		}
		return '';
	}

	public static function onOutputPageParserOutput( OutputPage &$out, ParserOutput $parserOutput ) {
		// Export the description from the main parser output into the OutputPage
		$description = $parserOutput->getProperty( 'description' );
		if ( $description !== false ) {
			$out->addMeta( 'description', $description );
		}
	}
}