r52508 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r52507‎ | r52508 | r52509 >
Date:14:12, 28 June 2009
Author:thomasv
Status:deferred
Tags:
Comment:
use EditPage::attemptSave hook, to check page format and username
Modified paths:
  • /trunk/extensions/ProofreadPage/ProofreadPage.i18n.php (modified) (history)
  • /trunk/extensions/ProofreadPage/ProofreadPage.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ProofreadPage/ProofreadPage.i18n.php
@@ -16,6 +16,12 @@
1717 'proofreadpage_index_expected' => 'Error: index expected',
1818 'proofreadpage_nosuch_index' => 'Error: no such index',
1919 'proofreadpage_nosuch_file' => 'Error: no such file',
 20+ 'proofreadpage_badpage' => 'Wrong Format',
 21+ 'proofreadpage_badpagetext' => 'The format of the page you attempted to save is incorrect.',
 22+ 'proofreadpage_nologin' => 'Not logged in',
 23+ 'proofreadpage_nologintext' => 'You must be [[Special:UserLogin|logged in]] to modify the proofreading status of pages.',
 24+ 'proofreadpage_notallowed' => 'Not Allowed',
 25+ 'proofreadpage_notallowedtext' => 'Not Allowed.',
2026 'proofreadpage_number_expected' => 'Error: numeric value expected',
2127 'proofreadpage_interval_too_large'=> 'Error: interval too large',
2228 'proofreadpage_invalid_interval' => 'Error: invalid interval',
Index: trunk/extensions/ProofreadPage/ProofreadPage.php
@@ -11,6 +11,7 @@
1212 $wgHooks['BeforePageDisplay'][] = 'pr_beforePageDisplay';
1313 $wgHooks['GetLinkColours'][] = 'pr_getLinkColoursHook';
1414 $wgHooks['ImageOpenShowImageInlineBefore'][] = 'pr_imageMessage';
 15+$wgHooks['EditPage::attemptSave'][] = 'pr_attemptSave';
1516 $wgHooks['ArticleSaveComplete'][] = 'pr_articleSave';
1617 $wgHooks['EditFormPreloadText'][] = 'pr_preloadText';
1718
@@ -750,16 +751,82 @@
751752 return $out;
752753 }
753754
 755+/*
 756+ * Try to parse a page.
 757+ * Return quality status of the page and username of the proofreader
 758+ * Return -1 if the page cannot be parsed
 759+ */
 760+function pr_parse_page( $text ) {
 761+ $page_regexp = "/^<noinclude>\{\{PageQuality\|(0|1|2|3|4)\|(.*?)\}\}(.*?)<\/noinclude>(.*?)<noinclude>(.*?)<\/noinclude>$/s";
 762+ if( !preg_match( $page_regexp, $text, $m ) ) {
 763+ return array( -1, null );
 764+ }
 765+ return array( intval($m[1]), $m[2] );
 766+}
754767
755768
 769+/*
 770+ * Check the format of pages in "Page" namespace.
 771+ * Todo: check that listed pages are unique for pages in "Index" namespace.
 772+ */
756773
 774+function pr_attemptSave( $editpage ) {
 775+ global $wgOut, $wgUser;
 776+
 777+ wfLoadExtensionMessages( 'ProofreadPage' );
 778+ $page_namespace = preg_quote( wfMsgForContent( 'proofreadpage_namespace' ), '/' );
 779+ $index_namespace = preg_quote( wfMsgForContent( 'proofreadpage_index_namespace' ), '/' );
 780+
 781+ $title = $editpage->mTitle;
 782+ if ( ! preg_match( "/^$page_namespace:(.*)$/", $title->getPrefixedText() ) ) {
 783+ return true;
 784+ }
 785+
 786+ //$text = $editpage->mArticle->replaceSection( $editpage->section, $editpage->textbox1, $editpage->summary, $editpage->edittime );
 787+ $text = $editpage->textbox1;
 788+
 789+ //parse the page
 790+ list( $q , $username ) = pr_parse_page( $text );
 791+ if( $q == -1 ) {
 792+ $wgOut->showErrorPage( 'proofreadpage_badpage', 'proofreadpage_badpagetext' );
 793+ return false;
 794+ }
 795+ //read previous revision, so that I know how much I need to add to pr_index
 796+ $rev = Revision::newFromTitle( $title );
 797+ if( $rev ) {
 798+ $old_text = $rev->getText();
 799+ list( $old_q , $old_username ) = pr_parse_page( $old_text );
 800+ } else {
 801+ $old_q = -1;
 802+ }
 803+
 804+ //check usernames
 805+ if( $old_q != -1 ) {
 806+ if( ($old_q != $q) && $wgUser->isAnon() ) {
 807+ $wgOut->showErrorPage( 'proofreadpage_nologin', 'proofreadpage_nologintext' );
 808+ return false;
 809+ }
 810+ if ( $wgUser->getName() != $username ) {
 811+ $wgOut->showErrorPage( 'proofreadpage_notallowed', 'proofreadpage_notallowedtext' );
 812+ return false;
 813+ }
 814+ if( ( ($q == 4) && ($old_q < 3) ) || ( ($q == 4) && ($old_q == 3) && ($old_username == $username) ) ) {
 815+ $wgOut->showErrorPage( 'proofreadpage_notallowed', 'proofreadpage_notallowedtext' );
 816+ return false;
 817+ }
 818+ }
 819+ else {
 820+ $old_q = 1;
 821+ }
 822+ return true;
 823+}
 824+
 825+
757826 /* update coloured links in index pages */
758827 function pr_articleSave( $article ) {
759828
760829 wfLoadExtensionMessages( 'ProofreadPage' );
761830 $page_namespace = preg_quote( wfMsgForContent( 'proofreadpage_namespace' ), '/' );
762 - $index_namespace = preg_quote( wfMsgForContent( 'proofreadpage_namespace' ), '/' );
763 -
764831 $title = $article->mTitle;
765832
766833 if ( preg_match( "/^$page_namespace:(.*)$/", $title->getPrefixedText() ) ) {
@@ -767,7 +834,7 @@
768835 pr_load_index( $title );
769836 }
770837 if ( $title->pr_index_title ) {
771 - $index_title = Title::makeTitleSafe( $index_namespace, $title->pr_index_title );
 838+ $index_title = Title::newFromText( $title->pr_index_title );
772839 $index_title->invalidateCache();
773840 }
774841 }

Status & tagging log