LinkTitles extension for MediaWiki
Automatically add links to existing pages.
All Classes Namespaces Functions Variables Modules Pages
Extension.php
1 <?php
24 namespace LinkTitles;
25 
29 class Extension {
30  const URL = 'https://github.com/bovender/LinkTitles';
31 
37  public static function onPageContentSave( &$wikiPage, &$user, &$content, &$summary,
38  $isMinor, $isWatch, $section, &$flags, &$status ) {
39  $config = new Config();
40  if ( !$config->parseOnEdit || $isMinor ) return true;
41  $source = Source::createFromPageandContent( $wikiPage, $content, $config );
42  $linker = new Linker( $config );
43  $result = $linker->linkContent( $source );
44  if ( $result ) {
45  $content = $source->setText( $result );
46  }
47  return true;
48  }
49 
50  /*
51  * Event handler for the InternalParseBeforeLinks hook.
52  *
53  * This handler is used if the parseOnRender configuration option is set.
54  */
55  public static function onInternalParseBeforeLinks( \Parser &$parser, &$text ) {
56  $config = new Config();
57  if ( !$config->parseOnRender ) return true;
58  $title = $parser->getTitle();
59  $source = Source::createFromParserAndText( $parser, $text, $config );
60  $linker = new Linker( $config );
61  $result = $linker->linkContent( $source );
62  if ( $result ) {
63  $text = $result;
64  }
65  return true;
66  }
67 
77  public static function processPage( \Title $title, \RequestContext $context ) {
78  $config = new Config();
79  $source = Source::createFromTitle( $title, $config );
80  if ( $source->hasContent() ) {
81  $linker = new Linker( $config );
82  $result = $linker->linkContent( $source );
83  if ( $result ) {
84  $content = $source->getContent()->getContentHandler()->unserializeContent( $result );
85  $source->getPage()->doEditContent(
86  $content,
87  \wfMessage( 'linktitles-bot-comment', self::URL ),
88  EDIT_MINOR | EDIT_FORCE_BOT,
89  false, // baseRevId
90  $context->getUser()
91  );
92  };
93  return true;
94  }
95  else {
96  return false;
97  }
98  }
99 
100  /*
101  * Adds the two magic words defined by this extension to the list of
102  * 'double-underscore' terms that are automatically removed before a
103  * page is displayed.
104  *
105  * @param Array $doubleUnderscoreIDs Array of magic word IDs.
106  * @return true
107  */
108  public static function onGetDoubleUnderscoreIDs( array &$doubleUnderscoreIDs ) {
109  $doubleUnderscoreIDs[] = 'MAG_LINKTITLES_NOTARGET';
110  $doubleUnderscoreIDs[] = 'MAG_LINKTITLES_NOAUTOLINKS';
111  return true;
112  }
113 
118  public static function onParserFirstCallInit( \Parser $parser ) {
119  $parser->setHook( 'noautolinks', 'LinkTitles\Extension::doNoautolinksTag' );
120  $parser->setHook( 'autolinks', 'LinkTitles\Extension::doAutolinksTag' );
121  }
122 
123  /*
124  * Removes the extra tag that this extension provides (<noautolinks>)
125  * by simply returning the text between the tags (if any).
126  * See https://www.mediawiki.org/wiki/Manual:Tag_extensions#Example
127  */
128  public static function doNoautolinksTag( $input, array $args, \Parser $parser, \PPFrame $frame ) {
129  Linker::lock();
130  $result = $parser->recursiveTagParse( $input, $frame );
131  Linker::unlock();
132  return $result;
133  }
134 
135  /*
136  * Removes the extra tag that this extension provides (<noautolinks>)
137  * by simply returning the text between the tags (if any).
138  * See https://www.mediawiki.org/wiki/Manual:Tag_extensions#How_do_I_render_wikitext_in_my_extension.3F
139  */
140  public static function doAutolinksTag( $input, array $args, \Parser $parser, \PPFrame $frame ) {
141  $config = new Config();
142  $linker = new Linker( $config );
143  $source = Source::createFromParserAndText( $parser, $input, $config );
144  Linker::unlock();
145  $result = $linker->linkContent( $source );
146  Linker::lock();
147  if ( $result ) {
148  return $parser->recursiveTagParse( $result, $frame );
149  } else {
150  return $parser->recursiveTagParse( $input, $frame );
151  }
152  }
153 }
154 
155 // vim: ts=2:sw=2:noet:comments^=\:///
Provides event handlers and entry points for the extension.
Definition: Extension.php:29
static processPage(\Title $title,\RequestContext $context)
Adds links to a single page.
Definition: Extension.php:77
Holds LinkTitles configuration.
Definition: Config.php:37
The LinkTitles class holds configuration for the LinkTitles extension.
Definition: Config.php:23
static onParserFirstCallInit(\Parser $parser)
Handles the ParserFirstCallInit hook and adds the <autolink>/</noautolink> tags.
Definition: Extension.php:118
Performs the actual linking of content to existing pages.
Definition: Linker.php:29
static onPageContentSave(&$wikiPage, &$user, &$content, &$summary, $isMinor, $isWatch, $section, &$flags, &$status)
Event handler for the PageContentSave hook.
Definition: Extension.php:37