LinkRendererFactory.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. * @author Kunal Mehta <legoktm@member.fsf.org>
  20. */
  21. namespace MediaWiki\Linker;
  22. use LinkCache;
  23. use NamespaceInfo;
  24. use TitleFormatter;
  25. use User;
  26. /**
  27. * Factory to create LinkRender objects
  28. * @since 1.28
  29. */
  30. class LinkRendererFactory {
  31. /**
  32. * @var TitleFormatter
  33. */
  34. private $titleFormatter;
  35. /**
  36. * @var LinkCache
  37. */
  38. private $linkCache;
  39. /**
  40. * @var NamespaceInfo
  41. */
  42. private $nsInfo;
  43. /**
  44. * @param TitleFormatter $titleFormatter
  45. * @param LinkCache $linkCache
  46. * @param NamespaceInfo $nsInfo
  47. */
  48. public function __construct(
  49. TitleFormatter $titleFormatter, LinkCache $linkCache, NamespaceInfo $nsInfo
  50. ) {
  51. $this->titleFormatter = $titleFormatter;
  52. $this->linkCache = $linkCache;
  53. $this->nsInfo = $nsInfo;
  54. }
  55. /**
  56. * @return LinkRenderer
  57. */
  58. public function create() {
  59. return new LinkRenderer( $this->titleFormatter, $this->linkCache, $this->nsInfo );
  60. }
  61. /**
  62. * @param User $user
  63. * @return LinkRenderer
  64. */
  65. public function createForUser( User $user ) {
  66. $linkRenderer = $this->create();
  67. $linkRenderer->setStubThreshold( $user->getStubThreshold() );
  68. return $linkRenderer;
  69. }
  70. /**
  71. * @param array $options
  72. * @return LinkRenderer
  73. */
  74. public function createFromLegacyOptions( array $options ) {
  75. $linkRenderer = $this->create();
  76. if ( in_array( 'forcearticlepath', $options, true ) ) {
  77. $linkRenderer->setForceArticlePath( true );
  78. }
  79. if ( in_array( 'http', $options, true ) ) {
  80. $linkRenderer->setExpandURLs( PROTO_HTTP );
  81. } elseif ( in_array( 'https', $options, true ) ) {
  82. $linkRenderer->setExpandURLs( PROTO_HTTPS );
  83. }
  84. if ( isset( $options['stubThreshold'] ) ) {
  85. $linkRenderer->setStubThreshold(
  86. $options['stubThreshold']
  87. );
  88. }
  89. return $linkRenderer;
  90. }
  91. }