Index: trunk/extensions/OAI/OAIRepo_body.php |
— | — | @@ -35,6 +35,44 @@ |
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 | + |
39 | 77 | $repo = new OAIRepo( $wgRequest ); |
40 | 78 | $repo->respond(); |
41 | 79 | } |
— | — | @@ -562,27 +600,25 @@ |
563 | 601 | function fetchRecord( $pageid ) { |
564 | 602 | extract( $this->_db->tableNames( 'updates', 'cur', 'page', 'revision', 'text' ) ); |
565 | 603 | if( $this->newSchema() ) { |
566 | | - $sql = "SELECT page_id,up_timestamp,up_action,up_sequence, |
567 | | - page_namespace, |
568 | | - page_title, |
569 | | - old_text, |
570 | | - old_flags, |
571 | | - rev_id, |
572 | | - rev_delete, |
573 | | - rev_comment, |
574 | | - rev_user, |
575 | | - rev_user_text, |
576 | | - rev_timestamp, |
577 | | - page_restrictions, |
578 | | - rev_minor_edit |
| 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 |
579 | 615 | FROM $updates,$page,$revision,$text |
580 | 616 | WHERE up_page=" . IntVal( $pageid ) . ' |
581 | 617 | AND page_id=up_page |
582 | 618 | AND page_latest=rev_id |
583 | 619 | AND rev_text_id=old_id |
584 | 620 | LIMIT 1'; |
585 | | - } else { // FIXME: this will work only with dublin core? |
586 | | - $sql = "SELECT page_id,up_timestamp,up_action,up_sequence, |
| 621 | + } else { |
| 622 | + $sql = "SELECT up_page,up_timestamp,up_action,up_sequence, |
587 | 623 | cur_namespace AS namespace, |
588 | 624 | cur_title AS title, |
589 | 625 | cur_text AS text, |
— | — | @@ -606,25 +642,23 @@ |
607 | 643 | $chunk = IntVal( $chunk ); |
608 | 644 | |
609 | 645 | if( $this->newSchema() ) { |
610 | | - $sql = "SELECT page_id,up_timestamp,up_action,up_sequence, |
611 | | - page_namespace, |
612 | | - page_title, |
613 | | - old_text, |
614 | | - old_flags, |
615 | | - rev_id, |
616 | | - rev_deleted, |
617 | | - rev_comment, |
618 | | - rev_user, |
619 | | - rev_user_text, |
620 | | - rev_timestamp, |
621 | | - page_restrictions, |
622 | | - rev_minor_edit |
| 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 |
623 | 657 | FROM $updates |
624 | 658 | LEFT JOIN $page ON page_id=up_page |
625 | 659 | LEFT JOIN $revision ON page_latest=rev_id |
626 | 660 | LEFT JOIN $text ON rev_text_id=old_id "; |
627 | | - } else { // FIXME: this will only work with dublin core? |
628 | | - $sql = "SELECT page_id,up_timestamp,up_action,up_sequence, |
| 661 | + } else { |
| 662 | + $sql = "SELECT up_page,up_timestamp,up_action,up_sequence, |
629 | 663 | cur_namespace AS namespace, |
630 | 664 | cur_title AS title, |
631 | 665 | cur_text AS text, |
— | — | @@ -761,9 +795,9 @@ |
762 | 796 | * @param object $row database row |
763 | 797 | */ |
764 | 798 | function WikiOAIRecord( $row ) { |
765 | | - $this->_id = $row->page_id; |
| 799 | + $this->_id = $row->up_page; |
766 | 800 | $this->_timestamp = $row->up_timestamp; |
767 | | - $this->_deleted = is_null( $row->page_title ); |
| 801 | + $this->_deleted = is_null( $row->title ); |
768 | 802 | $this->_row = $row; |
769 | 803 | } |
770 | 804 | |
— | — | @@ -794,9 +828,8 @@ |
795 | 829 | return "<metadata>\n$data</metadata>\n"; |
796 | 830 | } |
797 | 831 | |
798 | | - /** FIXME: seems to be completely broken (at least on MW 1.5+) */ |
799 | 832 | function renderDublinCore() { |
800 | | - $title = Title::makeTitle( $this->_row->page_namespace, $this->_row->page_title ); |
| 833 | + $title = Title::makeTitle( $this->_row->namespace, $this->_row->title ); |
801 | 834 | global $wgMimeType, $wgContLanguageCode; |
802 | 835 | |
803 | 836 | $out = oaiTag( 'oai_dc:dc', array( |
— | — | @@ -818,34 +851,72 @@ |
819 | 852 | } |
820 | 853 | |
821 | 854 | function renderMediaWiki() { |
822 | | - $writer = new OAIDumpWriter(); |
823 | | - |
824 | | - return $writer->openStream().$writer->openPage($this->_row). |
825 | | - $writer->writeRevision($this->_row).$writer->closePage().$writer->closeStream(); |
826 | | - } |
827 | | -} |
828 | | - |
829 | | -/** For the very first page output siteinfo, else same sa XmlDumpWriter */ |
830 | | -class OAIDumpWriter extends XmlDumpWriter { |
831 | | - static $isFirst = true; |
832 | | - |
833 | | - function siteInfo() { |
834 | | - if(OAIDumpWriter::$isFirst){ |
835 | | - $info = array( |
836 | | - $this->sitename(), |
837 | | - $this->homelink(), |
838 | | - $this->generator(), |
839 | | - $this->caseSetting(), |
840 | | - $this->namespaces() ); |
841 | | - OAIDumpWriter::$isFirst = false; |
842 | | - |
843 | | - return " <siteinfo>\n " . |
844 | | - implode( "\n ", $info ) . |
845 | | - "\n </siteinfo>\n"; |
846 | | - } else |
847 | | - return ""; |
| 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 ); |
| 871 | + if( $title->getNamespace() == NS_IMAGE ) { |
| 872 | + $out .= $this->renderUpload(); |
| 873 | + } |
| 874 | + $out .= "</page>\n"; |
| 875 | + $out .= "</mediawiki>\n"; |
| 876 | + return $out; |
848 | 877 | } |
849 | | - |
| 878 | + |
| 879 | + function renderUpload() { |
| 880 | + $fname = 'WikiOAIRecord::renderUpload'; |
| 881 | + $db =& wfGetDB( DB_SLAVE ); |
| 882 | + $imageRow = $db->selectRow( 'image', |
| 883 | + array( 'img_name', 'img_size', 'img_description', |
| 884 | + 'img_user', 'img_user_text', 'img_timestamp' ), |
| 885 | + array( 'img_name' => $this->_row->title ), |
| 886 | + $fname ); |
| 887 | + if( $imageRow ) { |
| 888 | + if( OAIRepo::newSchema() ) { |
| 889 | + $url = Image::imageUrl( $imageRow->img_name ); |
| 890 | + } else { |
| 891 | + $url = Image::wfImageUrl( $imageRow->img_name ); |
| 892 | + } |
| 893 | + if( $url{0} == '/' ) { |
| 894 | + global $wgServer; |
| 895 | + $url = $wgServer . $url; |
| 896 | + } |
| 897 | + return implode( "\n", array( |
| 898 | + "<upload>", |
| 899 | + oaiTag( 'timestamp', array(), wfTimestamp2ISO8601( $imageRow->img_timestamp ) ), |
| 900 | + $this->renderContributor( $imageRow->img_user, $imageRow->img_user_text ), |
| 901 | + oaiTag( 'comment', array(), $imageRow->img_description ), |
| 902 | + oaiTag( 'filename', array(), $imageRow->img_name ), |
| 903 | + oaiTag( 'src', array(), $url ), |
| 904 | + oaiTag( 'size', array(), $imageRow->img_size ), |
| 905 | + "</upload>\n" ) ); |
| 906 | + } else { |
| 907 | + return ''; |
| 908 | + } |
| 909 | + } |
| 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>'; |
| 919 | + } |
| 920 | + |
850 | 921 | } |
851 | 922 | |
852 | 923 | ?> |