RefreshLinksJob.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Background job to update links for a given title.
  4. *
  5. * @ingroup JobQueue
  6. */
  7. class RefreshLinksJob extends Job {
  8. function __construct( $title, $params = '', $id = 0 ) {
  9. parent::__construct( 'refreshLinks', $title, $params, $id );
  10. }
  11. /**
  12. * Run a refreshLinks job
  13. * @return boolean success
  14. */
  15. function run() {
  16. global $wgParser;
  17. wfProfileIn( __METHOD__ );
  18. $linkCache = LinkCache::singleton();
  19. $linkCache->clear();
  20. if ( is_null( $this->title ) ) {
  21. $this->error = "refreshLinks: Invalid title";
  22. wfProfileOut( __METHOD__ );
  23. return false;
  24. }
  25. $revision = Revision::newFromTitle( $this->title );
  26. if ( !$revision ) {
  27. $this->error = 'refreshLinks: Article not found "' . $this->title->getPrefixedDBkey() . '"';
  28. wfProfileOut( __METHOD__ );
  29. return false;
  30. }
  31. wfProfileIn( __METHOD__.'-parse' );
  32. $options = new ParserOptions;
  33. $parserOutput = $wgParser->parse( $revision->getText(), $this->title, $options, true, true, $revision->getId() );
  34. wfProfileOut( __METHOD__.'-parse' );
  35. wfProfileIn( __METHOD__.'-update' );
  36. $update = new LinksUpdate( $this->title, $parserOutput, false );
  37. $update->doUpdate();
  38. wfProfileOut( __METHOD__.'-update' );
  39. wfProfileOut( __METHOD__ );
  40. return true;
  41. }
  42. }
  43. /**
  44. * Background job to update links for a given title.
  45. * Newer version for high use templates.
  46. *
  47. * @ingroup JobQueue
  48. */
  49. class RefreshLinksJob2 extends Job {
  50. function __construct( $title, $params, $id = 0 ) {
  51. parent::__construct( 'refreshLinks2', $title, $params, $id );
  52. }
  53. /**
  54. * Run a refreshLinks2 job
  55. * @return boolean success
  56. */
  57. function run() {
  58. global $wgParser;
  59. wfProfileIn( __METHOD__ );
  60. $linkCache = LinkCache::singleton();
  61. $linkCache->clear();
  62. if( is_null( $this->title ) ) {
  63. $this->error = "refreshLinks2: Invalid title";
  64. wfProfileOut( __METHOD__ );
  65. return false;
  66. }
  67. if( !isset($this->params['start']) || !isset($this->params['end']) ) {
  68. $this->error = "refreshLinks2: Invalid params";
  69. wfProfileOut( __METHOD__ );
  70. return false;
  71. }
  72. $titles = $this->title->getBacklinkCache()->getLinks(
  73. 'templatelinks', $this->params['start'], $this->params['end']);
  74. # Not suitable for page load triggered job running!
  75. # Gracefully switch to refreshLinks jobs if this happens.
  76. if( php_sapi_name() != 'cli' ) {
  77. $jobs = array();
  78. foreach ( $titles as $title ) {
  79. $jobs[] = new RefreshLinksJob( $title, '' );
  80. }
  81. Job::batchInsert( $jobs );
  82. return true;
  83. }
  84. # Re-parse each page that transcludes this page and update their tracking links...
  85. foreach ( $titles as $title ) {
  86. $revision = Revision::newFromTitle( $title );
  87. if ( !$revision ) {
  88. $this->error = 'refreshLinks: Article not found "' . $title->getPrefixedDBkey() . '"';
  89. wfProfileOut( __METHOD__ );
  90. return false;
  91. }
  92. wfProfileIn( __METHOD__.'-parse' );
  93. $options = new ParserOptions;
  94. $parserOutput = $wgParser->parse( $revision->getText(), $title, $options, true, true, $revision->getId() );
  95. wfProfileOut( __METHOD__.'-parse' );
  96. wfProfileIn( __METHOD__.'-update' );
  97. $update = new LinksUpdate( $title, $parserOutput, false );
  98. $update->doUpdate();
  99. wfProfileOut( __METHOD__.'-update' );
  100. wfProfileOut( __METHOD__ );
  101. }
  102. return true;
  103. }
  104. }