1<?php
2/**
3 * Variant of QueryPage which uses a gallery to output results.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24use Wikimedia\Rdbms\IDatabase;
25use Wikimedia\Rdbms\IResultWrapper;
26
27/**
28 * Variant of QueryPage which uses a gallery to output results, thus
29 * suited for reports generating images
30 *
31 * @stable to extend
32 *
33 * @ingroup SpecialPage
34 * @author Rob Church <robchur@gmail.com>
35 */
36abstract class ImageQueryPage extends QueryPage {
37	/**
38	 * Format and output report results using the given information plus
39	 * OutputPage
40	 *
41	 * @stable to override
42	 *
43	 * @param OutputPage $out OutputPage to print to
44	 * @param Skin $skin User skin to use [unused]
45	 * @param IDatabase $dbr (read) connection to use
46	 * @param IResultWrapper $res Result pointer
47	 * @param int $num Number of available result rows
48	 * @param int $offset Paging offset
49	 */
50	protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
51		if ( $num > 0 ) {
52			$gallery = ImageGalleryBase::factory( false, $this->getContext() );
53
54			// $res might contain the whole 1,000 rows, so we read up to
55			// $num [should update this to use a Pager]
56			$i = 0;
57			foreach ( $res as $row ) {
58				$i++;
59				$namespace = $row->namespace ?? NS_FILE;
60				$title = Title::makeTitleSafe( $namespace, $row->title );
61				if ( $title instanceof Title && $title->getNamespace() === NS_FILE ) {
62					$gallery->add( $title, $this->getCellHtml( $row ) );
63				}
64				if ( $i === $num ) {
65					break;
66				}
67			}
68
69			$out->addHTML( $gallery->toHTML() );
70		}
71	}
72
73	/**
74	 * @stable to override
75	 *
76	 * @param Skin $skin
77	 * @param stdClass $result
78	 *
79	 * @return bool|string
80	 */
81	protected function formatResult( $skin, $result ) {
82		return false;
83	}
84
85	/**
86	 * Get additional HTML to be shown in a results' cell
87	 *
88	 * @stable to override
89	 *
90	 * @param stdClass $row Result row
91	 * @return string
92	 */
93	protected function getCellHtml( $row ) {
94		return '';
95	}
96}
97