r81193 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r81192‎ | r81193 | r81194 >
Date:18:54, 29 January 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Added support for making galleries based on property data
Modified paths:
  • /trunk/extensions/SemanticResultFormats/Gallery/SRF_Gallery.php (modified) (history)
  • /trunk/extensions/SemanticResultFormats/RELEASE-NOTES (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticResultFormats/Gallery/SRF_Gallery.php
@@ -31,22 +31,94 @@
3232 public function getResultText( $results, $outputmode )
3333 {
3434 global $smwgIQRunningNumber, $wgUser, $wgParser;
35 - $skin = $wgUser->getSkin();
3635
3736 $ig = new ImageGallery();
3837 $ig->setShowBytes( false );
3938 $ig->setShowFilename( false );
4039 $ig->setParser( $wgParser );
41 - $ig->useSkin( $skin );
 40+ $ig->useSkin( $wgUser->getSkin() );
4241 $ig->setCaption( $this->mIntro ); // set caption to IQ header
4342
44 - if ( isset( $this->m_params['perrow'] ) )
 43+ if ( isset( $this->m_params['perrow'] ) ) {
4544 $ig->setPerRow( $this->m_params['perrow'] );
46 - if ( isset( $this->m_params['widths'] ) )
 45+ }
 46+
 47+ if ( isset( $this->m_params['widths'] ) ) {
4748 $ig->setWidths( $this->m_params['widths'] );
48 - if ( isset( $this->m_params['heights'] ) )
 49+ }
 50+
 51+ if ( isset( $this->m_params['heights'] ) ) {
4952 $ig->setHeights( $this->m_params['heights'] );
 53+ }
 54+
 55+ $this->m_params['autocaptions'] = isset( $this->m_params['autocaptions'] ) ? $this->m_params['autocaptions'] != 'off' : true;
5056
 57+ $printReqLabels = array();
 58+
 59+ foreach ( $results->getPrintRequests() as $printReq ) {
 60+ $printReqLabels[] = $printReq->getLabel();
 61+ }
 62+
 63+ if ( isset( $this->m_params['imageproperty'] ) && in_array( $this->m_params['imageproperty'], $printReqLabels ) ) {
 64+ $captionProperty = isset( $this->m_params['captionproperty'] ) ? $this->m_params['captionproperty'] : '';
 65+ $this->addImageProperties( $results, $ig, $this->m_params['imageproperty'], $captionProperty );
 66+ }
 67+ else {
 68+ $this->addImagePages( $results, $ig );
 69+ }
 70+
 71+ return array( $ig->toHTML(), 'nowiki' => true, 'isHTML' => true );
 72+ }
 73+
 74+ /**
 75+ * Handles queries where the images (and optionally their captions) are specified as properties.
 76+ *
 77+ * @since 1.5.3
 78+ *
 79+ * @param SMWQueryResult $results
 80+ * @param ImageGallery $ig
 81+ * @param string $imageProperty
 82+ * @param string $captionProperty
 83+ */
 84+ protected function addImageProperties( SMWQueryResult $results, ImageGallery &$ig, $imageProperty, $captionProperty ) {
 85+ while ( /* array of SMWResultArray */ $row = $results->getNext() ) {
 86+ $images = array();
 87+ $captions = array();
 88+
 89+ for ( $i = 0, $n = count( $row ); $i < $n; $i++ ) {
 90+ if ( $row[$i]->getPrintRequest()->getLabel() == $imageProperty ) {
 91+ while ( ( $obj = $row[$i]->getNextObject() ) !== false ) {
 92+ if ( $obj->getTypeID() == '_wpg' ) {
 93+ $images[] = $obj->getTitle();
 94+ }
 95+ }
 96+ }
 97+ else if ( $row[$i]->getPrintRequest()->getLabel() == $captionProperty ) {
 98+ while ( ( $obj = $row[$i]->getNextObject() ) !== false ) {
 99+ $captions[] = $obj->getShortText( SMW_OUTPUT_HTML, $this->getLinker( true ) );
 100+ }
 101+ }
 102+ }
 103+
 104+ $amountMatches = count( $captions ) == count( $images );
 105+ $hasCaption = $amountMatches || count( $captions ) > 0;
 106+
 107+ foreach ( $images as $imgTitle ) {
 108+ $imgCaption= $hasCaption ? ( $amountMatches ? array_shift( $captions ) : $captions[0] ) : '';
 109+ $this->addImageToGallery( $ig, $imgTitle, $imgCaption );
 110+ }
 111+ }
 112+ }
 113+
 114+ /**
 115+ * Handles queries where the result objects are image pages.
 116+ *
 117+ * @since 1.5.3
 118+ *
 119+ * @param SMWQueryResult $results
 120+ * @param ImageGallery $ig
 121+ */
 122+ protected function addImagePages( SMWQueryResult $results, ImageGallery &$ig ) {
51123 while ( $row = $results->getNext() ) {
52124 $firstField = $row[0];
53125 $imgTitle = $firstField->getNextObject()->getTitle();
@@ -57,23 +129,39 @@
58130 $imgCaption = $row[1]->getNextObject();
59131 if ( is_object( $imgCaption ) ) {
60132 $imgCaption = $imgCaption->getShortText( SMW_OUTPUT_HTML, $this->getLinker( true ) );
61 - $imgCaption = $wgParser->recursiveTagParse( $imgCaption );
62133 }
63134 }
64135
65 - if ( empty( $imgCaption ) ) {
66 - $imgCaption = $imgTitle->getBaseText();
67 - $imgCaption = preg_replace( '#\.[^.]+$#', '', $imgCaption ); // Remove image extension
68 - }
 136+ $this->addImageToGallery( $ig, $imgTitle, $imgCaption );
 137+ }
 138+ }
 139+
 140+ /**
 141+ * Adds a single image to the gallery.
 142+ * Takes care of automatically adding a caption when none is provided and parsing it's wikitext.
 143+ *
 144+ * @since 1.5.3
 145+ *
 146+ * @param ImageGallery $ig The gallery to add the image to
 147+ * @param Title $imgTitle The title object of the page of the image
 148+ * @param string $imgCaption An optional caption for the image
 149+ */
 150+ protected function addImageToGallery( ImageGallery &$ig, Title $imgTitle, $imgCaption ) {
 151+ global $wgParser;
 152+
 153+ if ( empty( $imgCaption ) ) {
 154+ $imgCaption = $this->m_params['autocaptions'] ? preg_replace( '#\.[^.]+$#', '', $imgTitle->getBaseText() ) : '';
 155+ }
 156+ else {
 157+ $imgCaption = $wgParser->recursiveTagParse( $imgCaption );
 158+ }
69159
70 - $ig->add( $imgTitle, $imgCaption );
 160+ $ig->add( $imgTitle, $imgCaption );
71161
72 - // Only add real images (bug #5586)
73 - if ( $imgTitle->getNamespace() == NS_IMAGE ) {
74 - $wgParser->mOutput->addImage( $imgTitle->getDBkey() );
75 - }
 162+ // Only add real images (bug #5586)
 163+ if ( $imgTitle->getNamespace() == NS_IMAGE ) {
 164+ $wgParser->mOutput->addImage( $imgTitle->getDBkey() );
76165 }
77 -
78 - return array( $ig->toHTML(), 'nowiki' => true, 'isHTML' => true );
79166 }
 167+
80168 }
Index: trunk/extensions/SemanticResultFormats/RELEASE-NOTES
@@ -2,6 +2,11 @@
33 http://semantic-mediawiki.org/wiki/Semantic_Result_Formats
44
55
 6+== SRF 1.5.3 ==
 7+
 8+Changes in this version:
 9+* Support for images specified by properties in the gallery format.
 10+
611 == SRF 1.5.2 ==
712
813 Released on January 11 2011.

Follow-up revisions

RevisionCommit summaryAuthorDate
r81194Follow up to r81193jeroendedauw18:57, 29 January 2011

Status & tagging log