MediaWiki  master
ContentHandler.php
Go to the documentation of this file.
1 <?php
2 
35 use Wikimedia\Assert\Assert;
36 
56 abstract class ContentHandler {
86  public static function getContentText( Content $content = null ) {
88 
89  if ( $content === null ) {
90  return '';
91  }
92 
93  if ( $content instanceof TextContent ) {
94  return $content->getText();
95  }
96 
97  wfDebugLog( 'ContentHandler', 'Accessing ' . $content->getModel() . ' content as text!' );
98 
99  if ( $wgContentHandlerTextFallback == 'fail' ) {
100  throw new MWException(
101  "Attempt to get text from Content with model " .
102  $content->getModel()
103  );
104  }
105 
106  if ( $wgContentHandlerTextFallback == 'serialize' ) {
107  return $content->serialize();
108  }
109 
110  return null;
111  }
112 
137  public static function makeContent( $text, Title $title = null,
138  $modelId = null, $format = null ) {
139  if ( $modelId === null ) {
140  if ( $title === null ) {
141  throw new MWException( "Must provide a Title object or a content model ID." );
142  }
143 
144  $modelId = $title->getContentModel();
145  }
146 
147  return MediaWikiServices::getInstance()
148  ->getContentHandlerFactory()
149  ->getContentHandler( $modelId )
150  ->unserializeContent( $text, $format );
151  }
152 
189  public static function getDefaultModelFor( Title $title ) {
190  $slotRoleregistry = MediaWikiServices::getInstance()->getSlotRoleRegistry();
191  $mainSlotHandler = $slotRoleregistry->getRoleHandler( 'main' );
192  return $mainSlotHandler->getDefaultModel( $title );
193  }
194 
208  public static function getForTitle( Title $title ) {
209  return MediaWikiServices::getInstance()
210  ->getContentHandlerFactory()
211  ->getContentHandler( $title->getContentModel() );
212  }
213 
229  public static function getForContent( Content $content ) {
230  return MediaWikiServices::getInstance()
231  ->getContentHandlerFactory()
232  ->getContentHandler( $content->getModel() );
233  }
234 
264  public static function getForModelID( $modelId ) {
265  return MediaWikiServices::getInstance()
266  ->getContentHandlerFactory()
267  ->getContentHandler( $modelId );
268  }
269 
276  public static function cleanupHandlersCache() {
277  // No-op: no longer needed, since the instance cache is in the
278  // ContentHandlerFactory service, and services get reset between tests
279  }
280 
294  public static function getLocalizedName( $name, Language $lang = null ) {
295  // Messages: content-model-wikitext, content-model-text,
296  // content-model-javascript, content-model-css
297  $key = "content-model-$name";
298 
299  $msg = wfMessage( $key );
300  if ( $lang ) {
301  $msg->inLanguage( $lang );
302  }
303 
304  return $msg->exists() ? $msg->plain() : $name;
305  }
306 
315  public static function getContentModels() {
316  return MediaWikiServices::getInstance()->getContentHandlerFactory()->getContentModels();
317  }
318 
327  public static function getAllContentFormats() {
328  return MediaWikiServices::getInstance()->getContentHandlerFactory()->getAllContentFormats();
329  }
330 
331  // ------------------------------------------------------------------------
332 
336  protected $mModelID;
337 
342 
352  public function __construct( $modelId, $formats ) {
353  $this->mModelID = $modelId;
354  $this->mSupportedFormats = $formats;
355  }
356 
367  abstract public function serializeContent( Content $content, $format = null );
368 
379  public function exportTransform( $blob, $format = null ) {
380  return $blob;
381  }
382 
394  abstract public function unserializeContent( $blob, $format = null );
395 
407  public function importTransform( $blob, $format = null ) {
408  return $blob;
409  }
410 
419  abstract public function makeEmptyContent();
420 
438  public function makeRedirectContent( Title $destination, $text = '' ) {
439  return null;
440  }
441 
450  public function getModelID() {
451  return $this->mModelID;
452  }
453 
462  protected function checkModelID( $model_id ) {
463  if ( $model_id !== $this->mModelID ) {
464  throw new MWException( "Bad content model: " .
465  "expected {$this->mModelID} " .
466  "but got $model_id." );
467  }
468  }
469 
479  public function getSupportedFormats() {
481  }
482 
494  public function getDefaultFormat() {
495  return $this->mSupportedFormats[0];
496  }
497 
511  public function isSupportedFormat( $format ) {
512  if ( !$format ) {
513  return true; // this means "use the default"
514  }
515 
516  return in_array( $format, $this->mSupportedFormats );
517  }
518 
526  protected function checkFormat( $format ) {
527  if ( !$this->isSupportedFormat( $format ) ) {
528  throw new MWException(
529  "Format $format is not supported for content model "
530  . $this->getModelID()
531  );
532  }
533  }
534 
550  public function getActionOverrides() {
551  return [];
552  }
553 
581  public function createDifferenceEngine( IContextSource $context, $old = 0, $new = 0,
582  $rcid = 0, // FIXME: Deprecated, no longer used
583  $refreshCache = false, $unhide = false
584  ) {
585  $diffEngineClass = $this->getDiffEngineClass();
586  $differenceEngine = new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide );
587  Hooks::run( 'GetDifferenceEngine', [ $context, $old, $new, $refreshCache, $unhide,
588  &$differenceEngine ] );
589  return $differenceEngine;
590  }
591 
599  final public function getSlotDiffRenderer( IContextSource $context, array $options = [] ) {
600  $slotDiffRenderer = $this->getSlotDiffRendererWithOptions( $context, $options );
601  if ( get_class( $slotDiffRenderer ) === TextSlotDiffRenderer::class ) {
602  // To keep B/C, when SlotDiffRenderer is not overridden for a given content type
603  // but DifferenceEngine is, use that instead.
604  $differenceEngine = $this->createDifferenceEngine( $context );
605  if ( get_class( $differenceEngine ) !== DifferenceEngine::class ) {
606  // TODO turn this into a deprecation warning in a later release
607  LoggerFactory::getInstance( 'diff' )->info(
608  'Falling back to DifferenceEngineSlotDiffRenderer', [
609  'modelID' => $this->getModelID(),
610  'DifferenceEngine' => get_class( $differenceEngine ),
611  ] );
612  $slotDiffRenderer = new DifferenceEngineSlotDiffRenderer( $differenceEngine );
613  }
614  }
615  Hooks::run( 'GetSlotDiffRenderer', [ $this, &$slotDiffRenderer, $context ] );
616  return $slotDiffRenderer;
617  }
618 
626  return null;
627  }
628 
635  protected function getSlotDiffRendererWithOptions( IContextSource $context, $options = [] ) {
636  $internalRenderer = $this->getSlotDiffRendererInternal( $context );
637  // `getSlotDiffRendererInternal` has been overriden by a class using the deprecated method.
638  // Options will not work so exit early!
639  if ( $internalRenderer !== null ) {
640  return $internalRenderer;
641  }
642 
643  $contentLanguage = MediaWikiServices::getInstance()->getContentLanguage();
644  $statsdDataFactory = MediaWikiServices::getInstance()->getStatsdDataFactory();
645  $slotDiffRenderer = new TextSlotDiffRenderer();
646  $slotDiffRenderer->setStatsdDataFactory( $statsdDataFactory );
647  // XXX using the page language would be better, but it's unclear how that should be injected
648  $slotDiffRenderer->setLanguage( $contentLanguage );
649 
650  $inline = ( $options['diff-type'] ?? '' ) === 'inline';
651  $engine = 'wikidiff2';
652  $engine = DifferenceEngine::getEngine();
653 
654  if ( $engine === 'php' ) {
655  $slotDiffRenderer->setEngine( TextSlotDiffRenderer::ENGINE_PHP );
656  } elseif ( $engine === 'wikidiff2' ) {
657  if ( $inline ) {
658  $slotDiffRenderer->setEngine( TextSlotDiffRenderer::ENGINE_WIKIDIFF2_INLINE );
659  } else {
660  $slotDiffRenderer->setEngine( TextSlotDiffRenderer::ENGINE_WIKIDIFF2 );
661  }
662  } else {
663  $slotDiffRenderer->setEngine( TextSlotDiffRenderer::ENGINE_EXTERNAL, $engine );
664  }
665 
666  return $slotDiffRenderer;
667  }
668 
688  public function getPageLanguage( Title $title, Content $content = null ) {
689  global $wgLang;
690  $services = MediaWikiServices::getInstance();
691  $pageLang = $services->getContentLanguage();
692 
693  if ( $title->inNamespace( NS_MEDIAWIKI ) ) {
694  // Parse mediawiki messages with correct target language
695  list( /* $unused */, $lang ) = $services->getMessageCache()->figureMessage( $title->getText() );
696  $pageLang = $services->getLanguageFactory()->getLanguage( $lang );
697  }
698 
699  // Simplify hook handlers by only passing objects of one type, in case nothing
700  // else has unstubbed the StubUserLang object by now.
702 
703  Hooks::run( 'PageContentLanguage', [ $title, &$pageLang, $wgLang ] );
704 
705  return wfGetLangObj( $pageLang );
706  }
707 
728  public function getPageViewLanguage( Title $title, Content $content = null ) {
729  $pageLang = $this->getPageLanguage( $title, $content );
730 
731  if ( $title->getNamespace() !== NS_MEDIAWIKI ) {
732  // If the user chooses a variant, the content is actually
733  // in a language whose code is the variant code.
734  $variant = $this->getLanguageConverter( $pageLang )->getPreferredVariant();
735  if ( $pageLang->getCode() !== $variant ) {
736  $pageLang = MediaWikiServices::getInstance()->getLanguageFactory()
737  ->getLanguage( $variant );
738  }
739  }
740 
741  return $pageLang;
742  }
743 
762  public function canBeUsedOn( Title $title ) {
763  $ok = true;
764 
765  Hooks::run( 'ContentModelCanBeUsedOn', [ $this->getModelID(), $title, &$ok ] );
766 
767  return $ok;
768  }
769 
777  protected function getDiffEngineClass() {
778  return DifferenceEngine::class;
779  }
780 
795  public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
796  return false;
797  }
798 
804  private function getLanguageConverter( $language ) : ILanguageConverter {
805  return MediaWikiServices::getInstance()->getLanguageConverterFactory()
806  ->getLanguageConverter( $language );
807  }
808 
820  private function getChangeType(
821  Content $oldContent = null,
822  Content $newContent = null,
823  $flags = 0
824  ) {
825  $oldTarget = $oldContent !== null ? $oldContent->getRedirectTarget() : null;
826  $newTarget = $newContent !== null ? $newContent->getRedirectTarget() : null;
827 
828  // We check for the type of change in the given edit, and return string key accordingly
829 
830  // Blanking of a page
831  if ( $oldContent && $oldContent->getSize() > 0 &&
832  $newContent && $newContent->getSize() === 0
833  ) {
834  return 'blank';
835  }
836 
837  // Redirects
838  if ( $newTarget ) {
839  if ( !$oldTarget ) {
840  // New redirect page (by creating new page or by changing content page)
841  return 'new-redirect';
842  } elseif ( !$newTarget->equals( $oldTarget ) ||
843  $oldTarget->getFragment() !== $newTarget->getFragment()
844  ) {
845  // Redirect target changed
846  return 'changed-redirect-target';
847  }
848  } elseif ( $oldTarget ) {
849  // Changing an existing redirect into a non-redirect
850  return 'removed-redirect';
851  }
852 
853  // New page created
854  if ( $flags & EDIT_NEW && $newContent ) {
855  if ( $newContent->getSize() === 0 ) {
856  // New blank page
857  return 'newblank';
858  } else {
859  return 'newpage';
860  }
861  }
862 
863  // Removing more than 90% of the page
864  if ( $oldContent && $newContent && $oldContent->getSize() > 10 * $newContent->getSize() ) {
865  return 'replace';
866  }
867 
868  // Content model changed
869  if ( $oldContent && $newContent && $oldContent->getModel() !== $newContent->getModel() ) {
870  return 'contentmodelchange';
871  }
872 
873  return null;
874  }
875 
887  public function getAutosummary(
888  Content $oldContent = null,
889  Content $newContent = null,
890  $flags = 0
891  ) {
892  $changeType = $this->getChangeType( $oldContent, $newContent, $flags );
893 
894  // There's no applicable auto-summary for our case, so our auto-summary is empty.
895  if ( !$changeType ) {
896  return '';
897  }
898 
899  // Decide what kind of auto-summary is needed.
900  switch ( $changeType ) {
901  case 'new-redirect':
902  $newTarget = $newContent->getRedirectTarget();
903  $truncatedtext = $newContent->getTextForSummary(
904  250
905  - strlen( wfMessage( 'autoredircomment' )->inContentLanguage()->text() )
906  - strlen( $newTarget->getFullText() )
907  );
908 
909  return wfMessage( 'autoredircomment', $newTarget->getFullText() )
910  ->plaintextParams( $truncatedtext )->inContentLanguage()->text();
911  case 'changed-redirect-target':
912  $oldTarget = $oldContent->getRedirectTarget();
913  $newTarget = $newContent->getRedirectTarget();
914 
915  $truncatedtext = $newContent->getTextForSummary(
916  250
917  - strlen( wfMessage( 'autosumm-changed-redirect-target' )
918  ->inContentLanguage()->text() )
919  - strlen( $oldTarget->getFullText() )
920  - strlen( $newTarget->getFullText() )
921  );
922 
923  return wfMessage( 'autosumm-changed-redirect-target',
924  $oldTarget->getFullText(),
925  $newTarget->getFullText() )
926  ->rawParams( $truncatedtext )->inContentLanguage()->text();
927  case 'removed-redirect':
928  $oldTarget = $oldContent->getRedirectTarget();
929  $truncatedtext = $newContent->getTextForSummary(
930  250
931  - strlen( wfMessage( 'autosumm-removed-redirect' )
932  ->inContentLanguage()->text() )
933  - strlen( $oldTarget->getFullText() ) );
934 
935  return wfMessage( 'autosumm-removed-redirect', $oldTarget->getFullText() )
936  ->rawParams( $truncatedtext )->inContentLanguage()->text();
937  case 'newpage':
938  // If they're making a new article, give its text, truncated, in the summary.
939  $truncatedtext = $newContent->getTextForSummary(
940  200 - strlen( wfMessage( 'autosumm-new' )->inContentLanguage()->text() ) );
941 
942  return wfMessage( 'autosumm-new' )->rawParams( $truncatedtext )
943  ->inContentLanguage()->text();
944  case 'blank':
945  return wfMessage( 'autosumm-blank' )->inContentLanguage()->text();
946  case 'replace':
947  $truncatedtext = $newContent->getTextForSummary(
948  200 - strlen( wfMessage( 'autosumm-replace' )->inContentLanguage()->text() ) );
949 
950  return wfMessage( 'autosumm-replace' )->rawParams( $truncatedtext )
951  ->inContentLanguage()->text();
952  case 'newblank':
953  return wfMessage( 'autosumm-newblank' )->inContentLanguage()->text();
954  default:
955  return '';
956  }
957  }
958 
970  public function getChangeTag(
971  Content $oldContent = null,
972  Content $newContent = null,
973  $flags = 0
974  ) {
975  $changeType = $this->getChangeType( $oldContent, $newContent, $flags );
976 
977  // There's no applicable tag for this change.
978  if ( !$changeType ) {
979  return null;
980  }
981 
982  // Core tags use the same keys as ones returned from $this->getChangeType()
983  // but prefixed with pseudo namespace 'mw-', so we add the prefix before checking
984  // if this type of change should be tagged
985  $tag = 'mw-' . $changeType;
986 
987  // Not all change types are tagged, so we check against the list of defined tags.
988  if ( in_array( $tag, ChangeTags::getSoftwareTags() ) ) {
989  return $tag;
990  }
991 
992  return null;
993  }
994 
1010  public function getAutoDeleteReason( Title $title, &$hasHistory ) {
1011  $dbr = wfGetDB( DB_REPLICA );
1012  $revLookup = MediaWikiServices::getInstance()->getRevisionLookup();
1013 
1014  // Get the last revision
1015  $revRecord = $revLookup->getRevisionByTitle( $title );
1016 
1017  if ( $revRecord === null ) {
1018  return false;
1019  }
1020 
1021  // Get the article's contents
1022  $content = $revRecord->getContent( SlotRecord::MAIN );
1023  $blank = false;
1024 
1025  // If the page is blank, use the text from the previous revision,
1026  // which can only be blank if there's a move/import/protect dummy
1027  // revision involved
1028  if ( !$content || $content->isEmpty() ) {
1029  $prev = $revLookup->getPreviousRevision( $revRecord );
1030 
1031  if ( $prev ) {
1032  $revRecord = $prev;
1033  $content = $prev->getContent( SlotRecord::MAIN );
1034  $blank = true;
1035  }
1036  }
1037 
1038  $this->checkModelID( $revRecord->getSlot( SlotRecord::MAIN )->getModel() );
1039 
1040  // Find out if there was only one contributor
1041  // Only scan the last 20 revisions
1042  $revQuery = MediaWikiServices::getInstance()->getRevisionStore()->getQueryInfo();
1043  $res = $dbr->select(
1044  $revQuery['tables'],
1045  [ 'rev_user_text' => $revQuery['fields']['rev_user_text'] ],
1046  [
1047  'rev_page' => $title->getArticleID(),
1048  $dbr->bitAnd( 'rev_deleted', RevisionRecord::DELETED_USER ) . ' = 0'
1049  ],
1050  __METHOD__,
1051  [ 'LIMIT' => 20 ],
1052  $revQuery['joins']
1053  );
1054 
1055  if ( $res === false ) {
1056  // This page has no revisions, which is very weird
1057  return false;
1058  }
1059 
1060  $hasHistory = ( $res->numRows() > 1 );
1061  $row = $dbr->fetchObject( $res );
1062 
1063  if ( $row ) { // $row is false if the only contributor is hidden
1064  $onlyAuthor = $row->rev_user_text;
1065  // Try to find a second contributor
1066  foreach ( $res as $row ) {
1067  if ( $row->rev_user_text != $onlyAuthor ) { // T24999
1068  $onlyAuthor = false;
1069  break;
1070  }
1071  }
1072  } else {
1073  $onlyAuthor = false;
1074  }
1075 
1076  // Generate the summary with a '$1' placeholder
1077  if ( $blank ) {
1078  // The current revision is blank and the one before is also
1079  // blank. It's just not our lucky day
1080  $reason = wfMessage( 'exbeforeblank', '$1' )->inContentLanguage()->text();
1081  } else {
1082  if ( $onlyAuthor ) {
1083  $reason = wfMessage(
1084  'excontentauthor',
1085  '$1',
1086  $onlyAuthor
1087  )->inContentLanguage()->text();
1088  } else {
1089  $reason = wfMessage( 'excontent', '$1' )->inContentLanguage()->text();
1090  }
1091  }
1092 
1093  if ( $reason == '-' ) {
1094  // Allow these UI messages to be blanked out cleanly
1095  return '';
1096  }
1097 
1098  // Max content length = max comment length - length of the comment (excl. $1)
1099  $maxLength = CommentStore::COMMENT_CHARACTER_LIMIT - ( strlen( $reason ) - 2 );
1100  $text = $content ? $content->getTextForSummary( $maxLength ) : '';
1101 
1102  // Now replace the '$1' placeholder
1103  $reason = str_replace( '$1', $text, $reason );
1104 
1105  return $reason;
1106  }
1107 
1127  public function getUndoContent( $current, $undo, $undoafter, $undoIsLatest = false ) {
1128  Assert::parameterType( Revision::class . '|' . Content::class, $current, '$current' );
1129  if ( $current instanceof Content ) {
1130  Assert::parameter( $undo instanceof Content, '$undo',
1131  'Must be Content when $current is Content' );
1132  Assert::parameter( $undoafter instanceof Content, '$undoafter',
1133  'Must be Content when $current is Content' );
1134  $cur_content = $current;
1135  $undo_content = $undo;
1136  $undoafter_content = $undoafter;
1137  } else {
1138  Assert::parameter( $undo instanceof Revision, '$undo',
1139  'Must be Revision when $current is Revision' );
1140  Assert::parameter( $undoafter instanceof Revision, '$undoafter',
1141  'Must be Revision when $current is Revision' );
1142 
1143  wfDeprecated( __METHOD__ . ' with Revision objects', '1.32' );
1144 
1145  $cur_content = $current->getContent();
1146 
1147  if ( empty( $cur_content ) ) {
1148  return false; // no page
1149  }
1150 
1151  $undo_content = $undo->getContent();
1152  $undoafter_content = $undoafter->getContent();
1153 
1154  if ( !$undo_content || !$undoafter_content ) {
1155  return false; // no content to undo
1156  }
1157 
1158  $undoIsLatest = $current->getId() === $undo->getId();
1159  }
1160 
1161  try {
1162  $this->checkModelID( $cur_content->getModel() );
1163  $this->checkModelID( $undo_content->getModel() );
1164  if ( !$undoIsLatest ) {
1165  // If we are undoing the most recent revision,
1166  // its ok to revert content model changes. However
1167  // if we are undoing a revision in the middle, then
1168  // doing that will be confusing.
1169  $this->checkModelID( $undoafter_content->getModel() );
1170  }
1171  } catch ( MWException $e ) {
1172  // If the revisions have different content models
1173  // just return false
1174  return false;
1175  }
1176 
1177  if ( $cur_content->equals( $undo_content ) ) {
1178  // No use doing a merge if it's just a straight revert.
1179  return $undoafter_content;
1180  }
1181 
1182  $undone_content = $this->merge3( $undo_content, $undoafter_content, $cur_content );
1183 
1184  return $undone_content;
1185  }
1186 
1195  public function isParserCacheSupported() {
1196  return false;
1197  }
1198 
1208  public function supportsSections() {
1209  return false;
1210  }
1211 
1218  public function supportsCategories() {
1219  return true;
1220  }
1221 
1231  public function supportsRedirects() {
1232  return false;
1233  }
1234 
1240  public function supportsDirectEditing() {
1241  return false;
1242  }
1243 
1249  public function supportsDirectApiEditing() {
1250  return $this->supportsDirectEditing();
1251  }
1252 
1263  public function getFieldsForSearchIndex( SearchEngine $engine ) {
1264  $fields = [];
1265  $fields['category'] = $engine->makeSearchFieldMapping(
1266  'category',
1268  );
1269  $fields['category']->setFlag( SearchIndexField::FLAG_CASEFOLD );
1270 
1271  $fields['external_link'] = $engine->makeSearchFieldMapping(
1272  'external_link',
1274  );
1275 
1276  $fields['outgoing_link'] = $engine->makeSearchFieldMapping(
1277  'outgoing_link',
1279  );
1280 
1281  $fields['template'] = $engine->makeSearchFieldMapping(
1282  'template',
1284  );
1285  $fields['template']->setFlag( SearchIndexField::FLAG_CASEFOLD );
1286 
1287  $fields['content_model'] = $engine->makeSearchFieldMapping(
1288  'content_model',
1290  );
1291 
1292  return $fields;
1293  }
1294 
1304  protected function addSearchField( &$fields, SearchEngine $engine, $name, $type ) {
1305  $fields[$name] = $engine->makeSearchFieldMapping( $name, $type );
1306  return $fields;
1307  }
1308 
1320  public function getDataForSearchIndex(
1321  WikiPage $page,
1322  ParserOutput $output,
1323  SearchEngine $engine
1324  ) {
1325  $fieldData = [];
1326  $content = $page->getContent();
1327 
1328  if ( $content ) {
1329  $searchDataExtractor = new ParserOutputSearchDataExtractor();
1330 
1331  $fieldData['category'] = $searchDataExtractor->getCategories( $output );
1332  $fieldData['external_link'] = $searchDataExtractor->getExternalLinks( $output );
1333  $fieldData['outgoing_link'] = $searchDataExtractor->getOutgoingLinks( $output );
1334  $fieldData['template'] = $searchDataExtractor->getTemplates( $output );
1335 
1336  $text = $content->getTextForSearchIndex();
1337 
1338  $fieldData['text'] = $text;
1339  $fieldData['source_text'] = $text;
1340  $fieldData['text_bytes'] = $content->getSize();
1341  $fieldData['content_model'] = $content->getModel();
1342  }
1343 
1344  Hooks::run( 'SearchDataForIndex', [ &$fieldData, $this, $page, $output, $engine ] );
1345  return $fieldData;
1346  }
1347 
1357  public function getParserOutputForIndexing( WikiPage $page, ParserCache $cache = null ) {
1358  // TODO: MCR: ContentHandler should be called per slot, not for the whole page.
1359  // See T190066.
1360  $parserOptions = $page->makeParserOptions( 'canonical' );
1361  if ( $cache ) {
1362  $parserOutput = $cache->get( $page, $parserOptions );
1363  }
1364 
1365  if ( empty( $parserOutput ) ) {
1366  $renderer = MediaWikiServices::getInstance()->getRevisionRenderer();
1367  $revisionRecord = $this->latestRevision( $page );
1368  $parserOutput =
1369  $renderer->getRenderedRevision(
1370  $revisionRecord,
1371  $parserOptions
1372  )->getRevisionParserOutput();
1373  if ( $cache ) {
1374  $cache->save( $parserOutput, $page, $parserOptions );
1375  }
1376  }
1377  return $parserOutput;
1378  }
1379 
1380  private function latestRevision( WikiPage $page ): RevisionRecord {
1381  $revRecord = $page->getRevisionRecord();
1382  if ( $revRecord == null ) {
1383  // If the content represents a brand new page it's possible
1384  // we need to fetch it from the master.
1385  $page->loadPageData( WikiPage::READ_LATEST );
1386  $revRecord = $page->getRevisionRecord();
1387  if ( $revRecord == null ) {
1388  $text = $page->getTitle()->getPrefixedText();
1389  throw new MWException(
1390  "No revision could be loaded for page: $text" );
1391  }
1392  }
1393 
1394  return $revRecord;
1395  }
1396 
1427  public function getSecondaryDataUpdates(
1428  Title $title,
1429  Content $content,
1430  $role,
1431  SlotRenderingProvider $slotOutput
1432  ) {
1433  return [];
1434  }
1435 
1464  public function getDeletionUpdates( Title $title, $role ) {
1465  return [];
1466  }
1467 
1468 }
SearchIndexField\INDEX_TYPE_KEYWORD
const INDEX_TYPE_KEYWORD
KEYWORD fields are indexed without any processing, so are appropriate for e.g.
Definition: SearchIndexField.php:23
ContentHandler\getSecondaryDataUpdates
getSecondaryDataUpdates(Title $title, Content $content, $role, SlotRenderingProvider $slotOutput)
Returns a list of DeferrableUpdate objects for recording information about the given Content in some ...
Definition: ContentHandler.php:1427
ContentHandler
A content handler knows how do deal with a specific type of content on a wiki page.
Definition: ContentHandler.php:56
ContentHandler\getForModelID
static getForModelID( $modelId)
Returns the ContentHandler singleton for the given model ID.
Definition: ContentHandler.php:264
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
ContentHandler\getAllContentFormats
static getAllContentFormats()
Definition: ContentHandler.php:327
WikiPage\loadPageData
loadPageData( $from='fromdb')
Load the object from a given source by title.
Definition: WikiPage.php:458
DifferenceEngine\getEngine
static getEngine()
Process DiffEngine config and get a sane, usable engine.
Definition: DifferenceEngine.php:1440
WikiPage\getRevisionRecord
getRevisionRecord()
Get the latest revision.
Definition: WikiPage.php:774
ParserOutput
Definition: ParserOutput.php:25
ContentHandler\supportsDirectEditing
supportsDirectEditing()
Return true if this content model supports direct editing, such as via EditPage.
Definition: ContentHandler.php:1240
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:144
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
ContentHandler\getSlotDiffRendererInternal
getSlotDiffRendererInternal(IContextSource $context)
Return the SlotDiffRenderer appropriate for this content handler.
Definition: ContentHandler.php:625
ContentHandler\getActionOverrides
getActionOverrides()
Returns overrides for action handlers.
Definition: ContentHandler.php:550
ContentHandler\checkModelID
checkModelID( $model_id)
Definition: ContentHandler.php:462
ContentHandler\getAutoDeleteReason
getAutoDeleteReason(Title $title, &$hasHistory)
Auto-generates a deletion reason.
Definition: ContentHandler.php:1010
ContentHandler\unserializeContent
unserializeContent( $blob, $format=null)
Unserializes a Content object of the type supported by this ContentHandler.
ContentHandler\getPageViewLanguage
getPageViewLanguage(Title $title, Content $content=null)
Get the language in which the content of this page is written when viewed by user.
Definition: ContentHandler.php:728
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:48
ContentHandler\$mSupportedFormats
string[] $mSupportedFormats
Definition: ContentHandler.php:341
WikiPage\makeParserOptions
makeParserOptions( $context)
Get parser options suitable for rendering the primary article wikitext.
Definition: WikiPage.php:1995
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1198
ContentHandler\getAutosummary
getAutosummary(Content $oldContent=null, Content $newContent=null, $flags=0)
Return an applicable auto-summary if one exists for the given edit.
Definition: ContentHandler.php:887
TextSlotDiffRenderer\ENGINE_EXTERNAL
const ENGINE_EXTERNAL
Use an external executable.
Definition: TextSlotDiffRenderer.php:50
ContentHandler\getDeletionUpdates
getDeletionUpdates(Title $title, $role)
Returns a list of DeferrableUpdate objects for removing information about content in some secondary d...
Definition: ContentHandler.php:1464
ContentHandler\getForTitle
static getForTitle(Title $title)
Returns the appropriate ContentHandler singleton for the given title.
Definition: ContentHandler.php:208
TextSlotDiffRenderer\ENGINE_WIKIDIFF2
const ENGINE_WIKIDIFF2
Use the wikidiff2 PHP module.
Definition: TextSlotDiffRenderer.php:44
SearchIndexField\FLAG_CASEFOLD
const FLAG_CASEFOLD
Generic field flags.
Definition: SearchIndexField.php:44
ContentHandler\getChangeTag
getChangeTag(Content $oldContent=null, Content $newContent=null, $flags=0)
Return an applicable tag if one exists for the given edit or return null.
Definition: ContentHandler.php:970
$res
$res
Definition: testCompression.php:57
ContentHandler\serializeContent
serializeContent(Content $content, $format=null)
Serializes a Content object of the type supported by this ContentHandler.
$revQuery
$revQuery
Definition: testCompression.php:56
wfDebugLog
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Definition: GlobalFunctions.php:992
ContentHandler\supportsSections
supportsSections()
Returns true if this content model supports sections.
Definition: ContentHandler.php:1208
Revision\SlotRenderingProvider
A lazy provider of ParserOutput objects for a revision's individual slots.
Definition: SlotRenderingProvider.php:12
ContentHandler\isSupportedFormat
isSupportedFormat( $format)
Returns true if $format is a serialization format supported by this ContentHandler,...
Definition: ContentHandler.php:511
$dbr
$dbr
Definition: testCompression.php:54
ContentHandler\createDifferenceEngine
createDifferenceEngine(IContextSource $context, $old=0, $new=0, $rcid=0, $refreshCache=false, $unhide=false)
Factory for creating an appropriate DifferenceEngine for this content model.
Definition: ContentHandler.php:581
ContentHandler\canBeUsedOn
canBeUsedOn(Title $title)
Determines whether the content type handled by this ContentHandler can be used for the main slot of t...
Definition: ContentHandler.php:762
Revision
Definition: Revision.php:39
MediaWiki\Search\ParserOutputSearchDataExtractor
Extracts data from ParserOutput for indexing in the search engine.
Definition: ParserOutputSearchDataExtractor.php:29
ContentHandler\importTransform
importTransform( $blob, $format=null)
Apply import transformation (per default, returns $blob unchanged).
Definition: ContentHandler.php:407
ContentHandler\supportsRedirects
supportsRedirects()
Returns true if this content model supports redirects.
Definition: ContentHandler.php:1231
$wgContentHandlerTextFallback
$wgContentHandlerTextFallback
How to react if a plain text version of a non-text Content object is requested using ContentHandler::...
Definition: DefaultSettings.php:9019
ContentHandler\getDefaultModelFor
static getDefaultModelFor(Title $title)
Returns the name of the default content model to be used for the page with the given title.
Definition: ContentHandler.php:189
MWException
MediaWiki exception.
Definition: MWException.php:26
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1030
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
ContentHandler\getLanguageConverter
getLanguageConverter( $language)
Shorthand for getting a Language Converter for specific language.
Definition: ContentHandler.php:804
ContentHandler\getContentModels
static getContentModels()
Definition: ContentHandler.php:315
$blob
$blob
Definition: testCompression.php:70
ContentHandler\supportsDirectApiEditing
supportsDirectApiEditing()
Whether or not this content model supports direct editing via ApiEditPage.
Definition: ContentHandler.php:1249
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2463
$wgLang
$wgLang
Definition: Setup.php:774
WikiPage\getTitle
getTitle()
Get the title object of the article.
Definition: WikiPage.php:307
ContentHandler\getSlotDiffRendererWithOptions
getSlotDiffRendererWithOptions(IContextSource $context, $options=[])
Return the SlotDiffRenderer appropriate for this content handler.
Definition: ContentHandler.php:635
wfGetLangObj
wfGetLangObj( $langcode=false)
Return a Language object from $langcode.
Definition: GlobalFunctions.php:1148
TextSlotDiffRenderer\ENGINE_PHP
const ENGINE_PHP
Use the PHP diff implementation (DiffEngine).
Definition: TextSlotDiffRenderer.php:41
ContentHandler\merge3
merge3(Content $oldContent, Content $myContent, Content $yourContent)
Attempts to merge differences between three versions.
Definition: ContentHandler.php:795
ChangeTags\getSoftwareTags
static getSoftwareTags( $all=false)
Loads defined core tags, checks for invalid types (if not array), and filters for supported and enabl...
Definition: ChangeTags.php:63
ContentHandler\makeEmptyContent
makeEmptyContent()
Creates an empty Content object of the type supported by this ContentHandler.
$title
$title
Definition: testCompression.php:38
ContentHandler\getDataForSearchIndex
getDataForSearchIndex(WikiPage $page, ParserOutput $output, SearchEngine $engine)
Return fields to be indexed by search engine as representation of this document.
Definition: ContentHandler.php:1320
TextSlotDiffRenderer\ENGINE_WIKIDIFF2_INLINE
const ENGINE_WIKIDIFF2_INLINE
Use the wikidiff2 PHP module.
Definition: TextSlotDiffRenderer.php:47
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
ContentHandler\getSlotDiffRenderer
getSlotDiffRenderer(IContextSource $context, array $options=[])
Get an appropriate SlotDiffRenderer for this content model.
Definition: ContentHandler.php:599
ContentHandler\makeContent
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
Definition: ContentHandler.php:137
ContentHandler\getPageLanguage
getPageLanguage(Title $title, Content $content=null)
Get the language in which the content of the given page is written.
Definition: ContentHandler.php:688
ContentHandler\isParserCacheSupported
isParserCacheSupported()
Returns true for content models that support caching using the ParserCache mechanism.
Definition: ContentHandler.php:1195
$content
$content
Definition: router.php:76
ContentHandler\getDefaultFormat
getDefaultFormat()
The format used for serialization/deserialization by default by this ContentHandler.
Definition: ContentHandler.php:494
ILanguageConverter
The shared interface for all language converters.
Definition: ILanguageConverter.php:28
ContentHandler\getChangeType
getChangeType(Content $oldContent=null, Content $newContent=null, $flags=0)
Return type of change if one exists for the given edit.
Definition: ContentHandler.php:820
ContentHandler\cleanupHandlersCache
static cleanupHandlersCache()
Definition: ContentHandler.php:276
ContentHandler\getLocalizedName
static getLocalizedName( $name, Language $lang=null)
Returns the localized name for a given content model.
Definition: ContentHandler.php:294
ContentHandler\latestRevision
latestRevision(WikiPage $page)
Definition: ContentHandler.php:1380
ContentHandler\getDiffEngineClass
getDiffEngineClass()
Returns the name of the diff engine to use.
Definition: ContentHandler.php:777
ContentHandler\supportsCategories
supportsCategories()
Returns true if this content model supports categories.
Definition: ContentHandler.php:1218
ContentHandler\$mModelID
string $mModelID
Definition: ContentHandler.php:336
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:37
SearchEngine
Contain a class for special pages.
Definition: SearchEngine.php:34
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:53
$context
$context
Definition: load.php:43
ContentHandler\exportTransform
exportTransform( $blob, $format=null)
Applies transformations on export (returns the blob unchanged per default).
Definition: ContentHandler.php:379
Content
Base interface for content objects.
Definition: Content.php:34
ContentHandler\getFieldsForSearchIndex
getFieldsForSearchIndex(SearchEngine $engine)
Get fields definition for search index.
Definition: ContentHandler.php:1263
CommentStore\COMMENT_CHARACTER_LIMIT
const COMMENT_CHARACTER_LIMIT
Maximum length of a comment in UTF-8 characters.
Definition: CommentStore.php:37
EDIT_NEW
const EDIT_NEW
Definition: Defines.php:141
SearchEngine\makeSearchFieldMapping
makeSearchFieldMapping( $name, $type)
Create a search field definition.
Definition: SearchEngine.php:744
Title
Represents a title within MediaWiki.
Definition: Title.php:42
ContentHandler\makeRedirectContent
makeRedirectContent(Title $destination, $text='')
Creates a new Content object that acts as a redirect to the given page, or null if redirects are not ...
Definition: ContentHandler.php:438
ContentHandler\checkFormat
checkFormat( $format)
Convenient for checking whether a format provided as a parameter is actually supported.
Definition: ContentHandler.php:526
ContentHandler\getContentText
static getContentText(Content $content=null)
Convenience function for getting flat text from a Content object.
Definition: ContentHandler.php:86
$cache
$cache
Definition: mcc.php:33
DifferenceEngineSlotDiffRenderer
B/C adapter for turning a DifferenceEngine into a SlotDiffRenderer.
Definition: DifferenceEngineSlotDiffRenderer.php:32
ParserCache
Definition: ParserCache.php:30
ContentHandler\addSearchField
addSearchField(&$fields, SearchEngine $engine, $name, $type)
Add new field definition to array.
Definition: ContentHandler.php:1304
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:77
ContentHandler\__construct
__construct( $modelId, $formats)
Constructor, initializing the ContentHandler instance with its model ID and a list of supported forma...
Definition: ContentHandler.php:352
StubObject\unstub
static unstub(&$obj)
Unstubs an object, if it is a stub object.
Definition: StubObject.php:93
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:133
ContentHandler\getUndoContent
getUndoContent( $current, $undo, $undoafter, $undoIsLatest=false)
Get the Content object that needs to be saved in order to undo all revisions between $undo and $undoa...
Definition: ContentHandler.php:1127
ContentHandler\getSupportedFormats
getSupportedFormats()
Returns a list of serialization formats supported by the serializeContent() and unserializeContent() ...
Definition: ContentHandler.php:479
WikiPage\getContent
getContent( $audience=RevisionRecord::FOR_PUBLIC, User $user=null)
Get the content of the current revision.
Definition: WikiPage.php:795
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:39
ContentHandler\getModelID
getModelID()
Returns the model id that identifies the content model this ContentHandler can handle.
Definition: ContentHandler.php:450
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39
ContentHandler\getForContent
static getForContent(Content $content)
Returns the appropriate ContentHandler singleton for the given Content object.
Definition: ContentHandler.php:229
ContentHandler\getParserOutputForIndexing
getParserOutputForIndexing(WikiPage $page, ParserCache $cache=null)
Produce page output suitable for indexing.
Definition: ContentHandler.php:1357
TextSlotDiffRenderer
Renders a slot diff by doing a text diff on the native representation.
Definition: TextSlotDiffRenderer.php:38
SearchIndexField\INDEX_TYPE_TEXT
const INDEX_TYPE_TEXT
TEXT fields are suitable for natural language and may be subject to analysis such as stemming.
Definition: SearchIndexField.php:18
$type
$type
Definition: testCompression.php:52