Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 40 |
|
0.00% |
0 / 13 |
CRAP | |
0.00% |
0 / 1 |
| Anchor | |
0.00% |
0 / 40 |
|
0.00% |
0 / 13 |
342 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| serializeForApiResult | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLinkURL | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLocalURL | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFullURL | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCanonicalURL | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| toHtml | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| toArray | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| resolveTitle | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| buildMessage | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| setMessage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setTitleMessage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTitleMessage | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Flow\Model; |
| 4 | |
| 5 | use MediaWiki\Api\ApiSerializable; |
| 6 | use MediaWiki\Html\Html; |
| 7 | use MediaWiki\Language\RawMessage; |
| 8 | use MediaWiki\Message\Message; |
| 9 | use MediaWiki\Title\Title; |
| 10 | |
| 11 | /** |
| 12 | * Represents a mutable anchor as a Message instance along with |
| 13 | * a title, query parameters, and a fragment. |
| 14 | */ |
| 15 | class Anchor implements ApiSerializable { |
| 16 | /** |
| 17 | * Message used for the text of the anchor |
| 18 | * |
| 19 | * @var Message |
| 20 | */ |
| 21 | protected $message; |
| 22 | |
| 23 | /** |
| 24 | * Message used for the HTML title attribute of the anchor |
| 25 | * |
| 26 | * @var Message |
| 27 | */ |
| 28 | protected $titleMessage; |
| 29 | |
| 30 | /** |
| 31 | * Page title the anchor points to (not to be confused with title attribute) |
| 32 | * |
| 33 | * @var Title |
| 34 | */ |
| 35 | public $title; |
| 36 | |
| 37 | /** |
| 38 | * @var array |
| 39 | */ |
| 40 | public $query = []; |
| 41 | |
| 42 | /** |
| 43 | * @var string |
| 44 | */ |
| 45 | public $fragment; |
| 46 | |
| 47 | /** |
| 48 | * @param Message|string $message Text content of the anchor |
| 49 | * @param Title $title Page the anchor points to |
| 50 | * @param array $query Query parameters for the anchor |
| 51 | * @param string|null $fragment URL fragment of the anchor |
| 52 | * @param Message|string|null $htmlTitleMessage Title text of anchor |
| 53 | */ |
| 54 | public function __construct( $message, Title $title, array $query = [], $fragment = null, $htmlTitleMessage = null ) { |
| 55 | $this->title = $title; |
| 56 | $this->query = $query; |
| 57 | $this->fragment = $fragment; |
| 58 | |
| 59 | $this->setTitleMessage( $htmlTitleMessage ); |
| 60 | $this->setMessage( $message ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @return mixed |
| 65 | */ |
| 66 | public function serializeForApiResult() { |
| 67 | return $this->toArray(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @return string |
| 72 | */ |
| 73 | public function getLinkURL() { |
| 74 | return $this->resolveTitle()->getLinkURL( $this->query ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @return string |
| 79 | */ |
| 80 | public function getLocalURL() { |
| 81 | return $this->resolveTitle()->getLocalURL( $this->query ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @return string |
| 86 | */ |
| 87 | public function getFullURL() { |
| 88 | return $this->resolveTitle()->getFullURL( $this->query ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @return string |
| 93 | */ |
| 94 | public function getCanonicalURL() { |
| 95 | return $this->resolveTitle()->getCanonicalURL( $this->query ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @param string|null $content Optional |
| 100 | * @return string HTML |
| 101 | */ |
| 102 | public function toHtml( $content = null ) { |
| 103 | $text = $this->message->text(); |
| 104 | $titleText = $this->getTitleMessage()->text(); |
| 105 | |
| 106 | // Should we instead use Linker? |
| 107 | return Html::element( |
| 108 | 'a', |
| 109 | [ |
| 110 | 'href' => $this->getLinkURL(), |
| 111 | 'title' => $titleText, |
| 112 | ], |
| 113 | $content === null ? $text : $content |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | public function toArray() { |
| 118 | return [ |
| 119 | 'url' => $this->getLinkURL(), |
| 120 | 'title' => $this->getTitleMessage()->text(), // Title text |
| 121 | 'text' => $this->message->text(), // Main text of link |
| 122 | ]; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @return Title |
| 127 | */ |
| 128 | public function resolveTitle() { |
| 129 | $title = $this->title; |
| 130 | if ( $this->fragment !== null ) { |
| 131 | $title = clone $title; |
| 132 | $title->setFragment( $this->fragment ); |
| 133 | } |
| 134 | |
| 135 | return $title; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Canonicalizes and returns a message, or null if null was provided |
| 140 | * |
| 141 | * @param Message|string|null $rawMessage Message object, or text content, or null |
| 142 | * @return Message|null |
| 143 | */ |
| 144 | protected function buildMessage( $rawMessage ) { |
| 145 | if ( $rawMessage instanceof Message || $rawMessage === null ) { |
| 146 | return $rawMessage; |
| 147 | } else { |
| 148 | // wrap non-messages into a message class |
| 149 | $message = new RawMessage( '$1' ); |
| 150 | $message->plaintextParams( $rawMessage ); |
| 151 | return $message; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Sets the text of the anchor. If title message is currently |
| 157 | * null, it will also set that. |
| 158 | * |
| 159 | * @param Message|string $message Text content of the anchor, |
| 160 | * as Message or text content. |
| 161 | */ |
| 162 | public function setMessage( $message ) { |
| 163 | $this->message = $this->buildMessage( $message ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Sets the title attribute of the anchor |
| 168 | * |
| 169 | * @param Message|string $message Text for title attribute of anchor, |
| 170 | * as Message or text content. |
| 171 | */ |
| 172 | public function setTitleMessage( $message ) { |
| 173 | $this->titleMessage = $this->buildMessage( $message ); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Returns the effective title message. Takes into account defaulting |
| 178 | * to $this->message if there is none. |
| 179 | * |
| 180 | * @return Message |
| 181 | */ |
| 182 | protected function getTitleMessage() { |
| 183 | if ( $this->titleMessage !== null ) { |
| 184 | return $this->titleMessage; |
| 185 | } else { |
| 186 | return $this->message; |
| 187 | } |
| 188 | } |
| 189 | } |