PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/ImageTagging/ImageTagging_body.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 291 lines | 235 code | 26 blank | 30 comment | 14 complexity | 5f880bf36776c47b93771402e2d88c74 MD5 | raw file
  1. <?php
  2. /**
  3. * TaggedImages class
  4. *
  5. * @file
  6. * @ingroup Extensions
  7. */
  8. define( 'TAGGEDIMGS_PER_PAGE', 12 );
  9. /**
  10. * Photos tagged gallery
  11. *
  12. * Add images to the gallery using add(), then render that list to HTML using toHTML().
  13. */
  14. class TaggedImages extends SpecialPage {
  15. var $mQuery, $mImages, $mShowFilename;
  16. /**
  17. * Create a new tagged images object.
  18. */
  19. public function __construct() {
  20. global $wgRequest, $wgOut;
  21. $wgOut->addExtensionStyle( $wgScriptPath . '/extensions/ImageTagging/img_tagging.css' );
  22. $this->mQuery = preg_replace( "/[\"'<>]/", '', $wgRequest->getText( 'q' ) );
  23. $this->mStartPage = preg_replace( "/[\"'<>]/", '', $wgRequest->getVal( 'page' ) );
  24. $this->mCount = 0;
  25. if ( !$this->mStartPage ) {
  26. $this->mStartPage = 0;
  27. }
  28. $this->mImages = array();
  29. parent::__construct( 'TaggedImages' );
  30. }
  31. /**
  32. * Start doing stuff
  33. *
  34. * @param $par Mixed: parameter passed to the special page or null
  35. */
  36. public function execute( $par ) {
  37. global $wgOut;
  38. wfProfileIn( __METHOD__ );
  39. $dbr = wfGetDB( DB_SLAVE );
  40. $where = array();
  41. if ( $this->mQuery ) {
  42. $where = array(
  43. 'article_tag' => $this->mQuery
  44. );
  45. }
  46. $foo = $dbr->select(
  47. 'imagetags',
  48. 'img_name',
  49. $where,
  50. __METHOD__
  51. );
  52. $imageNames = array();
  53. foreach ( $foo as $omg ) {
  54. $imageNames[] = $omg;
  55. }
  56. $imageNamesString = implode( ',', $imageNames ); // @todo CHECKME
  57. $res = $dbr->select(
  58. 'image',
  59. array( 'img_name', 'img_timestamp' ),
  60. array( "img_name IN $imageNamesString" ),
  61. __METHOD__,
  62. array(
  63. 'ORDER BY' => 'img_timestamp DESC',
  64. 'LIMIT' => TAGGEDIMGS_PER_PAGE,
  65. 'OFFSET' => $this->mStartPage * TAGGEDIMGS_PER_PAGE
  66. )
  67. );
  68. foreach( $res as $o ) {
  69. $img = wfFindFile( $o->img_name );
  70. $this->add( $img, '' );
  71. }
  72. $res = $dbr->select(
  73. 'imagetags',
  74. 'COUNT(img_name) AS img_count',
  75. $where,
  76. __METHOD__,
  77. array( 'GROUP BY' => 'article_tag' )
  78. );
  79. $o = $dbr->fetchObject( $res );
  80. if ( $o ) {
  81. $this->mCount = $o->img_count;
  82. }
  83. $wgOut->setPageTitle( wfMsg( 'imagetagging-taggedimages-title', $this->mQuery ? $this->mQuery : 'all' ) );
  84. $wgOut->setRobotPolicy( 'noindex,nofollow' );
  85. $wgOut->addHTML( $this->toHTML() );
  86. wfProfileOut( __METHOD__ );
  87. }
  88. /**
  89. * Add an image to the gallery.
  90. *
  91. * @param $image Object: Image object that is added to the gallery
  92. * @param $html String: additional HTML text to be shown. The name and size
  93. * of the image are always shown.
  94. */
  95. function add( $image, $html = '' ) {
  96. $this->mImages[] = array( &$image, $html );
  97. }
  98. /**
  99. * Add an image at the beginning of the gallery.
  100. *
  101. * @param $image Object: Image object that is added to the gallery
  102. * @param $html String: additional HTML text to be shown. The name and size
  103. * of the image are always shown.
  104. */
  105. function insert( $image, $html = '' ) {
  106. array_unshift( $this->mImages, array( &$image, $html ) );
  107. }
  108. /**
  109. * @return Boolean: true if the gallery contains no images
  110. */
  111. function isEmpty() {
  112. return empty( $this->mImages );
  113. }
  114. function pagerStatusHTML() {
  115. wfProfileIn( __METHOD__ );
  116. $numPages = $this->mCount / TAGGEDIMGS_PER_PAGE;
  117. if ( !$this->mQuery ) {
  118. $this->mQuery = 'all';
  119. }
  120. $queryTitle = Title::newFromText( $this->mQuery, NS_MAIN );
  121. $html = wfMsg(
  122. 'imagetagging-taggedimages-displaying',
  123. $this->mStartPage * TAGGEDIMGS_PER_PAGE + 1,
  124. min( ( $this->mStartPage + 1 ) * TAGGEDIMGS_PER_PAGE, $this->mCount ),
  125. $this->mCount,
  126. '<a href="' . $queryTitle->getLocalURL() . '">' . $this->mQuery . '</a>'
  127. );
  128. wfProfileOut( __METHOD__ );
  129. return $html;
  130. }
  131. function pageNoLink( $pageNum, $pageText, $hrefPrefix, $cur ) {
  132. if ( $cur == false ) {
  133. $html = '<a href="'. $hrefPrefix . $pageNum . '">' . $pageText .
  134. '</a>';
  135. } else {
  136. $html = '<b>' . $pageText . '</b>';
  137. }
  138. $html .= '&#160;';
  139. return $html;
  140. }
  141. function pagerHTML( $topBottom ) {
  142. global $wgOut;
  143. $titleObj = SpecialPage::getTitleFor( 'TaggedImages' );
  144. $maxPages = 5; // 5 real pagers
  145. $numPages = ceil( $this->mCount / TAGGEDIMGS_PER_PAGE );
  146. if ( $numPages <= 1 ) {
  147. return '';
  148. }
  149. $html = "<span id=\"{$topBottom}pager\" class=\"pager\" style=\"float: right; text-align: right; right: 30px;\">";
  150. $hrefPrefix = $titleObj->escapeLocalURL(
  151. 'q=' . $this->mQuery . '&page='
  152. );
  153. // build prev button
  154. if ( $this->mStartPage - 1 >= 0 ) {
  155. $html .= $this->pageNoLink(
  156. $this->mStartPage - 1,
  157. wfMsg( 'allpagesprev' ),
  158. $hrefPrefix,
  159. false
  160. );
  161. }
  162. // build page # buttons
  163. for ( $i = $this->mStartPage - 2; $i < $this->mStartPage + $maxPages; $i++ ) {
  164. if ( $i >= $numPages ) {
  165. break;
  166. }
  167. if ( $i < 0 ) {
  168. continue;
  169. }
  170. $html .= $this->pageNoLink(
  171. $i,
  172. $i + 1,
  173. $hrefPrefix,
  174. ( $this->mStartPage == $i )
  175. );
  176. }
  177. // build next button
  178. if ( $this->mStartPage < $numPages - 1 ) {
  179. $html .= $this->pageNoLink(
  180. $this->mStartPage + 1,
  181. wfMsg( 'allpagesnext' ),
  182. $hrefPrefix,
  183. false
  184. );
  185. }
  186. $html .= '</span>';
  187. return $html;
  188. }
  189. /**
  190. * Return a HTML representation of the image gallery
  191. *
  192. * For each image in the gallery, display
  193. * - a thumbnail
  194. * - the image name
  195. * - the additional text provided when adding the image
  196. * - the size of the image
  197. */
  198. function toHTML() {
  199. global $wgLang, $wgUser;
  200. $sk = $wgUser->getSkin();
  201. $s = '<div>';
  202. $s .= $this->pagerStatusHTML();
  203. $s .= $this->pagerHTML( 'top' );
  204. $s .= '</div>';
  205. $s .= '<table class="gallery" cellspacing="0" cellpadding="0">';
  206. $i = 0;
  207. foreach ( $this->mImages as $pair ) {
  208. $img =& $pair[0];
  209. $text = $pair[1];
  210. $name = $img->getName();
  211. $nt = $img->getTitle();
  212. // Not an image. Just print the name and skip.
  213. if ( $nt->getNamespace() != NS_IMAGE ) {
  214. $s .= '<td><div class="gallerybox" style="height: 152px;">' .
  215. htmlspecialchars( $nt->getText() ) . '</div></td>' . ( ( $i%4 == 3 ) ? "</tr>\n" : '');
  216. $i++;
  217. continue;
  218. }
  219. $nb = '';
  220. $textlink = $this->mShowFilename ?
  221. $sk->makeKnownLinkObj( $nt, htmlspecialchars( $wgLang->truncate( $nt->getText(), 20 ) ) ) . "<br />\n" :
  222. '';
  223. $s .= ( $i%4 == 0 ) ? '<tr>' : '';
  224. $thumb = $img->transform(array( 'width' => 120, 'height' => 120 ) , 0 );
  225. $vpad = floor( ( 150 - $thumb->height ) /2 ) - 2;
  226. $s .= '<td><div class="gallerybox">' . '<div class="thumb" style="padding: ' . $vpad . 'px 0;">';
  227. # ATTENTION: The newline after <div class="gallerytext"> is needed to accommodate htmltivdy which
  228. # in version 4.8.6 generated crackpot html in its absence, see:
  229. # http://bugzilla.wikimedia.org/show_bug.cgi?id=1765 -Ævar
  230. $s .= $sk->makeKnownLinkObj( $nt, $thumb->toHtml() ) . '</div><div class="gallerytext">' . "\n" .
  231. $textlink . $text . $nb .
  232. '</div>';
  233. $s .= "</div></td>\n";
  234. $s .= ( $i%4 == 3 ) ? '</tr>' : '';
  235. $i++;
  236. }
  237. if ( $i %4 != 0 ) {
  238. $s .= "</tr>\n";
  239. }
  240. $s .= '</table>';
  241. $s .= $this->pagerHTML( 'bottom' );
  242. return $s;
  243. }
  244. } //class