Index: trunk/extensions/OAI/OAIRepo_body.php |
— | — | @@ -35,44 +35,6 @@ |
36 | 36 | # FIXME: Replace the DB error handler |
37 | 37 | header( 'Content-type: text/xml; charset=utf-8' ); |
38 | 38 | |
39 | | - require_once( 'includes/SpecialExport.php' ); |
40 | | - |
41 | | - if( !function_exists( 'revision2xml' ) ) { |
42 | | - // Quick hack for 1.5 compatibility. |
43 | | - // We'll improve this later to use the new |
44 | | - // native interface... |
45 | | - function revision2xml( $s, $full, $cur ) { |
46 | | - $fname = 'revision2xml'; |
47 | | - wfProfileIn( $fname ); |
48 | | - |
49 | | - $ts = wfTimestamp2ISO8601( $s->timestamp ); |
50 | | - $xml = " <revision>\n"; |
51 | | - if($full && !$cur) |
52 | | - $xml .= " <id>$s->id</id>\n"; |
53 | | - $xml .= " <timestamp>$ts</timestamp>\n"; |
54 | | - if($s->user) { |
55 | | - $u = "<username>" . xmlsafe( $s->user_text ) . "</username>"; |
56 | | - if($full) |
57 | | - $u .= "<id>$s->user</id>"; |
58 | | - } else { |
59 | | - $u = "<ip>" . xmlsafe( $s->user_text ) . "</ip>"; |
60 | | - } |
61 | | - $xml .= " <contributor>$u</contributor>\n"; |
62 | | - if( $s->minor_edit ) { |
63 | | - $xml .= " <minor/>\n"; |
64 | | - } |
65 | | - if($s->comment != "") { |
66 | | - $c = xmlsafe( $s->comment ); |
67 | | - $xml .= " <comment>$c</comment>\n"; |
68 | | - } |
69 | | - $t = xmlsafe( Revision::getRevisionText( $s, "" ) ); |
70 | | - $xml .= " <text>$t</text>\n"; |
71 | | - $xml .= " </revision>\n"; |
72 | | - wfProfileOut( $fname ); |
73 | | - return $xml; |
74 | | - } |
75 | | - } |
76 | | - |
77 | 39 | $repo = new OAIRepo( $wgRequest ); |
78 | 40 | $repo->respond(); |
79 | 41 | } |
— | — | @@ -497,6 +459,8 @@ |
498 | 460 | } |
499 | 461 | } |
500 | 462 | |
| 463 | + $writer = new OAIDumpWriter(); |
| 464 | + |
501 | 465 | # Fetch one extra row to check if we need a resumptionToken |
502 | 466 | $resultSet = $this->fetchRows( $from, $until, $this->chunkSize() + 1, $resume ); |
503 | 467 | $count = min( $resultSet->numRows(), $this->chunkSize() ); |
— | — | @@ -505,7 +469,7 @@ |
506 | 470 | $this->_lastSequence = null; |
507 | 471 | for( $i = 0; $i < $count; $i++ ) { |
508 | 472 | $row = $resultSet->fetchObject(); |
509 | | - $item = new WikiOAIRecord( $row ); |
| 473 | + $item = new WikiOAIRecord( $row, $writer ); |
510 | 474 | if( $withData ) { |
511 | 475 | echo $item->renderRecord( $metadataPrefix, $this->timeGranularity() ); |
512 | 476 | } else { |
— | — | @@ -529,11 +493,12 @@ |
530 | 494 | } |
531 | 495 | |
532 | 496 | function getRecord() { |
| 497 | + $writer = new OAIDumpWriter(); |
533 | 498 | $metadataPrefix = $this->validateMetadata( 'metadataPrefix' ); |
534 | 499 | if( !$this->errorCondition() ) { |
535 | 500 | $row = $this->getRecordItem( $this->_request['identifier'] ); |
536 | 501 | if( !$this->errorCondition() ) { |
537 | | - $item = new WikiOAIRecord( $row ); |
| 502 | + $item = new WikiOAIRecord( $row, $writer ); |
538 | 503 | echo "<GetRecord>\n"; |
539 | 504 | echo $item->renderRecord( $metadataPrefix, $this->timeGranularity() ); |
540 | 505 | echo "</GetRecord>\n"; |
— | — | @@ -600,25 +565,27 @@ |
601 | 566 | function fetchRecord( $pageid ) { |
602 | 567 | extract( $this->_db->tableNames( 'updates', 'cur', 'page', 'revision', 'text' ) ); |
603 | 568 | if( $this->newSchema() ) { |
604 | | - $sql = "SELECT up_page,up_timestamp,up_action,up_sequence, |
605 | | - page_namespace AS namespace, |
606 | | - page_title AS title, |
607 | | - old_text AS text, |
608 | | - old_flags AS flags, |
609 | | - rev_comment AS comment, |
610 | | - rev_user AS user, |
611 | | - rev_user_text AS user_text, |
612 | | - rev_timestamp AS timestamp, |
613 | | - page_restrictions AS restrictions, |
614 | | - rev_minor_edit AS minor_edit |
| 569 | + $sql = "SELECT page_id,up_timestamp,up_action,up_sequence, |
| 570 | + page_namespace, |
| 571 | + page_title, |
| 572 | + old_text, |
| 573 | + old_flags, |
| 574 | + rev_id, |
| 575 | + rev_delete, |
| 576 | + rev_comment, |
| 577 | + rev_user, |
| 578 | + rev_user_text, |
| 579 | + rev_timestamp, |
| 580 | + page_restrictions, |
| 581 | + rev_minor_edit |
615 | 582 | FROM $updates,$page,$revision,$text |
616 | 583 | WHERE up_page=" . IntVal( $pageid ) . ' |
617 | 584 | AND page_id=up_page |
618 | 585 | AND page_latest=rev_id |
619 | 586 | AND rev_text_id=old_id |
620 | 587 | LIMIT 1'; |
621 | | - } else { |
622 | | - $sql = "SELECT up_page,up_timestamp,up_action,up_sequence, |
| 588 | + } else { // FIXME: this will work only with dublin core? |
| 589 | + $sql = "SELECT page_id,up_timestamp,up_action,up_sequence, |
623 | 590 | cur_namespace AS namespace, |
624 | 591 | cur_title AS title, |
625 | 592 | cur_text AS text, |
— | — | @@ -642,23 +609,25 @@ |
643 | 610 | $chunk = IntVal( $chunk ); |
644 | 611 | |
645 | 612 | if( $this->newSchema() ) { |
646 | | - $sql = "SELECT up_page,up_timestamp,up_action,up_sequence, |
647 | | - page_namespace AS namespace, |
648 | | - page_title AS title, |
649 | | - old_text AS text, |
650 | | - old_flags AS flags, |
651 | | - rev_comment AS comment, |
652 | | - rev_user AS user, |
653 | | - rev_user_text AS user_text, |
654 | | - rev_timestamp AS timestamp, |
655 | | - page_restrictions AS restrictions, |
656 | | - rev_minor_edit AS minor_edit |
| 613 | + $sql = "SELECT page_id,up_timestamp,up_action,up_sequence, |
| 614 | + page_namespace, |
| 615 | + page_title, |
| 616 | + old_text, |
| 617 | + old_flags, |
| 618 | + rev_id, |
| 619 | + rev_deleted, |
| 620 | + rev_comment, |
| 621 | + rev_user, |
| 622 | + rev_user_text, |
| 623 | + rev_timestamp, |
| 624 | + page_restrictions, |
| 625 | + rev_minor_edit |
657 | 626 | FROM $updates |
658 | 627 | LEFT JOIN $page ON page_id=up_page |
659 | 628 | LEFT JOIN $revision ON page_latest=rev_id |
660 | 629 | LEFT JOIN $text ON rev_text_id=old_id "; |
661 | | - } else { |
662 | | - $sql = "SELECT up_page,up_timestamp,up_action,up_sequence, |
| 630 | + } else { // FIXME: this will only work with dublin core? |
| 631 | + $sql = "SELECT page_id,up_timestamp,up_action,up_sequence, |
663 | 632 | cur_namespace AS namespace, |
664 | 633 | cur_title AS title, |
665 | 634 | cur_text AS text, |
— | — | @@ -794,11 +763,12 @@ |
795 | 764 | /** |
796 | 765 | * @param object $row database row |
797 | 766 | */ |
798 | | - function WikiOAIRecord( $row ) { |
799 | | - $this->_id = $row->up_page; |
| 767 | + function WikiOAIRecord( $row, $writer ) { |
| 768 | + $this->_id = $row->page_id; |
800 | 769 | $this->_timestamp = $row->up_timestamp; |
801 | | - $this->_deleted = is_null( $row->title ); |
| 770 | + $this->_deleted = is_null( $row->page_title ); |
802 | 771 | $this->_row = $row; |
| 772 | + $this->_writer = $writer; |
803 | 773 | } |
804 | 774 | |
805 | 775 | function isDeleted() { |
— | — | @@ -828,8 +798,9 @@ |
829 | 799 | return "<metadata>\n$data</metadata>\n"; |
830 | 800 | } |
831 | 801 | |
| 802 | + /** FIXME: seems to be completely broken (at least on MW 1.5+) */ |
832 | 803 | function renderDublinCore() { |
833 | | - $title = Title::makeTitle( $this->_row->namespace, $this->_row->title ); |
| 804 | + $title = Title::makeTitle( $this->_row->page_namespace, $this->_row->page_title ); |
834 | 805 | global $wgMimeType, $wgContLanguageCode; |
835 | 806 | |
836 | 807 | $out = oaiTag( 'oai_dc:dc', array( |
— | — | @@ -851,37 +822,27 @@ |
852 | 823 | } |
853 | 824 | |
854 | 825 | function renderMediaWiki() { |
855 | | - global $wgContLanguageCode; |
856 | | - $title = Title::makeTitle( $this->_row->namespace, $this->_row->title ); |
857 | | - $out = oaiTag( 'mediawiki', array( |
858 | | - 'xmlns' => 'http://www.mediawiki.org/xml/export-0.2/', |
859 | | - 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', |
860 | | - 'xsi:schemaLocation' => 'http://www.mediawiki.org/xml/export-0.2/ ' . |
861 | | - 'http://www.mediawiki.org/xml/export-0.2.xsd', |
862 | | - 'version' => '0.2', |
863 | | - 'xml:lang' => $wgContLanguageCode ) ) . "\n"; |
864 | | - $out .= "<page>\n"; |
865 | | - $out .= oaiTag( 'title', array(), $title->getPrefixedText() ) . "\n"; |
866 | | - $out .= oaiTag( 'id', array(), $this->_id ) . "\n"; |
867 | | - if( $this->_row->restrictions ) { |
868 | | - $out .= oaiTag( 'restrictions', array(), $this->_row->restrictions ) . "\n"; |
869 | | - } |
870 | | - $out .= revision2xml( $this->_row, true, true ); |
| 826 | + $title = Title::makeTitle( $this->_row->page_namespace, $this->_row->page_title ); |
| 827 | + |
| 828 | + $out = $this->_writer->openStream().$this->_writer->openPage($this->_row). |
| 829 | + $this->_writer->writeRevision($this->_row); |
| 830 | + |
871 | 831 | if( $title->getNamespace() == NS_IMAGE ) { |
872 | 832 | $out .= $this->renderUpload(); |
873 | 833 | } |
874 | | - $out .= "</page>\n"; |
875 | | - $out .= "</mediawiki>\n"; |
| 834 | + |
| 835 | + $out .= $this->_writer->closePage().$this->_writer->closeStream(); |
| 836 | + |
876 | 837 | return $out; |
877 | | - } |
878 | | - |
| 838 | + } |
| 839 | + |
879 | 840 | function renderUpload() { |
880 | 841 | $fname = 'WikiOAIRecord::renderUpload'; |
881 | 842 | $db =& wfGetDB( DB_SLAVE ); |
882 | 843 | $imageRow = $db->selectRow( 'image', |
883 | 844 | array( 'img_name', 'img_size', 'img_description', |
884 | 845 | 'img_user', 'img_user_text', 'img_timestamp' ), |
885 | | - array( 'img_name' => $this->_row->title ), |
| 846 | + array( 'img_name' => $this->_row->page_title ), |
886 | 847 | $fname ); |
887 | 848 | if( $imageRow ) { |
888 | 849 | if( OAIRepo::newSchema() ) { |
— | — | @@ -906,17 +867,33 @@ |
907 | 868 | return ''; |
908 | 869 | } |
909 | 870 | } |
910 | | - |
911 | | - function renderContributor( $id, $text ) { |
912 | | - if( $id ) { |
913 | | - $tag = oaiTag( 'username', array(), $text ) . |
914 | | - oaiTag( 'id', array(), $id ); |
915 | | - } else { |
916 | | - $tag = oaiTag( 'ip', array(), $text ); |
917 | | - } |
918 | | - return '<contributor>' . $tag . '</contributor>'; |
| 871 | + |
| 872 | +} |
| 873 | + |
| 874 | +/** For the very first page output siteinfo, else same sa XmlDumpWriter */ |
| 875 | +class OAIDumpWriter extends XmlDumpWriter { |
| 876 | + |
| 877 | + function __construct(){ |
| 878 | + $this->isFirst = true; |
919 | 879 | } |
920 | | - |
| 880 | + |
| 881 | + function siteInfo() { |
| 882 | + if($this->isFirst){ |
| 883 | + $info = array( |
| 884 | + $this->sitename(), |
| 885 | + $this->homelink(), |
| 886 | + $this->generator(), |
| 887 | + $this->caseSetting(), |
| 888 | + $this->namespaces() ); |
| 889 | + $this->isFirst = false; |
| 890 | + |
| 891 | + return " <siteinfo>\n " . |
| 892 | + implode( "\n ", $info ) . |
| 893 | + "\n </siteinfo>\n"; |
| 894 | + } else |
| 895 | + return ""; |
| 896 | + } |
| 897 | + |
921 | 898 | } |
922 | 899 | |
923 | 900 | ?> |