Debian Codegraph @

vendor/wikimedia/parsoid/src/Core/LinkTargetTrait.php

raw
1 <?php
2 declare( strict_types = 1 );
3
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 * @author Addshore
22 */
23 namespace Wikimedia\Parsoid\Core;
24
25 /**
26 * Useful helpers for LinkTarget implementations.
27 *
28 * @see LinkTarget
29 */
30 trait LinkTargetTrait {
31
32 /**
33 * Convenience function to check if the target is in a given namespace.
34 *
35 * @param int $ns
36 * @return bool
37 */
38 public function inNamespace( int $ns ): bool {
39 '@phan-var LinkTarget $this';
40 // Core may someday switch to an enumerated type, but this is
41 // safe for now.
42 return $this->getNamespace() === $ns;
43 }
44
45 /**
46 * Whether the link target has a fragment.
47 *
48 * @return bool
49 */
50 public function hasFragment(): bool {
51 '@phan-var LinkTarget $this';
52 return $this->getFragment() !== '';
53 }
54
55 /**
56 * Get the main part of the link target, in text form.
57 *
58 * The main part is the link target without namespace prefix or hash fragment.
59 * The text form is used for display purposes.
60 *
61 * This is computed from the DB key by replacing any underscores with spaces.
62 *
63 * @note To get a title string that includes the namespace and/or fragment,
64 * use a TitleFormatter.
65 *
66 * @return string
67 */
68 public function getText(): string {
69 '@phan-var LinkTarget $this';
70 return strtr( $this->getDBKey(), '_', ' ' );
71 }
72
73 /**
74 * Whether this LinkTarget has an interwiki component.
75 *
76 * @return bool
77 */
78 public function isExternal(): bool {
79 '@phan-var LinkTarget $this';
80 return $this->getInterwiki() !== '';
81 }
82
83 /**
84 * Check whether the given LinkTarget refers to the same target as this LinkTarget.
85 *
86 * Two link targets are considered the same if they have the same interwiki prefix,
87 * are in the same namespace, have the same main part, and the same fragment.
88 *
89 * @param LinkTarget $other
90 * @return bool
91 */
92 public function isSameLinkAs( LinkTarget $other ): bool {
93 '@phan-var LinkTarget $this';
94 // NOTE: keep in sync with Title::isSameLinkAs()!
95 // NOTE: keep in sync with TitleValue::isSameLinkAs()!
96 // NOTE: === is needed for number-like titles
97 return ( $other->getInterwiki() === $this->getInterwiki() )
98 && ( $other->getDBkey() === $this->getDBkey() )
99 && ( $other->getNamespace() === $this->getNamespace() )
100 && ( $other->getFragment() === $this->getFragment() );
101 }
102
103 /**
104 * Return an informative human-readable representation of the link target
105 * namespace, for use in logging and debugging.
106 *
107 * @return string
108 */
109 public function getNamespaceName(): string {
110 '@phan-var LinkTarget $this';
111 if ( $this->getNamespace() === 0 ) {
112 return ''; // 0 is NS_MAIN
113 }
114 // A nicer version of this would convert the namespace to a
115 // human-readable string, but that's outside the bounds of the
116 // limited LinkTarget interface. As a fallback, just use a
117 // numeric namespace. Implementations can override this.
118 return '<' . strval( $this->getNamespace() ) . '>';
119 }
120
121 /**
122 * Return an informative human-readable representation of the link target,
123 * for use in logging and debugging.
124 *
125 * @return string
126 */
127 public function __toString(): string {
128 '@phan-var LinkTarget $this';
129 $result = '';
130 if ( $this->isExternal() ) {
131 $result .= $this->getInterwiki() . ':';
132 }
133 if ( $this->getNamespace() !== 0 ) { // 0 is NS_MAIN
134 '@phan-var LinkTargetTrait $this';
135 $result .= $this->getNamespaceName() . ':';
136 }
137 $result .= $this->getText();
138 if ( $this->hasFragment() ) {
139 $result .= '#' . $this->getFragment();
140 }
141 return $result;
142 }
143
144 }