LinkTitles extension for MediaWiki
Automatically add links to existing pages.
Target.php
1 <?php
24 namespace LinkTitles;
25 
29 class Target {
34  private $title;
35 
41  private $content;
42 
48  public $wordStart;
49 
55  public $wordEnd;
56 
61  private $config;
62 
63  private $caseSensitiveLinkValueRegex;
64 
65  private $nsText;
66 
75  public function __construct( $namespace, $title, Config &$config ) {
76  $this->title = \Title::makeTitleSafe( $namespace, $title );
77  $this->titleValue = $this->title->getTitleValue();
78  $this->config = $config;
79 
80  // Use unicode character properties rather than \b escape sequences
81  // to detect whole words containing non-ASCII characters as well.
82  // Note that this requires a PCRE library that was compiled with
83  // --enable-unicode-properties
84  ( $config->wordStartOnly ) ? $this->wordStart = '(?<!\pL|\pN)' : $this->wordStart = '';
85  ( $config->wordEndOnly ) ? $this->wordEnd = '(?!\pL|\pN)' : $this->wordEnd = '';
86  }
87 
92  public function getTitleText() {
93  return $this->title->getText();
94  }
95 
96  public function getPrefixedTitleText() {
97  return $this->title->getPrefixedText();
98  }
99 
106  public function getNsText() {
107  if ( $this->nsText === null ) {
108  $this->nsText = $this->title->getNsText();
109  }
110  return $this->nsText;
111  }
112 
118  public function getNsPrefix() {
119  return $this->getNsText() ? $this->getNsText() . ':' : '';
120  }
121 
127  public function getRegexSafeTitle() {
128  return preg_quote( $this->title->getText(), '/' );
129  }
130 
135  public function getCaseSensitiveRegex() {
136  return $this->buildRegex( $this->getCaseSensitiveLinkValueRegex() );
137  }
138 
144  public function getCaseInsensitiveRegex() {
145  return $this->buildRegex( $this->getRegexSafeTitle() ) . 'i';
146  }
147 
154  private function buildRegex( $searchTerm ) {
155  return '/(?<![\:\.\@\/\?\&])' . $this->wordStart . $searchTerm . $this->wordEnd . '/S';
156  }
157 
167  public function getCaseSensitiveLinkValueRegex() {
168  if ( $this->caseSensitiveLinkValueRegex === null ) {
169  $regexSafeTitle = $this->getRegexSafeTitle();
170  if ( $this->config->capitalLinks && preg_match( '/[a-zA-Z]/', $regexSafeTitle[0] ) ) {
171  $this->caseSensitiveLinkValueRegex = '((?i)' . $regexSafeTitle[0] . '(?-i)' . substr($regexSafeTitle, 1) . ')';
172  } else {
173  $this->caseSensitiveLinkValueRegex = '(' . $regexSafeTitle . ')';
174  }
175  }
176  return $this->caseSensitiveLinkValueRegex;
177  }
178 
185  public function getContent() {
186  if ( $this->content === null ) {
187  $this->content = \WikiPage::factory( $this->title )->getContent();
188  };
189  return $this->content;
190  }
191 
202  public function mayLinkTo( Source $source ) {
203  // If checking for redirects is enabled and the target page does
204  // indeed redirect to the current page, return the page title as-is
205  // (unlinked).
206  if ( $this->config->checkRedirect && $this->redirectsTo( $source ) ) {
207  return false;
208  };
209  // If the magic word __NOAUTOLINKTARGET__ is enabled and the target
210  // page does indeed contain this magic word, return the page title
211  // as-is (unlinked).
212  if ( $this->config->enableNoTargetMagicWord ) {
213  if ( $this->getContent()->matchMagicWord( \MagicWord::get('MAG_LINKTITLES_NOTARGET') ) ) {
214  return false;
215  }
216  };
217  return true;
218  }
219 
225  public function isSameTitle( Source $source) {
226  return $this->title->equals( $source->getTitle() );
227  }
228 
234  public function redirectsTo( $source ) {
235  if ( $this->getContent() ) {
236  $redirectTitle = $this->getContent()->getUltimateRedirectTarget();
237  return $redirectTitle && $redirectTitle->equals( $source->getTitle() );
238  }
239  }
240 }
__construct($namespace, $title, Config &$config)
Constructs a new Target object.
Definition: Target.php:75
isSameTitle(Source $source)
Determines if the Target&#39;s title is the same as another title.
Definition: Target.php:225
getNsText()
Gets the string representation of the target&#39;s namespace.
Definition: Target.php:106
redirectsTo($source)
Checks whether this target redirects to the source.
Definition: Target.php:234
getNsPrefix()
Gets the namespace prefix.
Definition: Target.php:118
$content
Caches the target page content as a object.
Definition: Target.php:41
mayLinkTo(Source $source)
Examines the current target page.
Definition: Target.php:202
getTitle()
Gets the title.
Definition: Source.php:160
getTitleText()
Gets the string representation of the target title.
Definition: Target.php:92
getCaseSensitiveRegex()
Builds a regular expression of the title.
Definition: Target.php:135
getCaseSensitiveLinkValueRegex()
Gets the (cached) regex for the link value.
Definition: Target.php:167
$title
A Title object for the target page currently being examined.
Definition: Target.php:34
getContent()
Returns the of the target page.
Definition: Target.php:185
$wordEnd
Regex that matches the end of a word; this expression depends on the setting of LinkTitles->wordEndOn...
Definition: Target.php:55
Represents a page that is a potential link target.
Definition: Target.php:29
$wordStart
Regex that matches the start of a word; this expression depends on the setting of LinkTitles->wordSta...
Definition: Target.php:48
$config
LinkTitles configuration.
Definition: Target.php:61
getRegexSafeTitle()
Gets the title string with certain characters escaped that may interfere with regular expressions...
Definition: Target.php:127
Holds LinkTitles configuration.
Definition: Config.php:37
getCaseInsensitiveRegex()
Builds a regular expression pattern for the title in a case-insensitive way.
Definition: Target.php:144
The LinkTitles class holds configuration for the LinkTitles extension.
Definition: Config.php:23
buildRegex($searchTerm)
Builds the basic regex that is used to match target page titles in a source text. ...
Definition: Target.php:154
Represents a page that is a potential link target.
Definition: Source.php:29