compareParserCache.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * @ingroup Maintenance
  20. */
  21. require_once __DIR__ . '/Maintenance.php';
  22. use MediaWiki\MediaWikiServices;
  23. /**
  24. * @ingroup Maintenance
  25. */
  26. class CompareParserCache extends Maintenance {
  27. public function __construct() {
  28. parent::__construct();
  29. $this->addDescription( 'Parse random pages and compare output to cache.' );
  30. $this->addOption( 'namespace', 'Page namespace number', true, true );
  31. $this->addOption( 'maxpages', 'Number of pages to try', true, true );
  32. }
  33. public function execute() {
  34. $pages = $this->getOption( 'maxpages' );
  35. $dbr = $this->getDB( DB_REPLICA );
  36. $totalsec = 0.0;
  37. $scanned = 0;
  38. $withcache = 0;
  39. $withdiff = 0;
  40. $parserCache = MediaWikiServices::getInstance()->getParserCache();
  41. $renderer = MediaWikiServices::getInstance()->getRevisionRenderer();
  42. while ( $pages-- > 0 ) {
  43. $row = $dbr->selectRow( 'page',
  44. // @todo Title::selectFields() or Title::getQueryInfo() or something
  45. [
  46. 'page_namespace', 'page_title', 'page_id',
  47. 'page_len', 'page_is_redirect', 'page_latest',
  48. ],
  49. [
  50. 'page_namespace' => $this->getOption( 'namespace' ),
  51. 'page_is_redirect' => 0,
  52. 'page_random >= ' . wfRandom()
  53. ],
  54. __METHOD__,
  55. [
  56. 'ORDER BY' => 'page_random',
  57. ]
  58. );
  59. if ( !$row ) {
  60. continue;
  61. }
  62. ++$scanned;
  63. $title = Title::newFromRow( $row );
  64. $page = WikiPage::factory( $title );
  65. $revision = $page->getRevision()->getRevisionRecord();
  66. $parserOptions = $page->makeParserOptions( 'canonical' );
  67. $parserOutputOld = $parserCache->get( $page, $parserOptions );
  68. if ( $parserOutputOld ) {
  69. $t1 = microtime( true );
  70. $parserOutputNew = $renderer->getRenderedRevision( $revision, $parserOptions )
  71. ->getRevisionParserOutput();
  72. $sec = microtime( true ) - $t1;
  73. $totalsec += $sec;
  74. $this->output( "Parsed '{$title->getPrefixedText()}' in $sec seconds.\n" );
  75. $this->output( "Found cache entry found for '{$title->getPrefixedText()}'..." );
  76. $oldHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputOld->getText() ) );
  77. $newHtml = trim( preg_replace( '#<!-- .+-->#Us', '', $parserOutputNew->getText() ) );
  78. $diff = wfDiff( $oldHtml, $newHtml );
  79. if ( strlen( $diff ) ) {
  80. $this->output( "differences found:\n\n$diff\n\n" );
  81. ++$withdiff;
  82. } else {
  83. $this->output( "No differences found.\n" );
  84. }
  85. ++$withcache;
  86. } else {
  87. $this->output( "No parser cache entry found for '{$title->getPrefixedText()}'.\n" );
  88. }
  89. }
  90. $ave = $totalsec ? $totalsec / $scanned : 0;
  91. $this->output( "Checked $scanned pages; $withcache had prior cache entries.\n" );
  92. $this->output( "Pages with differences found: $withdiff\n" );
  93. $this->output( "Average parse time: $ave sec\n" );
  94. }
  95. }
  96. $maintClass = CompareParserCache::class;
  97. require_once RUN_MAINTENANCE_IF_MAIN;