merged master
[lhc/web/wiklou.git] / includes / parser / CoreLinkFunctions.php
1 <?php
2 /**
3 * Link functions provided by MediaWiki core; experimental
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Parser
22 */
23
24 /**
25 * Various core link functions, registered in Parser::firstCallInit()
26 * @ingroup Parser
27 */
28 class CoreLinkFunctions {
29 /**
30 * @param $parser Parser_LinkHooks
31 * @return bool
32 */
33 static function register( $parser ) {
34 $parser->setLinkHook( NS_CATEGORY, array( __CLASS__, 'categoryLinkHook' ) );
35 return true;
36 }
37
38 /**
39 * @param $parser Parser
40 * @param $holders LinkHolderArray
41 * @param $markers LinkMarkerReplacer
42 * @param Title $title
43 * @param $titleText
44 * @param null $displayText
45 * @param bool $leadingColon
46 * @return bool
47 */
48 static function defaultLinkHook( $parser, $holders, $markers,
49 Title $title, $titleText, &$displayText = null, &$leadingColon = false ) {
50 if( isset($displayText) && $markers->findMarker( $displayText ) ) {
51 # There are links inside of the displayText
52 # For backwards compatibility the deepest links are dominant so this
53 # link should not be handled
54 $displayText = $markers->expand($displayText);
55 # Return false so that this link is reverted back to WikiText
56 return false;
57 }
58 return $holders->makeHolder( $title, isset($displayText) ? $displayText : $titleText, array(), '', '' );
59 }
60
61 /**
62 * @param $parser Parser
63 * @param $holders LinkHolderArray
64 * @param $markers LinkMarkerReplacer
65 * @param Title $title
66 * @param $titleText
67 * @param null $sortText
68 * @param bool $leadingColon
69 * @return bool|string
70 */
71 static function categoryLinkHook( $parser, $holders, $markers,
72 Title $title, $titleText, &$sortText = null, &$leadingColon = false ) {
73 global $wgContLang;
74 # When a category link starts with a : treat it as a normal link
75 if( $leadingColon ) return true;
76 if( isset($sortText) && $markers->findMarker( $sortText ) ) {
77 # There are links inside of the sortText
78 # For backwards compatibility the deepest links are dominant so this
79 # link should not be handled
80 $sortText = $markers->expand($sortText);
81 # Return false so that this link is reverted back to WikiText
82 return false;
83 }
84 if( !isset($sortText) ) $sortText = $parser->getDefaultSort();
85 $sortText = Sanitizer::decodeCharReferences( $sortText );
86 $sortText = str_replace( "\n", '', $sortText );
87 $sortText = $wgContLang->convertCategoryKey( $sortText );
88 $parser->mOutput->addCategory( $title->getDBkey(), $sortText );
89 return '';
90 }
91
92 }