SpecialPageData.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Special page to act as an endpoint for accessing raw page data.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * Special page to act as an endpoint for accessing raw page data.
  24. * The web server should generally be configured to make this accessible via a canonical URL/URI,
  25. * such as <http://my.domain.org/data/main/Foo>.
  26. *
  27. * @ingroup SpecialPage
  28. */
  29. class SpecialPageData extends SpecialPage {
  30. /**
  31. * @var PageDataRequestHandler|null
  32. */
  33. private $requestHandler = null;
  34. public function __construct() {
  35. parent::__construct( 'PageData' );
  36. }
  37. /**
  38. * Sets the request handler to be used by the special page.
  39. * May be used when a particular instance of PageDataRequestHandler is already
  40. * known, e.g. during testing.
  41. *
  42. * If no request handler is set using this method, a default handler is created
  43. * on demand by initDependencies().
  44. *
  45. * @param PageDataRequestHandler $requestHandler
  46. */
  47. public function setRequestHandler( PageDataRequestHandler $requestHandler ) {
  48. $this->requestHandler = $requestHandler;
  49. }
  50. /**
  51. * Initialize any un-initialized members from global context.
  52. * In particular, this initializes $this->requestHandler
  53. */
  54. protected function initDependencies() {
  55. if ( $this->requestHandler === null ) {
  56. $this->requestHandler = $this->newDefaultRequestHandler();
  57. }
  58. }
  59. /**
  60. * Creates a PageDataRequestHandler based on global defaults.
  61. *
  62. * @return PageDataRequestHandler
  63. */
  64. private function newDefaultRequestHandler() {
  65. return new PageDataRequestHandler();
  66. }
  67. /**
  68. * @see SpecialWikibasePage::execute
  69. *
  70. * @param string|null $subPage
  71. *
  72. * @throws HttpError
  73. */
  74. public function execute( $subPage ) {
  75. $this->initDependencies();
  76. // If there is no title, show an HTML form
  77. // TODO: Don't do this if HTML is not acceptable according to HTTP headers.
  78. if ( !$this->requestHandler->canHandleRequest( $subPage, $this->getRequest() ) ) {
  79. $this->showForm();
  80. return;
  81. }
  82. $this->requestHandler->handleRequest( $subPage, $this->getRequest(), $this->getOutput() );
  83. }
  84. /**
  85. * Shows an informative page to the user; Called when there is no page to output.
  86. */
  87. public function showForm() {
  88. $this->getOutput()->showErrorPage( 'pagedata-title', 'pagedata-text' );
  89. }
  90. public function isListed() {
  91. // Do not list this page in Special:SpecialPages
  92. return false;
  93. }
  94. }