MediaWiki  1.32.0
ContentHandler.php
Go to the documentation of this file.
1 <?php
28 use Wikimedia\Assert\Assert;
33 
53 abstract class ContentHandler {
83  public static function getContentText( Content $content = null ) {
85 
86  if ( is_null( $content ) ) {
87  return '';
88  }
89 
90  if ( $content instanceof TextContent ) {
91  return $content->getNativeData();
92  }
93 
94  wfDebugLog( 'ContentHandler', 'Accessing ' . $content->getModel() . ' content as text!' );
95 
96  if ( $wgContentHandlerTextFallback == 'fail' ) {
97  throw new MWException(
98  "Attempt to get text from Content with model " .
99  $content->getModel()
100  );
101  }
102 
103  if ( $wgContentHandlerTextFallback == 'serialize' ) {
104  return $content->serialize();
105  }
106 
107  return null;
108  }
109 
133  public static function makeContent( $text, Title $title = null,
134  $modelId = null, $format = null ) {
135  if ( is_null( $modelId ) ) {
136  if ( is_null( $title ) ) {
137  throw new MWException( "Must provide a Title object or a content model ID." );
138  }
139 
140  $modelId = $title->getContentModel();
141  }
142 
143  $handler = self::getForModelID( $modelId );
144 
145  return $handler->unserializeContent( $text, $format );
146  }
147 
182  public static function getDefaultModelFor( Title $title ) {
183  // NOTE: this method must not rely on $title->getContentModel() directly or indirectly,
184  // because it is used to initialize the mContentModel member.
185 
186  $ns = $title->getNamespace();
187 
188  $ext = false;
189  $m = null;
191 
192  // Hook can determine default model
193  if ( !Hooks::run( 'ContentHandlerDefaultModelFor', [ $title, &$model ] ) ) {
194  if ( !is_null( $model ) ) {
195  return $model;
196  }
197  }
198 
199  // Could this page contain code based on the title?
200  $isCodePage = NS_MEDIAWIKI == $ns && preg_match( '!\.(css|js|json)$!u', $title->getText(), $m );
201  if ( $isCodePage ) {
202  $ext = $m[1];
203  }
204 
205  // Is this a user subpage containing code?
206  $isCodeSubpage = NS_USER == $ns
207  && !$isCodePage
208  && preg_match( "/\\/.*\\.(js|css|json)$/", $title->getText(), $m );
209  if ( $isCodeSubpage ) {
210  $ext = $m[1];
211  }
212 
213  // Is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
214  $isWikitext = is_null( $model ) || $model == CONTENT_MODEL_WIKITEXT;
215  $isWikitext = $isWikitext && !$isCodePage && !$isCodeSubpage;
216 
217  if ( !$isWikitext ) {
218  switch ( $ext ) {
219  case 'js':
221  case 'css':
222  return CONTENT_MODEL_CSS;
223  case 'json':
224  return CONTENT_MODEL_JSON;
225  default:
226  return is_null( $model ) ? CONTENT_MODEL_TEXT : $model;
227  }
228  }
229 
230  // We established that it must be wikitext
231 
232  return CONTENT_MODEL_WIKITEXT;
233  }
234 
244  public static function getForTitle( Title $title ) {
245  $modelId = $title->getContentModel();
246 
247  return self::getForModelID( $modelId );
248  }
249 
260  public static function getForContent( Content $content ) {
261  $modelId = $content->getModel();
262 
263  return self::getForModelID( $modelId );
264  }
265 
269  protected static $handlers;
270 
297  public static function getForModelID( $modelId ) {
298  global $wgContentHandlers;
299 
300  if ( isset( self::$handlers[$modelId] ) ) {
301  return self::$handlers[$modelId];
302  }
303 
304  if ( empty( $wgContentHandlers[$modelId] ) ) {
305  $handler = null;
306 
307  Hooks::run( 'ContentHandlerForModelID', [ $modelId, &$handler ] );
308 
309  if ( $handler === null ) {
310  throw new MWUnknownContentModelException( $modelId );
311  }
312 
313  if ( !( $handler instanceof ContentHandler ) ) {
314  throw new MWException( "ContentHandlerForModelID must supply a ContentHandler instance" );
315  }
316  } else {
317  $classOrCallback = $wgContentHandlers[$modelId];
318 
319  if ( is_callable( $classOrCallback ) ) {
320  $handler = call_user_func( $classOrCallback, $modelId );
321  } else {
322  $handler = new $classOrCallback( $modelId );
323  }
324 
325  if ( !( $handler instanceof ContentHandler ) ) {
326  throw new MWException( "$classOrCallback from \$wgContentHandlers is not " .
327  "compatible with ContentHandler" );
328  }
329  }
330 
331  wfDebugLog( 'ContentHandler', 'Created handler for ' . $modelId
332  . ': ' . get_class( $handler ) );
333 
334  self::$handlers[$modelId] = $handler;
335 
336  return self::$handlers[$modelId];
337  }
338 
342  public static function cleanupHandlersCache() {
343  self::$handlers = [];
344  }
345 
359  public static function getLocalizedName( $name, Language $lang = null ) {
360  // Messages: content-model-wikitext, content-model-text,
361  // content-model-javascript, content-model-css
362  $key = "content-model-$name";
363 
364  $msg = wfMessage( $key );
365  if ( $lang ) {
366  $msg->inLanguage( $lang );
367  }
368 
369  return $msg->exists() ? $msg->plain() : $name;
370  }
371 
372  public static function getContentModels() {
373  global $wgContentHandlers;
374 
375  $models = array_keys( $wgContentHandlers );
376  Hooks::run( 'GetContentModels', [ &$models ] );
377  return $models;
378  }
379 
380  public static function getAllContentFormats() {
381  global $wgContentHandlers;
382 
383  $formats = [];
384 
385  foreach ( $wgContentHandlers as $model => $class ) {
386  $handler = self::getForModelID( $model );
387  $formats = array_merge( $formats, $handler->getSupportedFormats() );
388  }
389 
390  $formats = array_unique( $formats );
391 
392  return $formats;
393  }
394 
395  // ------------------------------------------------------------------------
396 
400  protected $mModelID;
401 
406 
416  public function __construct( $modelId, $formats ) {
417  $this->mModelID = $modelId;
418  $this->mSupportedFormats = $formats;
419  }
420 
431  abstract public function serializeContent( Content $content, $format = null );
432 
443  public function exportTransform( $blob, $format = null ) {
444  return $blob;
445  }
446 
457  abstract public function unserializeContent( $blob, $format = null );
458 
470  public function importTransform( $blob, $format = null ) {
471  return $blob;
472  }
473 
482  abstract public function makeEmptyContent();
483 
501  public function makeRedirectContent( Title $destination, $text = '' ) {
502  return null;
503  }
504 
513  public function getModelID() {
514  return $this->mModelID;
515  }
516 
525  protected function checkModelID( $model_id ) {
526  if ( $model_id !== $this->mModelID ) {
527  throw new MWException( "Bad content model: " .
528  "expected {$this->mModelID} " .
529  "but got $model_id." );
530  }
531  }
532 
542  public function getSupportedFormats() {
544  }
545 
557  public function getDefaultFormat() {
558  return $this->mSupportedFormats[0];
559  }
560 
574  public function isSupportedFormat( $format ) {
575  if ( !$format ) {
576  return true; // this means "use the default"
577  }
578 
579  return in_array( $format, $this->mSupportedFormats );
580  }
581 
589  protected function checkFormat( $format ) {
590  if ( !$this->isSupportedFormat( $format ) ) {
591  throw new MWException(
592  "Format $format is not supported for content model "
593  . $this->getModelID()
594  );
595  }
596  }
597 
613  public function getActionOverrides() {
614  return [];
615  }
616 
644  public function createDifferenceEngine( IContextSource $context, $old = 0, $new = 0,
645  $rcid = 0, // FIXME: Deprecated, no longer used
646  $refreshCache = false, $unhide = false
647  ) {
648  $diffEngineClass = $this->getDiffEngineClass();
649  $differenceEngine = new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide );
650  Hooks::run( 'GetDifferenceEngine', [ $context, $old, $new, $refreshCache, $unhide,
651  &$differenceEngine ] );
652  return $differenceEngine;
653  }
654 
661  final public function getSlotDiffRenderer( IContextSource $context ) {
662  $slotDiffRenderer = $this->getSlotDiffRendererInternal( $context );
663  if ( get_class( $slotDiffRenderer ) === TextSlotDiffRenderer::class ) {
664  // To keep B/C, when SlotDiffRenderer is not overridden for a given content type
665  // but DifferenceEngine is, use that instead.
666  $differenceEngine = $this->createDifferenceEngine( $context );
667  if ( get_class( $differenceEngine ) !== DifferenceEngine::class ) {
668  // TODO turn this into a deprecation warning in a later release
669  LoggerFactory::getInstance( 'diff' )->info(
670  'Falling back to DifferenceEngineSlotDiffRenderer', [
671  'modelID' => $this->getModelID(),
672  'DifferenceEngine' => get_class( $differenceEngine ),
673  ] );
674  $slotDiffRenderer = new DifferenceEngineSlotDiffRenderer( $differenceEngine );
675  }
676  }
677  Hooks::run( 'GetSlotDiffRenderer', [ $this, &$slotDiffRenderer, $context ] );
678  return $slotDiffRenderer;
679  }
680 
687  $contentLanguage = MediaWikiServices::getInstance()->getContentLanguage();
688  $statsdDataFactory = MediaWikiServices::getInstance()->getStatsdDataFactory();
689  $slotDiffRenderer = new TextSlotDiffRenderer();
690  $slotDiffRenderer->setStatsdDataFactory( $statsdDataFactory );
691  // XXX using the page language would be better, but it's unclear how that should be injected
692  $slotDiffRenderer->setLanguage( $contentLanguage );
693  $slotDiffRenderer->setWikiDiff2MovedParagraphDetectionCutoff(
694  $context->getConfig()->get( 'WikiDiff2MovedParagraphDetectionCutoff' )
695  );
696 
698  if ( $engine === false ) {
699  $slotDiffRenderer->setEngine( TextSlotDiffRenderer::ENGINE_PHP );
700  } elseif ( $engine === 'wikidiff2' ) {
701  $slotDiffRenderer->setEngine( TextSlotDiffRenderer::ENGINE_WIKIDIFF2 );
702  } else {
703  $slotDiffRenderer->setEngine( TextSlotDiffRenderer::ENGINE_EXTERNAL, $engine );
704  }
705 
706  return $slotDiffRenderer;
707  }
708 
728  public function getPageLanguage( Title $title, Content $content = null ) {
729  global $wgLang;
730  $pageLang = MediaWikiServices::getInstance()->getContentLanguage();
731 
732  if ( $title->getNamespace() == NS_MEDIAWIKI ) {
733  // Parse mediawiki messages with correct target language
734  list( /* $unused */, $lang ) = MessageCache::singleton()->figureMessage( $title->getText() );
735  $pageLang = Language::factory( $lang );
736  }
737 
738  Hooks::run( 'PageContentLanguage', [ $title, &$pageLang, $wgLang ] );
739 
740  return wfGetLangObj( $pageLang );
741  }
742 
763  public function getPageViewLanguage( Title $title, Content $content = null ) {
764  $pageLang = $this->getPageLanguage( $title, $content );
765 
766  if ( $title->getNamespace() !== NS_MEDIAWIKI ) {
767  // If the user chooses a variant, the content is actually
768  // in a language whose code is the variant code.
769  $variant = $pageLang->getPreferredVariant();
770  if ( $pageLang->getCode() !== $variant ) {
771  $pageLang = Language::factory( $variant );
772  }
773  }
774 
775  return $pageLang;
776  }
777 
794  public function canBeUsedOn( Title $title ) {
795  $ok = true;
796 
797  Hooks::run( 'ContentModelCanBeUsedOn', [ $this->getModelID(), $title, &$ok ] );
798 
799  return $ok;
800  }
801 
809  protected function getDiffEngineClass() {
811  }
812 
827  public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
828  return false;
829  }
830 
842  private function getChangeType(
843  Content $oldContent = null,
844  Content $newContent = null,
845  $flags = 0
846  ) {
847  $oldTarget = $oldContent !== null ? $oldContent->getRedirectTarget() : null;
848  $newTarget = $newContent !== null ? $newContent->getRedirectTarget() : null;
849 
850  // We check for the type of change in the given edit, and return string key accordingly
851 
852  // Blanking of a page
853  if ( $oldContent && $oldContent->getSize() > 0 &&
854  $newContent && $newContent->getSize() === 0
855  ) {
856  return 'blank';
857  }
858 
859  // Redirects
860  if ( $newTarget ) {
861  if ( !$oldTarget ) {
862  // New redirect page (by creating new page or by changing content page)
863  return 'new-redirect';
864  } elseif ( !$newTarget->equals( $oldTarget ) ||
865  $oldTarget->getFragment() !== $newTarget->getFragment()
866  ) {
867  // Redirect target changed
868  return 'changed-redirect-target';
869  }
870  } elseif ( $oldTarget ) {
871  // Changing an existing redirect into a non-redirect
872  return 'removed-redirect';
873  }
874 
875  // New page created
876  if ( $flags & EDIT_NEW && $newContent ) {
877  if ( $newContent->getSize() === 0 ) {
878  // New blank page
879  return 'newblank';
880  } else {
881  return 'newpage';
882  }
883  }
884 
885  // Removing more than 90% of the page
886  if ( $oldContent && $newContent && $oldContent->getSize() > 10 * $newContent->getSize() ) {
887  return 'replace';
888  }
889 
890  // Content model changed
891  if ( $oldContent && $newContent && $oldContent->getModel() !== $newContent->getModel() ) {
892  return 'contentmodelchange';
893  }
894 
895  return null;
896  }
897 
909  public function getAutosummary(
910  Content $oldContent = null,
911  Content $newContent = null,
912  $flags = 0
913  ) {
914  $changeType = $this->getChangeType( $oldContent, $newContent, $flags );
915 
916  // There's no applicable auto-summary for our case, so our auto-summary is empty.
917  if ( !$changeType ) {
918  return '';
919  }
920 
921  // Decide what kind of auto-summary is needed.
922  switch ( $changeType ) {
923  case 'new-redirect':
924  $newTarget = $newContent->getRedirectTarget();
925  $truncatedtext = $newContent->getTextForSummary(
926  250
927  - strlen( wfMessage( 'autoredircomment' )->inContentLanguage()->text() )
928  - strlen( $newTarget->getFullText() )
929  );
930 
931  return wfMessage( 'autoredircomment', $newTarget->getFullText() )
932  ->plaintextParams( $truncatedtext )->inContentLanguage()->text();
933  case 'changed-redirect-target':
934  $oldTarget = $oldContent->getRedirectTarget();
935  $newTarget = $newContent->getRedirectTarget();
936 
937  $truncatedtext = $newContent->getTextForSummary(
938  250
939  - strlen( wfMessage( 'autosumm-changed-redirect-target' )
940  ->inContentLanguage()->text() )
941  - strlen( $oldTarget->getFullText() )
942  - strlen( $newTarget->getFullText() )
943  );
944 
945  return wfMessage( 'autosumm-changed-redirect-target',
946  $oldTarget->getFullText(),
947  $newTarget->getFullText() )
948  ->rawParams( $truncatedtext )->inContentLanguage()->text();
949  case 'removed-redirect':
950  $oldTarget = $oldContent->getRedirectTarget();
951  $truncatedtext = $newContent->getTextForSummary(
952  250
953  - strlen( wfMessage( 'autosumm-removed-redirect' )
954  ->inContentLanguage()->text() )
955  - strlen( $oldTarget->getFullText() ) );
956 
957  return wfMessage( 'autosumm-removed-redirect', $oldTarget->getFullText() )
958  ->rawParams( $truncatedtext )->inContentLanguage()->text();
959  case 'newpage':
960  // If they're making a new article, give its text, truncated, in the summary.
961  $truncatedtext = $newContent->getTextForSummary(
962  200 - strlen( wfMessage( 'autosumm-new' )->inContentLanguage()->text() ) );
963 
964  return wfMessage( 'autosumm-new' )->rawParams( $truncatedtext )
965  ->inContentLanguage()->text();
966  case 'blank':
967  return wfMessage( 'autosumm-blank' )->inContentLanguage()->text();
968  case 'replace':
969  $truncatedtext = $newContent->getTextForSummary(
970  200 - strlen( wfMessage( 'autosumm-replace' )->inContentLanguage()->text() ) );
971 
972  return wfMessage( 'autosumm-replace' )->rawParams( $truncatedtext )
973  ->inContentLanguage()->text();
974  case 'newblank':
975  return wfMessage( 'autosumm-newblank' )->inContentLanguage()->text();
976  default:
977  return '';
978  }
979  }
980 
992  public function getChangeTag(
993  Content $oldContent = null,
994  Content $newContent = null,
995  $flags = 0
996  ) {
997  $changeType = $this->getChangeType( $oldContent, $newContent, $flags );
998 
999  // There's no applicable tag for this change.
1000  if ( !$changeType ) {
1001  return null;
1002  }
1003 
1004  // Core tags use the same keys as ones returned from $this->getChangeType()
1005  // but prefixed with pseudo namespace 'mw-', so we add the prefix before checking
1006  // if this type of change should be tagged
1007  $tag = 'mw-' . $changeType;
1008 
1009  // Not all change types are tagged, so we check against the list of defined tags.
1010  if ( in_array( $tag, ChangeTags::getSoftwareTags() ) ) {
1011  return $tag;
1012  }
1013 
1014  return null;
1015  }
1016 
1032  public function getAutoDeleteReason( Title $title, &$hasHistory ) {
1033  $dbr = wfGetDB( DB_REPLICA );
1034 
1035  // Get the last revision
1037 
1038  if ( is_null( $rev ) ) {
1039  return false;
1040  }
1041 
1042  // Get the article's contents
1043  $content = $rev->getContent();
1044  $blank = false;
1045 
1046  // If the page is blank, use the text from the previous revision,
1047  // which can only be blank if there's a move/import/protect dummy
1048  // revision involved
1049  if ( !$content || $content->isEmpty() ) {
1050  $prev = $rev->getPrevious();
1051 
1052  if ( $prev ) {
1053  $rev = $prev;
1054  $content = $rev->getContent();
1055  $blank = true;
1056  }
1057  }
1058 
1059  $this->checkModelID( $rev->getContentModel() );
1060 
1061  // Find out if there was only one contributor
1062  // Only scan the last 20 revisions
1064  $res = $dbr->select(
1065  $revQuery['tables'],
1066  [ 'rev_user_text' => $revQuery['fields']['rev_user_text'] ],
1067  [
1068  'rev_page' => $title->getArticleID(),
1069  $dbr->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0'
1070  ],
1071  __METHOD__,
1072  [ 'LIMIT' => 20 ],
1073  $revQuery['joins']
1074  );
1075 
1076  if ( $res === false ) {
1077  // This page has no revisions, which is very weird
1078  return false;
1079  }
1080 
1081  $hasHistory = ( $res->numRows() > 1 );
1082  $row = $dbr->fetchObject( $res );
1083 
1084  if ( $row ) { // $row is false if the only contributor is hidden
1085  $onlyAuthor = $row->rev_user_text;
1086  // Try to find a second contributor
1087  foreach ( $res as $row ) {
1088  if ( $row->rev_user_text != $onlyAuthor ) { // T24999
1089  $onlyAuthor = false;
1090  break;
1091  }
1092  }
1093  } else {
1094  $onlyAuthor = false;
1095  }
1096 
1097  // Generate the summary with a '$1' placeholder
1098  if ( $blank ) {
1099  // The current revision is blank and the one before is also
1100  // blank. It's just not our lucky day
1101  $reason = wfMessage( 'exbeforeblank', '$1' )->inContentLanguage()->text();
1102  } else {
1103  if ( $onlyAuthor ) {
1104  $reason = wfMessage(
1105  'excontentauthor',
1106  '$1',
1107  $onlyAuthor
1108  )->inContentLanguage()->text();
1109  } else {
1110  $reason = wfMessage( 'excontent', '$1' )->inContentLanguage()->text();
1111  }
1112  }
1113 
1114  if ( $reason == '-' ) {
1115  // Allow these UI messages to be blanked out cleanly
1116  return '';
1117  }
1118 
1119  // Max content length = max comment length - length of the comment (excl. $1)
1120  $text = $content ? $content->getTextForSummary( 255 - ( strlen( $reason ) - 2 ) ) : '';
1121 
1122  // Now replace the '$1' placeholder
1123  $reason = str_replace( '$1', $text, $reason );
1124 
1125  return $reason;
1126  }
1127 
1144  public function getUndoContent( $current, $undo, $undoafter, $undoIsLatest = false ) {
1145  Assert::parameterType( Revision::class . '|' . Content::class, $current, '$current' );
1146  if ( $current instanceof Content ) {
1147  Assert::parameter( $undo instanceof Content, '$undo',
1148  'Must be Content when $current is Content' );
1149  Assert::parameter( $undoafter instanceof Content, '$undoafter',
1150  'Must be Content when $current is Content' );
1151  $cur_content = $current;
1152  $undo_content = $undo;
1153  $undoafter_content = $undoafter;
1154  } else {
1155  Assert::parameter( $undo instanceof Revision, '$undo',
1156  'Must be Revision when $current is Revision' );
1157  Assert::parameter( $undoafter instanceof Revision, '$undoafter',
1158  'Must be Revision when $current is Revision' );
1159 
1160  $cur_content = $current->getContent();
1161 
1162  if ( empty( $cur_content ) ) {
1163  return false; // no page
1164  }
1165 
1166  $undo_content = $undo->getContent();
1167  $undoafter_content = $undoafter->getContent();
1168 
1169  if ( !$undo_content || !$undoafter_content ) {
1170  return false; // no content to undo
1171  }
1172 
1173  $undoIsLatest = $current->getId() === $undo->getId();
1174  }
1175 
1176  try {
1177  $this->checkModelID( $cur_content->getModel() );
1178  $this->checkModelID( $undo_content->getModel() );
1179  if ( !$undoIsLatest ) {
1180  // If we are undoing the most recent revision,
1181  // its ok to revert content model changes. However
1182  // if we are undoing a revision in the middle, then
1183  // doing that will be confusing.
1184  $this->checkModelID( $undoafter_content->getModel() );
1185  }
1186  } catch ( MWException $e ) {
1187  // If the revisions have different content models
1188  // just return false
1189  return false;
1190  }
1191 
1192  if ( $cur_content->equals( $undo_content ) ) {
1193  // No use doing a merge if it's just a straight revert.
1194  return $undoafter_content;
1195  }
1196 
1197  $undone_content = $this->merge3( $undo_content, $undoafter_content, $cur_content );
1198 
1199  return $undone_content;
1200  }
1201 
1218  public function makeParserOptions( $context ) {
1219  wfDeprecated( __METHOD__, '1.32' );
1221  }
1222 
1231  public function isParserCacheSupported() {
1232  return false;
1233  }
1234 
1244  public function supportsSections() {
1245  return false;
1246  }
1247 
1254  public function supportsCategories() {
1255  return true;
1256  }
1257 
1267  public function supportsRedirects() {
1268  return false;
1269  }
1270 
1276  public function supportsDirectEditing() {
1277  return false;
1278  }
1279 
1285  public function supportsDirectApiEditing() {
1286  return $this->supportsDirectEditing();
1287  }
1288 
1300  $fields['category'] = $engine->makeSearchFieldMapping(
1301  'category',
1303  );
1304  $fields['category']->setFlag( SearchIndexField::FLAG_CASEFOLD );
1305 
1306  $fields['external_link'] = $engine->makeSearchFieldMapping(
1307  'external_link',
1309  );
1310 
1311  $fields['outgoing_link'] = $engine->makeSearchFieldMapping(
1312  'outgoing_link',
1314  );
1315 
1316  $fields['template'] = $engine->makeSearchFieldMapping(
1317  'template',
1319  );
1320  $fields['template']->setFlag( SearchIndexField::FLAG_CASEFOLD );
1321 
1322  $fields['content_model'] = $engine->makeSearchFieldMapping(
1323  'content_model',
1325  );
1326 
1327  return $fields;
1328  }
1329 
1339  protected function addSearchField( &$fields, SearchEngine $engine, $name, $type ) {
1340  $fields[$name] = $engine->makeSearchFieldMapping( $name, $type );
1341  return $fields;
1342  }
1343 
1355  public function getDataForSearchIndex(
1356  WikiPage $page,
1359  ) {
1360  $fieldData = [];
1361  $content = $page->getContent();
1362 
1363  if ( $content ) {
1364  $searchDataExtractor = new ParserOutputSearchDataExtractor();
1365 
1366  $fieldData['category'] = $searchDataExtractor->getCategories( $output );
1367  $fieldData['external_link'] = $searchDataExtractor->getExternalLinks( $output );
1368  $fieldData['outgoing_link'] = $searchDataExtractor->getOutgoingLinks( $output );
1369  $fieldData['template'] = $searchDataExtractor->getTemplates( $output );
1370 
1371  $text = $content->getTextForSearchIndex();
1372 
1373  $fieldData['text'] = $text;
1374  $fieldData['source_text'] = $text;
1375  $fieldData['text_bytes'] = $content->getSize();
1376  $fieldData['content_model'] = $content->getModel();
1377  }
1378 
1379  Hooks::run( 'SearchDataForIndex', [ &$fieldData, $this, $page, $output, $engine ] );
1380  return $fieldData;
1381  }
1382 
1392  public function getParserOutputForIndexing( WikiPage $page, ParserCache $cache = null ) {
1393  // TODO: MCR: ContentHandler should be called per slot, not for the whole page.
1394  // See T190066.
1395  $parserOptions = $page->makeParserOptions( 'canonical' );
1396  if ( $cache ) {
1397  $parserOutput = $cache->get( $page, $parserOptions );
1398  }
1399 
1400  if ( empty( $parserOutput ) ) {
1401  $renderer = MediaWikiServices::getInstance()->getRevisionRenderer();
1402  $parserOutput =
1403  $renderer->getRenderedRevision(
1404  $page->getRevision()->getRevisionRecord(),
1405  $parserOptions
1406  )->getRevisionParserOutput();
1407  if ( $cache ) {
1408  $cache->save( $parserOutput, $page, $parserOptions );
1409  }
1410  }
1411  return $parserOutput;
1412  }
1413 
1444  public function getSecondaryDataUpdates(
1445  Title $title,
1446  Content $content,
1447  $role,
1448  SlotRenderingProvider $slotOutput
1449  ) {
1450  return [];
1451  }
1452 
1481  public function getDeletionUpdates( Title $title, $role ) {
1482  return [];
1483  }
1484 
1485 }
SearchIndexField\INDEX_TYPE_KEYWORD
const INDEX_TYPE_KEYWORD
Definition: SearchIndexField.php:11
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:1444
Revision\DELETED_USER
const DELETED_USER
Definition: Revision.php:49
ContentHandler\getSlotDiffRenderer
getSlotDiffRenderer(IContextSource $context)
Get an appropriate SlotDiffRenderer for this content model.
Definition: ContentHandler.php:661
CONTENT_MODEL_JSON
const CONTENT_MODEL_JSON
Definition: Defines.php:239
ContentHandler
A content handler knows how do deal with a specific type of content on a wiki page.
Definition: ContentHandler.php:53
ContentHandler\getForModelID
static getForModelID( $modelId)
Returns the ContentHandler singleton for the given model ID.
Definition: ContentHandler.php:297
ContentHandler\getAllContentFormats
static getAllContentFormats()
Definition: ContentHandler.php:380
DifferenceEngine\getEngine
static getEngine()
Process $wgExternalDiffEngine and get a sane, usable engine.
Definition: DifferenceEngine.php:1278
ParserOutput
Definition: ParserOutput.php:25
ContentHandler\supportsDirectEditing
supportsDirectEditing()
Return true if this content model supports direct editing, such as via EditPage.
Definition: ContentHandler.php:1276
$context
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2675
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
ContentHandler\makeParserOptions
makeParserOptions( $context)
Get parser options suitable for rendering and caching the article.
Definition: ContentHandler.php:1218
ContentHandler\getSlotDiffRendererInternal
getSlotDiffRendererInternal(IContextSource $context)
Return the SlotDiffRenderer appropriate for this content handler.
Definition: ContentHandler.php:686
CONTENT_MODEL_CSS
const CONTENT_MODEL_CSS
Definition: Defines.php:237
ContentHandler\getActionOverrides
getActionOverrides()
Returns overrides for action handlers.
Definition: ContentHandler.php:613
ContentHandler\checkModelID
checkModelID( $model_id)
Definition: ContentHandler.php:525
MWNamespace\getNamespaceContentModel
static getNamespaceContentModel( $index)
Get the default content model for a namespace This does not mean that all pages in that namespace hav...
Definition: MWNamespace.php:469
ContentHandler\getAutoDeleteReason
getAutoDeleteReason(Title $title, &$hasHistory)
Auto-generates a deletion reason.
Definition: ContentHandler.php:1032
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:763
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:44
ContentHandler\$mSupportedFormats
string[] $mSupportedFormats
Definition: ContentHandler.php:405
WikiPage\makeParserOptions
makeParserOptions( $context)
Get parser options suitable for rendering the primary article wikitext.
Definition: WikiPage.php:1917
WikiPage\getRevision
getRevision()
Get the latest revision.
Definition: WikiPage.php:765
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:909
TextSlotDiffRenderer\ENGINE_EXTERNAL
const ENGINE_EXTERNAL
Use an external executable.
Definition: TextSlotDiffRenderer.php:46
ContentHandler\getDeletionUpdates
getDeletionUpdates(Title $title, $role)
Returns a list of DeferrableUpdate objects for removing information about content in some secondary d...
Definition: ContentHandler.php:1481
ContentHandler\getForTitle
static getForTitle(Title $title)
Returns the appropriate ContentHandler singleton for the given title.
Definition: ContentHandler.php:244
$res
$res
Definition: database.txt:21
TextSlotDiffRenderer\ENGINE_WIKIDIFF2
const ENGINE_WIKIDIFF2
Use the wikidiff2 PHP module.
Definition: TextSlotDiffRenderer.php:43
SearchIndexField\FLAG_CASEFOLD
const FLAG_CASEFOLD
Generic field flags.
Definition: SearchIndexField.php:32
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:235
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:992
ContentHandler\serializeContent
serializeContent(Content $content, $format=null)
Serializes a Content object of the type supported by this ContentHandler.
$revQuery
$revQuery
Definition: testCompression.php:51
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:1082
ContentHandler\supportsSections
supportsSections()
Returns true if this content model supports sections.
Definition: ContentHandler.php:1244
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Revision\SlotRenderingProvider
A lazy provider of ParserOutput objects for a revision's individual slots.
Definition: SlotRenderingProvider.php:17
ContentHandler\isSupportedFormat
isSupportedFormat( $format)
Returns true if $format is a serialization format supported by this ContentHandler,...
Definition: ContentHandler.php:574
$differenceEngine
null for the wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify as strings Extensions should add to this list prev or next refreshes the diff cache allow viewing deleted revs & $differenceEngine
Definition: hooks.txt:1627
$dbr
$dbr
Definition: testCompression.php:50
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:644
ContentHandler\canBeUsedOn
canBeUsedOn(Title $title)
Determines whether the content type handled by this ContentHandler can be used on the given page.
Definition: ContentHandler.php:794
Revision
Definition: Revision.php:41
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:470
ContentHandler\supportsRedirects
supportsRedirects()
Returns true if this content model supports redirects.
Definition: ContentHandler.php:1267
Revision\newFromTitle
static newFromTitle(LinkTarget $linkTarget, $id=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given link target.
Definition: Revision.php:133
$wgContentHandlerTextFallback
$wgContentHandlerTextFallback
How to react if a plain text version of a non-text Content object is requested using ContentHandler::...
Definition: DefaultSettings.php:8609
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:182
Revision\getQueryInfo
static getQueryInfo( $options=[])
Return the tables, fields, and join conditions to be selected to create a new revision object.
Definition: Revision.php:521
MWException
MediaWiki exception.
Definition: MWException.php:26
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1118
$wgContentHandlers
$wgContentHandlers
Plugins for page content model handling.
Definition: DefaultSettings.php:1052
ContentHandler\getContentModels
static getContentModels()
Definition: ContentHandler.php:372
$blob
$blob
Definition: testCompression.php:65
ContentHandler\supportsDirectApiEditing
supportsDirectApiEditing()
Whether or not this content model supports direct editing via ApiEditPage.
Definition: ContentHandler.php:1285
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2693
$wgLang
$wgLang
Definition: Setup.php:902
WikiPage\getContent
getContent( $audience=Revision::FOR_PUBLIC, User $user=null)
Get the content of the current revision.
Definition: WikiPage.php:798
wfGetLangObj
wfGetLangObj( $langcode=false)
Return a Language object from $langcode.
Definition: GlobalFunctions.php:1281
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
TextSlotDiffRenderer\ENGINE_PHP
const ENGINE_PHP
Use the PHP diff implementation (DiffEngine).
Definition: TextSlotDiffRenderer.php:40
ContentHandler\merge3
merge3(Content $oldContent, Content $myContent, Content $yourContent)
Attempts to merge differences between three versions.
Definition: ContentHandler.php:827
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:57
ContentHandler\makeEmptyContent
makeEmptyContent()
Creates an empty Content object of the type supported by this ContentHandler.
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:1355
$engine
the value to return A Title object or null for latest all implement SearchIndexField $engine
Definition: hooks.txt:2938
$output
$output
Definition: SyntaxHighlight.php:334
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
MessageCache\singleton
static singleton()
Get the signleton instance of this class.
Definition: MessageCache.php:114
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:133
ContentHandler\getPageLanguage
getPageLanguage(Title $title, Content $content=null)
Get the language in which the content of the given page is written.
Definition: ContentHandler.php:728
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
ContentHandler\isParserCacheSupported
isParserCacheSupported()
Returns true for content models that support caching using the ParserCache mechanism.
Definition: ContentHandler.php:1231
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2213
ContentHandler\getDefaultFormat
getDefaultFormat()
The format used for serialization/deserialization by default by this ContentHandler.
Definition: ContentHandler.php:557
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:842
ContentHandler\cleanupHandlersCache
static cleanupHandlersCache()
Clean up handlers cache.
Definition: ContentHandler.php:342
ContentHandler\getLocalizedName
static getLocalizedName( $name, Language $lang=null)
Returns the localized name for a given content model.
Definition: ContentHandler.php:359
ContentHandler\getDiffEngineClass
getDiffEngineClass()
Returns the name of the diff engine to use.
Definition: ContentHandler.php:809
$refreshCache
null for the wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify as strings Extensions should add to this list prev or next $refreshCache
Definition: hooks.txt:1627
ContentHandler\supportsCategories
supportsCategories()
Returns true if this content model supports categories.
Definition: ContentHandler.php:1254
ParserOptions\newCanonical
static newCanonical( $context=null, $userLang=null)
Creates a "canonical" ParserOptions object.
Definition: ParserOptions.php:1061
ContentHandler\$mModelID
string $mModelID
Definition: ContentHandler.php:400
$handler
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
Definition: hooks.txt:813
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
ContentHandler\exportTransform
exportTransform( $blob, $format=null)
Applies transformations on export (returns the blob unchanged per default).
Definition: ContentHandler.php:443
Content
Base interface for content objects.
Definition: Content.php:34
ContentHandler\getFieldsForSearchIndex
getFieldsForSearchIndex(SearchEngine $engine)
Get fields definition for search index.
Definition: ContentHandler.php:1299
EDIT_NEW
const EDIT_NEW
Definition: Defines.php:152
text
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second as well as the first line of the second redirect text
Definition: All_system_messages.txt:1267
$unhide
null for the wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify as strings Extensions should add to this list prev or next refreshes the diff cache $unhide
Definition: hooks.txt:1627
Title
Represents a title within MediaWiki.
Definition: Title.php:39
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:501
ContentHandler\checkFormat
checkFormat( $format)
Convenient for checking whether a format provided as a parameter is actually supported.
Definition: ContentHandler.php:589
ContentHandler\getContentText
static getContentText(Content $content=null)
Convenience function for getting flat text from a Content object.
Definition: ContentHandler.php:83
$cache
$cache
Definition: mcc.php:33
$rev
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition: hooks.txt:1808
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ContentHandler\$handlers
static array $handlers
A Cache of ContentHandler instances by model id.
Definition: ContentHandler.php:269
DifferenceEngineSlotDiffRenderer
B/C adapter for turning a DifferenceEngine into a SlotDiffRenderer.
Definition: DifferenceEngineSlotDiffRenderer.php:31
ParserCache
Definition: ParserCache.php:30
NS_USER
const NS_USER
Definition: Defines.php:66
LoggerFactory
MediaWiki Logger LoggerFactory implements a PSR[0] compatible message logging system Named Psr Log LoggerInterface instances can be obtained from the MediaWiki Logger LoggerFactory::getInstance() static method. MediaWiki\Logger\LoggerFactory expects a class implementing the MediaWiki\Logger\Spi interface to act as a factory for new Psr\Log\LoggerInterface instances. The "Spi" in MediaWiki\Logger\Spi stands for "service provider interface". An SPI is an API intended to be implemented or extended by a third party. This software design pattern is intended to enable framework extension and replaceable components. It is specifically used in the MediaWiki\Logger\LoggerFactory service to allow alternate PSR-3 logging implementations to be easily integrated with MediaWiki. The service provider interface allows the backend logging library to be implemented in multiple ways. The $wgMWLoggerDefaultSpi global provides the classname of the default MediaWiki\Logger\Spi implementation to be loaded at runtime. This can either be the name of a class implementing the MediaWiki\Logger\Spi with a zero argument const ructor or a callable that will return an MediaWiki\Logger\Spi instance. Alternately the MediaWiki\Logger\LoggerFactory MediaWiki Logger LoggerFactory
Definition: logger.txt:5
$content
$content
Definition: pageupdater.txt:72
MWUnknownContentModelException
Exception thrown when an unregistered content model is requested.
Definition: MWUnknownContentModelException.php:10
ContentHandler\addSearchField
addSearchField(&$fields, SearchEngine $engine, $name, $type)
Add new field definition to array.
Definition: ContentHandler.php:1339
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:214
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:72
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
CONTENT_MODEL_JAVASCRIPT
const CONTENT_MODEL_JAVASCRIPT
Definition: Defines.php:236
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
ContentHandler\__construct
__construct( $modelId, $formats)
Constructor, initializing the ContentHandler instance with its model ID and a list of supported forma...
Definition: ContentHandler.php:416
CONTENT_MODEL_TEXT
const CONTENT_MODEL_TEXT
Definition: Defines.php:238
wfMessage
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
$ext
$ext
Definition: router.php:55
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:1144
ContentHandler\getSupportedFormats
getSupportedFormats()
Returns a list of serialization formats supported by the serializeContent() and unserializeContent() ...
Definition: ContentHandler.php:542
Language
Internationalisation code.
Definition: Language.php:35
ContentHandler\getModelID
getModelID()
Returns the model id that identifies the content model this ContentHandler can handle.
Definition: ContentHandler.php:513
ContentHandler\getForContent
static getForContent(Content $content)
Returns the appropriate ContentHandler singleton for the given Content object.
Definition: ContentHandler.php:260
ContentHandler\getParserOutputForIndexing
getParserOutputForIndexing(WikiPage $page, ParserCache $cache=null)
Produce page output suitable for indexing.
Definition: ContentHandler.php:1392
TextSlotDiffRenderer
Renders a slot diff by doing a text diff on the native representation.
Definition: TextSlotDiffRenderer.php:37
SearchIndexField\INDEX_TYPE_TEXT
const INDEX_TYPE_TEXT
Field types.
Definition: SearchIndexField.php:10
$type
$type
Definition: testCompression.php:48