r114453 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114452‎ | r114453 | r114454 >
Date:16:55, 23 March 2012
Author:daniel
Status:deferred
Tags:
Comment:
baby steps towards editing structured data
Modified paths:
  • /branches/Wikidata/phase3/includes/ContentHandler.php (modified) (history)
  • /branches/Wikidata/phase3/includes/EditPage.php (modified) (history)
  • /branches/Wikidata/phase3/includes/Revision.php (modified) (history)
  • /branches/Wikidata/phase3/includes/WikiPage.php (modified) (history)

Diff [purge]

Index: branches/Wikidata/phase3/includes/ContentHandler.php
@@ -304,84 +304,91 @@
305305 public function getAutoDeleteReason( Title $title, &$hasHistory ) {
306306 global $wgContLang;
307307
308 - $dbw = wfGetDB( DB_MASTER );
 308+ try {
 309+ $dbw = wfGetDB( DB_MASTER );
309310
310 - // Get the last revision
311 - $rev = Revision::newFromTitle( $title );
 311+ // Get the last revision
 312+ $rev = Revision::newFromTitle( $title );
312313
313 - if ( is_null( $rev ) ) {
314 - return false;
315 - }
 314+ if ( is_null( $rev ) ) {
 315+ return false;
 316+ }
316317
317 - // Get the article's contents
318 - $content = $rev->getContent();
319 - $blank = false;
 318+ // Get the article's contents
 319+ $content = $rev->getContent();
 320+ $blank = false;
320321
321 - // If the page is blank, use the text from the previous revision,
322 - // which can only be blank if there's a move/import/protect dummy revision involved
323 - if ( $content->getSize() == 0 ) {
324 - $prev = $rev->getPrevious();
 322+ // If the page is blank, use the text from the previous revision,
 323+ // which can only be blank if there's a move/import/protect dummy revision involved
 324+ if ( $content->getSize() == 0 ) {
 325+ $prev = $rev->getPrevious();
325326
326 - if ( $prev ) {
327 - $content = $rev->getContent();
328 - $blank = true;
 327+ if ( $prev ) {
 328+ $content = $rev->getContent();
 329+ $blank = true;
 330+ }
329331 }
330 - }
331332
332 - // Find out if there was only one contributor
333 - // Only scan the last 20 revisions
334 - $res = $dbw->select( 'revision', 'rev_user_text',
335 - array( 'rev_page' => $title->getArticleID(), $dbw->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' ),
336 - __METHOD__,
337 - array( 'LIMIT' => 20 )
338 - );
 333+ // Find out if there was only one contributor
 334+ // Only scan the last 20 revisions
 335+ $res = $dbw->select( 'revision', 'rev_user_text',
 336+ array( 'rev_page' => $title->getArticleID(), $dbw->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' ),
 337+ __METHOD__,
 338+ array( 'LIMIT' => 20 )
 339+ );
339340
340 - if ( $res === false ) {
341 - // This page has no revisions, which is very weird
342 - return false;
343 - }
 341+ if ( $res === false ) {
 342+ // This page has no revisions, which is very weird
 343+ return false;
 344+ }
344345
345 - $hasHistory = ( $res->numRows() > 1 );
346 - $row = $dbw->fetchObject( $res );
 346+ $hasHistory = ( $res->numRows() > 1 );
 347+ $row = $dbw->fetchObject( $res );
347348
348 - if ( $row ) { // $row is false if the only contributor is hidden
349 - $onlyAuthor = $row->rev_user_text;
350 - // Try to find a second contributor
351 - foreach ( $res as $row ) {
352 - if ( $row->rev_user_text != $onlyAuthor ) { // Bug 22999
353 - $onlyAuthor = false;
354 - break;
 349+ if ( $row ) { // $row is false if the only contributor is hidden
 350+ $onlyAuthor = $row->rev_user_text;
 351+ // Try to find a second contributor
 352+ foreach ( $res as $row ) {
 353+ if ( $row->rev_user_text != $onlyAuthor ) { // Bug 22999
 354+ $onlyAuthor = false;
 355+ break;
 356+ }
355357 }
 358+ } else {
 359+ $onlyAuthor = false;
356360 }
357 - } else {
358 - $onlyAuthor = false;
359 - }
360361
361 - // Generate the summary with a '$1' placeholder
362 - if ( $blank ) {
363 - // The current revision is blank and the one before is also
364 - // blank. It's just not our lucky day
365 - $reason = wfMsgForContent( 'exbeforeblank', '$1' );
366 - } else {
367 - if ( $onlyAuthor ) {
368 - $reason = wfMsgForContent( 'excontentauthor', '$1', $onlyAuthor );
 362+ // Generate the summary with a '$1' placeholder
 363+ if ( $blank ) {
 364+ // The current revision is blank and the one before is also
 365+ // blank. It's just not our lucky day
 366+ $reason = wfMsgForContent( 'exbeforeblank', '$1' );
369367 } else {
370 - $reason = wfMsgForContent( 'excontent', '$1' );
 368+ if ( $onlyAuthor ) {
 369+ $reason = wfMsgForContent( 'excontentauthor', '$1', $onlyAuthor );
 370+ } else {
 371+ $reason = wfMsgForContent( 'excontent', '$1' );
 372+ }
371373 }
372 - }
373374
374 - if ( $reason == '-' ) {
375 - // Allow these UI messages to be blanked out cleanly
376 - return '';
377 - }
 375+ if ( $reason == '-' ) {
 376+ // Allow these UI messages to be blanked out cleanly
 377+ return '';
 378+ }
378379
379 - // Max content length = max comment length - length of the comment (excl. $1)
380 - $text = $content->getTextForSummary( 255 - ( strlen( $reason ) - 2 ) );
 380+ // Max content length = max comment length - length of the comment (excl. $1)
 381+ $text = $content->getTextForSummary( 255 - ( strlen( $reason ) - 2 ) );
381382
382 - // Now replace the '$1' placeholder
383 - $reason = str_replace( '$1', $text, $reason );
 383+ // Now replace the '$1' placeholder
 384+ $reason = str_replace( '$1', $text, $reason );
384385
385 - return $reason;
 386+ return $reason;
 387+ } catch (MWException $e) {
 388+ # if a page is horribly broken, we still want to be able to delete it. so be lenient about errors here.
 389+ wfDebug("Error while building auto delete summary: $e");
 390+ }
 391+
 392+ return '';
386393 }
387394
388395 /**
Index: branches/Wikidata/phase3/includes/EditPage.php
@@ -706,6 +706,9 @@
707707 $this->content_model = $request->getText( 'model', $content_handler->getModelName() ); #may be overridden by revision
708708 $this->content_format = $request->getText( 'format', $content_handler->getDefaultFormat() ); #may be overridden by revision
709709
 710+ #TODO: check if the desired model is allowed in this namespace, and if a transition from the page's current model to the new model is allowed
 711+ #TODO: check if the desired content model supports the given content format!
 712+
710713 $this->live = $request->getCheck( 'live' );
711714 $this->editintro = $request->getText( 'editintro',
712715 // Custom edit intro for new sections
@@ -920,7 +923,7 @@
921924 */
922925 private function getCurrentContent() {
923926 $rev = $this->mArticle->getRevision();
924 - $content = $rev->getContent( Revision::RAW );
 927+ $content = $rev ? $rev->getContent( Revision::RAW ) : null;
925928
926929 if ( $content === false || $content === null ) {
927930 if ( !$this->content_model ) $this->content_model = $this->getTitle()->getContentModelName();
@@ -1520,7 +1523,7 @@
15211524 ( ( $this->minoredit && !$this->isNew ) ? EDIT_MINOR : 0 ) |
15221525 ( $bot ? EDIT_FORCE_BOT : 0 );
15231526
1524 - $doEditStatus = $this->mArticle->doEditContent( $content, $this->summary, $flags );
 1527+ $doEditStatus = $this->mArticle->doEditContent( $content, $this->summary, $flags, false, null, $this->content_format );
15251528
15261529 if ( $doEditStatus->isOK() ) {
15271530 $result['redirect'] = $content->isRedirect();
@@ -1918,6 +1921,9 @@
19191922
19201923 $wgOut->addHTML( Html::hidden( 'oldid', $this->oldid ) );
19211924
 1925+ $wgOut->addHTML( Html::hidden( 'format', $this->content_format ) );
 1926+ $wgOut->addHTML( Html::hidden( 'model', $this->content_model ) );
 1927+
19221928 if ( $this->section == 'new' ) {
19231929 $this->showSummaryInput( true, $this->summary );
19241930 $wgOut->addHTML( $this->getSummaryPreview( true, $this->summary ) );
@@ -1948,6 +1954,9 @@
19491955
19501956 $wgOut->addWikiText( $this->getCopywarn() );
19511957
 1958+ $wgOut->addHTML( Html::element( 'p', null, "model: " . $this->content_model ) ); #FIXME: content handler debug stuff, DELETE!
 1959+ $wgOut->addHTML( Html::element( 'p', null, "format: " . $this->content_format ) ); #FIXME: content handler debug stuff, DELETE!
 1960+
19521961 $wgOut->addHTML( $this->editFormTextAfterWarn );
19531962
19541963 $this->showStandardInputs();
@@ -2692,8 +2701,7 @@
26932702 # don't parse non-wikitext pages, show message about preview
26942703 # XXX: stupid php bug won't let us use $this->getContextTitle()->isCssJsSubpage() here -- This note has been there since r3530. Sure the bug was fixed time ago?
26952704
2696 - #FIXME: get appropriate content handler!
2697 - if ( $this->isCssJsSubpage || !$this->mTitle->isWikitextPage() ) {
 2705+ if ( $this->isCssJsSubpage || $this->mTitle->isCssOrJsPage() ) { #TODO: kill all special case handling for CSS/JS content!
26982706 if( $this->mTitle->isCssJsSubpage() ) {
26992707 $level = 'user';
27002708 } elseif( $this->mTitle->isCssOrJsPage() ) {
@@ -2715,30 +2723,39 @@
27162724 } else {
27172725 throw new MWException( 'A CSS/JS (sub)page but which is not css nor js!' );
27182726 }
2719 - }
 2727+ } #FIXME: else $previewtext is undefined!
27202728
27212729 $parserOutput = $wgParser->parse( $previewtext, $this->mTitle, $parserOptions );
27222730 $previewHTML = $parserOutput->mText;
2723 - $previewHTML .= "<pre class=\"$class\" dir=\"ltr\">\n" . htmlspecialchars( $this->textbox1 ) . "\n</pre>\n";
 2731+ $previewHTML .= "<pre class=\"$class\" dir=\"ltr\">\n" . htmlspecialchars( $this->textbox1 ) . "\n</pre>\n"; #FIXME: use content object!
27242732 } else {
27252733 $rt = Title::newFromRedirectArray( $this->textbox1 );
27262734 if ( $rt ) {
27272735 $previewHTML = $this->mArticle->viewRedirect( $rt, false );
27282736 } else {
2729 - $toparse = $this->textbox1;
 2737+ $content = ContentHandler::makeContent( $this->textbox1, $this->getTitle(), $this->content_model, $this->content_format );
27302738
27312739 # If we're adding a comment, we need to show the
27322740 # summary as the headline
27332741 if ( $this->section == "new" && $this->summary != "" ) {
2734 - $toparse = wfMsgForContent( 'newsectionheaderdefaultlevel', $this->summary ) . "\n\n" . $toparse;
 2742+ $content = $content->addSectionHeader( $this->summary );
27352743 }
27362744
 2745+ $toparse_orig = $content->serialize( $this->content_format );
 2746+ $toparse = $toparse_orig;
27372747 wfRunHooks( 'EditPageGetPreviewText', array( $this, &$toparse ) );
27382748
 2749+ if ( $toparse !== $toparse_orig ) {
 2750+ #hook changed the text, create new Content object
 2751+ $content = ContentHandler::makeContent( $toparse, $this->getTitle(), $this->content_model, $this->content_format );
 2752+ }
 2753+
 2754+ wfRunHooks( 'EditPageGetPreviewContent', array( $this, &$content ) ); # FIXME: document new hook
 2755+
27392756 $parserOptions->enableLimitReport();
27402757
2741 - $toparse = $wgParser->preSaveTransform( $toparse, $this->mTitle, $wgUser, $parserOptions );
2742 - $parserOutput = $wgParser->parse( $toparse, $this->mTitle, $parserOptions );
 2758+ $content = $content->preSaveTransform( $this->mTitle, $wgUser, $parserOptions );
 2759+ $parserOutput = $content->getParserOutput( $this->mTitle, null, $parserOptions );
27432760
27442761 $previewHTML = $parserOutput->getText();
27452762 $this->mParserOutput = $parserOutput;
Index: branches/Wikidata/phase3/includes/Revision.php
@@ -479,14 +479,8 @@
480480 $this->mSha1 = isset( $row['sha1'] ) ? strval( $row['sha1'] ) : null;
481481
482482 $this->mContentModelName = isset( $row['content_model'] ) ? strval( $row['content_model'] ) : null;
483 - $this->mContentFormat = isset( $row['content_format'] ) ? strval( $row['content_format'] ) : null;
 483+ $this->mContentFormat = isset( $row['content_format'] ) ? strval( $row['content_format'] ) : null;
484484
485 - if( !isset( $row->rev_content_format ) || is_null( $row->rev_content_format ) ) {
486 - $this->mContentFormat = null; # determine on demand if needed
487 - } else {
488 - $this->mContentFormat = $row->rev_content_format;
489 - }
490 -
491485 // Enforce spacing trimming on supplied text
492486 $this->mComment = isset( $row['comment'] ) ? trim( strval( $row['comment'] ) ) : null;
493487 $this->mText = isset( $row['text'] ) ? rtrim( strval( $row['text'] ) ) : null;
@@ -843,7 +837,7 @@
844838 $this->mText = $this->loadText();
845839 }
846840
847 - $this->mContent = $handler->unserialize( $this->mText, $title, $format );
 841+ $this->mContent = is_null( $this->mText ) ? null : $handler->unserialize( $this->mText, $format );
848842 }
849843
850844 return $this->mContent;
@@ -1104,29 +1098,30 @@
11051099 $rev_id = isset( $this->mId )
11061100 ? $this->mId
11071101 : $dbw->nextSequenceValue( 'revision_rev_id_seq' );
1108 - $dbw->insert( 'revision',
1109 - array(
1110 - 'rev_id' => $rev_id,
1111 - 'rev_page' => $this->mPage,
1112 - 'rev_text_id' => $this->mTextId,
1113 - 'rev_comment' => $this->mComment,
1114 - 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
1115 - 'rev_user' => $this->mUser,
1116 - 'rev_user_text' => $this->mUserText,
1117 - 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
1118 - 'rev_deleted' => $this->mDeleted,
1119 - 'rev_len' => $this->mSize,
1120 - 'rev_parent_id' => is_null( $this->mParentId )
1121 - ? $this->getPreviousRevisionId( $dbw )
1122 - : $this->mParentId,
1123 - 'rev_sha1' => is_null( $this->mSha1 )
1124 - ? Revision::base36Sha1( $this->mText )
1125 - : $this->mSha1,
1126 - 'rev_content_model' => $this->getContentModelName(),
1127 - 'rev_content_format' => $this->getContentFormat(),
1128 - ), __METHOD__
1129 - );
11301102
 1103+ $row = array(
 1104+ 'rev_id' => $rev_id,
 1105+ 'rev_page' => $this->mPage,
 1106+ 'rev_text_id' => $this->mTextId,
 1107+ 'rev_comment' => $this->mComment,
 1108+ 'rev_minor_edit' => $this->mMinorEdit ? 1 : 0,
 1109+ 'rev_user' => $this->mUser,
 1110+ 'rev_user_text' => $this->mUserText,
 1111+ 'rev_timestamp' => $dbw->timestamp( $this->mTimestamp ),
 1112+ 'rev_deleted' => $this->mDeleted,
 1113+ 'rev_len' => $this->mSize,
 1114+ 'rev_parent_id' => is_null( $this->mParentId )
 1115+ ? $this->getPreviousRevisionId( $dbw )
 1116+ : $this->mParentId,
 1117+ 'rev_sha1' => is_null( $this->mSha1 )
 1118+ ? Revision::base36Sha1( $this->mText )
 1119+ : $this->mSha1,
 1120+ 'rev_content_model' => $this->getContentModelName(),
 1121+ 'rev_content_format' => $this->getContentFormat(),
 1122+ );
 1123+
 1124+ $dbw->insert( 'revision', $row, __METHOD__ );
 1125+
11311126 $this->mId = !is_null( $rev_id ) ? $rev_id : $dbw->insertId();
11321127
11331128 wfRunHooks( 'RevisionInsertComplete', array( &$this, $data, $flags ) );
Index: branches/Wikidata/phase3/includes/WikiPage.php
@@ -1397,7 +1397,9 @@
13981398 'parent_id' => $oldid,
13991399 'user' => $user->getId(),
14001400 'user_text' => $user->getName(),
1401 - 'timestamp' => $now
 1401+ 'timestamp' => $now,
 1402+ 'content_model' => $content->getModelName(),
 1403+ 'content_format' => $serialisation_format,
14021404 ) );
14031405
14041406 $changed = !$content->equals( $old_content );
@@ -1502,7 +1504,9 @@
15031505 'len' => $newsize,
15041506 'user' => $user->getId(),
15051507 'user_text' => $user->getName(),
1506 - 'timestamp' => $now
 1508+ 'timestamp' => $now,
 1509+ 'content_model' => $content->getModelName(),
 1510+ 'content_format' => $serialisation_format,
15071511 ) );
15081512 $revisionId = $revision->insertOn( $dbw );
15091513

Status & tagging log