compareParserCache.php 3.2 KB

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