summaryrefslogtreecommitdiff
path: root/includes/parser/CoreLinkFunctions.php
blob: 1cabf766aabeaa32662ddc9a88d32261f936d564 (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
<?php
/**
 * Link functions provided by MediaWiki core; experimental
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Parser
 */

/**
 * Various core link functions, registered in Parser::firstCallInit()
 * @ingroup Parser
 */
class CoreLinkFunctions {
	/**
	 * @param $parser Parser_LinkHooks
	 * @return bool
	 */
	static function register( $parser ) {
		$parser->setLinkHook( NS_CATEGORY, array( __CLASS__, 'categoryLinkHook' ) );
		return true;
	}

	/**
	 * @param $parser Parser
	 * @param $holders LinkHolderArray
	 * @param $markers LinkMarkerReplacer
	 * @param Title $title
	 * @param $titleText
	 * @param null $displayText
	 * @param bool $leadingColon
	 * @return bool
	 */
	static function defaultLinkHook( $parser, $holders, $markers,
			Title $title, $titleText, &$displayText = null, &$leadingColon = false ) {
		if( isset( $displayText ) && $markers->findMarker( $displayText ) ) {
			# There are links inside of the displayText
			# For backwards compatibility the deepest links are dominant so this
			# link should not be handled
			$displayText = $markers->expand( $displayText );
			# Return false so that this link is reverted back to WikiText
			return false;
		}
		return $holders->makeHolder( $title, isset( $displayText ) ? $displayText : $titleText, array(), '', '' );
	}

	/**
	 * @param  $parser Parser
	 * @param  $holders LinkHolderArray
	 * @param  $markers LinkMarkerReplacer
	 * @param Title $title
	 * @param  $titleText
	 * @param null $sortText
	 * @param bool $leadingColon
	 * @return bool|string
	 */
	static function categoryLinkHook( $parser, $holders, $markers,
			Title $title, $titleText, &$sortText = null, &$leadingColon = false ) {
		global $wgContLang;
		# When a category link starts with a : treat it as a normal link
		if( $leadingColon ) return true;
		if( isset( $sortText ) && $markers->findMarker( $sortText ) ) {
			# There are links inside of the sortText
			# For backwards compatibility the deepest links are dominant so this
			# link should not be handled
			$sortText = $markers->expand( $sortText );
			# Return false so that this link is reverted back to WikiText
			return false;
		}
		if( !isset( $sortText ) ) $sortText = $parser->getDefaultSort();
		$sortText = Sanitizer::decodeCharReferences( $sortText );
		$sortText = str_replace( "\n", '', $sortText );
		$sortText = $wgContLang->convertCategoryKey( $sortText );
		$parser->mOutput->addCategory( $title->getDBkey(), $sortText );
		return '';
	}

}