LinkTitles extension for MediaWiki
Automatically add links to existing pages.
Source.php
1 <?php
24 namespace LinkTitles;
25 
29 class Source {
35  public $config;
36 
37  private $title;
38 
39  private $text;
40 
41  private $page;
42 
43  private $content;
44 
50  public static function createFromTitle( \Title $title, Config $config ) {
51  $source = new Source( $config );
52  $source->title = $title;
53  return $source;
54  }
55 
67  public static function createFromTitleAndText( \Title $title, $text, Config $config ) {
68  $source = Source::createFromTitle( $title, $config);
69  $source->text = $text;
70  return $source;
71  }
72 
84  public static function createFromPageandContent( \WikiPage $page, \Content $content, Config $config ) {
85  $source = new Source( $config );
86  $source->page = $page;
87  $source->content = $content;
88  return $source;
89  }
90 
98  public static function createFromParser( \Parser $parser, Config $config ) {
99  $source = new Source( $config );
100  $source->title = $parser->getTitle();
101  return $source;
102  }
103 
115  public static function createFromParserAndText( \Parser $parser, $text, Config $config ) {
116  $source = Source::createFromParser( $parser, $config );
117  $source->text = $text;
118  return $source;
119  }
120 
125  private function __construct( Config $config) {
126  $this->config = $config;
127  }
128 
133  public function canBeLinked() {
134  return $this->hasDesiredNamespace() && !$this->hasNoAutolinksMagicWord();
135  }
136 
142  public function hasDesiredNamespace() {
143  return in_array( $this->getTitle()->getNamespace(), $this->config->sourceNamespaces );
144  }
145 
151  public function hasNoAutolinksMagicWord() {
152  return \MagicWord::get( 'MAG_LINKTITLES_NOAUTOLINKS' )->match( $this->getText() );
153  }
154 
160  public function getTitle() {
161  if ( $this->title === null ) {
162  // Access the property directly to avoid an infinite loop.
163  if ( $this->page != null) {
164  $this->title = $this->page->getTitle();
165  } else {
166  throw new Exception( 'Unable to create Title for this Source because Page is null.' );
167  }
168  }
169  return $this->title;
170  }
171 
176  public function getNamespace() {
177  return $this->getTitle()->getNamespace();
178  }
179 
187  public function getContent() {
188  if ( $this->content === null ) {
189  $this->content = $this->getPage()->getContent();
190  }
191  return $this->content;
192  }
193 
199  public function hasContent() {
200  return $this->getContent() != null;
201  }
202 
210  public function getText() {
211  if ( $this->text === null ) {
212  $content = $this->getContent();
213  $this->text = $content->getContentHandler()->serializeContent( $content );
214  }
215  return $this->text;
216  }
217 
224  public function setText( $text ) {
225  $this->content = $this->content->getContentHandler()->unserializeContent( $text );
226  $this->text = $text;
227  return $this->content;
228  }
229 
234  public function getPage() {
235  if ( $this->page === null ) {
236  // Access the property directly to avoid an infinite loop.
237  if ( $this->title != null) {
238  $this->page = \WikiPage::factory( $this->title );
239  } else {
240  throw new Exception( 'Unable to create Page for this Source because Title is null.' );
241  }
242  }
243  return $this->page;
244  }
245 }
getContent()
Gets the Content object for the source page.
Definition: Source.php:187
__construct(Config $config)
Private constructor.
Definition: Source.php:125
canBeLinked()
Determines whether or not this page may be linked to.
Definition: Source.php:133
getTitle()
Gets the title.
Definition: Source.php:160
static createFromPageandContent(\WikiPage $page,\Content $content, Config $config)
Creates a Source object with a given WikiPage and a Content.
Definition: Source.php:84
static createFromParser(\Parser $parser, Config $config)
Creates a Source object with a given Parser.
Definition: Source.php:98
hasDesiredNamespace()
Determines whether the Source is in a desired namespace, i.e.
Definition: Source.php:142
getText()
Gets the text of the corresponding Wiki page.
Definition: Source.php:210
getPage()
Returns the source page object.
Definition: Source.php:234
static createFromTitle(\Title $title, Config $config)
Creates a Source object from a .
Definition: Source.php:50
static createFromParserAndText(\Parser $parser, $text, Config $config)
Creates a Source object with a given Parser and text.
Definition: Source.php:115
setText($text)
Unserializes text to the page&#39;s content.
Definition: Source.php:224
Holds LinkTitles configuration.
Definition: Config.php:37
static createFromTitleAndText(\Title $title, $text, Config $config)
Creates a Source object with a given Title and a text.
Definition: Source.php:67
getNamespace()
Gets the namespace of the source Title.
Definition: Source.php:176
$config
The LinKTitles configuration for this Source.
Definition: Source.php:35
The LinkTitles class holds configuration for the LinkTitles extension.
Definition: Config.php:23
hasNoAutolinksMagicWord()
Determines whether the source page contains the NOAUTOLINKS magic word.
Definition: Source.php:151
hasContent()
Determines whether the source page has content.
Definition: Source.php:199
Represents a page that is a potential link target.
Definition: Source.php:29